MediaWiki REL1_31
TitleValue.php
Go to the documentation of this file.
1<?php
24use Wikimedia\Assert\Assert;
25
35class TitleValue implements LinkTarget {
36
41 protected $namespace;
42
47 protected $dbkey;
48
53 protected $fragment;
54
59 protected $interwiki;
60
78 public function __construct( $namespace, $dbkey, $fragment = '', $interwiki = '' ) {
79 Assert::parameterType( 'integer', $namespace, '$namespace' );
80 Assert::parameterType( 'string', $dbkey, '$dbkey' );
81 Assert::parameterType( 'string', $fragment, '$fragment' );
82 Assert::parameterType( 'string', $interwiki, '$interwiki' );
83
84 // Sanity check, no full validation or normalization applied here!
85 Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey',
86 "invalid DB key '$dbkey'" );
87 Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
88
89 $this->namespace = $namespace;
90 $this->dbkey = $dbkey;
91 $this->fragment = $fragment;
92 $this->interwiki = $interwiki;
93 }
94
99 public function getNamespace() {
100 return $this->namespace;
101 }
102
108 public function inNamespace( $ns ) {
109 return $this->namespace == $ns;
110 }
111
116 public function getFragment() {
117 return $this->fragment;
118 }
119
124 public function hasFragment() {
125 return $this->fragment !== '';
126 }
127
135 public function getDBkey() {
136 return $this->dbkey;
137 }
138
151 public function getText() {
152 return str_replace( '_', ' ', $this->getDBkey() );
153 }
154
163 public function createFragmentTarget( $fragment ) {
164 return new TitleValue(
165 $this->namespace,
166 $this->dbkey,
167 $fragment,
168 $this->interwiki
169 );
170 }
171
178 public function isExternal() {
179 return $this->interwiki !== '';
180 }
181
188 public function getInterwiki() {
189 return $this->interwiki;
190 }
191
200 public function __toString() {
201 $name = $this->namespace . ':' . $this->dbkey;
202
203 if ( $this->fragment !== '' ) {
204 $name .= '#' . $this->fragment;
205 }
206
207 if ( $this->interwiki !== '' ) {
208 $name = $this->interwiki . ':' . $name;
209 }
210
211 return $name;
212 }
213}
Represents a page (or page fragment) title within MediaWiki.
isExternal()
Whether it has an interwiki part.
string $fragment
__construct( $namespace, $dbkey, $fragment='', $interwiki='')
Constructs a TitleValue.
getInterwiki()
Returns the interwiki part.
inNamespace( $ns)
getText()
Returns the title in text form, without namespace prefix or fragment.
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
string $interwiki
getDBkey()
Returns the title's DB key, as supplied to the constructor, without namespace prefix or fragment.
string $dbkey
__toString()
Returns a string representation of the title, for logging.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302