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 global $wgQueryPageDefaultLimit;
46 $out = $this->getOutput();
49 $this->outputHeader();
51 $opts = new FormOptions();
53 $opts->add( 'target', '' );
54 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
55 $opts->add( 'limit', $wgQueryPageDefaultLimit );
56 $opts->add( 'from', 0 );
57 $opts->add( 'back', 0 );
58 $opts->add( 'hideredirs', false );
59 $opts->add( 'hidetrans', false );
60 $opts->add( 'hidelinks', false );
61 $opts->add( 'hideimages', false );
63 $opts->fetchValuesFromRequest( $this->getRequest() );
64 $opts->validateIntBounds( 'limit', 0, 5000 );
66 // Give precedence to subpage syntax
67 if ( isset( $par ) ) {
68 $opts->setValue( 'target', $par );
71 // Bind to member variable
74 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
75 if ( !$this->target
) {
76 $out->addHTML( $this->whatlinkshereForm() );
81 $this->getSkin()->setRelevantTitle( $this->target
);
83 $this->selfTitle
= $this->getPageTitle( $this->target
->getPrefixedDBkey() );
85 $out->setPageTitle( $this->msg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
86 $out->addBacklinkSubtitle( $this->target
);
87 $this->showIndirectLinks(
90 $opts->getValue( 'limit' ),
91 $opts->getValue( 'from' ),
92 $opts->getValue( 'back' )
97 * @param int $level Recursion level
98 * @param Title $target Target title
99 * @param int $limit Number of entries to display
100 * @param int $from Display from this article ID (default: 0)
101 * @param int $back Display from this article ID at backwards scrolling (default: 0)
103 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
104 global $wgMaxRedirectLinksRetrieved;
105 $out = $this->getOutput();
106 $dbr = wfGetDB( DB_SLAVE
);
109 $hidelinks = $this->opts
->getValue( 'hidelinks' );
110 $hideredirs = $this->opts
->getValue( 'hideredirs' );
111 $hidetrans = $this->opts
->getValue( 'hidetrans' );
112 $hideimages = $target->getNamespace() != NS_FILE ||
$this->opts
->getValue( 'hideimages' );
114 $fetchlinks = ( !$hidelinks ||
!$hideredirs );
119 'pl_namespace' => $target->getNamespace(),
120 'pl_title' => $target->getDBkey(),
123 $plConds['rd_from'] = null;
124 } elseif ( $hidelinks ) {
125 $plConds[] = 'rd_from is NOT NULL';
130 'tl_namespace' => $target->getNamespace(),
131 'tl_title' => $target->getDBkey(),
136 'il_to' => $target->getDBkey(),
139 $namespace = $this->opts
->getValue( 'namespace' );
140 if ( is_int( $namespace ) ) {
141 $plConds['page_namespace'] = $namespace;
142 $tlConds['page_namespace'] = $namespace;
143 $ilConds['page_namespace'] = $namespace;
147 $tlConds[] = "tl_from >= $from";
148 $plConds[] = "pl_from >= $from";
149 $ilConds[] = "il_from >= $from";
152 // Read an extra row as an at-end check
153 $queryLimit = $limit +
1;
155 $options['LIMIT'] = $queryLimit;
156 $fields = array( 'page_id', 'page_namespace', 'page_title', 'rd_from' );
158 $joinConds = array( 'redirect' => array( 'LEFT JOIN', array(
160 'rd_namespace' => $target->getNamespace(),
161 'rd_title' => $target->getDBkey(),
162 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'
166 $options['ORDER BY'] = 'pl_from';
167 $plRes = $dbr->select( array( 'pagelinks', 'page', 'redirect' ), $fields,
168 $plConds, __METHOD__
, $options,
174 $options['ORDER BY'] = 'tl_from';
175 $tlRes = $dbr->select( array( 'templatelinks', 'page', 'redirect' ), $fields,
176 $tlConds, __METHOD__
, $options,
181 if ( !$hideimages ) {
182 $options['ORDER BY'] = 'il_from';
183 $ilRes = $dbr->select( array( 'imagelinks', 'page', 'redirect' ), $fields,
184 $ilConds, __METHOD__
, $options,
189 if ( ( !$fetchlinks ||
!$plRes->numRows() )
190 && ( $hidetrans ||
!$tlRes->numRows() )
191 && ( $hideimages ||
!$ilRes->numRows() )
194 if ( !$this->including() ) {
195 $out->addHTML( $this->whatlinkshereForm() );
197 // Show filters only if there are links
198 if ( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages ) {
199 $out->addHTML( $this->getFilterPanel() );
201 $errMsg = is_int( $namespace ) ?
'nolinkshere-ns' : 'nolinkshere';
202 $out->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
209 // Read the rows into an array and remove duplicates
210 // templatelinks comes second so that the templatelinks row overwrites the
211 // pagelinks row, so we get (inclusion) rather than nothing
213 foreach ( $plRes as $row ) {
214 $row->is_template
= 0;
216 $rows[$row->page_id
] = $row;
220 foreach ( $tlRes as $row ) {
221 $row->is_template
= 1;
223 $rows[$row->page_id
] = $row;
226 if ( !$hideimages ) {
227 foreach ( $ilRes as $row ) {
228 $row->is_template
= 0;
230 $rows[$row->page_id
] = $row;
234 // Sort by key and then change the keys to 0-based indices
236 $rows = array_values( $rows );
238 $numRows = count( $rows );
240 // Work out the start and end IDs, for prev/next links
241 if ( $numRows > $limit ) {
242 // More rows available after these ones
243 // Get the ID from the last row in the result set
244 $nextId = $rows[$limit]->page_id
;
245 // Remove undisplayed rows
246 $rows = array_slice( $rows, 0, $limit );
248 // No more rows after
254 if ( !$this->including() ) {
255 $out->addHTML( $this->whatlinkshereForm() );
256 $out->addHTML( $this->getFilterPanel() );
257 $out->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
259 $prevnext = $this->getPrevNext( $prevId, $nextId );
260 $out->addHTML( $prevnext );
263 $out->addHTML( $this->listStart( $level ) );
264 foreach ( $rows as $row ) {
265 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
267 if ( $row->rd_from
&& $level < 2 ) {
268 $out->addHTML( $this->listItem( $row, $nt, true ) );
269 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
270 $out->addHTML( Xml
::closeElement( 'li' ) );
272 $out->addHTML( $this->listItem( $row, $nt ) );
276 $out->addHTML( $this->listEnd() );
279 if ( !$this->including() ) {
280 $out->addHTML( $prevnext );
285 protected function listStart( $level ) {
286 return Xml
::openElement( 'ul', ( $level ?
array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
289 protected function listItem( $row, $nt, $notClose = false ) {
290 $dirmark = $this->getLanguage()->getDirMark();
292 # local message cache
293 static $msgcache = null;
294 if ( $msgcache === null ) {
295 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
296 'whatlinkshere-links', 'isimage' );
298 foreach ( $msgs as $msg ) {
299 $msgcache[$msg] = $this->msg( $msg )->escaped();
303 if ( $row->rd_from
) {
304 $query = array( 'redirect' => 'no' );
309 $link = Linker
::linkKnown(
316 // Display properties (redirect or template)
319 if ( $row->rd_from
) {
320 $props[] = $msgcache['isredirect'];
322 if ( $row->is_template
) {
323 $props[] = $msgcache['istemplate'];
325 if ( $row->is_image
) {
326 $props[] = $msgcache['isimage'];
329 if ( count( $props ) ) {
330 $propsText = $this->msg( 'parentheses' )
331 ->rawParams( implode( $msgcache['semicolon-separator'], $props ) )->escaped();
334 # Space for utilities links, with a what-links-here link provided
335 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
336 $wlh = Xml
::wrapClass(
337 $this->msg( 'parentheses' )->rawParams( $wlhLink )->escaped(),
338 'mw-whatlinkshere-tools'
342 Xml
::openElement( 'li' ) . "$link $propsText $dirmark $wlh\n" :
343 Xml
::tags( 'li', null, "$link $propsText $dirmark $wlh" ) . "\n";
346 protected function listEnd() {
347 return Xml
::closeElement( 'ul' );
350 protected function wlhLink( Title
$target, $text ) {
351 static $title = null;
352 if ( $title === null ) {
353 $title = $this->getPageTitle();
356 return Linker
::linkKnown(
360 array( 'target' => $target->getPrefixedText() )
364 function makeSelfLink( $text, $query ) {
365 return Linker
::linkKnown(
373 function getPrevNext( $prevId, $nextId ) {
374 $currentLimit = $this->opts
->getValue( 'limit' );
375 $prev = $this->msg( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
376 $next = $this->msg( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
378 $changed = $this->opts
->getChangedValues();
379 unset( $changed['target'] ); // Already in the request title
381 if ( 0 != $prevId ) {
382 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
383 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
385 if ( 0 != $nextId ) {
386 $overrides = array( 'from' => $nextId, 'back' => $prevId );
387 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
390 $limitLinks = array();
391 $lang = $this->getLanguage();
392 foreach ( $this->limits
as $limit ) {
393 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
394 $overrides = array( 'limit' => $limit );
395 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
398 $nums = $lang->pipeList( $limitLinks );
400 return $this->msg( 'viewprevnext' )->rawParams( $prev, $next, $nums )->escaped();
403 function whatlinkshereForm() {
406 // We get nicer value from the title object
407 $this->opts
->consumeValue( 'target' );
408 // Reset these for new requests
409 $this->opts
->consumeValues( array( 'back', 'from' ) );
411 $target = $this->target ?
$this->target
->getPrefixedText() : '';
412 $namespace = $this->opts
->consumeValue( 'namespace' );
415 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
417 # Values that should not be forgotten
418 $f .= Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
419 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
420 $f .= Html
::hidden( $name, $value );
423 $f .= Xml
::fieldset( $this->msg( 'whatlinkshere' )->text() );
426 $f .= Xml
::inputLabel( $this->msg( 'whatlinkshere-page' )->text(), 'target',
427 'mw-whatlinkshere-target', 40, $target );
432 $f .= Html
::namespaceSelector(
434 'selected' => $namespace,
436 'label' => $this->msg( 'namespace' )->text()
438 'name' => 'namespace',
440 'class' => 'namespaceselector',
447 $f .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() );
450 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
456 * Create filter panel
458 * @return string HTML fieldset and filter panel with the show/hide links
460 function getFilterPanel() {
461 $show = $this->msg( 'show' )->escaped();
462 $hide = $this->msg( 'hide' )->escaped();
464 $changed = $this->opts
->getChangedValues();
465 unset( $changed['target'] ); // Already in the request title
468 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
469 if ( $this->target
->getNamespace() == NS_FILE
) {
470 $types[] = 'hideimages';
473 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans',
474 // 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
475 // To be sure they will be found by grep
476 foreach ( $types as $type ) {
477 $chosen = $this->opts
->getValue( $type );
478 $msg = $chosen ?
$show : $hide;
479 $overrides = array( $type => !$chosen );
480 $links[] = $this->msg( "whatlinkshere-{$type}" )->rawParams(
481 $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) )->escaped();
484 return Xml
::fieldset(
485 $this->msg( 'whatlinkshere-filters' )->text(),
486 $this->getLanguage()->pipeList( $links )
490 protected function getGroupName() {