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 $out = $this->getOutput();
54 $opts = new FormOptions();
56 $opts->add( 'target', '' );
57 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
58 $opts->add( 'limit', 50 );
59 $opts->add( 'from', 0 );
60 $opts->add( 'back', 0 );
61 $opts->add( 'hideredirs', false );
62 $opts->add( 'hidetrans', false );
63 $opts->add( 'hidelinks', false );
64 $opts->add( 'hideimages', false );
66 $opts->fetchValuesFromRequest( $this->getRequest() );
67 $opts->validateIntBounds( 'limit', 0, 5000 );
69 // Give precedence to subpage syntax
71 $opts->setValue( 'target', $par );
74 // Bind to member variable
77 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
78 if( !$this->target
) {
79 $out->addHTML( $this->whatlinkshereForm() );
83 $this->getSkin()->setRelevantTitle( $this->target
);
86 $this->selfTitle
= $this->getTitle( $this->target
->getPrefixedDBkey() );
88 $out->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
89 $out->setSubtitle( wfMsg( 'whatlinkshere-backlink', Linker
::link( $this->target
, $this->target
->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
91 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
92 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
96 * @param $level int Recursion level
97 * @param $target Title Target title
98 * @param $limit int Number of entries to display
99 * @param $from Title Display from this article ID
100 * @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['page_is_redirect'] = 0;
124 } elseif( $hidelinks ) {
125 $plConds['page_is_redirect'] = 1;
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', 'page_is_redirect' );
163 $options['ORDER BY'] = 'pl_from';
164 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
165 $plConds, __METHOD__
, $options );
169 $options['ORDER BY'] = 'tl_from';
170 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
171 $tlConds, __METHOD__
, $options );
175 $options['ORDER BY'] = 'il_from';
176 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
177 $ilConds, __METHOD__
, $options );
180 if( ( !$fetchlinks ||
!$dbr->numRows($plRes) ) && ( $hidetrans ||
!$dbr->numRows($tlRes) ) && ( $hideimages ||
!$dbr->numRows($ilRes) ) ) {
182 $out->addHTML( $this->whatlinkshereForm() );
184 // Show filters only if there are links
185 if( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages )
186 $out->addHTML( $this->getFilterPanel() );
188 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
189 $out->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
194 // Read the rows into an array and remove duplicates
195 // templatelinks comes second so that the templatelinks row overwrites the
196 // pagelinks row, so we get (inclusion) rather than nothing
198 foreach ( $plRes as $row ) {
199 $row->is_template
= 0;
201 $rows[$row->page_id
] = $row;
205 foreach ( $tlRes as $row ) {
206 $row->is_template
= 1;
208 $rows[$row->page_id
] = $row;
212 foreach ( $ilRes as $row ) {
213 $row->is_template
= 0;
215 $rows[$row->page_id
] = $row;
219 // Sort by key and then change the keys to 0-based indices
221 $rows = array_values( $rows );
223 $numRows = count( $rows );
225 // Work out the start and end IDs, for prev/next links
226 if ( $numRows > $limit ) {
227 // More rows available after these ones
228 // Get the ID from the last row in the result set
229 $nextId = $rows[$limit]->page_id
;
230 // Remove undisplayed rows
231 $rows = array_slice( $rows, 0, $limit );
233 // No more rows after
239 $out->addHTML( $this->whatlinkshereForm() );
240 $out->addHTML( $this->getFilterPanel() );
241 $out->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
243 $prevnext = $this->getPrevNext( $prevId, $nextId );
244 $out->addHTML( $prevnext );
247 $out->addHTML( $this->listStart( $level ) );
248 foreach ( $rows as $row ) {
249 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
251 if ( $row->page_is_redirect
&& $level < 2 ) {
252 $out->addHTML( $this->listItem( $row, $nt, true ) );
253 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
254 $out->addHTML( Xml
::closeElement( 'li' ) );
256 $out->addHTML( $this->listItem( $row, $nt ) );
260 $out->addHTML( $this->listEnd() );
263 $out->addHTML( $prevnext );
267 protected function listStart( $level ) {
268 return Xml
::openElement( 'ul', ( $level ?
array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
271 protected function listItem( $row, $nt, $notClose = false ) {
272 # local message cache
273 static $msgcache = null;
274 if ( $msgcache === null ) {
275 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
276 'whatlinkshere-links', 'isimage' );
278 foreach ( $msgs as $msg ) {
279 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
283 if( $row->page_is_redirect
) {
284 $query = array( 'redirect' => 'no' );
289 $link = Linker
::linkKnown(
296 // Display properties (redirect or template)
299 if ( $row->page_is_redirect
)
300 $props[] = $msgcache['isredirect'];
301 if ( $row->is_template
)
302 $props[] = $msgcache['istemplate'];
304 $props[] = $msgcache['isimage'];
306 if ( count( $props ) ) {
307 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
310 # Space for utilities links, with a what-links-here link provided
311 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
312 $wlh = Xml
::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
315 Xml
::openElement( 'li' ) . "$link $propsText $wlh\n" :
316 Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
319 protected function listEnd() {
320 return Xml
::closeElement( 'ul' );
323 protected function wlhLink( Title
$target, $text ) {
324 static $title = null;
325 if ( $title === null )
326 $title = $this->getTitle();
328 return Linker
::linkKnown(
332 array( 'target' => $target->getPrefixedText() )
336 function makeSelfLink( $text, $query ) {
337 return Linker
::linkKnown(
345 function getPrevNext( $prevId, $nextId ) {
347 $currentLimit = $this->opts
->getValue( 'limit' );
348 $fmtLimit = $wgLang->formatNum( $currentLimit );
349 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
350 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
352 $changed = $this->opts
->getChangedValues();
353 unset($changed['target']); // Already in the request title
355 if ( 0 != $prevId ) {
356 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
357 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
359 if ( 0 != $nextId ) {
360 $overrides = array( 'from' => $nextId, 'back' => $prevId );
361 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
364 $limitLinks = array();
365 foreach ( $this->limits
as $limit ) {
366 $prettyLimit = $wgLang->formatNum( $limit );
367 $overrides = array( 'limit' => $limit );
368 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
371 $nums = $wgLang->pipeList( $limitLinks );
373 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
376 function whatlinkshereForm() {
379 // We get nicer value from the title object
380 $this->opts
->consumeValue( 'target' );
381 // Reset these for new requests
382 $this->opts
->consumeValues( array( 'back', 'from' ) );
384 $target = $this->target ?
$this->target
->getPrefixedText() : '';
385 $namespace = $this->opts
->consumeValue( 'namespace' );
388 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
390 # Values that should not be forgotten
391 $f .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() );
392 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
393 $f .= Html
::hidden( $name, $value );
396 $f .= Xml
::fieldset( wfMsg( 'whatlinkshere' ) );
399 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
400 'mw-whatlinkshere-target', 40, $target );
405 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
406 Xml
::namespaceSelector( $namespace, '' );
411 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
414 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
420 * Create filter panel
422 * @return string HTML fieldset and filter panel with the show/hide links
424 function getFilterPanel() {
426 $show = wfMsgHtml( 'show' );
427 $hide = wfMsgHtml( 'hide' );
429 $changed = $this->opts
->getChangedValues();
430 unset($changed['target']); // Already in the request title
433 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
434 if( $this->target
->getNamespace() == NS_FILE
)
435 $types[] = 'hideimages';
437 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
438 // To be sure they will be find by grep
439 foreach( $types as $type ) {
440 $chosen = $this->opts
->getValue( $type );
441 $msg = $chosen ?
$show : $hide;
442 $overrides = array( $type => !$chosen );
443 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
445 return Xml
::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );