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 
43  private $namespace;
44 
46  private $dbkey;
47 
49  private $fragment;
50 
52  private $interwiki;
53 
62  public $prefixedText = null;
63 
82  public static function tryNew( $namespace, $title, $fragment = '', $interwiki = '' ) {
83  if ( !is_int( $namespace ) ) {
84  throw new ParameterTypeException( '$namespace', 'int' );
85  }
86 
87  try {
88  return new static( $namespace, $title, $fragment, $interwiki );
89  } catch ( ParameterAssertionException $ex ) {
90  return null;
91  }
92  }
93 
107  public static function newFromPage( PageReference $page ): TitleValue {
108  return new TitleValue( $page->getNamespace(), $page->getDBkey() );
109  }
110 
124  public static function castPageToLinkTarget( ?PageReference $page ): ?LinkTarget {
125  if ( !$page || $page instanceof LinkTarget ) {
126  return $page;
127  }
128 
129  return self::newFromPage( $page );
130  }
131 
151  public function __construct( $namespace, $title, $fragment = '', $interwiki = '' ) {
153 
154  $this->namespace = $namespace;
155  $this->dbkey = strtr( $title, ' ', '_' );
156  $this->fragment = $fragment;
157  $this->interwiki = $interwiki;
158  }
159 
173  public static function assertValidSpec( $namespace, $title, $fragment = '', $interwiki = '' ) {
174  if ( !is_int( $namespace ) ) {
175  throw new ParameterTypeException( '$namespace', 'int' );
176  }
177  if ( !is_string( $title ) ) {
178  throw new ParameterTypeException( '$title', 'string' );
179  }
180  if ( !is_string( $fragment ) ) {
181  throw new ParameterTypeException( '$fragment', 'string' );
182  }
183  if ( !is_string( $interwiki ) ) {
184  throw new ParameterTypeException( '$interwiki', 'string' );
185  }
186 
187  Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title',
188  "invalid name '$title'" );
189 
190  // NOTE: As of MW 1.34, [[#]] is rendered as a valid link, pointing to the empty
191  // page title, effectively leading to the wiki's main page. This means that a completely
192  // empty TitleValue has to be considered valid, for consistency with Title.
193  // Also note that [[#foo]] is a valid on-page section links, and that [[acme:#foo]] is
194  // a valid interwiki link.
195  Assert::parameter(
196  $title !== '' || $namespace === NS_MAIN,
197  '$title',
198  'should not be empty unless namespace is main'
199  );
200  }
201 
206  public function getNamespace(): int {
207  return $this->namespace;
208  }
209 
215  public function inNamespace( int $ns ): bool {
216  return $this->namespace == $ns;
217  }
218 
223  public function getFragment(): string {
224  return $this->fragment;
225  }
226 
231  public function hasFragment(): bool {
232  return $this->fragment !== '';
233  }
234 
242  public function getDBkey(): string {
243  return $this->dbkey;
244  }
245 
258  public function getText(): string {
259  return str_replace( '_', ' ', $this->dbkey );
260  }
261 
270  public function createFragmentTarget( string $fragment ): self {
271  return new TitleValue(
272  $this->namespace,
273  $this->dbkey,
274  $fragment,
275  $this->interwiki
276  );
277  }
278 
285  public function isExternal(): bool {
286  return $this->interwiki !== '';
287  }
288 
295  public function getInterwiki(): string {
296  return $this->interwiki;
297  }
298 
307  public function __toString(): string {
308  $name = $this->namespace . ':' . $this->dbkey;
309 
310  if ( $this->fragment !== '' ) {
311  $name .= '#' . $this->fragment;
312  }
313 
314  if ( $this->interwiki !== '' ) {
315  $name = $this->interwiki . ':' . $name;
316  }
317 
318  return $name;
319  }
320 
326  public function isSameLinkAs( LinkTarget $other ): bool {
327  // NOTE: keep in sync with Title::isSameLinkAs()!
328  return ( $other->getInterwiki() === $this->getInterwiki() )
329  && ( $other->getDBkey() === $this->getDBkey() )
330  && ( $other->getNamespace() === $this->getNamespace() )
331  && ( $other->getFragment() === $this->getFragment() );
332  }
333 }
TitleValue\inNamespace
inNamespace(int $ns)
Definition: TitleValue.php:215
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:62
if
if(ini_get( 'mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition: Setup.php:92
TitleValue\isSameLinkAs
isSameLinkAs(LinkTarget $other)
Definition: TitleValue.php:326
TitleValue\$namespace
int $namespace
Definition: TitleValue.php:43
TitleValue\isExternal
isExternal()
Whether it has an interwiki part.
Definition: TitleValue.php:285
TitleValue\$interwiki
string $interwiki
Definition: TitleValue.php:52
TitleValue\createFragmentTarget
createFragmentTarget(string $fragment)
Creates a new TitleValue for a different fragment of the same page.
Definition: TitleValue.php:270
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:49
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
TitleValue\castPageToLinkTarget
static castPageToLinkTarget(?PageReference $page)
Casts a PageReference to a LinkTarget.
Definition: TitleValue.php:124
TitleValue\newFromPage
static newFromPage(PageReference $page)
Constructs a TitleValue from a local PageReference.
Definition: TitleValue.php:107
TitleValue\getDBkey
getDBkey()
Returns the title's DB key, as supplied to the constructor, without namespace prefix or fragment.
Definition: TitleValue.php:242
TitleValue\tryNew
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:82
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:307
TitleValue\assertValidSpec
static assertValidSpec( $namespace, $title, $fragment='', $interwiki='')
Asserts that the given parameters could be used to construct a TitleValue object.
Definition: TitleValue.php:173
TitleValue\hasFragment
hasFragment()
Definition: TitleValue.php:231
Page\PageReference\getDBkey
getDBkey()
Get the page title in DB key form.
TitleValue\getFragment
getFragment()
Definition: TitleValue.php:223
TitleValue\getText
getText()
Returns the title in text form, without namespace prefix or fragment.
Definition: TitleValue.php:258
TitleValue\getInterwiki
getInterwiki()
Returns the interwiki part.
Definition: TitleValue.php:295
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:206
TitleValue\$dbkey
string $dbkey
Definition: TitleValue.php:46
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
TitleValue\__construct
__construct( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue.
Definition: TitleValue.php:151
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40