Debian Codegraph @

includes/block/Restriction/PageRestriction.php

raw symbols SCIP
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 use MediaWiki\Title\Title;
26
27 class PageRestriction extends AbstractRestriction {
28
29 /**
30 * @inheritDoc
31 */
32 public const TYPE = 'page';
33
34 /**
35 * @inheritDoc
36 */
37 public const TYPE_ID = 1;
38
39 /**
40 * @var Title|false|null
41 */
42 protected $title;
43
44 /**
45 * @inheritDoc
46 */
47 public function matches( Title $title ) {
48 if ( !$this->getTitle() ) {
49 return false;
50 }
51
52 return $title->equals( $this->getTitle() );
53 }
54
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 * @since 1.33
68 * @return Title|false
69 */
70 public function getTitle() {
71 // If the title does not exist, set to false to prevent multiple database
72 // queries.
73 $this->title ??= Title::newFromID( $this->value ) ?? false;
74
75 return $this->title;
76 }
77
78 /**
79 * @inheritDoc
80 */
81 public static function newFromRow( \stdClass $row ) {
82 /** @var self $restriction */
83 $restriction = parent::newFromRow( $row );
84 '@phan-var self $restriction';
85
86 // If the page_namespace and the page_title were provided, add the title to
87 // the restriction.
88 if ( isset( $row->page_namespace ) && isset( $row->page_title ) ) {
89 // Clone the row so it is not mutated.
90 $row = clone $row;
91 $row->page_id = $row->ir_value;
92 $title = Title::newFromRow( $row );
93 $restriction->setTitle( $title );
94 }
95
96 return $restriction;
97 }
98
99 /**
100 * @internal
101 * @since 1.36
102 * @param string|Title $title
103 * @return self
104 */
105 public static function newFromTitle( $title ) {
106 if ( is_string( $title ) ) {
107 $title = Title::newFromText( $title );
108 }
109
110 $restriction = new self( 0, $title->getArticleID() );
111 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Title is always valid
112 $restriction->setTitle( $title );
113
114 return $restriction;
115 }
116 }