MediaWiki master
TitleFormatter.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Title;
11
12use InvalidArgumentException;
16use Wikimedia\Parsoid\Core\LinkTarget;
17
36
43 public function __construct(
47 ) {
48 $this->language = $language;
49 $this->genderCache = $genderCache;
50 $this->nsInfo = $nsInfo;
51 }
52
68 public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
69 $out = '';
70 if ( $interwiki !== '' ) {
71 $out = $interwiki . ':';
72 }
73
74 if ( $namespace != 0 ) {
75 try {
76 $nsName = $this->getNamespaceName( $namespace, $text );
77 } catch ( InvalidArgumentException ) {
78 // See T165149. Awkward, but better than erroneously linking to the main namespace.
79 $nsName = $this->language->getNsText( NS_SPECIAL ) . ":Badtitle/NS{$namespace}";
80 }
81
82 $out .= $nsName . ':';
83 }
84 $out .= $text;
85
86 if ( $fragment !== '' ) {
87 $out .= '#' . $fragment;
88 }
89
90 $out = str_replace( '_', ' ', $out );
91
92 return $out;
93 }
94
102 public function getText( $title ) {
103 if ( $title instanceof LinkTarget ) {
104 return $title->getText();
105 } elseif ( $title instanceof PageReference ) {
106 return strtr( $title->getDBKey(), '_', ' ' );
107 } else {
108 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
109 }
110 }
111
120 public function getPrefixedText( $title ) {
121 if ( $title instanceof LinkTarget ) {
122 if ( !isset( $title->prefixedText ) ) {
123 $title->prefixedText = $this->formatTitle(
124 $title->getNamespace(),
125 $title->getText(),
126 '',
127 $title->getInterwiki()
128 );
129 }
130 return $title->prefixedText;
131 } elseif ( $title instanceof PageReference ) {
132 $title->assertWiki( PageReference::LOCAL );
133 return $this->formatTitle(
134 $title->getNamespace(),
135 $this->getText( $title )
136 );
137 } else {
138 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
139 }
140 }
141
151 public function getPrefixedDBkey( $target ) {
152 if ( $target instanceof LinkTarget ) {
153 return strtr( $this->formatTitle(
154 $target->getNamespace(),
155 $target->getDBkey(),
156 '',
157 $target->getInterwiki()
158 ), ' ', '_' );
159 } elseif ( $target instanceof PageReference ) {
160 $target->assertWiki( PageReference::LOCAL );
161 return strtr( $this->formatTitle(
162 $target->getNamespace(),
163 $target->getDBkey()
164 ), ' ', '_' );
165 } else {
166 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $target ) );
167 }
168 }
169
178 public function getPrefixedURL( $target ) {
179 return wfUrlencode( $this->getPrefixedDBkey( $target ) );
180 }
181
189 public function getFullText( $title ) {
190 if ( $title instanceof LinkTarget ) {
191 return $this->formatTitle(
192 $title->getNamespace(),
193 $title->getText(),
194 $title->getFragment(),
195 $title->getInterwiki()
196 );
197 } elseif ( $title instanceof PageReference ) {
198 $title->assertWiki( PageReference::LOCAL );
199 return $this->formatTitle(
200 $title->getNamespace(),
201 $this->getText( $title )
202 );
203 } else {
204 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
205 }
206 }
207
219 public function getNamespaceName( $namespace, $text ) {
220 if ( $this->language->needsGenderDistinction() &&
221 $this->nsInfo->hasGenderDistinction( $namespace )
222 ) {
223 // NOTE: we are assuming here that the title text is a user name!
224 $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
225 $name = $this->language->getGenderNsText( $namespace, $gender );
226 } else {
227 $name = $this->language->getNsText( $namespace );
228 }
229
230 if ( $name === false ) {
231 throw new InvalidArgumentException( 'Unknown namespace ID: ' . $namespace );
232 }
233
234 return $name;
235 }
236
237}
238
240class_alias( TitleFormatter::class, 'TitleFormatter' );
const NS_SPECIAL
Definition Defines.php:40
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
Look up "gender" user preference.
Base class for language-specific code.
Definition Language.php:70
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.
getPrefixedURL( $target)
Return a URL-encoded title (not an actual URL)
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.