Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / title / TitleValue.php
bloba0f3b6f9b22b0240132fb2331e4aba0694464f79
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 Wikimedia\Assert\Assert;
26 /**
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 * @note TitleValue represents the title of a local page (or fragment of a page).
33 * It does not represent a link, and does not support interwiki prefixes etc.
35 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
36 * @since 1.23
38 class TitleValue {
39 /**
40 * @var int
42 protected $namespace;
44 /**
45 * @var string
47 protected $dbkey;
49 /**
50 * @var string
52 protected $fragment;
54 /**
55 * Constructs a TitleValue.
57 * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either
58 * from a database entry, or by a TitleParser. We could apply "some" normalization here,
59 * such as substituting spaces by underscores, but that would encourage the use of
60 * un-normalized text when constructing TitleValues. For constructing a TitleValue from
61 * user input or external sources, use a TitleParser.
63 * @param int $namespace The namespace ID. This is not validated.
64 * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
65 * @param string $fragment The fragment title. Use '' to represent the whole page.
66 * No validation or normalization is applied.
68 * @throws InvalidArgumentException
70 public function __construct( $namespace, $dbkey, $fragment = '' ) {
71 Assert::parameterType( 'integer', $namespace, '$namespace' );
72 Assert::parameterType( 'string', $dbkey, '$dbkey' );
73 Assert::parameterType( 'string', $fragment, '$fragment' );
75 // Sanity check, no full validation or normalization applied here!
76 Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey', 'invalid DB key' );
77 Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
79 $this->namespace = $namespace;
80 $this->dbkey = $dbkey;
81 $this->fragment = $fragment;
84 /**
85 * @return int
87 public function getNamespace() {
88 return $this->namespace;
91 /**
92 * @return string
94 public function getFragment() {
95 return $this->fragment;
98 /**
99 * Returns the title's DB key, as supplied to the constructor,
100 * without namespace prefix or fragment.
102 * @return string
104 public function getDBkey() {
105 return $this->dbkey;
109 * Returns the title in text form,
110 * without namespace prefix or fragment.
112 * This is computed from the DB key by replacing any underscores with spaces.
114 * @note To get a title string that includes the namespace and/or fragment,
115 * use a TitleFormatter.
117 * @return string
119 public function getText() {
120 return str_replace( '_', ' ', $this->getDBkey() );
124 * Creates a new TitleValue for a different fragment of the same page.
126 * @param string $fragment The fragment name, or "" for the entire page.
128 * @return TitleValue
130 public function createFragmentTitle( $fragment ) {
131 return new TitleValue( $this->namespace, $this->dbkey, $fragment );
135 * Returns a string representation of the title, for logging. This is purely informative
136 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
137 * the correct string representation for a given use.
139 * @return string
141 public function __toString() {
142 $name = $this->namespace . ':' . $this->dbkey;
144 if ( $this->fragment !== '' ) {
145 $name .= '#' . $this->fragment;
148 return $name;