Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / includes / title / TitleValue.php
1 <?php
2 /**
3 * Representation of a page title within %MediaWiki.
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 * @license GPL 2+
22 * @author Daniel Kinzler
23 */
24 use MediaWiki\Linker\LinkTarget;
25 use Wikimedia\Assert\Assert;
26
27 /**
28 * Represents a page (or page fragment) title within %MediaWiki.
29 *
30 * @note In contrast to Title, this is designed to be a plain value object. That is,
31 * it is immutable, does not use global state, and causes no side effects.
32 *
33 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
34 * @since 1.23
35 */
36 class TitleValue implements LinkTarget {
37 /**
38 * @var int
39 */
40 protected $namespace;
41
42 /**
43 * @var string
44 */
45 protected $dbkey;
46
47 /**
48 * @var string
49 */
50 protected $fragment;
51
52 /**
53 * @var string
54 */
55 protected $interwiki;
56
57 /**
58 * Constructs a TitleValue.
59 *
60 * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either
61 * from a database entry, or by a TitleParser. We could apply "some" normalization here,
62 * such as substituting spaces by underscores, but that would encourage the use of
63 * un-normalized text when constructing TitleValues. For constructing a TitleValue from
64 * user input or external sources, use a TitleParser.
65 *
66 * @param int $namespace The namespace ID. This is not validated.
67 * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
68 * @param string $fragment The fragment title. Use '' to represent the whole page.
69 * No validation or normalization is applied.
70 * @param string $interwiki The interwiki component
71 *
72 * @throws InvalidArgumentException
73 */
74 public function __construct( $namespace, $dbkey, $fragment = '', $interwiki = '' ) {
75 Assert::parameterType( 'integer', $namespace, '$namespace' );
76 Assert::parameterType( 'string', $dbkey, '$dbkey' );
77 Assert::parameterType( 'string', $fragment, '$fragment' );
78 Assert::parameterType( 'string', $interwiki, '$interwiki' );
79
80 // Sanity check, no full validation or normalization applied here!
81 Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey', 'invalid DB key' );
82 Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
83
84 $this->namespace = $namespace;
85 $this->dbkey = $dbkey;
86 $this->fragment = $fragment;
87 $this->interwiki = $interwiki;
88 }
89
90 /**
91 * @return int
92 */
93 public function getNamespace() {
94 return $this->namespace;
95 }
96
97 /**
98 * @since 1.27
99 * @param int $ns
100 * @return bool
101 */
102 public function inNamespace( $ns ) {
103 return $this->namespace == $ns;
104 }
105
106 /**
107 * @return string
108 */
109 public function getFragment() {
110 return $this->fragment;
111 }
112
113 /**
114 * @since 1.27
115 * @return bool
116 */
117 public function hasFragment() {
118 return $this->fragment !== '';
119 }
120
121 /**
122 * Returns the title's DB key, as supplied to the constructor,
123 * without namespace prefix or fragment.
124 *
125 * @return string
126 */
127 public function getDBkey() {
128 return $this->dbkey;
129 }
130
131 /**
132 * Returns the title in text form,
133 * without namespace prefix or fragment.
134 *
135 * This is computed from the DB key by replacing any underscores with spaces.
136 *
137 * @note To get a title string that includes the namespace and/or fragment,
138 * use a TitleFormatter.
139 *
140 * @return string
141 */
142 public function getText() {
143 return str_replace( '_', ' ', $this->getDBkey() );
144 }
145
146 /**
147 * Creates a new TitleValue for a different fragment of the same page.
148 *
149 * @since 1.27
150 * @param string $fragment The fragment name, or "" for the entire page.
151 *
152 * @return TitleValue
153 */
154 public function createFragmentTarget( $fragment ) {
155 return new TitleValue(
156 $this->namespace,
157 $this->dbkey,
158 $fragment,
159 $this->interwiki
160 );
161 }
162
163 /**
164 * Whether it has an interwiki part
165 *
166 * @since 1.27
167 * @return bool
168 */
169 public function isExternal() {
170 return $this->interwiki !== '';
171 }
172
173 /**
174 * Returns the interwiki part
175 *
176 * @since 1.27
177 * @return string
178 */
179 public function getInterwiki() {
180 return $this->interwiki;
181 }
182
183 /**
184 * Returns a string representation of the title, for logging. This is purely informative
185 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
186 * the correct string representation for a given use.
187 *
188 * @return string
189 */
190 public function __toString() {
191 $name = $this->namespace . ':' . $this->dbkey;
192
193 if ( $this->fragment !== '' ) {
194 $name .= '#' . $this->fragment;
195 }
196
197 if ( $this->interwiki !== '' ) {
198 $name = $this->interwiki . ':' . $name;
199 }
200
201 return $name;
202 }
203 }