MediaWiki  master
TitleValue.php
Go to the documentation of this file.
1 <?php
26 
36 class TitleValue implements LinkTarget {
37 
42  protected $namespace;
43 
48  protected $dbkey;
49 
54  protected $fragment;
55 
60  protected $interwiki;
61 
70  public $prefixedText = null;
71 
88  public function __construct( $namespace, $title, $fragment = '', $interwiki = '' ) {
89  self::assertValidSpec( $namespace, $title, $fragment, $interwiki );
90 
91  $this->namespace = $namespace;
92  $this->dbkey = strtr( $title, ' ', '_' );
93  $this->fragment = $fragment;
94  $this->interwiki = $interwiki;
95  }
96 
110  public static function assertValidSpec( $namespace, $title, $fragment = '', $interwiki = '' ) {
111  if ( !is_int( $namespace ) ) {
112  throw new ParameterTypeException( '$namespace', 'int' );
113  }
114  if ( !is_string( $title ) ) {
115  throw new ParameterTypeException( '$title', 'string' );
116  }
117  if ( !is_string( $fragment ) ) {
118  throw new ParameterTypeException( '$fragment', 'string' );
119  }
120  if ( !is_string( $interwiki ) ) {
121  throw new ParameterTypeException( '$interwiki', 'string' );
122  }
123 
124  Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title',
125  "invalid name '$title'" );
126 
127  // NOTE: As of MW 1.34, [[#]] is rendered as a valid link, pointing to the empty
128  // page title, effectively leading to the wiki's main page. This means that a completely
129  // empty TitleValue has to be considered valid, for consistency with Title.
130  // Also note that [[#foo]] is a valid on-page section links, and that [[acme:#foo]] is
131  // a valid interwiki link.
132  Assert::parameter(
133  $title !== '' || $namespace === NS_MAIN,
134  '$title',
135  'should not be empty unless namespace is main'
136  );
137  }
138 
143  public function getNamespace() {
144  return $this->namespace;
145  }
146 
152  public function inNamespace( $ns ) {
153  return $this->namespace == $ns;
154  }
155 
160  public function getFragment() {
161  return $this->fragment;
162  }
163 
168  public function hasFragment() {
169  return $this->fragment !== '';
170  }
171 
179  public function getDBkey() {
180  return $this->dbkey;
181  }
182 
195  public function getText() {
196  return str_replace( '_', ' ', $this->dbkey );
197  }
198 
207  public function createFragmentTarget( $fragment ) {
208  return new TitleValue(
209  $this->namespace,
210  $this->dbkey,
211  $fragment,
212  $this->interwiki
213  );
214  }
215 
222  public function isExternal() {
223  return $this->interwiki !== '';
224  }
225 
232  public function getInterwiki() {
233  return $this->interwiki;
234  }
235 
244  public function __toString() {
245  $name = $this->namespace . ':' . $this->dbkey;
246 
247  if ( $this->fragment !== '' ) {
248  $name .= '#' . $this->fragment;
249  }
250 
251  if ( $this->interwiki !== '' ) {
252  $name = $this->interwiki . ':' . $name;
253  }
254 
255  return $name;
256  }
257 }
getText()
Returns the title in text form, without namespace prefix or fragment.
Definition: TitleValue.php:195
getInterwiki()
Returns the interwiki part.
Definition: TitleValue.php:232
const NS_MAIN
Definition: Defines.php:60
string $dbkey
Definition: TitleValue.php:48
__toString()
Returns a string representation of the title, for logging.
Definition: TitleValue.php:244
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:36
static assertValidSpec( $namespace, $title, $fragment='', $interwiki='')
Asserts that the given parameters could be used to construct a TitleValue object. ...
Definition: TitleValue.php:110
__construct( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue.
Definition: TitleValue.php:88
inNamespace( $ns)
Definition: TitleValue.php:152
string $prefixedText
Text form including namespace/interwiki, initialised on demand.
Definition: TitleValue.php:70
int $namespace
Definition: TitleValue.php:42
isExternal()
Whether it has an interwiki part.
Definition: TitleValue.php:222
string $interwiki
Definition: TitleValue.php:60
string $fragment
Definition: TitleValue.php:54
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
Definition: TitleValue.php:207
getDBkey()
Returns the title&#39;s DB key, as supplied to the constructor, without namespace prefix or fragment...
Definition: TitleValue.php:179