MediaWiki REL1_36
TitleValue.php
Go to the documentation of this file.
1<?php
25use Wikimedia\Assert\Assert;
26use Wikimedia\Assert\ParameterAssertionException;
27use Wikimedia\Assert\ParameterTypeException;
28
40class TitleValue implements LinkTarget {
41
46 protected $namespace;
47
52 protected $dbkey;
53
58 protected $fragment;
59
64 protected $interwiki;
65
74 public $prefixedText = null;
75
94 public static function tryNew( $namespace, $title, $fragment = '', $interwiki = '' ) {
95 if ( !is_int( $namespace ) ) {
96 throw new ParameterTypeException( '$namespace', 'int' );
97 }
98
99 try {
100 return new static( $namespace, $title, $fragment, $interwiki );
101 } catch ( ParameterAssertionException $ex ) {
102 return null;
103 }
104 }
105
119 public static function newFromPage( PageIdentity $page ) : TitleValue {
120 if ( $page->getWikiId() ) {
121 // TODO: we could allow "foreign" PageIdentities by providing an interwiki prefix,
122 // but the exact semantics seem unclear. For instance, would the interwiki prefix
123 // be valid in the context of the local wiki, or the wiki indicated by getWikiId()?
124 throw new InvalidArgumentException( 'Not a local PageIdentity: ' . $page );
125 }
126
127 return new TitleValue( $page->getNamespace(), $page->getDBkey() );
128 }
129
149 public function __construct( $namespace, $title, $fragment = '', $interwiki = '' ) {
150 self::assertValidSpec( $namespace, $title, $fragment, $interwiki );
151
152 $this->namespace = $namespace;
153 $this->dbkey = strtr( $title, ' ', '_' );
154 $this->fragment = $fragment;
155 $this->interwiki = $interwiki;
156 }
157
171 public static function assertValidSpec( $namespace, $title, $fragment = '', $interwiki = '' ) {
172 if ( !is_int( $namespace ) ) {
173 throw new ParameterTypeException( '$namespace', 'int' );
174 }
175 if ( !is_string( $title ) ) {
176 throw new ParameterTypeException( '$title', 'string' );
177 }
178 if ( !is_string( $fragment ) ) {
179 throw new ParameterTypeException( '$fragment', 'string' );
180 }
181 if ( !is_string( $interwiki ) ) {
182 throw new ParameterTypeException( '$interwiki', 'string' );
183 }
184
185 Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title',
186 "invalid name '$title'" );
187
188 // NOTE: As of MW 1.34, [[#]] is rendered as a valid link, pointing to the empty
189 // page title, effectively leading to the wiki's main page. This means that a completely
190 // empty TitleValue has to be considered valid, for consistency with Title.
191 // Also note that [[#foo]] is a valid on-page section links, and that [[acme:#foo]] is
192 // a valid interwiki link.
193 Assert::parameter(
194 $title !== '' || $namespace === NS_MAIN,
195 '$title',
196 'should not be empty unless namespace is main'
197 );
198 }
199
204 public function getNamespace() {
205 return $this->namespace;
206 }
207
213 public function inNamespace( $ns ) {
214 return $this->namespace == $ns;
215 }
216
221 public function getFragment() {
222 return $this->fragment;
223 }
224
229 public function hasFragment() {
230 return $this->fragment !== '';
231 }
232
240 public function getDBkey() {
241 return $this->dbkey;
242 }
243
256 public function getText() {
257 return str_replace( '_', ' ', $this->dbkey );
258 }
259
268 public function createFragmentTarget( $fragment ) {
269 return new TitleValue(
270 $this->namespace,
271 $this->dbkey,
272 $fragment,
273 $this->interwiki
274 );
275 }
276
283 public function isExternal() {
284 return $this->interwiki !== '';
285 }
286
293 public function getInterwiki() {
294 return $this->interwiki;
295 }
296
305 public function __toString(): string {
306 $name = $this->namespace . ':' . $this->dbkey;
307
308 if ( $this->fragment !== '' ) {
309 $name .= '#' . $this->fragment;
310 }
311
312 if ( $this->interwiki !== '' ) {
313 $name = $this->interwiki . ':' . $name;
314 }
315
316 return $name;
317 }
318
324 public function isSameLinkAs( LinkTarget $other ) {
325 // NOTE: keep in sync with Title::isSameLinkAs()!
326 return ( $other->getInterwiki() === $this->getInterwiki() )
327 && ( $other->getDBkey() === $this->getDBkey() )
328 && ( $other->getNamespace() === $this->getNamespace() )
329 && ( $other->getFragment() === $this->getFragment() );
330 }
331}
const NS_MAIN
Definition Defines.php:64
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:87
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.
isSameLinkAs(LinkTarget $other)
static newFromPage(PageIdentity $page)
Constructs a TitleValue from a local PageIdentity.
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.
getInterwiki()
The interwiki component of this LinkTarget.
getFragment()
Get the link fragment (i.e.
getNamespace()
Get the namespace index.
getDBkey()
Get the main part with underscores.
Interface for objects (potentially) representing an editable wiki page.