MediaWiki REL1_33
LinkRenderer.php
Go to the documentation of this file.
1<?php
22
23use DummyLinker;
24use Hooks;
25use Html;
26use HtmlArmor;
27use LinkCache;
28use Linker;
30use MWNamespace;
31use Sanitizer;
32use Title;
34
42
48 private $forceArticlePath = false;
49
55 private $expandUrls = false;
56
60 private $stubThreshold = 0;
61
66
70 private $linkCache;
71
77 private $runLegacyBeginHook = true;
78
84 $this->titleFormatter = $titleFormatter;
85 $this->linkCache = $linkCache;
86 }
87
91 public function setForceArticlePath( $force ) {
92 $this->forceArticlePath = $force;
93 }
94
98 public function getForceArticlePath() {
100 }
101
105 public function setExpandURLs( $expand ) {
106 $this->expandUrls = $expand;
107 }
108
112 public function getExpandURLs() {
113 return $this->expandUrls;
114 }
115
119 public function setStubThreshold( $threshold ) {
120 $this->stubThreshold = $threshold;
121 }
122
126 public function getStubThreshold() {
128 }
129
133 public function setRunLegacyBeginHook( $run ) {
134 $this->runLegacyBeginHook = $run;
135 }
136
144 public function makeLink(
145 LinkTarget $target, $text = null, array $extraAttribs = [], array $query = []
146 ) {
147 $title = Title::newFromLinkTarget( $target );
148 if ( $title->isKnown() ) {
149 return $this->makeKnownLink( $target, $text, $extraAttribs, $query );
150 } else {
151 return $this->makeBrokenLink( $target, $text, $extraAttribs, $query );
152 }
153 }
154
161 private function getLegacyOptions( $isKnown ) {
162 $options = [ 'stubThreshold' => $this->stubThreshold ];
163 if ( $this->forceArticlePath ) {
164 $options[] = 'forcearticlepath';
165 }
166 if ( $this->expandUrls === PROTO_HTTP ) {
167 $options[] = 'http';
168 } elseif ( $this->expandUrls === PROTO_HTTPS ) {
169 $options[] = 'https';
170 }
171
172 $options[] = $isKnown ? 'known' : 'broken';
173
174 return $options;
175 }
176
177 private function runBeginHook( LinkTarget $target, &$text, &$extraAttribs, &$query, $isKnown ) {
178 $ret = null;
179 if ( !Hooks::run( 'HtmlPageLinkRendererBegin',
180 [ $this, $target, &$text, &$extraAttribs, &$query, &$ret ] )
181 ) {
182 return $ret;
183 }
184
185 // Now run the legacy hook
186 return $this->runLegacyBeginHook( $target, $text, $extraAttribs, $query, $isKnown );
187 }
188
189 private function runLegacyBeginHook( LinkTarget $target, &$text, &$extraAttribs, &$query,
190 $isKnown
191 ) {
192 if ( !$this->runLegacyBeginHook || !Hooks::isRegistered( 'LinkBegin' ) ) {
193 // Disabled, or nothing registered
194 return null;
195 }
196
197 $realOptions = $options = $this->getLegacyOptions( $isKnown );
198 $ret = null;
199 $dummy = new DummyLinker();
200 $title = Title::newFromLinkTarget( $target );
201 if ( $text !== null ) {
202 $realHtml = $html = HtmlArmor::getHtml( $text );
203 } else {
204 $realHtml = $html = null;
205 }
206 if ( !Hooks::run( 'LinkBegin',
207 [ $dummy, $title, &$html, &$extraAttribs, &$query, &$options, &$ret ], '1.28' )
208 ) {
209 return $ret;
210 }
211
212 if ( $html !== null && $html !== $realHtml ) {
213 // &$html was modified, so re-armor it as $text
214 $text = new HtmlArmor( $html );
215 }
216
217 // Check if they changed any of the options, hopefully not!
218 if ( $options !== $realOptions ) {
219 $factory = MediaWikiServices::getInstance()->getLinkRendererFactory();
220 // They did, so create a separate instance and have that take over the rest
221 $newRenderer = $factory->createFromLegacyOptions( $options );
222 // Don't recurse the hook...
223 $newRenderer->setRunLegacyBeginHook( false );
224 if ( in_array( 'known', $options, true ) ) {
225 return $newRenderer->makeKnownLink( $title, $text, $extraAttribs, $query );
226 } elseif ( in_array( 'broken', $options, true ) ) {
227 return $newRenderer->makeBrokenLink( $title, $text, $extraAttribs, $query );
228 } else {
229 return $newRenderer->makeLink( $title, $text, $extraAttribs, $query );
230 }
231 }
232
233 return null;
234 }
235
247 public function makePreloadedLink(
248 LinkTarget $target, $text = null, $classes = '', array $extraAttribs = [], array $query = []
249 ) {
250 // Run begin hook
251 $ret = $this->runBeginHook( $target, $text, $extraAttribs, $query, true );
252 if ( $ret !== null ) {
253 return $ret;
254 }
255 $target = $this->normalizeTarget( $target );
256 $url = $this->getLinkURL( $target, $query );
257 $attribs = [ 'class' => $classes ];
258 $prefixedText = $this->titleFormatter->getPrefixedText( $target );
259 if ( $prefixedText !== '' ) {
260 $attribs['title'] = $prefixedText;
261 }
262
263 $attribs = [
264 'href' => $url,
265 ] + $this->mergeAttribs( $attribs, $extraAttribs );
266
267 if ( $text === null ) {
268 $text = $this->getLinkText( $target );
269 }
270
271 return $this->buildAElement( $target, $text, $attribs, true );
272 }
273
281 public function makeKnownLink(
282 LinkTarget $target, $text = null, array $extraAttribs = [], array $query = []
283 ) {
284 $classes = [];
285 if ( $target->isExternal() ) {
286 $classes[] = 'extiw';
287 }
288 $colour = $this->getLinkClasses( $target );
289 if ( $colour !== '' ) {
290 $classes[] = $colour;
291 }
292
293 return $this->makePreloadedLink(
294 $target,
295 $text,
296 $classes ? implode( ' ', $classes ) : '',
298 $query
299 );
300 }
301
309 public function makeBrokenLink(
310 LinkTarget $target, $text = null, array $extraAttribs = [], array $query = []
311 ) {
312 // Run legacy hook
313 $ret = $this->runBeginHook( $target, $text, $extraAttribs, $query, false );
314 if ( $ret !== null ) {
315 return $ret;
316 }
317
318 # We don't want to include fragments for broken links, because they
319 # generally make no sense.
320 if ( $target->hasFragment() ) {
321 $target = $target->createFragmentTarget( '' );
322 }
323 $target = $this->normalizeTarget( $target );
324
325 if ( !isset( $query['action'] ) && $target->getNamespace() !== NS_SPECIAL ) {
326 $query['action'] = 'edit';
327 $query['redlink'] = '1';
328 }
329
330 $url = $this->getLinkURL( $target, $query );
331 $attribs = [ 'class' => 'new' ];
332 $prefixedText = $this->titleFormatter->getPrefixedText( $target );
333 if ( $prefixedText !== '' ) {
334 // This ends up in parser cache!
335 $attribs['title'] = wfMessage( 'red-link-title', $prefixedText )
336 ->inContentLanguage()
337 ->text();
338 }
339
340 $attribs = [
341 'href' => $url,
342 ] + $this->mergeAttribs( $attribs, $extraAttribs );
343
344 if ( $text === null ) {
345 $text = $this->getLinkText( $target );
346 }
347
348 return $this->buildAElement( $target, $text, $attribs, false );
349 }
350
360 private function buildAElement( LinkTarget $target, $text, array $attribs, $isKnown ) {
361 $ret = null;
362 if ( !Hooks::run( 'HtmlPageLinkRendererEnd',
363 [ $this, $target, $isKnown, &$text, &$attribs, &$ret ] )
364 ) {
365 return $ret;
366 }
367
368 $html = HtmlArmor::getHtml( $text );
369
370 // Run legacy hook
371 if ( Hooks::isRegistered( 'LinkEnd' ) ) {
372 $dummy = new DummyLinker();
373 $title = Title::newFromLinkTarget( $target );
374 $options = $this->getLegacyOptions( $isKnown );
375 if ( !Hooks::run( 'LinkEnd',
376 [ $dummy, $title, $options, &$html, &$attribs, &$ret ], '1.28' )
377 ) {
378 return $ret;
379 }
380 }
381
382 return Html::rawElement( 'a', $attribs, $html );
383 }
384
389 private function getLinkText( LinkTarget $target ) {
390 $prefixedText = $this->titleFormatter->getPrefixedText( $target );
391 // If the target is just a fragment, with no title, we return the fragment
392 // text. Otherwise, we return the title text itself.
393 if ( $prefixedText === '' && $target->hasFragment() ) {
394 return $target->getFragment();
395 }
396
397 return $prefixedText;
398 }
399
400 private function getLinkURL( LinkTarget $target, array $query = [] ) {
401 // TODO: Use a LinkTargetResolver service instead of Title
402 $title = Title::newFromLinkTarget( $target );
403 if ( $this->forceArticlePath ) {
404 $realQuery = $query;
405 $query = [];
406 } else {
407 $realQuery = [];
408 }
409 $url = $title->getLinkURL( $query, false, $this->expandUrls );
410
411 if ( $this->forceArticlePath && $realQuery ) {
412 $url = wfAppendQuery( $url, $realQuery );
413 }
414
415 return $url;
416 }
417
425 private function normalizeTarget( LinkTarget $target ) {
426 return Linker::normaliseSpecialPage( $target );
427 }
428
437 private function mergeAttribs( $defaults, $attribs ) {
438 if ( !$attribs ) {
439 return $defaults;
440 }
441 # Merge the custom attribs with the default ones, and iterate
442 # over that, deleting all "false" attributes.
443 $ret = [];
444 $merged = Sanitizer::mergeAttributes( $defaults, $attribs );
445 foreach ( $merged as $key => $val ) {
446 # A false value suppresses the attribute
447 if ( $val !== false ) {
448 $ret[$key] = $val;
449 }
450 }
451 return $ret;
452 }
453
460 public function getLinkClasses( LinkTarget $target ) {
461 // Make sure the target is in the cache
462 $id = $this->linkCache->addLinkObj( $target );
463 if ( $id == 0 ) {
464 // Doesn't exist
465 return '';
466 }
467
468 if ( $this->linkCache->getGoodLinkFieldObj( $target, 'redirect' ) ) {
469 # Page is a redirect
470 return 'mw-redirect';
471 } elseif ( $this->stubThreshold > 0 && MWNamespace::isContent( $target->getNamespace() )
472 && $this->linkCache->getGoodLinkFieldObj( $target, 'length' ) < $this->stubThreshold
473 ) {
474 # Page is a stub
475 return 'stub';
476 }
477
478 return '';
479 }
480}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Hooks class.
Definition Hooks.php:34
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:28
static getHtml( $input)
Provide a string or HtmlArmor object and get safe HTML back.
Definition HtmlArmor.php:50
This class is a collection of static functions that serve two purposes:
Definition Html.php:49
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition LinkCache.php:34
Some internal bits split of from Skin.php.
Definition Linker.php:34
static normaliseSpecialPage(LinkTarget $target)
Definition Linker.php:211
This is a utility class with only static functions for dealing with namespaces that encodes all the "...
Class that generates HTML links for pages.
bool $forceArticlePath
Whether to force the pretty article path.
makePreloadedLink(LinkTarget $target, $text=null, $classes='', array $extraAttribs=[], array $query=[])
If you have already looked up the proper CSS classes using LinkRenderer::getLinkClasses() or some oth...
getLegacyOptions( $isKnown)
Get the options in the legacy format.
getLinkURL(LinkTarget $target, array $query=[])
runLegacyBeginHook(LinkTarget $target, &$text, &$extraAttribs, &$query, $isKnown)
buildAElement(LinkTarget $target, $text, array $attribs, $isKnown)
Builds the final element.
runBeginHook(LinkTarget $target, &$text, &$extraAttribs, &$query, $isKnown)
makeBrokenLink(LinkTarget $target, $text=null, array $extraAttribs=[], array $query=[])
getLinkClasses(LinkTarget $target)
Return the CSS classes of a known link.
mergeAttribs( $defaults, $attribs)
Merges two sets of attributes.
getLinkText(LinkTarget $target)
makeKnownLink(LinkTarget $target, $text=null, array $extraAttribs=[], array $query=[])
makeLink(LinkTarget $target, $text=null, array $extraAttribs=[], array $query=[])
__construct(TitleFormatter $titleFormatter, LinkCache $linkCache)
normalizeTarget(LinkTarget $target)
Normalizes the provided target.
string bool int $expandUrls
A PROTO_* constant or false.
bool $runLegacyBeginHook
Whether to run the legacy Linker hooks.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getInstance()
Returns the global default instance of the top level service locator.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:33
Represents a title within MediaWiki.
Definition Title.php:40
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1999
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2003
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2011
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2012
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1617
const PROTO_HTTPS
Definition Defines.php:229
const NS_SPECIAL
Definition Defines.php:62
const PROTO_HTTP
Definition Defines.php:228
hasFragment()
Whether the link target has a fragment.
getFragment()
Get the link fragment (i.e.
getNamespace()
Get the namespace index.
isExternal()
Whether this LinkTarget has an interwiki component.
createFragmentTarget( $fragment)
Creates a new LinkTarget for a different fragment of the same page.
A title formatter service for MediaWiki.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
if(! $dbr->tableExists( 'profiling')) $expand