MediaWiki  REL1_31
TitleValue.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\Assert\Assert;
25 
35 class 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 }
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
TitleValue\$namespace
int $namespace
Definition: TitleValue.php:41
TitleValue\isExternal
isExternal()
Whether it has an interwiki part.
Definition: TitleValue.php:178
TitleValue\createFragmentTarget
createFragmentTarget( $fragment)
Creates a new TitleValue for a different fragment of the same page.
Definition: TitleValue.php:163
TitleValue\$interwiki
string $interwiki
Definition: TitleValue.php:59
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
TitleValue\$fragment
string $fragment
Definition: TitleValue.php:53
TitleValue\getDBkey
getDBkey()
Returns the title's DB key, as supplied to the constructor, without namespace prefix or fragment.
Definition: TitleValue.php:135
if
if(file_exists("$IP/StartProfiler.php"))
Definition: Setup.php:42
TitleValue\__toString
__toString()
Returns a string representation of the title, for logging.
Definition: TitleValue.php:200
TitleValue\hasFragment
hasFragment()
Definition: TitleValue.php:124
TitleValue\getFragment
getFragment()
Definition: TitleValue.php:116
TitleValue\getText
getText()
Returns the title in text form, without namespace prefix or fragment.
Definition: TitleValue.php:151
TitleValue\getInterwiki
getInterwiki()
Returns the interwiki part.
Definition: TitleValue.php:188
TitleValue\getNamespace
getNamespace()
Definition: TitleValue.php:99
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
TitleValue\$dbkey
string $dbkey
Definition: TitleValue.php:47
TitleValue\__construct
__construct( $namespace, $dbkey, $fragment='', $interwiki='')
Constructs a TitleValue.
Definition: TitleValue.php:78
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
TitleValue\inNamespace
inNamespace( $ns)
Definition: TitleValue.php:108
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:35