Merge "Consolidate mobile and desktop designs for login form"
[lhc/web/wiklou.git] / includes / title / TitleValue.php
1 <?php
2 /**
3 * Representation of a page title within %MediaWiki.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @license GPL 2+
22 * @author Daniel Kinzler
23 */
24
25 /**
26 * Represents a page (or page fragment) title within %MediaWiki.
27 *
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.
30 *
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.
33 *
34 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
35 * @since 1.23
36 */
37 class TitleValue {
38 /**
39 * @var int
40 */
41 protected $namespace;
42
43 /**
44 * @var string
45 */
46 protected $dbkey;
47
48 /**
49 * @var string
50 */
51 protected $fragment;
52
53 /**
54 * Constructs a TitleValue.
55 *
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.
61 *
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.
66 *
67 * @throws InvalidArgumentException
68 */
69 public function __construct( $namespace, $dbkey, $fragment = '' ) {
70 if ( !is_int( $namespace ) ) {
71 throw new InvalidArgumentException( '$namespace must be an integer' );
72 }
73
74 if ( !is_string( $dbkey ) ) {
75 throw new InvalidArgumentException( '$dbkey must be a string' );
76 }
77
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 );
81 }
82
83 if ( !is_string( $fragment ) ) {
84 throw new InvalidArgumentException( '$fragment must be a string' );
85 }
86
87 if ( $dbkey === '' ) {
88 throw new InvalidArgumentException( '$dbkey must not be empty' );
89 }
90
91 $this->namespace = $namespace;
92 $this->dbkey = $dbkey;
93 $this->fragment = $fragment;
94 }
95
96 /**
97 * @return int
98 */
99 public function getNamespace() {
100 return $this->namespace;
101 }
102
103 /**
104 * @return string
105 */
106 public function getFragment() {
107 return $this->fragment;
108 }
109
110 /**
111 * Returns the title's DB key, as supplied to the constructor,
112 * without namespace prefix or fragment.
113 *
114 * @return string
115 */
116 public function getDBkey() {
117 return $this->dbkey;
118 }
119
120 /**
121 * Returns the title in text form,
122 * without namespace prefix or fragment.
123 *
124 * This is computed from the DB key by replacing any underscores with spaces.
125 *
126 * @note To get a title string that includes the namespace and/or fragment,
127 * use a TitleFormatter.
128 *
129 * @return string
130 */
131 public function getText() {
132 return str_replace( '_', ' ', $this->getDBkey() );
133 }
134
135 /**
136 * Creates a new TitleValue for a different fragment of the same page.
137 *
138 * @param string $fragment The fragment name, or "" for the entire page.
139 *
140 * @return TitleValue
141 */
142 public function createFragmentTitle( $fragment ) {
143 return new TitleValue( $this->namespace, $this->dbkey, $fragment );
144 }
145
146 /**
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.
150 *
151 * @return string
152 */
153 public function __toString() {
154 $name = $this->namespace . ':' . $this->dbkey;
155
156 if ( $this->fragment !== '' ) {
157 $name .= '#' . $this->fragment;
158 }
159
160 return $name;
161 }
162 }