3 * Implements Special:Whatlinkshere
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @todo Use some variant of Pager or something; the pagination here is lousy.
25 * Implements Special:Whatlinkshere
27 * @ingroup SpecialPage
29 class SpecialWhatLinksHere
extends IncludableSpecialPage
{
30 /** @var FormOptions */
38 protected $limits = array( 20, 50, 100, 250, 500 );
40 public function __construct() {
41 parent
::__construct( 'Whatlinkshere' );
44 function execute( $par ) {
45 $out = $this->getOutput();
48 $this->outputHeader();
50 $opts = new FormOptions();
52 $opts->add( 'target', '' );
53 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
54 $opts->add( 'limit', $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
55 $opts->add( 'from', 0 );
56 $opts->add( 'back', 0 );
57 $opts->add( 'hideredirs', false );
58 $opts->add( 'hidetrans', false );
59 $opts->add( 'hidelinks', false );
60 $opts->add( 'hideimages', false );
62 $opts->fetchValuesFromRequest( $this->getRequest() );
63 $opts->validateIntBounds( 'limit', 0, 5000 );
65 // Give precedence to subpage syntax
66 if ( $par !== null ) {
67 $opts->setValue( 'target', $par );
70 // Bind to member variable
73 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
74 if ( !$this->target
) {
75 $out->addHTML( $this->whatlinkshereForm() );
80 $this->getSkin()->setRelevantTitle( $this->target
);
82 $this->selfTitle
= $this->getPageTitle( $this->target
->getPrefixedDBkey() );
84 $out->setPageTitle( $this->msg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
85 $out->addBacklinkSubtitle( $this->target
);
86 $this->showIndirectLinks(
89 $opts->getValue( 'limit' ),
90 $opts->getValue( 'from' ),
91 $opts->getValue( 'back' )
96 * @param int $level Recursion level
97 * @param Title $target Target title
98 * @param int $limit Number of entries to display
99 * @param int $from Display from this article ID (default: 0)
100 * @param int $back Display from this article ID at backwards scrolling (default: 0)
102 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
103 $out = $this->getOutput();
104 $dbr = wfGetDB( DB_SLAVE
);
106 $hidelinks = $this->opts
->getValue( 'hidelinks' );
107 $hideredirs = $this->opts
->getValue( 'hideredirs' );
108 $hidetrans = $this->opts
->getValue( 'hidetrans' );
109 $hideimages = $target->getNamespace() != NS_FILE ||
$this->opts
->getValue( 'hideimages' );
111 $fetchlinks = ( !$hidelinks ||
!$hideredirs );
113 // Build query conds in concert for all three tables...
114 $conds['pagelinks'] = array(
115 'pl_namespace' => $target->getNamespace(),
116 'pl_title' => $target->getDBkey(),
118 $conds['templatelinks'] = array(
119 'tl_namespace' => $target->getNamespace(),
120 'tl_title' => $target->getDBkey(),
122 $conds['imagelinks'] = array(
123 'il_to' => $target->getDBkey(),
126 $useLinkNamespaceDBFields = $this->getConfig()->get( 'UseLinkNamespaceDBFields' );
127 $namespace = $this->opts
->getValue( 'namespace' );
128 if ( is_int( $namespace ) ) {
129 if ( $useLinkNamespaceDBFields ) {
130 $conds['pagelinks']['pl_from_namespace'] = $namespace;
131 $conds['templatelinks']['tl_from_namespace'] = $namespace;
132 $conds['imagelinks']['il_from_namespace'] = $namespace;
134 $conds['pagelinks']['page_namespace'] = $namespace;
135 $conds['templatelinks']['page_namespace'] = $namespace;
136 $conds['imagelinks']['page_namespace'] = $namespace;
141 $conds['templatelinks'][] = "tl_from >= $from";
142 $conds['pagelinks'][] = "pl_from >= $from";
143 $conds['imagelinks'][] = "il_from >= $from";
147 $conds['pagelinks']['rd_from'] = null;
148 } elseif ( $hidelinks ) {
149 $conds['pagelinks'][] = 'rd_from is NOT NULL';
152 $queryFunc = function ( $dbr, $table, $fromCol ) use ( $conds, $target, $limit, $useLinkNamespaceDBFields ) {
153 // Read an extra row as an at-end check
154 $queryLimit = $limit +
1;
156 "rd_from = $fromCol",
157 'rd_title' => $target->getDBkey(),
158 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'
160 if ( $useLinkNamespaceDBFields ) { // migration check
161 $on['rd_namespace'] = $target->getNamespace();
163 // Inner LIMIT is 2X in case of stale backlinks with wrong namespaces
164 $subQuery = $dbr->selectSqlText(
165 array( $table, 'page', 'redirect' ),
166 array( $fromCol, 'rd_from' ),
168 __CLASS__
. '::showIndirectLinks',
169 array( 'ORDER BY' => $fromCol, 'LIMIT' => 2 * $queryLimit ),
171 'page' => array( 'INNER JOIN', "$fromCol = page_id" ),
172 'redirect' => array( 'LEFT JOIN', $on )
176 array( 'page', 'temp_backlink_range' => "($subQuery)" ),
177 array( 'page_id', 'page_namespace', 'page_title', 'rd_from' ),
179 __CLASS__
. '::showIndirectLinks',
180 array( 'ORDER BY' => 'page_id', 'LIMIT' => $queryLimit ),
181 array( 'page' => array( 'INNER JOIN', "$fromCol = page_id" ) )
186 $plRes = $queryFunc( $dbr, 'pagelinks', 'pl_from' );
190 $tlRes = $queryFunc( $dbr, 'templatelinks', 'tl_from' );
193 if ( !$hideimages ) {
194 $ilRes = $queryFunc( $dbr, 'imagelinks', 'il_from' );
197 if ( ( !$fetchlinks ||
!$plRes->numRows() )
198 && ( $hidetrans ||
!$tlRes->numRows() )
199 && ( $hideimages ||
!$ilRes->numRows() )
202 if ( !$this->including() ) {
203 $out->addHTML( $this->whatlinkshereForm() );
205 // Show filters only if there are links
206 if ( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages ) {
207 $out->addHTML( $this->getFilterPanel() );
209 $errMsg = is_int( $namespace ) ?
'nolinkshere-ns' : 'nolinkshere';
210 $out->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
211 $out->setStatusCode( 404 );
218 // Read the rows into an array and remove duplicates
219 // templatelinks comes second so that the templatelinks row overwrites the
220 // pagelinks row, so we get (inclusion) rather than nothing
222 foreach ( $plRes as $row ) {
223 $row->is_template
= 0;
225 $rows[$row->page_id
] = $row;
229 foreach ( $tlRes as $row ) {
230 $row->is_template
= 1;
232 $rows[$row->page_id
] = $row;
235 if ( !$hideimages ) {
236 foreach ( $ilRes as $row ) {
237 $row->is_template
= 0;
239 $rows[$row->page_id
] = $row;
243 // Sort by key and then change the keys to 0-based indices
245 $rows = array_values( $rows );
247 $numRows = count( $rows );
249 // Work out the start and end IDs, for prev/next links
250 if ( $numRows > $limit ) {
251 // More rows available after these ones
252 // Get the ID from the last row in the result set
253 $nextId = $rows[$limit]->page_id
;
254 // Remove undisplayed rows
255 $rows = array_slice( $rows, 0, $limit );
257 // No more rows after
263 if ( !$this->including() ) {
264 $out->addHTML( $this->whatlinkshereForm() );
265 $out->addHTML( $this->getFilterPanel() );
266 $out->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
268 $prevnext = $this->getPrevNext( $prevId, $nextId );
269 $out->addHTML( $prevnext );
272 $out->addHTML( $this->listStart( $level ) );
273 foreach ( $rows as $row ) {
274 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
276 if ( $row->rd_from
&& $level < 2 ) {
277 $out->addHTML( $this->listItem( $row, $nt, $target, true ) );
278 $this->showIndirectLinks( $level +
1, $nt, $this->getConfig()->get( 'MaxRedirectLinksRetrieved' ) );
279 $out->addHTML( Xml
::closeElement( 'li' ) );
281 $out->addHTML( $this->listItem( $row, $nt, $target ) );
285 $out->addHTML( $this->listEnd() );
288 if ( !$this->including() ) {
289 $out->addHTML( $prevnext );
294 protected function listStart( $level ) {
295 return Xml
::openElement( 'ul', ( $level ?
array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
298 protected function listItem( $row, $nt, $target, $notClose = false ) {
299 $dirmark = $this->getLanguage()->getDirMark();
301 # local message cache
302 static $msgcache = null;
303 if ( $msgcache === null ) {
304 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
305 'whatlinkshere-links', 'isimage' );
307 foreach ( $msgs as $msg ) {
308 $msgcache[$msg] = $this->msg( $msg )->escaped();
312 if ( $row->rd_from
) {
313 $query = array( 'redirect' => 'no' );
318 $link = Linker
::linkKnown(
325 // Display properties (redirect or template)
328 if ( $row->rd_from
) {
329 $props[] = $msgcache['isredirect'];
331 if ( $row->is_template
) {
332 $props[] = $msgcache['istemplate'];
334 if ( $row->is_image
) {
335 $props[] = $msgcache['isimage'];
338 wfRunHooks( 'WhatLinksHereProps', array( $row, $nt, $target, &$props ) );
340 if ( count( $props ) ) {
341 $propsText = $this->msg( 'parentheses' )
342 ->rawParams( implode( $msgcache['semicolon-separator'], $props ) )->escaped();
345 # Space for utilities links, with a what-links-here link provided
346 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
347 $wlh = Xml
::wrapClass(
348 $this->msg( 'parentheses' )->rawParams( $wlhLink )->escaped(),
349 'mw-whatlinkshere-tools'
353 Xml
::openElement( 'li' ) . "$link $propsText $dirmark $wlh\n" :
354 Xml
::tags( 'li', null, "$link $propsText $dirmark $wlh" ) . "\n";
357 protected function listEnd() {
358 return Xml
::closeElement( 'ul' );
361 protected function wlhLink( Title
$target, $text ) {
362 static $title = null;
363 if ( $title === null ) {
364 $title = $this->getPageTitle();
367 return Linker
::linkKnown(
371 array( 'target' => $target->getPrefixedText() )
375 function makeSelfLink( $text, $query ) {
376 return Linker
::linkKnown(
384 function getPrevNext( $prevId, $nextId ) {
385 $currentLimit = $this->opts
->getValue( 'limit' );
386 $prev = $this->msg( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
387 $next = $this->msg( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
389 $changed = $this->opts
->getChangedValues();
390 unset( $changed['target'] ); // Already in the request title
392 if ( 0 != $prevId ) {
393 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
394 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
396 if ( 0 != $nextId ) {
397 $overrides = array( 'from' => $nextId, 'back' => $prevId );
398 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
401 $limitLinks = array();
402 $lang = $this->getLanguage();
403 foreach ( $this->limits
as $limit ) {
404 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
405 $overrides = array( 'limit' => $limit );
406 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
409 $nums = $lang->pipeList( $limitLinks );
411 return $this->msg( 'viewprevnext' )->rawParams( $prev, $next, $nums )->escaped();
414 function whatlinkshereForm() {
415 // We get nicer value from the title object
416 $this->opts
->consumeValue( 'target' );
417 // Reset these for new requests
418 $this->opts
->consumeValues( array( 'back', 'from' ) );
420 $target = $this->target ?
$this->target
->getPrefixedText() : '';
421 $namespace = $this->opts
->consumeValue( 'namespace' );
424 $f = Xml
::openElement( 'form', array( 'action' => wfScript() ) );
426 # Values that should not be forgotten
427 $f .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
428 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
429 $f .= Html
::hidden( $name, $value );
432 $f .= Xml
::fieldset( $this->msg( 'whatlinkshere' )->text() );
434 # Target input (.mw-searchInput enables suggestions)
435 $f .= Xml
::inputLabel( $this->msg( 'whatlinkshere-page' )->text(), 'target',
436 'mw-whatlinkshere-target', 40, $target, array( 'class' => 'mw-searchInput' ) );
441 $f .= Html
::namespaceSelector(
443 'selected' => $namespace,
445 'label' => $this->msg( 'namespace' )->text()
447 'name' => 'namespace',
449 'class' => 'namespaceselector',
456 $f .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() );
459 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
465 * Create filter panel
467 * @return string HTML fieldset and filter panel with the show/hide links
469 function getFilterPanel() {
470 $show = $this->msg( 'show' )->escaped();
471 $hide = $this->msg( 'hide' )->escaped();
473 $changed = $this->opts
->getChangedValues();
474 unset( $changed['target'] ); // Already in the request title
477 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
478 if ( $this->target
->getNamespace() == NS_FILE
) {
479 $types[] = 'hideimages';
482 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans',
483 // 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
484 // To be sure they will be found by grep
485 foreach ( $types as $type ) {
486 $chosen = $this->opts
->getValue( $type );
487 $msg = $chosen ?
$show : $hide;
488 $overrides = array( $type => !$chosen );
489 $links[] = $this->msg( "whatlinkshere-{$type}" )->rawParams(
490 $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) )->escaped();
493 return Xml
::fieldset(
494 $this->msg( 'whatlinkshere-filters' )->text(),
495 $this->getLanguage()->pipeList( $links )
499 protected function getGroupName() {