Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / title / TitleValue.php
blob73e1dc2063215a20e48d850fb2d4833eb6b85202
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
36 class TitleValue {
37 /**
38 * @var int
40 protected $namespace;
42 /**
43 * @var string
45 protected $dbkey;
47 /**
48 * @var string
50 protected $fragment;
52 /**
53 * Constructs a TitleValue.
55 * @note: TitleValue expects a valid DB key; typically, a TitleValue is constructed either
56 * from a database entry, or by a TitleParser. We could apply "some" normalization here,
57 * such as substituting spaces by underscores, but that would encourage the use of
58 * un-normalized text when constructing TitleValues. For constructing a TitleValue from
59 * user input or external sources, use a TitleParser.
61 * @param int $namespace The namespace ID. This is not validated.
62 * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
63 * @param string $fragment The fragment title. Use '' to represent the whole page.
64 * No validation or normalization is applied.
66 * @throws InvalidArgumentException
68 public function __construct( $namespace, $dbkey, $fragment = '' ) {
69 if ( !is_int( $namespace ) ) {
70 throw new InvalidArgumentException( '$namespace must be an integer' );
73 if ( !is_string( $dbkey ) ) {
74 throw new InvalidArgumentException( '$dbkey must be a string' );
77 // Sanity check, no full validation or normalization applied here!
78 if ( preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ) ) {
79 throw new InvalidArgumentException( '$dbkey must be a valid DB key: ' . $dbkey );
82 if ( !is_string( $fragment ) ) {
83 throw new InvalidArgumentException( '$fragment must be a string' );
86 if ( $dbkey === '' ) {
87 throw new InvalidArgumentException( '$dbkey must not be empty' );
90 $this->namespace = $namespace;
91 $this->dbkey = $dbkey;
92 $this->fragment = $fragment;
95 /**
96 * @return int
98 public function getNamespace() {
99 return $this->namespace;
103 * @return string
105 public function getFragment() {
106 return $this->fragment;
110 * Returns the title's DB key, as supplied to the constructor,
111 * without namespace prefix or fragment.
113 * @return string
115 public function getDBkey() {
116 return $this->dbkey;
120 * Returns the title in text form,
121 * without namespace prefix or fragment.
123 * This is computed from the DB key by replacing any underscores with spaces.
125 * @note: To get a title string that includes the namespace and/or fragment,
126 * use a TitleFormatter.
128 * @return string
130 public function getText() {
131 return str_replace( '_', ' ', $this->getDBkey() );
135 * Creates a new TitleValue for a different fragment of the same page.
137 * @param string $fragment The fragment name, or "" for the entire page.
139 * @return TitleValue
141 public function createFragmentTitle( $fragment ) {
142 return new TitleValue( $this->namespace, $this->dbkey, $fragment );
146 * Returns a string representation of the title, for logging. This is purely informative
147 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
148 * the correct string representation for a given use.
150 * @return string
152 public function __toString() {
153 $name = $this->namespace . ':' . $this->dbkey;
155 if ( $this->fragment !== '' ) {
156 $name .= '#' . $this->fragment;
159 return $name;