Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / title / TitleValue.php
blob7c370f1ae56d29e4e347be1f4a9e43da725288de
1 <?php
2 /**
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
20 * @file
21 * @license GPL 2+
22 * @author Daniel Kinzler
24 use MediaWiki\Linker\LinkTarget;
25 use Wikimedia\Assert\Assert;
27 /**
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
34 * @since 1.23
36 class TitleValue implements LinkTarget {
37 /**
38 * @var int
40 protected $namespace;
42 /**
43 * @var string
45 protected $dbkey;
47 /**
48 * @var string
50 protected $fragment;
52 /**
53 * @var string
55 protected $interwiki;
57 /**
58 * Constructs a TitleValue.
60 * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either
61 * from a database entry, or by a TitleParser. We could apply "some" normalization here,
62 * such as substituting spaces by underscores, but that would encourage the use of
63 * un-normalized text when constructing TitleValues. For constructing a TitleValue from
64 * user input or external sources, use a TitleParser.
66 * @param int $namespace The namespace ID. This is not validated.
67 * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
68 * @param string $fragment The fragment title. Use '' to represent the whole page.
69 * No validation or normalization is applied.
70 * @param string $interwiki The interwiki component
72 * @throws InvalidArgumentException
74 public function __construct( $namespace, $dbkey, $fragment = '', $interwiki = '' ) {
75 Assert::parameterType( 'integer', $namespace, '$namespace' );
76 Assert::parameterType( 'string', $dbkey, '$dbkey' );
77 Assert::parameterType( 'string', $fragment, '$fragment' );
78 Assert::parameterType( 'string', $interwiki, '$interwiki' );
80 // Sanity check, no full validation or normalization applied here!
81 Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey',
82 "invalid DB key '$dbkey'" );
83 Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
85 $this->namespace = $namespace;
86 $this->dbkey = $dbkey;
87 $this->fragment = $fragment;
88 $this->interwiki = $interwiki;
91 /**
92 * @return int
94 public function getNamespace() {
95 return $this->namespace;
98 /**
99 * @since 1.27
100 * @param int $ns
101 * @return bool
103 public function inNamespace( $ns ) {
104 return $this->namespace == $ns;
108 * @return string
110 public function getFragment() {
111 return $this->fragment;
115 * @since 1.27
116 * @return bool
118 public function hasFragment() {
119 return $this->fragment !== '';
123 * Returns the title's DB key, as supplied to the constructor,
124 * without namespace prefix or fragment.
126 * @return string
128 public function getDBkey() {
129 return $this->dbkey;
133 * Returns the title in text form,
134 * without namespace prefix or fragment.
136 * This is computed from the DB key by replacing any underscores with spaces.
138 * @note To get a title string that includes the namespace and/or fragment,
139 * use a TitleFormatter.
141 * @return string
143 public function getText() {
144 return str_replace( '_', ' ', $this->getDBkey() );
148 * Creates a new TitleValue for a different fragment of the same page.
150 * @since 1.27
151 * @param string $fragment The fragment name, or "" for the entire page.
153 * @return TitleValue
155 public function createFragmentTarget( $fragment ) {
156 return new TitleValue(
157 $this->namespace,
158 $this->dbkey,
159 $fragment,
160 $this->interwiki
165 * Whether it has an interwiki part
167 * @since 1.27
168 * @return bool
170 public function isExternal() {
171 return $this->interwiki !== '';
175 * Returns the interwiki part
177 * @since 1.27
178 * @return string
180 public function getInterwiki() {
181 return $this->interwiki;
185 * Returns a string representation of the title, for logging. This is purely informative
186 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
187 * the correct string representation for a given use.
189 * @return string
191 public function __toString() {
192 $name = $this->namespace . ':' . $this->dbkey;
194 if ( $this->fragment !== '' ) {
195 $name .= '#' . $this->fragment;
198 if ( $this->interwiki !== '' ) {
199 $name = $this->interwiki . ':' . $name;
202 return $name;