Merge "Migration of browsertests* Jenkins jobs to selenium* jobs"
[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 MediaWiki\Linker\LinkTarget;
25 use Wikimedia\Assert\Assert;
26
27 /**
28 * Represents a page (or page fragment) title within %MediaWiki.
29 *
30 * @note In contrast to Title, this is designed to be a plain value object. That is,
31 * it is immutable, does not use global state, and causes no side effects.
32 *
33 * @note TitleValue represents the title of a local page (or fragment of a page).
34 * It does not represent a link, and does not support interwiki prefixes etc.
35 *
36 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
37 * @since 1.23
38 */
39 class TitleValue implements LinkTarget {
40 /**
41 * @var int
42 */
43 protected $namespace;
44
45 /**
46 * @var string
47 */
48 protected $dbkey;
49
50 /**
51 * @var string
52 */
53 protected $fragment;
54
55 /**
56 * Constructs a TitleValue.
57 *
58 * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either
59 * from a database entry, or by a TitleParser. We could apply "some" normalization here,
60 * such as substituting spaces by underscores, but that would encourage the use of
61 * un-normalized text when constructing TitleValues. For constructing a TitleValue from
62 * user input or external sources, use a TitleParser.
63 *
64 * @param int $namespace The namespace ID. This is not validated.
65 * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
66 * @param string $fragment The fragment title. Use '' to represent the whole page.
67 * No validation or normalization is applied.
68 *
69 * @throws InvalidArgumentException
70 */
71 public function __construct( $namespace, $dbkey, $fragment = '' ) {
72 Assert::parameterType( 'integer', $namespace, '$namespace' );
73 Assert::parameterType( 'string', $dbkey, '$dbkey' );
74 Assert::parameterType( 'string', $fragment, '$fragment' );
75
76 // Sanity check, no full validation or normalization applied here!
77 Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey', 'invalid DB key' );
78 Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
79
80 $this->namespace = $namespace;
81 $this->dbkey = $dbkey;
82 $this->fragment = $fragment;
83 }
84
85 /**
86 * @return int
87 */
88 public function getNamespace() {
89 return $this->namespace;
90 }
91
92 /**
93 * @return string
94 */
95 public function getFragment() {
96 return $this->fragment;
97 }
98
99 /**
100 * @since 1.27
101 * @return bool
102 */
103 public function hasFragment() {
104 return $this->fragment !== '';
105 }
106
107 /**
108 * Returns the title's DB key, as supplied to the constructor,
109 * without namespace prefix or fragment.
110 *
111 * @return string
112 */
113 public function getDBkey() {
114 return $this->dbkey;
115 }
116
117 /**
118 * Returns the title in text form,
119 * without namespace prefix or fragment.
120 *
121 * This is computed from the DB key by replacing any underscores with spaces.
122 *
123 * @note To get a title string that includes the namespace and/or fragment,
124 * use a TitleFormatter.
125 *
126 * @return string
127 */
128 public function getText() {
129 return str_replace( '_', ' ', $this->getDBkey() );
130 }
131
132 /**
133 * Creates a new TitleValue for a different fragment of the same page.
134 *
135 * @since 1.27
136 * @param string $fragment The fragment name, or "" for the entire page.
137 *
138 * @return TitleValue
139 */
140 public function createFragmentTarget( $fragment ) {
141 return new TitleValue( $this->namespace, $this->dbkey, $fragment );
142 }
143
144 /**
145 * Returns a string representation of the title, for logging. This is purely informative
146 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
147 * the correct string representation for a given use.
148 *
149 * @return string
150 */
151 public function __toString() {
152 $name = $this->namespace . ':' . $this->dbkey;
153
154 if ( $this->fragment !== '' ) {
155 $name .= '#' . $this->fragment;
156 }
157
158 return $name;
159 }
160 }