MediaWiki  master
TitleValue.php
Go to the documentation of this file.
1 <?php
25 use Wikimedia\Assert\Assert;
26 use Wikimedia\Assert\ParameterAssertionException;
27 use Wikimedia\Assert\ParameterTypeException;
28 
40 class 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( PageReference $page ): TitleValue {
120  return new TitleValue( $page->getNamespace(), $page->getDBkey() );
121  }
122 
136  public static function castPageToLinkTarget( ?PageReference $page ): ?LinkTarget {
137  if ( !$page || $page instanceof LinkTarget ) {
138  return $page;
139  }
140 
141  return self::newFromPage( $page );
142  }
143 
163  public function __construct( $namespace, $title, $fragment = '', $interwiki = '' ) {
165 
166  $this->namespace = $namespace;
167  $this->dbkey = strtr( $title, ' ', '_' );
168  $this->fragment = $fragment;
169  $this->interwiki = $interwiki;
170  }
171 
185  public static function assertValidSpec( $namespace, $title, $fragment = '', $interwiki = '' ) {
186  if ( !is_int( $namespace ) ) {
187  throw new ParameterTypeException( '$namespace', 'int' );
188  }
189  if ( !is_string( $title ) ) {
190  throw new ParameterTypeException( '$title', 'string' );
191  }
192  if ( !is_string( $fragment ) ) {
193  throw new ParameterTypeException( '$fragment', 'string' );
194  }
195  if ( !is_string( $interwiki ) ) {
196  throw new ParameterTypeException( '$interwiki', 'string' );
197  }
198 
199  Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title',
200  "invalid name '$title'" );
201 
202  // NOTE: As of MW 1.34, [[#]] is rendered as a valid link, pointing to the empty
203  // page title, effectively leading to the wiki's main page. This means that a completely
204  // empty TitleValue has to be considered valid, for consistency with Title.
205  // Also note that [[#foo]] is a valid on-page section links, and that [[acme:#foo]] is
206  // a valid interwiki link.
207  Assert::parameter(
208  $title !== '' || $namespace === NS_MAIN,
209  '$title',
210  'should not be empty unless namespace is main'
211  );
212  }
213 
218  public function getNamespace() {
219  return $this->namespace;
220  }
221 
227  public function inNamespace( $ns ) {
228  return $this->namespace == $ns;
229  }
230 
235  public function getFragment() {
236  return $this->fragment;
237  }
238 
243  public function hasFragment() {
244  return $this->fragment !== '';
245  }
246 
254  public function getDBkey() {
255  return $this->dbkey;
256  }
257 
270  public function getText() {
271  return str_replace( '_', ' ', $this->dbkey );
272  }
273 
282  public function createFragmentTarget( $fragment ) {
283  return new TitleValue(
284  $this->namespace,
285  $this->dbkey,
286  $fragment,
287  $this->interwiki
288  );
289  }
290 
297  public function isExternal() {
298  return $this->interwiki !== '';
299  }
300 
307  public function getInterwiki() {
308  return $this->interwiki;
309  }
310 
319  public function __toString(): string {
320  $name = $this->namespace . ':' . $this->dbkey;
321 
322  if ( $this->fragment !== '' ) {
323  $name .= '#' . $this->fragment;
324  }
325 
326  if ( $this->interwiki !== '' ) {
327  $name = $this->interwiki . ':' . $name;
328  }
329 
330  return $name;
331  }
332 
338  public function isSameLinkAs( LinkTarget $other ) {
339  // NOTE: keep in sync with Title::isSameLinkAs()!
340  return ( $other->getInterwiki() === $this->getInterwiki() )
341  && ( $other->getDBkey() === $this->getDBkey() )
342  && ( $other->getNamespace() === $this->getNamespace() )
343  && ( $other->getFragment() === $this->getFragment() );
344  }
345 }
MediaWiki\Linker\LinkTarget\getInterwiki
getInterwiki()
The interwiki component of this LinkTarget.
TitleValue\$prefixedText
string $prefixedText
Text form including namespace/interwiki, initialised on demand.
Definition: TitleValue.php:74
if
if(ini_get( 'mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition: Setup.php:88
TitleValue\isSameLinkAs
isSameLinkAs(LinkTarget $other)
Definition: TitleValue.php:338
TitleValue\$namespace
int $namespace
Definition: TitleValue.php:46
TitleValue\isExternal
isExternal()
Whether it has an interwiki part.
Definition: TitleValue.php:297
TitleValue\createFragmentTarget
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
Definition: TitleValue.php:282
TitleValue\$interwiki
string $interwiki
Definition: TitleValue.php:64
Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition: PageReference.php:49
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
TitleValue\$fragment
string $fragment
Definition: TitleValue.php:58
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
TitleValue\castPageToLinkTarget
static castPageToLinkTarget(?PageReference $page)
Casts a PageReference to a LinkTarget.
Definition: TitleValue.php:136
TitleValue\newFromPage
static newFromPage(PageReference $page)
Constructs a TitleValue from a local PageReference.
Definition: TitleValue.php:119
TitleValue\getDBkey
getDBkey()
Returns the title's DB key, as supplied to the constructor, without namespace prefix or fragment.
Definition: TitleValue.php:254
TitleValue\tryNew
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:94
Page\PageReference\getNamespace
getNamespace()
Returns the page's namespace number.
$title
$title
Definition: testCompression.php:38
TitleValue\__toString
__toString()
Returns a string representation of the title, for logging.
Definition: TitleValue.php:319
TitleValue\assertValidSpec
static assertValidSpec( $namespace, $title, $fragment='', $interwiki='')
Asserts that the given parameters could be used to construct a TitleValue object.
Definition: TitleValue.php:185
TitleValue\hasFragment
hasFragment()
Definition: TitleValue.php:243
Page\PageReference\getDBkey
getDBkey()
Get the page title in DB key form.
TitleValue\getFragment
getFragment()
Definition: TitleValue.php:235
TitleValue\getText
getText()
Returns the title in text form, without namespace prefix or fragment.
Definition: TitleValue.php:270
TitleValue\getInterwiki
getInterwiki()
Returns the interwiki part.
Definition: TitleValue.php:307
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
MediaWiki\Linker\LinkTarget\getFragment
getFragment()
Get the link fragment (i.e.
TitleValue\getNamespace
getNamespace()
Definition: TitleValue.php:218
TitleValue\$dbkey
string $dbkey
Definition: TitleValue.php:52
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
TitleValue\__construct
__construct( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue.
Definition: TitleValue.php:163
TitleValue\inNamespace
inNamespace( $ns)
Definition: TitleValue.php:227
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40