MediaWiki REL1_30
SpecialWhatlinkshere.php
Go to the documentation of this file.
1<?php
25
33 protected $opts;
34
35 protected $selfTitle;
36
38 protected $target;
39
40 protected $limits = [ 20, 50, 100, 250, 500 ];
41
42 public function __construct() {
43 parent::__construct( 'Whatlinkshere' );
44 }
45
46 function execute( $par ) {
47 $out = $this->getOutput();
48
49 $this->setHeaders();
50 $this->outputHeader();
51 $this->addHelpLink( 'Help:What links here' );
52
53 $opts = new FormOptions();
54
55 $opts->add( 'target', '' );
56 $opts->add( 'namespace', '', FormOptions::INTNULL );
57 $opts->add( 'limit', $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
58 $opts->add( 'from', 0 );
59 $opts->add( 'back', 0 );
60 $opts->add( 'hideredirs', false );
61 $opts->add( 'hidetrans', false );
62 $opts->add( 'hidelinks', false );
63 $opts->add( 'hideimages', false );
64 $opts->add( 'invert', false );
65
67 $opts->validateIntBounds( 'limit', 0, 5000 );
68
69 // Give precedence to subpage syntax
70 if ( $par !== null ) {
71 $opts->setValue( 'target', $par );
72 }
73
74 // Bind to member variable
75 $this->opts = $opts;
76
77 $this->target = Title::newFromText( $opts->getValue( 'target' ) );
78 if ( !$this->target ) {
79 if ( !$this->including() ) {
80 $out->addHTML( $this->whatlinkshereForm() );
81 }
82
83 return;
84 }
85
86 $this->getSkin()->setRelevantTitle( $this->target );
87
88 $this->selfTitle = $this->getPageTitle( $this->target->getPrefixedDBkey() );
89
90 $out->setPageTitle( $this->msg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
91 $out->addBacklinkSubtitle( $this->target );
92 $this->showIndirectLinks(
93 0,
94 $this->target,
95 $opts->getValue( 'limit' ),
96 $opts->getValue( 'from' ),
97 $opts->getValue( 'back' )
98 );
99 }
100
108 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
109 $out = $this->getOutput();
111
112 $hidelinks = $this->opts->getValue( 'hidelinks' );
113 $hideredirs = $this->opts->getValue( 'hideredirs' );
114 $hidetrans = $this->opts->getValue( 'hidetrans' );
115 $hideimages = $target->getNamespace() != NS_FILE || $this->opts->getValue( 'hideimages' );
116
117 $fetchlinks = ( !$hidelinks || !$hideredirs );
118
119 // Build query conds in concert for all three tables...
120 $conds['pagelinks'] = [
121 'pl_namespace' => $target->getNamespace(),
122 'pl_title' => $target->getDBkey(),
123 ];
124 $conds['templatelinks'] = [
125 'tl_namespace' => $target->getNamespace(),
126 'tl_title' => $target->getDBkey(),
127 ];
128 $conds['imagelinks'] = [
129 'il_to' => $target->getDBkey(),
130 ];
131
132 $namespace = $this->opts->getValue( 'namespace' );
133 $invert = $this->opts->getValue( 'invert' );
134 $nsComparison = ( $invert ? '!= ' : '= ' ) . $dbr->addQuotes( $namespace );
135 if ( is_int( $namespace ) ) {
136 $conds['pagelinks'][] = "pl_from_namespace $nsComparison";
137 $conds['templatelinks'][] = "tl_from_namespace $nsComparison";
138 $conds['imagelinks'][] = "il_from_namespace $nsComparison";
139 }
140
141 if ( $from ) {
142 $conds['templatelinks'][] = "tl_from >= $from";
143 $conds['pagelinks'][] = "pl_from >= $from";
144 $conds['imagelinks'][] = "il_from >= $from";
145 }
146
147 if ( $hideredirs ) {
148 $conds['pagelinks']['rd_from'] = null;
149 } elseif ( $hidelinks ) {
150 $conds['pagelinks'][] = 'rd_from is NOT NULL';
151 }
152
153 $queryFunc = function ( IDatabase $dbr, $table, $fromCol ) use (
154 $conds, $target, $limit
155 ) {
156 // Read an extra row as an at-end check
157 $queryLimit = $limit + 1;
158 $on = [
159 "rd_from = $fromCol",
160 'rd_title' => $target->getDBkey(),
161 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'
162 ];
163 $on['rd_namespace'] = $target->getNamespace();
164 // Inner LIMIT is 2X in case of stale backlinks with wrong namespaces
165 $subQuery = $dbr->selectSQLText(
166 [ $table, 'redirect', 'page' ],
167 [ $fromCol, 'rd_from' ],
168 $conds[$table],
169 __CLASS__ . '::showIndirectLinks',
170 // Force JOIN order per T106682 to avoid large filesorts
171 [ 'ORDER BY' => $fromCol, 'LIMIT' => 2 * $queryLimit, 'STRAIGHT_JOIN' ],
172 [
173 'page' => [ 'INNER JOIN', "$fromCol = page_id" ],
174 'redirect' => [ 'LEFT JOIN', $on ]
175 ]
176 );
177 return $dbr->select(
178 [ 'page', 'temp_backlink_range' => "($subQuery)" ],
179 [ 'page_id', 'page_namespace', 'page_title', 'rd_from', 'page_is_redirect' ],
180 [],
181 __CLASS__ . '::showIndirectLinks',
182 [ 'ORDER BY' => 'page_id', 'LIMIT' => $queryLimit ],
183 [ 'page' => [ 'INNER JOIN', "$fromCol = page_id" ] ]
184 );
185 };
186
187 if ( $fetchlinks ) {
188 $plRes = $queryFunc( $dbr, 'pagelinks', 'pl_from' );
189 }
190
191 if ( !$hidetrans ) {
192 $tlRes = $queryFunc( $dbr, 'templatelinks', 'tl_from' );
193 }
194
195 if ( !$hideimages ) {
196 $ilRes = $queryFunc( $dbr, 'imagelinks', 'il_from' );
197 }
198
199 if ( ( !$fetchlinks || !$plRes->numRows() )
200 && ( $hidetrans || !$tlRes->numRows() )
201 && ( $hideimages || !$ilRes->numRows() )
202 ) {
203 if ( 0 == $level ) {
204 if ( !$this->including() ) {
205 $out->addHTML( $this->whatlinkshereForm() );
206
207 // Show filters only if there are links
208 if ( $hidelinks || $hidetrans || $hideredirs || $hideimages ) {
209 $out->addHTML( $this->getFilterPanel() );
210 }
211 $errMsg = is_int( $namespace ) ? 'nolinkshere-ns' : 'nolinkshere';
212 $out->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
213 $out->setStatusCode( 404 );
214 }
215 }
216
217 return;
218 }
219
220 // Read the rows into an array and remove duplicates
221 // templatelinks comes second so that the templatelinks row overwrites the
222 // pagelinks row, so we get (inclusion) rather than nothing
223 if ( $fetchlinks ) {
224 foreach ( $plRes as $row ) {
225 $row->is_template = 0;
226 $row->is_image = 0;
227 $rows[$row->page_id] = $row;
228 }
229 }
230 if ( !$hidetrans ) {
231 foreach ( $tlRes as $row ) {
232 $row->is_template = 1;
233 $row->is_image = 0;
234 $rows[$row->page_id] = $row;
235 }
236 }
237 if ( !$hideimages ) {
238 foreach ( $ilRes as $row ) {
239 $row->is_template = 0;
240 $row->is_image = 1;
241 $rows[$row->page_id] = $row;
242 }
243 }
244
245 // Sort by key and then change the keys to 0-based indices
246 ksort( $rows );
247 $rows = array_values( $rows );
248
249 $numRows = count( $rows );
250
251 // Work out the start and end IDs, for prev/next links
252 if ( $numRows > $limit ) {
253 // More rows available after these ones
254 // Get the ID from the last row in the result set
255 $nextId = $rows[$limit]->page_id;
256 // Remove undisplayed rows
257 $rows = array_slice( $rows, 0, $limit );
258 } else {
259 // No more rows after
260 $nextId = false;
261 }
262 $prevId = $from;
263
264 // use LinkBatch to make sure, that all required data (associated with Titles)
265 // is loaded in one query
266 $lb = new LinkBatch();
267 foreach ( $rows as $row ) {
268 $lb->add( $row->page_namespace, $row->page_title );
269 }
270 $lb->execute();
271
272 if ( $level == 0 ) {
273 if ( !$this->including() ) {
274 $out->addHTML( $this->whatlinkshereForm() );
275 $out->addHTML( $this->getFilterPanel() );
276 $out->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
277
278 $prevnext = $this->getPrevNext( $prevId, $nextId );
279 $out->addHTML( $prevnext );
280 }
281 }
282 $out->addHTML( $this->listStart( $level ) );
283 foreach ( $rows as $row ) {
284 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
285
286 if ( $row->rd_from && $level < 2 ) {
287 $out->addHTML( $this->listItem( $row, $nt, $target, true ) );
288 $this->showIndirectLinks(
289 $level + 1,
290 $nt,
291 $this->getConfig()->get( 'MaxRedirectLinksRetrieved' )
292 );
293 $out->addHTML( Xml::closeElement( 'li' ) );
294 } else {
295 $out->addHTML( $this->listItem( $row, $nt, $target ) );
296 }
297 }
298
299 $out->addHTML( $this->listEnd() );
300
301 if ( $level == 0 ) {
302 if ( !$this->including() ) {
303 $out->addHTML( $prevnext );
304 }
305 }
306 }
307
308 protected function listStart( $level ) {
309 return Xml::openElement( 'ul', ( $level ? [] : [ 'id' => 'mw-whatlinkshere-list' ] ) );
310 }
311
312 protected function listItem( $row, $nt, $target, $notClose = false ) {
313 $dirmark = $this->getLanguage()->getDirMark();
314
315 # local message cache
316 static $msgcache = null;
317 if ( $msgcache === null ) {
318 static $msgs = [ 'isredirect', 'istemplate', 'semicolon-separator',
319 'whatlinkshere-links', 'isimage', 'editlink' ];
320 $msgcache = [];
321 foreach ( $msgs as $msg ) {
322 $msgcache[$msg] = $this->msg( $msg )->escaped();
323 }
324 }
325
326 if ( $row->rd_from ) {
327 $query = [ 'redirect' => 'no' ];
328 } else {
329 $query = [];
330 }
331
332 $link = $this->getLinkRenderer()->makeKnownLink(
333 $nt,
334 null,
335 $row->page_is_redirect ? [ 'class' => 'mw-redirect' ] : [],
336 $query
337 );
338
339 // Display properties (redirect or template)
340 $propsText = '';
341 $props = [];
342 if ( $row->rd_from ) {
343 $props[] = $msgcache['isredirect'];
344 }
345 if ( $row->is_template ) {
346 $props[] = $msgcache['istemplate'];
347 }
348 if ( $row->is_image ) {
349 $props[] = $msgcache['isimage'];
350 }
351
352 Hooks::run( 'WhatLinksHereProps', [ $row, $nt, $target, &$props ] );
353
354 if ( count( $props ) ) {
355 $propsText = $this->msg( 'parentheses' )
356 ->rawParams( implode( $msgcache['semicolon-separator'], $props ) )->escaped();
357 }
358
359 # Space for utilities links, with a what-links-here link provided
360 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'], $msgcache['editlink'] );
361 $wlh = Xml::wrapClass(
362 $this->msg( 'parentheses' )->rawParams( $wlhLink )->escaped(),
363 'mw-whatlinkshere-tools'
364 );
365
366 return $notClose ?
367 Xml::openElement( 'li' ) . "$link $propsText $dirmark $wlh\n" :
368 Xml::tags( 'li', null, "$link $propsText $dirmark $wlh" ) . "\n";
369 }
370
371 protected function listEnd() {
372 return Xml::closeElement( 'ul' );
373 }
374
375 protected function wlhLink( Title $target, $text, $editText ) {
376 static $title = null;
377 if ( $title === null ) {
378 $title = $this->getPageTitle();
379 }
380
382
383 if ( $text !== null ) {
384 $text = new HtmlArmor( $text );
385 }
386
387 // always show a "<- Links" link
388 $links = [
389 'links' => $linkRenderer->makeKnownLink(
390 $title,
391 $text,
392 [],
393 [ 'target' => $target->getPrefixedText() ]
394 ),
395 ];
396
397 // if the page is editable, add an edit link
398 if (
399 // check user permissions
400 $this->getUser()->isAllowed( 'edit' ) &&
401 // check, if the content model is editable through action=edit
402 ContentHandler::getForTitle( $target )->supportsDirectEditing()
403 ) {
404 if ( $editText !== null ) {
405 $editText = new HtmlArmor( $editText );
406 }
407
408 $links['edit'] = $linkRenderer->makeKnownLink(
409 $target,
410 $editText,
411 [],
412 [ 'action' => 'edit' ]
413 );
414 }
415
416 // build the links html
417 return $this->getLanguage()->pipeList( $links );
418 }
419
420 function makeSelfLink( $text, $query ) {
421 if ( $text !== null ) {
422 $text = new HtmlArmor( $text );
423 }
424
425 return $this->getLinkRenderer()->makeKnownLink(
426 $this->selfTitle,
427 $text,
428 [],
429 $query
430 );
431 }
432
433 function getPrevNext( $prevId, $nextId ) {
434 $currentLimit = $this->opts->getValue( 'limit' );
435 $prev = $this->msg( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
436 $next = $this->msg( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
437
438 $changed = $this->opts->getChangedValues();
439 unset( $changed['target'] ); // Already in the request title
440
441 if ( 0 != $prevId ) {
442 $overrides = [ 'from' => $this->opts->getValue( 'back' ) ];
443 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
444 }
445 if ( 0 != $nextId ) {
446 $overrides = [ 'from' => $nextId, 'back' => $prevId ];
447 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
448 }
449
450 $limitLinks = [];
451 $lang = $this->getLanguage();
452 foreach ( $this->limits as $limit ) {
453 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
454 $overrides = [ 'limit' => $limit ];
455 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
456 }
457
458 $nums = $lang->pipeList( $limitLinks );
459
460 return $this->msg( 'viewprevnext' )->rawParams( $prev, $next, $nums )->escaped();
461 }
462
463 function whatlinkshereForm() {
464 // We get nicer value from the title object
465 $this->opts->consumeValue( 'target' );
466 // Reset these for new requests
467 $this->opts->consumeValues( [ 'back', 'from' ] );
468
469 $target = $this->target ? $this->target->getPrefixedText() : '';
470 $namespace = $this->opts->consumeValue( 'namespace' );
471 $nsinvert = $this->opts->consumeValue( 'invert' );
472
473 # Build up the form
474 $f = Xml::openElement( 'form', [ 'action' => wfScript() ] );
475
476 # Values that should not be forgotten
477 $f .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
478 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
479 $f .= Html::hidden( $name, $value );
480 }
481
482 $f .= Xml::fieldset( $this->msg( 'whatlinkshere' )->text() );
483
484 # Target input (.mw-searchInput enables suggestions)
485 $f .= Xml::inputLabel( $this->msg( 'whatlinkshere-page' )->text(), 'target',
486 'mw-whatlinkshere-target', 40, $target, [ 'class' => 'mw-searchInput' ] );
487
488 $f .= ' ';
489
490 # Namespace selector
491 $f .= Html::namespaceSelector(
492 [
493 'selected' => $namespace,
494 'all' => '',
495 'label' => $this->msg( 'namespace' )->text()
496 ], [
497 'name' => 'namespace',
498 'id' => 'namespace',
499 'class' => 'namespaceselector',
500 ]
501 );
502
503 $f .= '&#160;' .
504 Xml::checkLabel(
505 $this->msg( 'invert' )->text(),
506 'invert',
507 'nsinvert',
508 $nsinvert,
509 [ 'title' => $this->msg( 'tooltip-whatlinkshere-invert' )->text() ]
510 );
511
512 $f .= ' ';
513
514 # Submit
515 $f .= Xml::submitButton( $this->msg( 'whatlinkshere-submit' )->text() );
516
517 # Close
518 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
519
520 return $f;
521 }
522
528 function getFilterPanel() {
529 $show = $this->msg( 'show' )->escaped();
530 $hide = $this->msg( 'hide' )->escaped();
531
532 $changed = $this->opts->getChangedValues();
533 unset( $changed['target'] ); // Already in the request title
534
535 $links = [];
536 $types = [ 'hidetrans', 'hidelinks', 'hideredirs' ];
537 if ( $this->target->getNamespace() == NS_FILE ) {
538 $types[] = 'hideimages';
539 }
540
541 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans',
542 // 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
543 // To be sure they will be found by grep
544 foreach ( $types as $type ) {
545 $chosen = $this->opts->getValue( $type );
546 $msg = $chosen ? $show : $hide;
547 $overrides = [ $type => !$chosen ];
548 $links[] = $this->msg( "whatlinkshere-{$type}" )->rawParams(
549 $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) )->escaped();
550 }
551
552 return Xml::fieldset(
553 $this->msg( 'whatlinkshere-filters' )->text(),
554 $this->getLanguage()->pipeList( $links )
555 );
556 }
557
566 public function prefixSearchSubpages( $search, $limit, $offset ) {
567 return $this->prefixSearchString( $search, $limit, $offset );
568 }
569
570 protected function getGroupName() {
571 return 'pagetools';
572 }
573}
within a display generated by the Derivative if and wherever such third party notices normally appear The contents of the NOTICE file are for informational purposes only and do not modify the License You may add Your own attribution notices within Derivative Works that You alongside or as an addendum to the NOTICE text from the provided that such additional attribution notices cannot be construed as modifying the License You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for or distribution of Your or for any such Derivative Works as a provided Your and distribution of the Work otherwise complies with the conditions stated in this License Submission of Contributions Unless You explicitly state any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this without any additional terms or conditions Notwithstanding the nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions Trademarks This License does not grant permission to use the trade service or product names of the except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file Disclaimer of Warranty Unless required by applicable law or agreed to in Licensor provides the WITHOUT WARRANTIES OR CONDITIONS OF ANY either express or including
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
static getForTitle(Title $title)
Returns the appropriate ContentHandler singleton for the given title.
Helper class to keep track of options when mixing links and form elements.
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
setValue( $name, $value, $force=false)
Use to set the value of an option.
fetchValuesFromRequest(WebRequest $r, $optionKeys=null)
Fetch values for all options (or selected options) from the given WebRequest, making them available f...
validateIntBounds( $name, $min, $max)
const INTNULL
Integer type or null, maps to WebRequest::getIntOrNull() This is useful for the namespace selector.
getValue( $name)
Get the value for the given option name.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:28
Shortcut to construct an includable special page.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
getSkin()
Shortcut to get the skin being used for this instance.
msg( $key)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
prefixSearchString( $search, $limit, $offset)
Perform a regular substring search for prefixSearchSubpages.
MediaWiki Linker LinkRenderer null $linkRenderer
Implements Special:Whatlinkshere.
execute( $par)
Default execute method Checks user permissions.
wlhLink(Title $target, $text, $editText)
listItem( $row, $nt, $target, $notClose=false)
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
getFilterPanel()
Create filter panel.
showIndirectLinks( $level, $target, $limit, $from=0, $back=0)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getPrevNext( $prevId, $nextId)
Represents a title within MediaWiki.
Definition Title.php:39
getNamespace()
Get the namespace index, i.e.
Definition Title.php:978
getDBkey()
Get the main part with underscores.
Definition Title.php:955
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1550
if(! $regexes) $dbr
Definition cleanup.php:94
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition hooks.txt:2746
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:862
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:2989
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:1610
const NS_FILE
Definition Defines.php:71
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:40
const DB_REPLICA
Definition defines.php:25
if(!isset( $args[0])) $lang