MediaWiki REL1_35
TitleValue.php
Go to the documentation of this file.
1<?php
24use Wikimedia\Assert\Assert;
25use Wikimedia\Assert\ParameterAssertionException;
26use Wikimedia\Assert\ParameterTypeException;
27
39class TitleValue implements LinkTarget {
40
45 protected $namespace;
46
51 protected $dbkey;
52
57 protected $fragment;
58
63 protected $interwiki;
64
73 public $prefixedText = null;
74
93 public static function tryNew( $namespace, $title, $fragment = '', $interwiki = '' ) {
94 if ( !is_int( $namespace ) ) {
95 throw new ParameterTypeException( '$namespace', 'int' );
96 }
97
98 try {
99 return new static( $namespace, $title, $fragment, $interwiki );
100 } catch ( ParameterAssertionException $ex ) {
101 return null;
102 }
103 }
104
124 public function __construct( $namespace, $title, $fragment = '', $interwiki = '' ) {
126
127 $this->namespace = $namespace;
128 $this->dbkey = strtr( $title, ' ', '_' );
129 $this->fragment = $fragment;
130 $this->interwiki = $interwiki;
131 }
132
146 public static function assertValidSpec( $namespace, $title, $fragment = '', $interwiki = '' ) {
147 if ( !is_int( $namespace ) ) {
148 throw new ParameterTypeException( '$namespace', 'int' );
149 }
150 if ( !is_string( $title ) ) {
151 throw new ParameterTypeException( '$title', 'string' );
152 }
153 if ( !is_string( $fragment ) ) {
154 throw new ParameterTypeException( '$fragment', 'string' );
155 }
156 if ( !is_string( $interwiki ) ) {
157 throw new ParameterTypeException( '$interwiki', 'string' );
158 }
159
160 Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title',
161 "invalid name '$title'" );
162
163 // NOTE: As of MW 1.34, [[#]] is rendered as a valid link, pointing to the empty
164 // page title, effectively leading to the wiki's main page. This means that a completely
165 // empty TitleValue has to be considered valid, for consistency with Title.
166 // Also note that [[#foo]] is a valid on-page section links, and that [[acme:#foo]] is
167 // a valid interwiki link.
168 Assert::parameter(
169 $title !== '' || $namespace === NS_MAIN,
170 '$title',
171 'should not be empty unless namespace is main'
172 );
173 }
174
179 public function getNamespace() {
180 return $this->namespace;
181 }
182
188 public function inNamespace( $ns ) {
189 return $this->namespace == $ns;
190 }
191
196 public function getFragment() {
197 return $this->fragment;
198 }
199
204 public function hasFragment() {
205 return $this->fragment !== '';
206 }
207
215 public function getDBkey() {
216 return $this->dbkey;
217 }
218
231 public function getText() {
232 return str_replace( '_', ' ', $this->dbkey );
233 }
234
243 public function createFragmentTarget( $fragment ) {
244 return new TitleValue(
245 $this->namespace,
246 $this->dbkey,
247 $fragment,
248 $this->interwiki
249 );
250 }
251
258 public function isExternal() {
259 return $this->interwiki !== '';
260 }
261
268 public function getInterwiki() {
269 return $this->interwiki;
270 }
271
280 public function __toString() {
281 $name = $this->namespace . ':' . $this->dbkey;
282
283 if ( $this->fragment !== '' ) {
284 $name .= '#' . $this->fragment;
285 }
286
287 if ( $this->interwiki !== '' ) {
288 $name = $this->interwiki . ':' . $name;
289 }
290
291 return $name;
292 }
293}
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:85
Represents a page (or page fragment) title within MediaWiki.
isExternal()
Whether it has an interwiki part.
__construct( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue.
string $fragment
getInterwiki()
Returns the interwiki part.
static assertValidSpec( $namespace, $title, $fragment='', $interwiki='')
Asserts that the given parameters could be used to construct a TitleValue object.
string $prefixedText
Text form including namespace/interwiki, initialised on demand.
inNamespace( $ns)
getText()
Returns the title in text form, without namespace prefix or fragment.
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
string $interwiki
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
getDBkey()
Returns the title's DB key, as supplied to the constructor, without namespace prefix or fragment.
string $dbkey
__toString()
Returns a string representation of the title, for logging.
const NS_MAIN
Definition Defines.php:70