Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| PageStoreRecord | |
100.00% |
12 / 12 |
|
100.00% |
7 / 7 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| isNew | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isRedirect | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLatest | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getTouched | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLanguage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Page; |
| 8 | |
| 9 | use MediaWiki\Utils\MWTimestamp; |
| 10 | use stdClass; |
| 11 | use Wikimedia\Assert\Assert; |
| 12 | use Wikimedia\Timestamp\TimestampFormat as TS; |
| 13 | |
| 14 | /** |
| 15 | * Immutable data record representing an editable page on a wiki. |
| 16 | * |
| 17 | * Corresponds to a row in the page table. |
| 18 | * |
| 19 | * @see https://www.mediawiki.org/wiki/Manual:Modeling_pages |
| 20 | * |
| 21 | * @since 1.36 |
| 22 | */ |
| 23 | class PageStoreRecord extends PageIdentityValue implements ExistingPageRecord { |
| 24 | |
| 25 | /** |
| 26 | * Fields that must be present in the row object passed to the constructor. |
| 27 | * Note that page_lang is optional, so it is not included here. |
| 28 | * |
| 29 | * @since 1.37 |
| 30 | */ |
| 31 | public const REQUIRED_FIELDS = [ |
| 32 | 'page_id', |
| 33 | 'page_namespace', |
| 34 | 'page_title', |
| 35 | 'page_is_redirect', |
| 36 | 'page_is_new', |
| 37 | 'page_latest', |
| 38 | 'page_touched', |
| 39 | ]; |
| 40 | |
| 41 | /** |
| 42 | * Fields from the page table. |
| 43 | * |
| 44 | * @var stdClass |
| 45 | */ |
| 46 | private $row; |
| 47 | |
| 48 | /** |
| 49 | * The $row object must provide all fields listed in PageStoreRecord::REQUIRED_FIELDS. |
| 50 | * |
| 51 | * @param stdClass $row A row from the page table |
| 52 | * @param string|false $wikiId The Id of the wiki this page belongs to, |
| 53 | * or self::LOCAL for the local wiki. |
| 54 | */ |
| 55 | public function __construct( stdClass $row, $wikiId ) { |
| 56 | foreach ( self::REQUIRED_FIELDS as $field ) { |
| 57 | Assert::parameter( isset( $row->$field ), '$row->' . $field, 'is required' ); |
| 58 | } |
| 59 | |
| 60 | Assert::parameter( $row->page_id > 0, '$pageId', 'must be greater than zero (page must exist)' ); |
| 61 | |
| 62 | parent::__construct( $row->page_id, $row->page_namespace, $row->page_title, $wikiId ); |
| 63 | |
| 64 | $this->row = $row; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * False if the page has had more than one edit. |
| 69 | */ |
| 70 | public function isNew(): bool { |
| 71 | return (bool)$this->row->page_is_new; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * True if the page is a redirect. |
| 76 | */ |
| 77 | public function isRedirect(): bool { |
| 78 | return (bool)$this->row->page_is_redirect; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * The ID of the page'S latest revision. |
| 83 | * |
| 84 | * @param string|false $wikiId |
| 85 | * |
| 86 | * @return int |
| 87 | */ |
| 88 | public function getLatest( $wikiId = self::LOCAL ): int { |
| 89 | $this->assertWiki( $wikiId ); |
| 90 | return (int)$this->row->page_latest; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Timestamp at which the page was last rerendered. |
| 95 | */ |
| 96 | public function getTouched(): string { |
| 97 | return MWTimestamp::convert( TS::MW, $this->row->page_touched ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Language in which the page is written. |
| 102 | */ |
| 103 | public function getLanguage(): ?string { |
| 104 | return $this->getField( 'page_lang' ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Return the raw value for the given field as returned by the database query. |
| 109 | * |
| 110 | * Numeric values may be encoded as strings. |
| 111 | * Boolean values may be represented as integers (or numeric strings). |
| 112 | * Timestamps will use the database's native format. |
| 113 | * |
| 114 | * @internal |
| 115 | * |
| 116 | * @param string $field |
| 117 | * |
| 118 | * @return string|int|bool|null |
| 119 | */ |
| 120 | public function getField( string $field ) { |
| 121 | // Field may be missing entirely. |
| 122 | return $this->row->$field ?? null; |
| 123 | } |
| 124 | |
| 125 | } |