RCLFilters: make target and to/from parameters sticky again
[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 /**
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 Assert::parameterType( 'integer', $namespace, '$namespace' );
81 Assert::parameterType( 'string', $dbkey, '$dbkey' );
82 Assert::parameterType( 'string', $fragment, '$fragment' );
83 Assert::parameterType( 'string', $interwiki, '$interwiki' );
84
85 // Sanity check, no full validation or normalization applied here!
86 Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey',
87 "invalid DB key '$dbkey'" );
88 Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
89
90 $this->namespace = $namespace;
91 $this->dbkey = $dbkey;
92 $this->fragment = $fragment;
93 $this->interwiki = $interwiki;
94 }
95
96 /**
97 * @since 1.23
98 * @return int
99 */
100 public function getNamespace() {
101 return $this->namespace;
102 }
103
104 /**
105 * @since 1.27
106 * @param int $ns
107 * @return bool
108 */
109 public function inNamespace( $ns ) {
110 return $this->namespace == $ns;
111 }
112
113 /**
114 * @since 1.23
115 * @return string
116 */
117 public function getFragment() {
118 return $this->fragment;
119 }
120
121 /**
122 * @since 1.27
123 * @return bool
124 */
125 public function hasFragment() {
126 return $this->fragment !== '';
127 }
128
129 /**
130 * Returns the title's DB key, as supplied to the constructor,
131 * without namespace prefix or fragment.
132 * @since 1.23
133 *
134 * @return string
135 */
136 public function getDBkey() {
137 return $this->dbkey;
138 }
139
140 /**
141 * Returns the title in text form,
142 * without namespace prefix or fragment.
143 * @since 1.23
144 *
145 * This is computed from the DB key by replacing any underscores with spaces.
146 *
147 * @note To get a title string that includes the namespace and/or fragment,
148 * use a TitleFormatter.
149 *
150 * @return string
151 */
152 public function getText() {
153 return str_replace( '_', ' ', $this->getDBkey() );
154 }
155
156 /**
157 * Creates a new TitleValue for a different fragment of the same page.
158 *
159 * @since 1.27
160 * @param string $fragment The fragment name, or "" for the entire page.
161 *
162 * @return TitleValue
163 */
164 public function createFragmentTarget( $fragment ) {
165 return new TitleValue(
166 $this->namespace,
167 $this->dbkey,
168 $fragment,
169 $this->interwiki
170 );
171 }
172
173 /**
174 * Whether it has an interwiki part
175 *
176 * @since 1.27
177 * @return bool
178 */
179 public function isExternal() {
180 return $this->interwiki !== '';
181 }
182
183 /**
184 * Returns the interwiki part
185 *
186 * @since 1.27
187 * @return string
188 */
189 public function getInterwiki() {
190 return $this->interwiki;
191 }
192
193 /**
194 * Returns a string representation of the title, for logging. This is purely informative
195 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
196 * the correct string representation for a given use.
197 * @since 1.23
198 *
199 * @return string
200 */
201 public function __toString() {
202 $name = $this->namespace . ':' . $this->dbkey;
203
204 if ( $this->fragment !== '' ) {
205 $name .= '#' . $this->fragment;
206 }
207
208 if ( $this->interwiki !== '' ) {
209 $name = $this->interwiki . ':' . $name;
210 }
211
212 return $name;
213 }
214 }