3 * Representation of a page title within MediaWiki.
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.
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.
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
21 * @author Daniel Kinzler
23 use MediaWiki\Linker\LinkTarget
;
24 use Wikimedia\Assert\Assert
;
25 use Wikimedia\Assert\ParameterTypeException
;
28 * Represents a page (or page fragment) title within MediaWiki.
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.
33 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
36 class TitleValue
implements LinkTarget
{
39 * @deprecated in 1.31. This class is immutable. Use the getter for access.
45 * @deprecated in 1.31. This class is immutable. Use the getter for access.
51 * @deprecated in 1.31. This class is immutable. Use the getter for access.
57 * @deprecated in 1.31. This class is immutable. Use the getter for access.
63 * Text form including namespace/interwiki, initialised on demand
65 * Only public to share cache with TitleFormatter
70 public $prefixedText = null;
73 * Constructs a TitleValue.
75 * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either
76 * from a database entry, or by a TitleParser. We could apply "some" normalization here,
77 * such as substituting spaces by underscores, but that would encourage the use of
78 * un-normalized text when constructing TitleValues. For constructing a TitleValue from
79 * user input or external sources, use a TitleParser.
81 * @param int $namespace The namespace ID. This is not validated.
82 * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
83 * @param string $fragment The fragment title. Use '' to represent the whole page.
84 * No validation or normalization is applied.
85 * @param string $interwiki The interwiki component
87 * @throws InvalidArgumentException
89 public function __construct( $namespace, $dbkey, $fragment = '', $interwiki = '' ) {
90 if ( !is_int( $namespace ) ) {
91 throw new ParameterTypeException( '$namespace', 'int' );
93 if ( !is_string( $dbkey ) ) {
94 throw new ParameterTypeException( '$dbkey', 'string' );
96 if ( !is_string( $fragment ) ) {
97 throw new ParameterTypeException( '$fragment', 'string' );
99 if ( !is_string( $interwiki ) ) {
100 throw new ParameterTypeException( '$interwiki', 'string' );
103 // Sanity check, no full validation or normalization applied here!
104 Assert
::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey',
105 "invalid DB key '$dbkey'" );
106 Assert
::parameter( $dbkey !== '' ||
( $fragment !== '' && $namespace === NS_MAIN
),
107 '$dbkey', 'should not be empty unless namespace is main and fragment is non-empty' );
109 $this->namespace = $namespace;
110 $this->dbkey
= $dbkey;
111 $this->fragment
= $fragment;
112 $this->interwiki
= $interwiki;
119 public function getNamespace() {
120 return $this->namespace;
128 public function inNamespace( $ns ) {
129 return $this->namespace == $ns;
136 public function getFragment() {
137 return $this->fragment
;
144 public function hasFragment() {
145 return $this->fragment
!== '';
149 * Returns the title's DB key, as supplied to the constructor,
150 * without namespace prefix or fragment.
155 public function getDBkey() {
160 * Returns the title in text form,
161 * without namespace prefix or fragment.
164 * This is computed from the DB key by replacing any underscores with spaces.
166 * @note To get a title string that includes the namespace and/or fragment,
167 * use a TitleFormatter.
171 public function getText() {
172 return str_replace( '_', ' ', $this->dbkey
);
176 * Creates a new TitleValue for a different fragment of the same page.
179 * @param string $fragment The fragment name, or "" for the entire page.
183 public function createFragmentTarget( $fragment ) {
184 return new TitleValue(
193 * Whether it has an interwiki part
198 public function isExternal() {
199 return $this->interwiki
!== '';
203 * Returns the interwiki part
208 public function getInterwiki() {
209 return $this->interwiki
;
213 * Returns a string representation of the title, for logging. This is purely informative
214 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
215 * the correct string representation for a given use.
220 public function __toString() {
221 $name = $this->namespace . ':' . $this->dbkey
;
223 if ( $this->fragment
!== '' ) {
224 $name .= '#' . $this->fragment
;
227 if ( $this->interwiki
!== '' ) {
228 $name = $this->interwiki
. ':' . $name;