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 SpecialPage
{
43 protected $limits = array( 20, 50, 100, 250, 500 );
45 public function __construct() {
46 parent
::__construct( 'Whatlinkshere' );
49 function execute( $par ) {
50 global $wgQueryPageDefaultLimit;
51 $out = $this->getOutput();
54 $this->outputHeader();
56 $opts = new FormOptions();
58 $opts->add( 'target', '' );
59 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
60 $opts->add( 'limit', $wgQueryPageDefaultLimit );
61 $opts->add( 'from', 0 );
62 $opts->add( 'back', 0 );
63 $opts->add( 'hideredirs', false );
64 $opts->add( 'hidetrans', false );
65 $opts->add( 'hidelinks', false );
66 $opts->add( 'hideimages', false );
68 $opts->fetchValuesFromRequest( $this->getRequest() );
69 $opts->validateIntBounds( 'limit', 0, 5000 );
71 // Give precedence to subpage syntax
73 $opts->setValue( 'target', $par );
76 // Bind to member variable
79 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
80 if( !$this->target
) {
81 $out->addHTML( $this->whatlinkshereForm() );
85 $this->getSkin()->setRelevantTitle( $this->target
);
87 $this->selfTitle
= $this->getTitle( $this->target
->getPrefixedDBkey() );
89 $out->setPageTitle( $this->msg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
90 $out->addBacklinkSubtitle( $this->target
);
92 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
93 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
97 * @param $level int Recursion level
98 * @param $target Title Target title
99 * @param $limit int Number of entries to display
100 * @param $from Title Display from this article ID
101 * @param $back Title Display from this article ID at backwards scrolling
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 // Enforce join order, sometimes namespace selector may
156 // trigger filesorts which are far less efficient than scanning many entries
157 $options[] = 'STRAIGHT_JOIN';
159 $options['LIMIT'] = $queryLimit;
160 $fields = array( 'page_id', 'page_namespace', 'page_title', 'rd_from' );
162 $joinConds = array( 'redirect' => array( 'LEFT JOIN', array(
164 'rd_namespace' => $target->getNamespace(),
165 'rd_title' => $target->getDBkey(),
166 '(rd_interwiki is NULL) or (rd_interwiki = \'\')'
170 $options['ORDER BY'] = 'pl_from';
171 $plRes = $dbr->select( array( 'pagelinks', 'page', 'redirect' ), $fields,
172 $plConds, __METHOD__
, $options,
177 $options['ORDER BY'] = 'tl_from';
178 $tlRes = $dbr->select( array( 'templatelinks', 'page', 'redirect' ), $fields,
179 $tlConds, __METHOD__
, $options,
184 $options['ORDER BY'] = 'il_from';
185 $ilRes = $dbr->select( array( 'imagelinks', 'page', 'redirect' ), $fields,
186 $ilConds, __METHOD__
, $options,
190 if( ( !$fetchlinks ||
!$dbr->numRows($plRes) ) && ( $hidetrans ||
!$dbr->numRows($tlRes) ) && ( $hideimages ||
!$dbr->numRows($ilRes) ) ) {
192 $out->addHTML( $this->whatlinkshereForm() );
194 // Show filters only if there are links
195 if( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages )
196 $out->addHTML( $this->getFilterPanel() );
198 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
199 $out->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
204 // Read the rows into an array and remove duplicates
205 // templatelinks comes second so that the templatelinks row overwrites the
206 // pagelinks row, so we get (inclusion) rather than nothing
208 foreach ( $plRes as $row ) {
209 $row->is_template
= 0;
211 $rows[$row->page_id
] = $row;
215 foreach ( $tlRes as $row ) {
216 $row->is_template
= 1;
218 $rows[$row->page_id
] = $row;
222 foreach ( $ilRes as $row ) {
223 $row->is_template
= 0;
225 $rows[$row->page_id
] = $row;
229 // Sort by key and then change the keys to 0-based indices
231 $rows = array_values( $rows );
233 $numRows = count( $rows );
235 // Work out the start and end IDs, for prev/next links
236 if ( $numRows > $limit ) {
237 // More rows available after these ones
238 // Get the ID from the last row in the result set
239 $nextId = $rows[$limit]->page_id
;
240 // Remove undisplayed rows
241 $rows = array_slice( $rows, 0, $limit );
243 // No more rows after
249 $out->addHTML( $this->whatlinkshereForm() );
250 $out->addHTML( $this->getFilterPanel() );
251 $out->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
253 $prevnext = $this->getPrevNext( $prevId, $nextId );
254 $out->addHTML( $prevnext );
257 $out->addHTML( $this->listStart( $level ) );
258 foreach ( $rows as $row ) {
259 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
261 if ( $row->rd_from
&& $level < 2 ) {
262 $out->addHTML( $this->listItem( $row, $nt, true ) );
263 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
264 $out->addHTML( Xml
::closeElement( 'li' ) );
266 $out->addHTML( $this->listItem( $row, $nt ) );
270 $out->addHTML( $this->listEnd() );
273 $out->addHTML( $prevnext );
277 protected function listStart( $level ) {
278 return Xml
::openElement( 'ul', ( $level ?
array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
281 protected function listItem( $row, $nt, $notClose = false ) {
282 $dirmark = $this->getLanguage()->getDirMark();
284 # local message cache
285 static $msgcache = null;
286 if ( $msgcache === null ) {
287 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
288 'whatlinkshere-links', 'isimage' );
290 foreach ( $msgs as $msg ) {
291 $msgcache[$msg] = $this->msg( $msg )->escaped();
295 if( $row->rd_from
) {
296 $query = array( 'redirect' => 'no' );
301 $link = Linker
::linkKnown(
308 // Display properties (redirect or template)
312 $props[] = $msgcache['isredirect'];
313 if ( $row->is_template
)
314 $props[] = $msgcache['istemplate'];
316 $props[] = $msgcache['isimage'];
318 if ( count( $props ) ) {
319 $propsText = $this->msg( 'parentheses' )->rawParams( implode( $msgcache['semicolon-separator'], $props ) )->escaped();
322 # Space for utilities links, with a what-links-here link provided
323 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
324 $wlh = Xml
::wrapClass( $this->msg( 'parentheses' )->rawParams( $wlhLink )->escaped(), 'mw-whatlinkshere-tools' );
327 Xml
::openElement( 'li' ) . "$link $propsText $dirmark $wlh\n" :
328 Xml
::tags( 'li', null, "$link $propsText $dirmark $wlh" ) . "\n";
331 protected function listEnd() {
332 return Xml
::closeElement( 'ul' );
335 protected function wlhLink( Title
$target, $text ) {
336 static $title = null;
337 if ( $title === null )
338 $title = $this->getTitle();
340 return Linker
::linkKnown(
344 array( 'target' => $target->getPrefixedText() )
348 function makeSelfLink( $text, $query ) {
349 return Linker
::linkKnown(
357 function getPrevNext( $prevId, $nextId ) {
358 $currentLimit = $this->opts
->getValue( 'limit' );
359 $prev = $this->msg( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
360 $next = $this->msg( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
362 $changed = $this->opts
->getChangedValues();
363 unset($changed['target']); // Already in the request title
365 if ( 0 != $prevId ) {
366 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
367 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
369 if ( 0 != $nextId ) {
370 $overrides = array( 'from' => $nextId, 'back' => $prevId );
371 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
374 $limitLinks = array();
375 $lang = $this->getLanguage();
376 foreach ( $this->limits
as $limit ) {
377 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
378 $overrides = array( 'limit' => $limit );
379 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
382 $nums = $lang->pipeList( $limitLinks );
384 return $this->msg( 'viewprevnext' )->rawParams( $prev, $next, $nums )->escaped();
387 function whatlinkshereForm() {
390 // We get nicer value from the title object
391 $this->opts
->consumeValue( 'target' );
392 // Reset these for new requests
393 $this->opts
->consumeValues( array( 'back', 'from' ) );
395 $target = $this->target ?
$this->target
->getPrefixedText() : '';
396 $namespace = $this->opts
->consumeValue( 'namespace' );
399 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
401 # Values that should not be forgotten
402 $f .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() );
403 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
404 $f .= Html
::hidden( $name, $value );
407 $f .= Xml
::fieldset( $this->msg( 'whatlinkshere' )->text() );
410 $f .= Xml
::inputLabel( $this->msg( 'whatlinkshere-page' )->text(), 'target',
411 'mw-whatlinkshere-target', 40, $target );
416 $f .= Html
::namespaceSelector(
418 'selected' => $namespace,
420 'label' => $this->msg( 'namespace' )->text()
422 'name' => 'namespace',
424 'class' => 'namespaceselector',
431 $f .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() );
434 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
440 * Create filter panel
442 * @return string HTML fieldset and filter panel with the show/hide links
444 function getFilterPanel() {
445 $show = $this->msg( 'show' )->escaped();
446 $hide = $this->msg( 'hide' )->escaped();
448 $changed = $this->opts
->getChangedValues();
449 unset($changed['target']); // Already in the request title
452 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
453 if( $this->target
->getNamespace() == NS_FILE
)
454 $types[] = 'hideimages';
456 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
457 // To be sure they will be found by grep
458 foreach( $types as $type ) {
459 $chosen = $this->opts
->getValue( $type );
460 $msg = $chosen ?
$show : $hide;
461 $overrides = array( $type => !$chosen );
462 $links[] = $this->msg( "whatlinkshere-{$type}" )->rawParams(
463 $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) )->escaped();
465 return Xml
::fieldset( $this->msg( 'whatlinkshere-filters' )->text(), $this->getLanguage()->pipeList( $links ) );