MediaWiki  master
TitleValue.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\Assert\Assert;
25 use Wikimedia\Assert\ParameterAssertionException;
26 use Wikimedia\Assert\ParameterTypeException;
27 
39 class TitleValue implements LinkTarget {
40 
45  protected $namespace;
46 
51  protected $dbkey;
52 
57  protected $fragment;
58 
63  protected $interwiki;
64 
73  public $prefixedText = null;
74 
93  public static function tryNew( $namespace, $title, $fragment = '', $interwiki = '' ) {
94  if ( !is_int( $namespace ) ) {
95  throw new ParameterTypeException( '$namespace', 'int' );
96  }
97 
98  try {
99  return new static( $namespace, $title, $fragment, $interwiki );
100  } catch ( ParameterAssertionException $ex ) {
101  return null;
102  }
103  }
104 
124  public function __construct( $namespace, $title, $fragment = '', $interwiki = '' ) {
126 
127  $this->namespace = $namespace;
128  $this->dbkey = strtr( $title, ' ', '_' );
129  $this->fragment = $fragment;
130  $this->interwiki = $interwiki;
131  }
132 
146  public static function assertValidSpec( $namespace, $title, $fragment = '', $interwiki = '' ) {
147  if ( !is_int( $namespace ) ) {
148  throw new ParameterTypeException( '$namespace', 'int' );
149  }
150  if ( !is_string( $title ) ) {
151  throw new ParameterTypeException( '$title', 'string' );
152  }
153  if ( !is_string( $fragment ) ) {
154  throw new ParameterTypeException( '$fragment', 'string' );
155  }
156  if ( !is_string( $interwiki ) ) {
157  throw new ParameterTypeException( '$interwiki', 'string' );
158  }
159 
160  Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title',
161  "invalid name '$title'" );
162 
163  // NOTE: As of MW 1.34, [[#]] is rendered as a valid link, pointing to the empty
164  // page title, effectively leading to the wiki's main page. This means that a completely
165  // empty TitleValue has to be considered valid, for consistency with Title.
166  // Also note that [[#foo]] is a valid on-page section links, and that [[acme:#foo]] is
167  // a valid interwiki link.
168  Assert::parameter(
169  $title !== '' || $namespace === NS_MAIN,
170  '$title',
171  'should not be empty unless namespace is main'
172  );
173  }
174 
179  public function getNamespace() {
180  return $this->namespace;
181  }
182 
188  public function inNamespace( $ns ) {
189  return $this->namespace == $ns;
190  }
191 
196  public function getFragment() {
197  return $this->fragment;
198  }
199 
204  public function hasFragment() {
205  return $this->fragment !== '';
206  }
207 
215  public function getDBkey() {
216  return $this->dbkey;
217  }
218 
231  public function getText() {
232  return str_replace( '_', ' ', $this->dbkey );
233  }
234 
243  public function createFragmentTarget( $fragment ) {
244  return new TitleValue(
245  $this->namespace,
246  $this->dbkey,
247  $fragment,
248  $this->interwiki
249  );
250  }
251 
258  public function isExternal() {
259  return $this->interwiki !== '';
260  }
261 
268  public function getInterwiki() {
269  return $this->interwiki;
270  }
271 
280  public function __toString() {
281  $name = $this->namespace . ':' . $this->dbkey;
282 
283  if ( $this->fragment !== '' ) {
284  $name .= '#' . $this->fragment;
285  }
286 
287  if ( $this->interwiki !== '' ) {
288  $name = $this->interwiki . ':' . $name;
289  }
290 
291  return $name;
292  }
293 }
TitleValue\$prefixedText
string $prefixedText
Text form including namespace/interwiki, initialised on demand.
Definition: TitleValue.php:73
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:45
TitleValue\isExternal
isExternal()
Whether it has an interwiki part.
Definition: TitleValue.php:258
TitleValue\createFragmentTarget
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
Definition: TitleValue.php:243
TitleValue\$interwiki
string $interwiki
Definition: TitleValue.php:63
NS_MAIN
const NS_MAIN
Definition: Defines.php:69
TitleValue\$fragment
string $fragment
Definition: TitleValue.php:57
TitleValue\getDBkey
getDBkey()
Returns the title's DB key, as supplied to the constructor, without namespace prefix or fragment.
Definition: TitleValue.php:215
TitleValue\tryNew
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:93
$title
$title
Definition: testCompression.php:38
TitleValue\__toString
__toString()
Returns a string representation of the title, for logging.
Definition: TitleValue.php:280
TitleValue\assertValidSpec
static assertValidSpec( $namespace, $title, $fragment='', $interwiki='')
Asserts that the given parameters could be used to construct a TitleValue object.
Definition: TitleValue.php:146
TitleValue\hasFragment
hasFragment()
Definition: TitleValue.php:204
TitleValue\getFragment
getFragment()
Definition: TitleValue.php:196
TitleValue\getText
getText()
Returns the title in text form, without namespace prefix or fragment.
Definition: TitleValue.php:231
TitleValue\getInterwiki
getInterwiki()
Returns the interwiki part.
Definition: TitleValue.php:268
TitleValue\getNamespace
getNamespace()
Definition: TitleValue.php:179
TitleValue\$dbkey
string $dbkey
Definition: TitleValue.php:51
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
TitleValue\__construct
__construct( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue.
Definition: TitleValue.php:124
TitleValue\inNamespace
inNamespace( $ns)
Definition: TitleValue.php:188
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:39