aboutsummaryrefslogtreecommitdiffstats
path: root/includes/page/PageStoreRecord.php
blob: a264613bdf3ea677e5cfc0cf46c7de9710c9399a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\Page;

use MWTimestamp;
use stdClass;
use Wikimedia\Assert\Assert;

/**
 * Immutable data record representing an editable page on a wiki.
 *
 * Corresponds to a row in the page table.
 *
 * @since 1.36
 */
class PageStoreRecord extends PageIdentityValue implements ExistingPageRecord {

	/**
	 * Fields that must be present in the row object passed to the constructor.
	 * Note that page_lang is optional, so it is not included here.
	 *
	 * @since 1.37
	 */
	public const REQUIRED_FIELDS = [
		'page_id',
		'page_namespace',
		'page_title',
		'page_is_redirect',
		'page_is_new',
		'page_latest',
		'page_touched',
	];

	/**
	 * Fields from the page table.
	 *
	 * @var stdClass
	 */
	private $row;

	/**
	 * The $row object must provide all fields listed in PageStoreRecord::REQUIRED_FIELDS.
	 *
	 * @param stdClass $row A row from the page table
	 * @param string|bool $wikiId The Id of the wiki this page belongs to,
	 *        or self::LOCAL for the local wiki.
	 */
	public function __construct( stdClass $row, $wikiId ) {
		foreach ( self::REQUIRED_FIELDS as $field ) {
			Assert::parameter( isset( $row->$field ), '$row->' . $field, 'is required' );
		}

		Assert::parameter( $row->page_id > 0, '$pageId', 'must be greater than zero (page must exist)' );

		parent::__construct( $row->page_id, $row->page_namespace, $row->page_title, $wikiId );

		$this->row = $row;
	}

	/**
	 * False if the page has had more than one edit.
	 *
	 * @return bool
	 */
	public function isNew(): bool {
		return (bool)$this->row->page_is_new;
	}

	/**
	 * True if the page is a redirect.
	 *
	 * @return bool
	 */
	public function isRedirect(): bool {
		return (bool)$this->row->page_is_redirect;
	}

	/**
	 * The ID of the page'S latest revision.
	 *
	 * @param bool $wikiId
	 *
	 * @return int
	 */
	public function getLatest( $wikiId = self::LOCAL ): int {
		$this->assertWiki( $wikiId );
		return (int)$this->row->page_latest;
	}

	/**
	 * Timestamp at which the page was last rerendered.
	 *
	 * @return string
	 */
	public function getTouched(): string {
		return MWTimestamp::convert( TS_MW, $this->row->page_touched );
	}

	/**
	 * Language in which the page is written.
	 *
	 * @return ?string
	 */
	public function getLanguage(): ?string {
		return $this->getField( 'page_lang' );
	}

	/**
	 * Return the raw value for the given field as returned by the database query.
	 *
	 * Numeric values may be encoded as strings.
	 * Boolean values may be represented as integers (or numeric strings).
	 * Timestamps will use the database's native format.
	 *
	 * @internal
	 *
	 * @param string $field
	 *
	 * @return string|int|bool|null
	 */
	public function getField( string $field ) {
		// Field may be missing entirely.
		return $this->row->$field ?? null;
	}

}