MediaWiki REL1_30
TitleValue.php
Go to the documentation of this file.
1<?php
25use Wikimedia\Assert\Assert;
26
36class TitleValue implements LinkTarget {
40 protected $namespace;
41
45 protected $dbkey;
46
50 protected $fragment;
51
55 protected $interwiki;
56
74 public function __construct( $namespace, $dbkey, $fragment = '', $interwiki = '' ) {
75 Assert::parameterType( 'integer', $namespace, '$namespace' );
76 Assert::parameterType( 'string', $dbkey, '$dbkey' );
77 Assert::parameterType( 'string', $fragment, '$fragment' );
78 Assert::parameterType( 'string', $interwiki, '$interwiki' );
79
80 // Sanity check, no full validation or normalization applied here!
81 Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey',
82 "invalid DB key '$dbkey'" );
83 Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
84
85 $this->namespace = $namespace;
86 $this->dbkey = $dbkey;
87 $this->fragment = $fragment;
88 $this->interwiki = $interwiki;
89 }
90
94 public function getNamespace() {
95 return $this->namespace;
96 }
97
103 public function inNamespace( $ns ) {
104 return $this->namespace == $ns;
105 }
106
110 public function getFragment() {
111 return $this->fragment;
112 }
113
118 public function hasFragment() {
119 return $this->fragment !== '';
120 }
121
128 public function getDBkey() {
129 return $this->dbkey;
130 }
131
143 public function getText() {
144 return str_replace( '_', ' ', $this->getDBkey() );
145 }
146
155 public function createFragmentTarget( $fragment ) {
156 return new TitleValue(
157 $this->namespace,
158 $this->dbkey,
159 $fragment,
160 $this->interwiki
161 );
162 }
163
170 public function isExternal() {
171 return $this->interwiki !== '';
172 }
173
180 public function getInterwiki() {
181 return $this->interwiki;
182 }
183
191 public function __toString() {
192 $name = $this->namespace . ':' . $this->dbkey;
193
194 if ( $this->fragment !== '' ) {
195 $name .= '#' . $this->fragment;
196 }
197
198 if ( $this->interwiki !== '' ) {
199 $name = $this->interwiki . ':' . $name;
200 }
201
202 return $name;
203 }
204}
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