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