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
;
27 * Represents a page (or page fragment) title within MediaWiki.
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.
32 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
35 class TitleValue
implements LinkTarget
{
38 * @deprecated in 1.31. This class is immutable. Use the getter for access.
44 * @deprecated in 1.31. This class is immutable. Use the getter for access.
50 * @deprecated in 1.31. This class is immutable. Use the getter for access.
56 * @deprecated in 1.31. This class is immutable. Use the getter for access.
62 * Constructs a TitleValue.
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.
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
76 * @throws InvalidArgumentException
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' );
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' );
89 $this->namespace = $namespace;
90 $this->dbkey
= $dbkey;
91 $this->fragment
= $fragment;
92 $this->interwiki
= $interwiki;
99 public function getNamespace() {
100 return $this->namespace;
108 public function inNamespace( $ns ) {
109 return $this->namespace == $ns;
116 public function getFragment() {
117 return $this->fragment
;
124 public function hasFragment() {
125 return $this->fragment
!== '';
129 * Returns the title's DB key, as supplied to the constructor,
130 * without namespace prefix or fragment.
135 public function getDBkey() {
140 * Returns the title in text form,
141 * without namespace prefix or fragment.
144 * This is computed from the DB key by replacing any underscores with spaces.
146 * @note To get a title string that includes the namespace and/or fragment,
147 * use a TitleFormatter.
151 public function getText() {
152 return str_replace( '_', ' ', $this->getDBkey() );
156 * Creates a new TitleValue for a different fragment of the same page.
159 * @param string $fragment The fragment name, or "" for the entire page.
163 public function createFragmentTarget( $fragment ) {
164 return new TitleValue(
173 * Whether it has an interwiki part
178 public function isExternal() {
179 return $this->interwiki
!== '';
183 * Returns the interwiki part
188 public function getInterwiki() {
189 return $this->interwiki
;
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.
200 public function __toString() {
201 $name = $this->namespace . ':' . $this->dbkey
;
203 if ( $this->fragment
!== '' ) {
204 $name .= '#' . $this->fragment
;
207 if ( $this->interwiki
!== '' ) {
208 $name = $this->interwiki
. ':' . $name;