Merge "Simplify HTMLTitleTextField::validate"
[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 * @author Daniel Kinzler
22 */
23 use MediaWiki\Linker\LinkTarget;
24 use Wikimedia\Assert\Assert;
25 use Wikimedia\Assert\ParameterTypeException;
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 /**
39 * @deprecated in 1.31. This class is immutable. Use the getter for access.
40 * @var int
41 */
42 protected $namespace;
43
44 /**
45 * @deprecated in 1.31. This class is immutable. Use the getter for access.
46 * @var string
47 */
48 protected $dbkey;
49
50 /**
51 * @deprecated in 1.31. This class is immutable. Use the getter for access.
52 * @var string
53 */
54 protected $fragment;
55
56 /**
57 * @deprecated in 1.31. This class is immutable. Use the getter for access.
58 * @var string
59 */
60 protected $interwiki;
61
62 /**
63 * Text form including namespace/interwiki, initialised on demand
64 *
65 * Only public to share cache with TitleFormatter
66 *
67 * @private
68 * @var string
69 */
70 public $prefixedText = null;
71
72 /**
73 * Constructs a TitleValue.
74 *
75 * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either
76 * from a database entry, or by a TitleParser. We could apply "some" normalization here,
77 * such as substituting spaces by underscores, but that would encourage the use of
78 * un-normalized text when constructing TitleValues. For constructing a TitleValue from
79 * user input or external sources, use a TitleParser.
80 *
81 * @param int $namespace The namespace ID. This is not validated.
82 * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
83 * @param string $fragment The fragment title. Use '' to represent the whole page.
84 * No validation or normalization is applied.
85 * @param string $interwiki The interwiki component
86 *
87 * @throws InvalidArgumentException
88 */
89 public function __construct( $namespace, $dbkey, $fragment = '', $interwiki = '' ) {
90 if ( !is_int( $namespace ) ) {
91 throw new ParameterTypeException( '$namespace', 'int' );
92 }
93 if ( !is_string( $dbkey ) ) {
94 throw new ParameterTypeException( '$dbkey', 'string' );
95 }
96 if ( !is_string( $fragment ) ) {
97 throw new ParameterTypeException( '$fragment', 'string' );
98 }
99 if ( !is_string( $interwiki ) ) {
100 throw new ParameterTypeException( '$interwiki', 'string' );
101 }
102
103 // Sanity check, no full validation or normalization applied here!
104 Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey',
105 "invalid DB key '$dbkey'" );
106 Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
107
108 $this->namespace = $namespace;
109 $this->dbkey = $dbkey;
110 $this->fragment = $fragment;
111 $this->interwiki = $interwiki;
112 }
113
114 /**
115 * @since 1.23
116 * @return int
117 */
118 public function getNamespace() {
119 return $this->namespace;
120 }
121
122 /**
123 * @since 1.27
124 * @param int $ns
125 * @return bool
126 */
127 public function inNamespace( $ns ) {
128 return $this->namespace == $ns;
129 }
130
131 /**
132 * @since 1.23
133 * @return string
134 */
135 public function getFragment() {
136 return $this->fragment;
137 }
138
139 /**
140 * @since 1.27
141 * @return bool
142 */
143 public function hasFragment() {
144 return $this->fragment !== '';
145 }
146
147 /**
148 * Returns the title's DB key, as supplied to the constructor,
149 * without namespace prefix or fragment.
150 * @since 1.23
151 *
152 * @return string
153 */
154 public function getDBkey() {
155 return $this->dbkey;
156 }
157
158 /**
159 * Returns the title in text form,
160 * without namespace prefix or fragment.
161 * @since 1.23
162 *
163 * This is computed from the DB key by replacing any underscores with spaces.
164 *
165 * @note To get a title string that includes the namespace and/or fragment,
166 * use a TitleFormatter.
167 *
168 * @return string
169 */
170 public function getText() {
171 return str_replace( '_', ' ', $this->dbkey );
172 }
173
174 /**
175 * Creates a new TitleValue for a different fragment of the same page.
176 *
177 * @since 1.27
178 * @param string $fragment The fragment name, or "" for the entire page.
179 *
180 * @return TitleValue
181 */
182 public function createFragmentTarget( $fragment ) {
183 return new TitleValue(
184 $this->namespace,
185 $this->dbkey,
186 $fragment,
187 $this->interwiki
188 );
189 }
190
191 /**
192 * Whether it has an interwiki part
193 *
194 * @since 1.27
195 * @return bool
196 */
197 public function isExternal() {
198 return $this->interwiki !== '';
199 }
200
201 /**
202 * Returns the interwiki part
203 *
204 * @since 1.27
205 * @return string
206 */
207 public function getInterwiki() {
208 return $this->interwiki;
209 }
210
211 /**
212 * Returns a string representation of the title, for logging. This is purely informative
213 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
214 * the correct string representation for a given use.
215 * @since 1.23
216 *
217 * @return string
218 */
219 public function __toString() {
220 $name = $this->namespace . ':' . $this->dbkey;
221
222 if ( $this->fragment !== '' ) {
223 $name .= '#' . $this->fragment;
224 }
225
226 if ( $this->interwiki !== '' ) {
227 $name = $this->interwiki . ':' . $name;
228 }
229
230 return $name;
231 }
232 }