MediaWiki  master
TitleValue.php
Go to the documentation of this file.
1 <?php
27 
37 class TitleValue implements LinkTarget {
38 
43  protected $namespace;
44 
49  protected $dbkey;
50 
55  protected $fragment;
56 
61  protected $interwiki;
62 
71  public $prefixedText = null;
72 
91  public static function tryNew( $namespace, $title, $fragment = '', $interwiki = '' ) {
92  if ( !is_int( $namespace ) ) {
93  throw new ParameterTypeException( '$namespace', 'int' );
94  }
95 
96  try {
97  return new static( $namespace, $title, $fragment, $interwiki );
98  } catch ( ParameterAssertionException $ex ) {
99  return null;
100  }
101  }
102 
120  public function __construct( $namespace, $title, $fragment = '', $interwiki = '' ) {
121  self::assertValidSpec( $namespace, $title, $fragment, $interwiki );
122 
123  $this->namespace = $namespace;
124  $this->dbkey = strtr( $title, ' ', '_' );
125  $this->fragment = $fragment;
126  $this->interwiki = $interwiki;
127  }
128 
142  public static function assertValidSpec( $namespace, $title, $fragment = '', $interwiki = '' ) {
143  if ( !is_int( $namespace ) ) {
144  throw new ParameterTypeException( '$namespace', 'int' );
145  }
146  if ( !is_string( $title ) ) {
147  throw new ParameterTypeException( '$title', 'string' );
148  }
149  if ( !is_string( $fragment ) ) {
150  throw new ParameterTypeException( '$fragment', 'string' );
151  }
152  if ( !is_string( $interwiki ) ) {
153  throw new ParameterTypeException( '$interwiki', 'string' );
154  }
155 
156  Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title',
157  "invalid name '$title'" );
158 
159  // NOTE: As of MW 1.34, [[#]] is rendered as a valid link, pointing to the empty
160  // page title, effectively leading to the wiki's main page. This means that a completely
161  // empty TitleValue has to be considered valid, for consistency with Title.
162  // Also note that [[#foo]] is a valid on-page section links, and that [[acme:#foo]] is
163  // a valid interwiki link.
164  Assert::parameter(
165  $title !== '' || $namespace === NS_MAIN,
166  '$title',
167  'should not be empty unless namespace is main'
168  );
169  }
170 
175  public function getNamespace() {
176  return $this->namespace;
177  }
178 
184  public function inNamespace( $ns ) {
185  return $this->namespace == $ns;
186  }
187 
192  public function getFragment() {
193  return $this->fragment;
194  }
195 
200  public function hasFragment() {
201  return $this->fragment !== '';
202  }
203 
211  public function getDBkey() {
212  return $this->dbkey;
213  }
214 
227  public function getText() {
228  return str_replace( '_', ' ', $this->dbkey );
229  }
230 
239  public function createFragmentTarget( $fragment ) {
240  return new TitleValue(
241  $this->namespace,
242  $this->dbkey,
243  $fragment,
244  $this->interwiki
245  );
246  }
247 
254  public function isExternal() {
255  return $this->interwiki !== '';
256  }
257 
264  public function getInterwiki() {
265  return $this->interwiki;
266  }
267 
276  public function __toString() {
277  $name = $this->namespace . ':' . $this->dbkey;
278 
279  if ( $this->fragment !== '' ) {
280  $name .= '#' . $this->fragment;
281  }
282 
283  if ( $this->interwiki !== '' ) {
284  $name = $this->interwiki . ':' . $name;
285  }
286 
287  return $name;
288  }
289 }
getText()
Returns the title in text form, without namespace prefix or fragment.
Definition: TitleValue.php:227
getInterwiki()
Returns the interwiki part.
Definition: TitleValue.php:264
const NS_MAIN
Definition: Defines.php:60
string $dbkey
Definition: TitleValue.php:49
__toString()
Returns a string representation of the title, for logging.
Definition: TitleValue.php:276
if(ini_get( 'mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition: Setup.php:58
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:37
static assertValidSpec( $namespace, $title, $fragment='', $interwiki='')
Asserts that the given parameters could be used to construct a TitleValue object. ...
Definition: TitleValue.php:142
__construct( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue.
Definition: TitleValue.php:120
inNamespace( $ns)
Definition: TitleValue.php:184
string $prefixedText
Text form including namespace/interwiki, initialised on demand.
Definition: TitleValue.php:71
int $namespace
Definition: TitleValue.php:43
isExternal()
Whether it has an interwiki part.
Definition: TitleValue.php:254
string $interwiki
Definition: TitleValue.php:61
string $fragment
Definition: TitleValue.php:55
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
Definition: TitleValue.php:239
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:91
getDBkey()
Returns the title&#39;s DB key, as supplied to the constructor, without namespace prefix or fragment...
Definition: TitleValue.php:211