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
= $this->getTitle( $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 ) {
267 # local message cache
268 static $msgcache = null;
269 if ( $msgcache === null ) {
270 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
271 'whatlinkshere-links', 'isimage', 'hist' );
273 foreach ( $msgs as $msg ) {
274 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
278 if( $row->page_is_redirect
) {
279 $query = array( 'redirect' => 'no' );
284 $link = $this->skin
->linkKnown(
291 // Display properties (redirect or template)
294 if ( $row->page_is_redirect
)
295 $props[] = $msgcache['isredirect'];
296 if ( $row->is_template
)
297 $props[] = $msgcache['istemplate'];
299 $props[] = $msgcache['isimage'];
301 if ( count( $props ) ) {
302 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
305 # Space for utilities links, with a what-links-here link provided
307 $tools[] = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
308 $tools[] = $this->skin
->linkKnown( $nt, $msgcache['hist'], array(), array( 'action' => 'history' ) );
309 $wlh = Xml
::wrapClass( '(' . $wgLang->pipeList( $tools ) . ')', 'mw-whatlinkshere-tools' );
312 Xml
::openElement( 'li' ) . "$link $propsText $wlh\n" :
313 Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
316 protected function listEnd() {
317 return Xml
::closeElement( 'ul' );
320 protected function wlhLink( Title
$target, $text ) {
321 static $title = null;
322 if ( $title === null )
323 $title = $this->getTitle();
325 return $this->skin
->linkKnown(
329 array( 'target' => $target->getPrefixedText() )
333 function makeSelfLink( $text, $query ) {
334 return $this->skin
->linkKnown(
342 function getPrevNext( $prevId, $nextId ) {
344 $currentLimit = $this->opts
->getValue( 'limit' );
345 $fmtLimit = $wgLang->formatNum( $currentLimit );
346 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
347 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
349 $changed = $this->opts
->getChangedValues();
350 unset($changed['target']); // Already in the request title
352 if ( 0 != $prevId ) {
353 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
354 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
356 if ( 0 != $nextId ) {
357 $overrides = array( 'from' => $nextId, 'back' => $prevId );
358 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
361 $limitLinks = array();
362 foreach ( $this->limits
as $limit ) {
363 $prettyLimit = $wgLang->formatNum( $limit );
364 $overrides = array( 'limit' => $limit );
365 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
368 $nums = $wgLang->pipeList( $limitLinks );
370 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
373 function whatlinkshereForm() {
376 // We get nicer value from the title object
377 $this->opts
->consumeValue( 'target' );
378 // Reset these for new requests
379 $this->opts
->consumeValues( array( 'back', 'from' ) );
381 $target = $this->target ?
$this->target
->getPrefixedText() : '';
382 $namespace = $this->opts
->consumeValue( 'namespace' );
385 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
387 # Values that should not be forgotten
388 $f .= Xml
::hidden( 'title', $this->getTitle()->getPrefixedText() );
389 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
390 $f .= Xml
::hidden( $name, $value );
393 $f .= Xml
::fieldset( wfMsg( 'whatlinkshere' ) );
396 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
397 'mw-whatlinkshere-target', 40, $target );
402 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
403 Xml
::namespaceSelector( $namespace, '' );
408 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
411 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
417 * Create filter panel
419 * @return string HTML fieldset and filter panel with the show/hide links
421 function getFilterPanel() {
423 $show = wfMsgHtml( 'show' );
424 $hide = wfMsgHtml( 'hide' );
426 $changed = $this->opts
->getChangedValues();
427 unset($changed['target']); // Already in the request title
430 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
431 if( $this->target
->getNamespace() == NS_FILE
)
432 $types[] = 'hideimages';
434 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
435 // To be sure they will be find by grep
436 foreach( $types as $type ) {
437 $chosen = $this->opts
->getValue( $type );
438 $msg = $chosen ?
$show : $hide;
439 $overrides = array( $type => !$chosen );
440 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
442 return Xml
::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );