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
{
32 protected $opts, $target, $selfTitle;
37 protected $limits = array( 20, 50, 100, 250, 500 );
39 public function __construct() {
40 parent
::__construct( 'Whatlinkshere' );
42 $this->skin
= $wgUser->getSkin();
45 function execute( $par ) {
46 global $wgOut, $wgRequest;
50 $opts = new FormOptions();
52 $opts->add( 'target', '' );
53 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
54 $opts->add( 'limit', 50 );
55 $opts->add( 'from', 0 );
56 $opts->add( 'back', 0 );
57 $opts->add( 'hideredirs', false );
58 $opts->add( 'hidetrans', false );
59 $opts->add( 'hidelinks', false );
60 $opts->add( 'hideimages', false );
62 $opts->fetchValuesFromRequest( $wgRequest );
63 $opts->validateIntBounds( 'limit', 0, 5000 );
65 // Give precedence to subpage syntax
67 $opts->setValue( 'target', $par );
70 // Bind to member variable
73 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
74 if( !$this->target
) {
75 $wgOut->addHTML( $this->whatlinkshereForm() );
79 $this->selfTitle
= $this->getTitle( $this->target
->getPrefixedDBkey() );
81 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
82 $wgOut->setSubtitle( wfMsg( 'whatlinkshere-backlink', $this->skin
->link( $this->target
, $this->target
->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
84 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
85 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
89 * @param $level int Recursion level
90 * @param $target Title Target title
91 * @param $limit int Number of entries to display
92 * @param $from Title Display from this article ID
93 * @param $back Title Display from this article ID at backwards scrolling
96 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
97 global $wgOut, $wgMaxRedirectLinksRetrieved;
98 $dbr = wfGetDB( DB_SLAVE
);
101 $hidelinks = $this->opts
->getValue( 'hidelinks' );
102 $hideredirs = $this->opts
->getValue( 'hideredirs' );
103 $hidetrans = $this->opts
->getValue( 'hidetrans' );
104 $hideimages = $target->getNamespace() != NS_FILE ||
$this->opts
->getValue( 'hideimages' );
106 $fetchlinks = (!$hidelinks ||
!$hideredirs);
111 'pl_namespace' => $target->getNamespace(),
112 'pl_title' => $target->getDBkey(),
115 $plConds['page_is_redirect'] = 0;
116 } elseif( $hidelinks ) {
117 $plConds['page_is_redirect'] = 1;
122 'tl_namespace' => $target->getNamespace(),
123 'tl_title' => $target->getDBkey(),
128 'il_to' => $target->getDBkey(),
131 $namespace = $this->opts
->getValue( 'namespace' );
132 if ( is_int($namespace) ) {
133 $plConds['page_namespace'] = $namespace;
134 $tlConds['page_namespace'] = $namespace;
135 $ilConds['page_namespace'] = $namespace;
139 $tlConds[] = "tl_from >= $from";
140 $plConds[] = "pl_from >= $from";
141 $ilConds[] = "il_from >= $from";
144 // Read an extra row as an at-end check
145 $queryLimit = $limit +
1;
147 // Enforce join order, sometimes namespace selector may
148 // trigger filesorts which are far less efficient than scanning many entries
149 $options[] = 'STRAIGHT_JOIN';
151 $options['LIMIT'] = $queryLimit;
152 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
155 $options['ORDER BY'] = 'pl_from';
156 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
157 $plConds, __METHOD__
, $options );
161 $options['ORDER BY'] = 'tl_from';
162 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
163 $tlConds, __METHOD__
, $options );
167 $options['ORDER BY'] = 'il_from';
168 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
169 $ilConds, __METHOD__
, $options );
172 if( ( !$fetchlinks ||
!$dbr->numRows($plRes) ) && ( $hidetrans ||
!$dbr->numRows($tlRes) ) && ( $hideimages ||
!$dbr->numRows($ilRes) ) ) {
174 $wgOut->addHTML( $this->whatlinkshereForm() );
176 // Show filters only if there are links
177 if( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages )
178 $wgOut->addHTML( $this->getFilterPanel() );
180 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
181 $wgOut->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
186 // Read the rows into an array and remove duplicates
187 // templatelinks comes second so that the templatelinks row overwrites the
188 // pagelinks row, so we get (inclusion) rather than nothing
190 foreach ( $plRes as $row ) {
191 $row->is_template
= 0;
193 $rows[$row->page_id
] = $row;
197 foreach ( $tlRes as $row ) {
198 $row->is_template
= 1;
200 $rows[$row->page_id
] = $row;
204 foreach ( $ilRes as $row ) {
205 $row->is_template
= 0;
207 $rows[$row->page_id
] = $row;
211 // Sort by key and then change the keys to 0-based indices
213 $rows = array_values( $rows );
215 $numRows = count( $rows );
217 // Work out the start and end IDs, for prev/next links
218 if ( $numRows > $limit ) {
219 // More rows available after these ones
220 // Get the ID from the last row in the result set
221 $nextId = $rows[$limit]->page_id
;
222 // Remove undisplayed rows
223 $rows = array_slice( $rows, 0, $limit );
225 // No more rows after
231 $wgOut->addHTML( $this->whatlinkshereForm() );
232 $wgOut->addHTML( $this->getFilterPanel() );
233 $wgOut->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
235 $prevnext = $this->getPrevNext( $prevId, $nextId );
236 $wgOut->addHTML( $prevnext );
239 $wgOut->addHTML( $this->listStart() );
240 foreach ( $rows as $row ) {
241 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
243 if ( $row->page_is_redirect
&& $level < 2 ) {
244 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
245 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
246 $wgOut->addHTML( Xml
::closeElement( 'li' ) );
248 $wgOut->addHTML( $this->listItem( $row, $nt ) );
252 $wgOut->addHTML( $this->listEnd() );
255 $wgOut->addHTML( $prevnext );
259 protected function listStart() {
260 return Xml
::openElement( 'ul', array ( 'id' => 'mw-whatlinkshere-list' ) );
263 protected function listItem( $row, $nt, $notClose = false ) {
266 # local message cache
267 static $msgcache = null;
268 if ( $msgcache === null ) {
269 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
270 'whatlinkshere-links', 'isimage', 'hist' );
272 foreach ( $msgs as $msg ) {
273 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
277 if( $row->page_is_redirect
) {
278 $query = array( 'redirect' => 'no' );
283 $link = $this->skin
->linkKnown(
290 // Display properties (redirect or template)
293 if ( $row->page_is_redirect
)
294 $props[] = $msgcache['isredirect'];
295 if ( $row->is_template
)
296 $props[] = $msgcache['istemplate'];
298 $props[] = $msgcache['isimage'];
300 if ( count( $props ) ) {
301 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
304 # Space for utilities links, with a what-links-here link provided
306 $tools[] = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
307 $tools[] = $this->skin
->linkKnown( $nt, $msgcache['hist'], array(), array( 'action' => 'history' ) );
308 $wlh = Xml
::wrapClass( '(' . $wgLang->pipeList( $tools ) . ')', 'mw-whatlinkshere-tools' );
311 Xml
::openElement( 'li' ) . "$link $propsText $wlh\n" :
312 Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
315 protected function listEnd() {
316 return Xml
::closeElement( 'ul' );
319 protected function wlhLink( Title
$target, $text ) {
320 static $title = null;
321 if ( $title === null )
322 $title = $this->getTitle();
324 return $this->skin
->linkKnown(
328 array( 'target' => $target->getPrefixedText() )
332 function makeSelfLink( $text, $query ) {
333 return $this->skin
->linkKnown(
341 function getPrevNext( $prevId, $nextId ) {
343 $currentLimit = $this->opts
->getValue( 'limit' );
344 $fmtLimit = $wgLang->formatNum( $currentLimit );
345 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
346 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
348 $changed = $this->opts
->getChangedValues();
349 unset($changed['target']); // Already in the request title
351 if ( 0 != $prevId ) {
352 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
353 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
355 if ( 0 != $nextId ) {
356 $overrides = array( 'from' => $nextId, 'back' => $prevId );
357 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
360 $limitLinks = array();
361 foreach ( $this->limits
as $limit ) {
362 $prettyLimit = $wgLang->formatNum( $limit );
363 $overrides = array( 'limit' => $limit );
364 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
367 $nums = $wgLang->pipeList( $limitLinks );
369 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
372 function whatlinkshereForm() {
375 // We get nicer value from the title object
376 $this->opts
->consumeValue( 'target' );
377 // Reset these for new requests
378 $this->opts
->consumeValues( array( 'back', 'from' ) );
380 $target = $this->target ?
$this->target
->getPrefixedText() : '';
381 $namespace = $this->opts
->consumeValue( 'namespace' );
384 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
386 # Values that should not be forgotten
387 $f .= Xml
::hidden( 'title', $this->getTitle()->getPrefixedText() );
388 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
389 $f .= Xml
::hidden( $name, $value );
392 $f .= Xml
::fieldset( wfMsg( 'whatlinkshere' ) );
395 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
396 'mw-whatlinkshere-target', 40, $target );
401 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
402 Xml
::namespaceSelector( $namespace, '' );
407 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
410 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
416 * Create filter panel
418 * @return string HTML fieldset and filter panel with the show/hide links
420 function getFilterPanel() {
422 $show = wfMsgHtml( 'show' );
423 $hide = wfMsgHtml( 'hide' );
425 $changed = $this->opts
->getChangedValues();
426 unset($changed['target']); // Already in the request title
429 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
430 if( $this->target
->getNamespace() == NS_FILE
)
431 $types[] = 'hideimages';
433 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
434 // To be sure they will be find by grep
435 foreach( $types as $type ) {
436 $chosen = $this->opts
->getValue( $type );
437 $msg = $chosen ?
$show : $hide;
438 $overrides = array( $type => !$chosen );
439 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
441 return Xml
::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );