18a06ea01d276250f8d7866d5c74884f4fd19392
[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 use Wikimedia\Assert\Assert;
25
26 /**
27 * Represents a page (or page fragment) title within %MediaWiki.
28 *
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.
31 *
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.
34 *
35 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
36 * @since 1.23
37 */
38 class TitleValue implements LinkTarget {
39 /**
40 * @var int
41 */
42 protected $namespace;
43
44 /**
45 * @var string
46 */
47 protected $dbkey;
48
49 /**
50 * @var string
51 */
52 protected $fragment;
53
54 /**
55 * Constructs a TitleValue.
56 *
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.
62 *
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.
67 *
68 * @throws InvalidArgumentException
69 */
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' );
74
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' );
78
79 $this->namespace = $namespace;
80 $this->dbkey = $dbkey;
81 $this->fragment = $fragment;
82 }
83
84 /**
85 * @return int
86 */
87 public function getNamespace() {
88 return $this->namespace;
89 }
90
91 /**
92 * @return string
93 */
94 public function getFragment() {
95 return $this->fragment;
96 }
97
98 /**
99 * @since 1.27
100 * @return bool
101 */
102 public function hasFragment() {
103 return $this->fragment !== '';
104 }
105
106 /**
107 * Returns the title's DB key, as supplied to the constructor,
108 * without namespace prefix or fragment.
109 *
110 * @return string
111 */
112 public function getDBkey() {
113 return $this->dbkey;
114 }
115
116 /**
117 * Returns the title in text form,
118 * without namespace prefix or fragment.
119 *
120 * This is computed from the DB key by replacing any underscores with spaces.
121 *
122 * @note To get a title string that includes the namespace and/or fragment,
123 * use a TitleFormatter.
124 *
125 * @return string
126 */
127 public function getText() {
128 return str_replace( '_', ' ', $this->getDBkey() );
129 }
130
131 /**
132 * Creates a new TitleValue for a different fragment of the same page.
133 *
134 * @param string $fragment The fragment name, or "" for the entire page.
135 *
136 * @return TitleValue
137 */
138 public function createFragmentTitle( $fragment ) {
139 return new TitleValue( $this->namespace, $this->dbkey, $fragment );
140 }
141
142 /**
143 * Returns a string representation of the title, for logging. This is purely informative
144 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
145 * the correct string representation for a given use.
146 *
147 * @return string
148 */
149 public function __toString() {
150 $name = $this->namespace . ':' . $this->dbkey;
151
152 if ( $this->fragment !== '' ) {
153 $name .= '#' . $this->fragment;
154 }
155
156 return $name;
157 }
158 }