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