Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / title / TitleValue.php
blob5cac34704d7a3c26bbe6bf3c64e8f92f72d9e3f6
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
25 /**
26 * Represents a page (or page fragment) title within %MediaWiki.
28 * @note In contrast to Title, this is designed to be a plain value object. That is,
29 * it is immutable, does not use global state, and causes no side effects.
31 * @note TitleValue represents the title of a local page (or fragment of a page).
32 * It does not represent a link, and does not support interwiki prefixes etc.
34 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
35 * @since 1.23
37 class TitleValue {
38 /**
39 * @var int
41 protected $namespace;
43 /**
44 * @var string
46 protected $dbkey;
48 /**
49 * @var string
51 protected $fragment;
53 /**
54 * Constructs a TitleValue.
56 * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either
57 * from a database entry, or by a TitleParser. We could apply "some" normalization here,
58 * such as substituting spaces by underscores, but that would encourage the use of
59 * un-normalized text when constructing TitleValues. For constructing a TitleValue from
60 * user input or external sources, use a TitleParser.
62 * @param int $namespace The namespace ID. This is not validated.
63 * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
64 * @param string $fragment The fragment title. Use '' to represent the whole page.
65 * No validation or normalization is applied.
67 * @throws InvalidArgumentException
69 public function __construct( $namespace, $dbkey, $fragment = '' ) {
70 if ( !is_int( $namespace ) ) {
71 throw new InvalidArgumentException( '$namespace must be an integer' );
74 if ( !is_string( $dbkey ) ) {
75 throw new InvalidArgumentException( '$dbkey must be a string' );
78 // Sanity check, no full validation or normalization applied here!
79 if ( preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ) ) {
80 throw new InvalidArgumentException( '$dbkey must be a valid DB key: ' . $dbkey );
83 if ( !is_string( $fragment ) ) {
84 throw new InvalidArgumentException( '$fragment must be a string' );
87 if ( $dbkey === '' ) {
88 throw new InvalidArgumentException( '$dbkey must not be empty' );
91 $this->namespace = $namespace;
92 $this->dbkey = $dbkey;
93 $this->fragment = $fragment;
96 /**
97 * @return int
99 public function getNamespace() {
100 return $this->namespace;
104 * @return string
106 public function getFragment() {
107 return $this->fragment;
111 * Returns the title's DB key, as supplied to the constructor,
112 * without namespace prefix or fragment.
114 * @return string
116 public function getDBkey() {
117 return $this->dbkey;
121 * Returns the title in text form,
122 * without namespace prefix or fragment.
124 * This is computed from the DB key by replacing any underscores with spaces.
126 * @note To get a title string that includes the namespace and/or fragment,
127 * use a TitleFormatter.
129 * @return string
131 public function getText() {
132 return str_replace( '_', ' ', $this->getDBkey() );
136 * Creates a new TitleValue for a different fragment of the same page.
138 * @param string $fragment The fragment name, or "" for the entire page.
140 * @return TitleValue
142 public function createFragmentTitle( $fragment ) {
143 return new TitleValue( $this->namespace, $this->dbkey, $fragment );
147 * Returns a string representation of the title, for logging. This is purely informative
148 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
149 * the correct string representation for a given use.
151 * @return string
153 public function __toString() {
154 $name = $this->namespace . ':' . $this->dbkey;
156 if ( $this->fragment !== '' ) {
157 $name .= '#' . $this->fragment;
160 return $name;