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