Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / block / Restriction / PageRestriction.php
1 <?php
2 /**
3 * A block restriction object of type 'Page'.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Block\Restriction;
24
25 class PageRestriction extends AbstractRestriction {
26
27 /**
28 * @inheritDoc
29 */
30 const TYPE = 'page';
31
32 /**
33 * @inheritDoc
34 */
35 const TYPE_ID = 1;
36
37 /**
38 * @var \Title|bool
39 */
40 protected $title;
41
42 /**
43 * @inheritDoc
44 */
45 public function matches( \Title $title ) {
46 if ( !$this->getTitle() ) {
47 return false;
48 }
49
50 return $title->equals( $this->getTitle() );
51 }
52
53 /**
54 * Set the title.
55 *
56 * @since 1.33
57 * @param \Title $title
58 * @return self
59 */
60 public function setTitle( \Title $title ) {
61 $this->title = $title;
62
63 return $this;
64 }
65
66 /**
67 * Get Title.
68 *
69 * @since 1.33
70 * @return \Title|null
71 */
72 public function getTitle() {
73 if ( $this->title === null ) {
74 $this->title = \Title::newFromID( $this->value );
75
76 // If the title does not exist, set to false to prevent multiple database
77 // queries.
78 if ( $this->title === null ) {
79 $this->title = false;
80 }
81 }
82
83 return $this->title ?? null;
84 }
85
86 /**
87 * @inheritDoc
88 */
89 public static function newFromRow( \stdClass $row ) {
90 /** @var self $restriction */
91 $restriction = parent::newFromRow( $row );
92 '@phan-var self $restriction';
93
94 // If the page_namespace and the page_title were provided, add the title to
95 // the restriction.
96 if ( isset( $row->page_namespace ) && isset( $row->page_title ) ) {
97 // Clone the row so it is not mutated.
98 $row = clone $row;
99 $row->page_id = $row->ir_value;
100 $title = \Title::newFromRow( $row );
101 $restriction->setTitle( $title );
102 }
103
104 return $restriction;
105 }
106 }