MediaWiki master
TitleFormatter.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Title;
25
26use InvalidArgumentException;
30use Wikimedia\Parsoid\Core\LinkTarget;
31
50
57 public function __construct(
61 ) {
62 $this->language = $language;
63 $this->genderCache = $genderCache;
64 $this->nsInfo = $nsInfo;
65 }
66
82 public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
83 $out = '';
84 if ( $interwiki !== '' ) {
85 $out = $interwiki . ':';
86 }
87
88 if ( $namespace != 0 ) {
89 try {
90 $nsName = $this->getNamespaceName( $namespace, $text );
91 } catch ( InvalidArgumentException $e ) {
92 // See T165149. Awkward, but better than erroneously linking to the main namespace.
93 $nsName = $this->language->getNsText( NS_SPECIAL ) . ":Badtitle/NS{$namespace}";
94 }
95
96 $out .= $nsName . ':';
97 }
98 $out .= $text;
99
100 if ( $fragment !== '' ) {
101 $out .= '#' . $fragment;
102 }
103
104 $out = str_replace( '_', ' ', $out );
105
106 return $out;
107 }
108
116 public function getText( $title ) {
117 if ( $title instanceof LinkTarget ) {
118 return $title->getText();
119 } elseif ( $title instanceof PageReference ) {
120 return strtr( $title->getDBKey(), '_', ' ' );
121 } else {
122 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
123 }
124 }
125
134 public function getPrefixedText( $title ) {
135 if ( $title instanceof LinkTarget ) {
136 if ( !isset( $title->prefixedText ) ) {
137 $title->prefixedText = $this->formatTitle(
138 $title->getNamespace(),
139 $title->getText(),
140 '',
141 $title->getInterwiki()
142 );
143 }
144 return $title->prefixedText;
145 } elseif ( $title instanceof PageReference ) {
146 $title->assertWiki( PageReference::LOCAL );
147 return $this->formatTitle(
148 $title->getNamespace(),
149 $this->getText( $title )
150 );
151 } else {
152 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
153 }
154 }
155
165 public function getPrefixedDBkey( $target ) {
166 if ( $target instanceof LinkTarget ) {
167 return strtr( $this->formatTitle(
168 $target->getNamespace(),
169 $target->getDBkey(),
170 '',
171 $target->getInterwiki()
172 ), ' ', '_' );
173 } elseif ( $target instanceof PageReference ) {
174 $target->assertWiki( PageReference::LOCAL );
175 return strtr( $this->formatTitle(
176 $target->getNamespace(),
177 $target->getDBkey()
178 ), ' ', '_' );
179 } else {
180 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $target ) );
181 }
182 }
183
191 public function getFullText( $title ) {
192 if ( $title instanceof LinkTarget ) {
193 return $this->formatTitle(
194 $title->getNamespace(),
195 $title->getText(),
196 $title->getFragment(),
197 $title->getInterwiki()
198 );
199 } elseif ( $title instanceof PageReference ) {
200 $title->assertWiki( PageReference::LOCAL );
201 return $this->formatTitle(
202 $title->getNamespace(),
203 $this->getText( $title )
204 );
205 } else {
206 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
207 }
208 }
209
221 public function getNamespaceName( $namespace, $text ) {
222 if ( $this->language->needsGenderDistinction() &&
223 $this->nsInfo->hasGenderDistinction( $namespace )
224 ) {
225 // NOTE: we are assuming here that the title text is a user name!
226 $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
227 $name = $this->language->getGenderNsText( $namespace, $gender );
228 } else {
229 $name = $this->language->getNsText( $namespace );
230 }
231
232 if ( $name === false ) {
233 throw new InvalidArgumentException( 'Unknown namespace ID: ' . $namespace );
234 }
235
236 return $name;
237 }
238
239}
240
242class_alias( TitleFormatter::class, 'TitleFormatter' );
const NS_SPECIAL
Definition Defines.php:54
Look up "gender" user preference.
Base class for language-specific code.
Definition Language.php:81
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
A title formatter service for MediaWiki.
getNamespaceName( $namespace, $text)
Returns the name of the namespace for the given title.
getPrefixedText( $title)
Returns the title formatted for display, including the namespace name.
formatTitle( $namespace, $text, $fragment='', $interwiki='')
Returns the title formatted for display.
getFullText( $title)
Returns the title formatted for display, with namespace and fragment.
__construct(Language $language, GenderCache $genderCache, NamespaceInfo $nsInfo)
getText( $title)
Returns the title text formatted for display, without namespace or fragment.
getPrefixedDBkey( $target)
Return the title in prefixed database key form, with interwiki and namespace.
Represents the target of a wiki link.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.