3 * @todo Use some variant of Pager or something; the pagination here is lousy.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
23 * implements Special:Whatlinkshere
24 * @ingroup SpecialPage
26 class SpecialWhatLinksHere
extends SpecialPage
{
29 protected $opts, $target, $selfTitle;
34 protected $limits = array( 20, 50, 100, 250, 500 );
36 public function __construct() {
37 parent
::__construct( 'Whatlinkshere' );
39 $this->skin
= $wgUser->getSkin();
42 function execute( $par ) {
43 global $wgOut, $wgRequest;
47 $opts = new FormOptions();
49 $opts->add( 'target', '' );
50 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
51 $opts->add( 'limit', 50 );
52 $opts->add( 'from', 0 );
53 $opts->add( 'back', 0 );
54 $opts->add( 'hideredirs', false );
55 $opts->add( 'hidetrans', false );
56 $opts->add( 'hidelinks', false );
57 $opts->add( 'hideimages', false );
59 $opts->fetchValuesFromRequest( $wgRequest );
60 $opts->validateIntBounds( 'limit', 0, 5000 );
62 // Give precedence to subpage syntax
64 $opts->setValue( 'target', $par );
67 // Bind to member variable
70 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
71 if( !$this->target
) {
72 $wgOut->addHTML( $this->whatlinkshereForm() );
76 $this->selfTitle
= SpecialPage
::getTitleFor( 'Whatlinkshere', $this->target
->getPrefixedDBkey() );
78 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
79 $wgOut->setSubtitle( wfMsg( 'whatlinkshere-backlink', $this->skin
->link( $this->target
, $this->target
->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
81 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
82 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
86 * @param $level int Recursion level
87 * @param $target Title Target title
88 * @param $limit int Number of entries to display
89 * @param $from Title Display from this article ID
90 * @param $back Title Display from this article ID at backwards scrolling
93 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
94 global $wgOut, $wgMaxRedirectLinksRetrieved;
95 $dbr = wfGetDB( DB_SLAVE
);
98 $hidelinks = $this->opts
->getValue( 'hidelinks' );
99 $hideredirs = $this->opts
->getValue( 'hideredirs' );
100 $hidetrans = $this->opts
->getValue( 'hidetrans' );
101 $hideimages = $target->getNamespace() != NS_FILE ||
$this->opts
->getValue( 'hideimages' );
103 $fetchlinks = (!$hidelinks ||
!$hideredirs);
108 'pl_namespace' => $target->getNamespace(),
109 'pl_title' => $target->getDBkey(),
112 $plConds['page_is_redirect'] = 0;
113 } elseif( $hidelinks ) {
114 $plConds['page_is_redirect'] = 1;
119 'tl_namespace' => $target->getNamespace(),
120 'tl_title' => $target->getDBkey(),
125 'il_to' => $target->getDBkey(),
128 $namespace = $this->opts
->getValue( 'namespace' );
129 if ( is_int($namespace) ) {
130 $plConds['page_namespace'] = $namespace;
131 $tlConds['page_namespace'] = $namespace;
132 $ilConds['page_namespace'] = $namespace;
136 $tlConds[] = "tl_from >= $from";
137 $plConds[] = "pl_from >= $from";
138 $ilConds[] = "il_from >= $from";
141 // Read an extra row as an at-end check
142 $queryLimit = $limit +
1;
144 // Enforce join order, sometimes namespace selector may
145 // trigger filesorts which are far less efficient than scanning many entries
146 $options[] = 'STRAIGHT_JOIN';
148 $options['LIMIT'] = $queryLimit;
149 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
152 $options['ORDER BY'] = 'pl_from';
153 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
154 $plConds, __METHOD__
, $options );
158 $options['ORDER BY'] = 'tl_from';
159 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
160 $tlConds, __METHOD__
, $options );
164 $options['ORDER BY'] = 'il_from';
165 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
166 $ilConds, __METHOD__
, $options );
169 if( ( !$fetchlinks ||
!$dbr->numRows($plRes) ) && ( $hidetrans ||
!$dbr->numRows($tlRes) ) && ( $hideimages ||
!$dbr->numRows($ilRes) ) ) {
171 $wgOut->addHTML( $this->whatlinkshereForm() );
173 // Show filters only if there are links
174 if( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages )
175 $wgOut->addHTML( $this->getFilterPanel() );
177 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
178 $wgOut->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
183 // Read the rows into an array and remove duplicates
184 // templatelinks comes second so that the templatelinks row overwrites the
185 // pagelinks row, so we get (inclusion) rather than nothing
187 while ( $row = $dbr->fetchObject( $plRes ) ) {
188 $row->is_template
= 0;
190 $rows[$row->page_id
] = $row;
192 $dbr->freeResult( $plRes );
196 while ( $row = $dbr->fetchObject( $tlRes ) ) {
197 $row->is_template
= 1;
199 $rows[$row->page_id
] = $row;
201 $dbr->freeResult( $tlRes );
204 while ( $row = $dbr->fetchObject( $ilRes ) ) {
205 $row->is_template
= 0;
207 $rows[$row->page_id
] = $row;
209 $dbr->freeResult( $ilRes );
212 // Sort by key and then change the keys to 0-based indices
214 $rows = array_values( $rows );
216 $numRows = count( $rows );
218 // Work out the start and end IDs, for prev/next links
219 if ( $numRows > $limit ) {
220 // More rows available after these ones
221 // Get the ID from the last row in the result set
222 $nextId = $rows[$limit]->page_id
;
223 // Remove undisplayed rows
224 $rows = array_slice( $rows, 0, $limit );
226 // No more rows after
232 $wgOut->addHTML( $this->whatlinkshereForm() );
233 $wgOut->addHTML( $this->getFilterPanel() );
234 $wgOut->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
236 $prevnext = $this->getPrevNext( $prevId, $nextId );
237 $wgOut->addHTML( $prevnext );
240 $wgOut->addHTML( $this->listStart() );
241 foreach ( $rows as $row ) {
242 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
244 if ( $row->page_is_redirect
&& $level < 2 ) {
245 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
246 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
247 $wgOut->addHTML( Xml
::closeElement( 'li' ) );
249 $wgOut->addHTML( $this->listItem( $row, $nt ) );
253 $wgOut->addHTML( $this->listEnd() );
256 $wgOut->addHTML( $prevnext );
260 protected function listStart() {
261 return Xml
::openElement( 'ul', array ( 'id' => 'mw-whatlinkshere-list' ) );
264 protected function listItem( $row, $nt, $notClose = false ) {
265 # local message cache
266 static $msgcache = null;
267 if ( $msgcache === null ) {
268 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
269 'whatlinkshere-links', 'isimage' );
271 foreach ( $msgs as $msg ) {
272 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
276 if( $row->page_is_redirect
) {
277 $query = array( 'redirect' => 'no' );
282 $link = $this->skin
->linkKnown(
289 // Display properties (redirect or template)
292 if ( $row->page_is_redirect
)
293 $props[] = $msgcache['isredirect'];
294 if ( $row->is_template
)
295 $props[] = $msgcache['istemplate'];
297 $props[] = $msgcache['isimage'];
299 if ( count( $props ) ) {
300 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
303 # Space for utilities links, with a what-links-here link provided
304 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
305 $wlh = Xml
::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
308 Xml
::openElement( 'li' ) . "$link $propsText $wlh\n" :
309 Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
312 protected function listEnd() {
313 return Xml
::closeElement( 'ul' );
316 protected function wlhLink( Title
$target, $text ) {
317 static $title = null;
318 if ( $title === null )
319 $title = SpecialPage
::getTitleFor( 'Whatlinkshere' );
321 return $this->skin
->linkKnown(
325 array( 'target' => $target->getPrefixedText() )
329 function makeSelfLink( $text, $query ) {
330 return $this->skin
->linkKnown(
338 function getPrevNext( $prevId, $nextId ) {
340 $currentLimit = $this->opts
->getValue( 'limit' );
341 $fmtLimit = $wgLang->formatNum( $currentLimit );
342 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
343 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
345 $changed = $this->opts
->getChangedValues();
346 unset($changed['target']); // Already in the request title
348 if ( 0 != $prevId ) {
349 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
350 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
352 if ( 0 != $nextId ) {
353 $overrides = array( 'from' => $nextId, 'back' => $prevId );
354 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
357 $limitLinks = array();
358 foreach ( $this->limits
as $limit ) {
359 $prettyLimit = $wgLang->formatNum( $limit );
360 $overrides = array( 'limit' => $limit );
361 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
364 $nums = $wgLang->pipeList( $limitLinks );
366 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
369 function whatlinkshereForm() {
372 // We get nicer value from the title object
373 $this->opts
->consumeValue( 'target' );
374 // Reset these for new requests
375 $this->opts
->consumeValues( array( 'back', 'from' ) );
377 $target = $this->target ?
$this->target
->getPrefixedText() : '';
378 $namespace = $this->opts
->consumeValue( 'namespace' );
381 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
383 # Values that should not be forgotten
384 $f .= Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Whatlinkshere' )->getPrefixedText() );
385 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
386 $f .= Xml
::hidden( $name, $value );
389 $f .= Xml
::fieldset( wfMsg( 'whatlinkshere' ) );
392 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
393 'mw-whatlinkshere-target', 40, $target );
398 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
399 Xml
::namespaceSelector( $namespace, '' );
404 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
407 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
413 * Create filter panel
415 * @return string HTML fieldset and filter panel with the show/hide links
417 function getFilterPanel() {
419 $show = wfMsgHtml( 'show' );
420 $hide = wfMsgHtml( 'hide' );
422 $changed = $this->opts
->getChangedValues();
423 unset($changed['target']); // Already in the request title
426 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
427 if( $this->target
->getNamespace() == NS_FILE
)
428 $types[] = 'hideimages';
430 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
431 // To be sure they will be find by grep
432 foreach( $types as $type ) {
433 $chosen = $this->opts
->getValue( $type );
434 $msg = $chosen ?
$show : $hide;
435 $overrides = array( $type => !$chosen );
436 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
438 return Xml
::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );