MediaWiki  1.34.4
TitleValue.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\Assert\Assert;
25 use Wikimedia\Assert\ParameterTypeException;
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  if ( !is_int( $namespace ) ) {
90  throw new ParameterTypeException( '$namespace', 'int' );
91  }
92  if ( !is_string( $title ) ) {
93  throw new ParameterTypeException( '$title', 'string' );
94  }
95  if ( !is_string( $fragment ) ) {
96  throw new ParameterTypeException( '$fragment', 'string' );
97  }
98  if ( !is_string( $interwiki ) ) {
99  throw new ParameterTypeException( '$interwiki', 'string' );
100  }
101 
102  // Sanity check, no full validation or normalization applied here!
103  Assert::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title',
104  "invalid name '$title'" );
105  Assert::parameter(
106  $title !== '' ||
107  ( $namespace === NS_MAIN && ( $fragment !== '' || $interwiki !== '' ) ),
108  '$title',
109  'should not be empty unless namespace is main and fragment or interwiki is non-empty'
110  );
111 
112  $this->namespace = $namespace;
113  $this->dbkey = strtr( $title, ' ', '_' );
114  $this->fragment = $fragment;
115  $this->interwiki = $interwiki;
116  }
117 
122  public function getNamespace() {
123  return $this->namespace;
124  }
125 
131  public function inNamespace( $ns ) {
132  return $this->namespace == $ns;
133  }
134 
139  public function getFragment() {
140  return $this->fragment;
141  }
142 
147  public function hasFragment() {
148  return $this->fragment !== '';
149  }
150 
158  public function getDBkey() {
159  return $this->dbkey;
160  }
161 
174  public function getText() {
175  return str_replace( '_', ' ', $this->dbkey );
176  }
177 
186  public function createFragmentTarget( $fragment ) {
187  return new TitleValue(
188  $this->namespace,
189  $this->dbkey,
190  $fragment,
191  $this->interwiki
192  );
193  }
194 
201  public function isExternal() {
202  return $this->interwiki !== '';
203  }
204 
211  public function getInterwiki() {
212  return $this->interwiki;
213  }
214 
223  public function __toString() {
224  $name = $this->namespace . ':' . $this->dbkey;
225 
226  if ( $this->fragment !== '' ) {
227  $name .= '#' . $this->fragment;
228  }
229 
230  if ( $this->interwiki !== '' ) {
231  $name = $this->interwiki . ':' . $name;
232  }
233 
234  return $name;
235  }
236 }
TitleValue\$prefixedText
string $prefixedText
Text form including namespace/interwiki, initialised on demand.
Definition: TitleValue.php:70
TitleValue\$namespace
int $namespace
Definition: TitleValue.php:42
TitleValue\isExternal
isExternal()
Whether it has an interwiki part.
Definition: TitleValue.php:201
TitleValue\createFragmentTarget
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
Definition: TitleValue.php:186
TitleValue\$interwiki
string $interwiki
Definition: TitleValue.php:60
NS_MAIN
const NS_MAIN
Definition: Defines.php:69
TitleValue\$fragment
string $fragment
Definition: TitleValue.php:54
TitleValue\getDBkey
getDBkey()
Returns the title's DB key, as supplied to the constructor, without namespace prefix or fragment.
Definition: TitleValue.php:158
$title
$title
Definition: testCompression.php:34
TitleValue\__toString
__toString()
Returns a string representation of the title, for logging.
Definition: TitleValue.php:223
TitleValue\hasFragment
hasFragment()
Definition: TitleValue.php:147
TitleValue\getFragment
getFragment()
Definition: TitleValue.php:139
TitleValue\getText
getText()
Returns the title in text form, without namespace prefix or fragment.
Definition: TitleValue.php:174
TitleValue\getInterwiki
getInterwiki()
Returns the interwiki part.
Definition: TitleValue.php:211
TitleValue\getNamespace
getNamespace()
Definition: TitleValue.php:122
TitleValue\$dbkey
string $dbkey
Definition: TitleValue.php:48
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
TitleValue\__construct
__construct( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue.
Definition: TitleValue.php:88
TitleValue\inNamespace
inNamespace( $ns)
Definition: TitleValue.php:131
if
if($IP===false)
Definition: initImageData.php:4
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36