| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\Page; |
| 22 | |
| 23 | use MediaWiki\Utils\MWTimestamp; |
| 24 | use stdClass; |
| 25 | use Wikimedia\Assert\Assert; |
| 26 | |
| 27 | /** |
| 28 | * Immutable data record representing an editable page on a wiki. |
| 29 | * |
| 30 | * Corresponds to a row in the page table. |
| 31 | * |
| 32 | * @see https://www.mediawiki.org/wiki/Manual:Modeling_pages |
| 33 | * |
| 34 | * @since 1.36 |
| 35 | */ |
| 36 | class PageStoreRecord extends PageIdentityValue implements ExistingPageRecord { |
| 37 | |
| 38 | /** |
| 39 | * Fields that must be present in the row object passed to the constructor. |
| 40 | * Note that page_lang is optional, so it is not included here. |
| 41 | * |
| 42 | * @since 1.37 |
| 43 | */ |
| 44 | public const REQUIRED_FIELDS = [ |
| 45 | 'page_id', |
| 46 | 'page_namespace', |
| 47 | 'page_title', |
| 48 | 'page_is_redirect', |
| 49 | 'page_is_new', |
| 50 | 'page_latest', |
| 51 | 'page_touched', |
| 52 | ]; |
| 53 | |
| 54 | /** |
| 55 | * Fields from the page table. |
| 56 | * |
| 57 | * @var stdClass |
| 58 | */ |
| 59 | private $row; |
| 60 | |
| 61 | /** |
| 62 | * The $row object must provide all fields listed in PageStoreRecord::REQUIRED_FIELDS. |
| 63 | * |
| 64 | * @param stdClass $row A row from the page table |
| 65 | * @param string|false $wikiId The Id of the wiki this page belongs to, |
| 66 | * or self::LOCAL for the local wiki. |
| 67 | */ |
| 68 | public function __construct( stdClass $row, $wikiId ) { |
| 69 | foreach ( self::REQUIRED_FIELDS as $field ) { |
| 70 | Assert::parameter( isset( $row->$field ), '$row->' . $field, 'is required' ); |
| 71 | } |
| 72 | |
| 73 | Assert::parameter( $row->page_id > 0, '$pageId', 'must be greater than zero (page must exist)' ); |
| 74 | |
| 75 | parent::__construct( $row->page_id, $row->page_namespace, $row->page_title, $wikiId ); |
| 76 | |
| 77 | $this->row = $row; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * False if the page has had more than one edit. |
| 82 | * |
| 83 | * @return bool |
| 84 | */ |
| 85 | public function isNew(): bool { |
| 86 | return (bool)$this->row->page_is_new; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * True if the page is a redirect. |
| 91 | * |
| 92 | * @return bool |
| 93 | */ |
| 94 | public function isRedirect(): bool { |
| 95 | return (bool)$this->row->page_is_redirect; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * The ID of the page'S latest revision. |
| 100 | * |
| 101 | * @param string|false $wikiId |
| 102 | * |
| 103 | * @return int |
| 104 | */ |
| 105 | public function getLatest( $wikiId = self::LOCAL ): int { |
| 106 | $this->assertWiki( $wikiId ); |
| 107 | return (int)$this->row->page_latest; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Timestamp at which the page was last rerendered. |
| 112 | * |
| 113 | * @return string |
| 114 | */ |
| 115 | public function getTouched(): string { |
| 116 | return MWTimestamp::convert( TS_MW, $this->row->page_touched ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Language in which the page is written. |
| 121 | * |
| 122 | * @return ?string |
| 123 | */ |
| 124 | public function getLanguage(): ?string { |
| 125 | return $this->getField( 'page_lang' ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Return the raw value for the given field as returned by the database query. |
| 130 | * |
| 131 | * Numeric values may be encoded as strings. |
| 132 | * Boolean values may be represented as integers (or numeric strings). |
| 133 | * Timestamps will use the database's native format. |
| 134 | * |
| 135 | * @internal |
| 136 | * |
| 137 | * @param string $field |
| 138 | * |
| 139 | * @return string|int|bool|null |
| 140 | */ |
| 141 | public function getField( string $field ) { |
| 142 | // Field may be missing entirely. |
| 143 | return $this->row->$field ?? null; |
| 144 | } |
| 145 | |
| 146 | } |