Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
PageStoreRecord
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
7 / 7
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 isNew
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRedirect
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLatest
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTouched
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLanguage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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
21namespace MediaWiki\Page;
22
23use MediaWiki\Utils\MWTimestamp;
24use stdClass;
25use 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 */
36class 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    public function isNew(): bool {
84        return (bool)$this->row->page_is_new;
85    }
86
87    /**
88     * True if the page is a redirect.
89     */
90    public function isRedirect(): bool {
91        return (bool)$this->row->page_is_redirect;
92    }
93
94    /**
95     * The ID of the page'S latest revision.
96     *
97     * @param string|false $wikiId
98     *
99     * @return int
100     */
101    public function getLatest( $wikiId = self::LOCAL ): int {
102        $this->assertWiki( $wikiId );
103        return (int)$this->row->page_latest;
104    }
105
106    /**
107     * Timestamp at which the page was last rerendered.
108     */
109    public function getTouched(): string {
110        return MWTimestamp::convert( TS_MW, $this->row->page_touched );
111    }
112
113    /**
114     * Language in which the page is written.
115     */
116    public function getLanguage(): ?string {
117        return $this->getField( 'page_lang' );
118    }
119
120    /**
121     * Return the raw value for the given field as returned by the database query.
122     *
123     * Numeric values may be encoded as strings.
124     * Boolean values may be represented as integers (or numeric strings).
125     * Timestamps will use the database's native format.
126     *
127     * @internal
128     *
129     * @param string $field
130     *
131     * @return string|int|bool|null
132     */
133    public function getField( string $field ) {
134        // Field may be missing entirely.
135        return $this->row->$field ?? null;
136    }
137
138}