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