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( 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 = '' ) {
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() {
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 }
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
Page\PageIdentity\getDBkey
getDBkey()
Get the page title in DB key form.
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:85
TitleValue\$namespace
int $namespace
Definition: TitleValue.php:46
TitleValue\isExternal
isExternal()
Whether it has an interwiki part.
Definition: TitleValue.php:283
TitleValue\createFragmentTarget
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
Definition: TitleValue.php:268
TitleValue\$interwiki
string $interwiki
Definition: TitleValue.php:64
NS_MAIN
const NS_MAIN
Definition: Defines.php:63
TitleValue\$fragment
string $fragment
Definition: TitleValue.php:58
TitleValue\getDBkey
getDBkey()
Returns the title's DB key, as supplied to the constructor, without namespace prefix or fragment.
Definition: TitleValue.php:240
TitleValue\tryNew
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:94
$title
$title
Definition: testCompression.php:38
TitleValue\newFromPage
static newFromPage(PageIdentity $page)
Constructs a TitleValue from a local PageIdentity.
Definition: TitleValue.php:119
TitleValue\__toString
__toString()
Returns a string representation of the title, for logging.
Definition: TitleValue.php:305
TitleValue\assertValidSpec
static assertValidSpec( $namespace, $title, $fragment='', $interwiki='')
Asserts that the given parameters could be used to construct a TitleValue object.
Definition: TitleValue.php:171
TitleValue\hasFragment
hasFragment()
Definition: TitleValue.php:229
TitleValue\getFragment
getFragment()
Definition: TitleValue.php:221
TitleValue\getText
getText()
Returns the title in text form, without namespace prefix or fragment.
Definition: TitleValue.php:256
TitleValue\getInterwiki
getInterwiki()
Returns the interwiki part.
Definition: TitleValue.php:293
TitleValue\getNamespace
getNamespace()
Definition: TitleValue.php:204
TitleValue\$dbkey
string $dbkey
Definition: TitleValue.php:52
Page\PageIdentity\getWikiId
getWikiId()
Get the ID of the wiki this page belongs to.
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
TitleValue\__construct
__construct( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue.
Definition: TitleValue.php:149
TitleValue\inNamespace
inNamespace( $ns)
Definition: TitleValue.php:213
Page\PageIdentity\getNamespace
getNamespace()
Returns the page's namespace number.
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40