MediaWiki REL1_34
TitleValue.php
Go to the documentation of this file.
1<?php
24use Wikimedia\Assert\Assert;
25use Wikimedia\Assert\ParameterTypeException;
26
36class 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}
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:57
Represents a page (or page fragment) title within MediaWiki.
isExternal()
Whether it has an interwiki part.
__construct( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue.
string $fragment
getInterwiki()
Returns the interwiki part.
string $prefixedText
Text form including namespace/interwiki, initialised on demand.
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.
const NS_MAIN
Definition Defines.php:69