Merge "Http::getProxy() method to get proxy configuration"
[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 * Returns the title's DB key, as supplied to the constructor,
100 * without namespace prefix or fragment.
101 *
102 * @return string
103 */
104 public function getDBkey() {
105 return $this->dbkey;
106 }
107
108 /**
109 * Returns the title in text form,
110 * without namespace prefix or fragment.
111 *
112 * This is computed from the DB key by replacing any underscores with spaces.
113 *
114 * @note To get a title string that includes the namespace and/or fragment,
115 * use a TitleFormatter.
116 *
117 * @return string
118 */
119 public function getText() {
120 return str_replace( '_', ' ', $this->getDBkey() );
121 }
122
123 /**
124 * Creates a new TitleValue for a different fragment of the same page.
125 *
126 * @param string $fragment The fragment name, or "" for the entire page.
127 *
128 * @return TitleValue
129 */
130 public function createFragmentTitle( $fragment ) {
131 return new TitleValue( $this->namespace, $this->dbkey, $fragment );
132 }
133
134 /**
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.
138 *
139 * @return string
140 */
141 public function __toString() {
142 $name = $this->namespace . ':' . $this->dbkey;
143
144 if ( $this->fragment !== '' ) {
145 $name .= '#' . $this->fragment;
146 }
147
148 return $name;
149 }
150 }