3 * @todo Use some variant of Pager or something; the pagination here is lousy.
10 * implements Special:Whatlinkshere
11 * @ingroup SpecialPage
13 class SpecialWhatLinksHere
extends SpecialPage
{
16 protected $opts, $target, $selfTitle;
21 protected $limits = array( 20, 50, 100, 250, 500 );
23 public function __construct() {
24 parent
::__construct( 'Whatlinkshere' );
26 $this->skin
= $wgUser->getSkin();
29 function execute( $par ) {
30 global $wgOut, $wgRequest;
34 $opts = new FormOptions();
36 $opts->add( 'target', '' );
37 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
38 $opts->add( 'limit', 50 );
39 $opts->add( 'from', 0 );
40 $opts->add( 'back', 0 );
41 $opts->add( 'hideredirs', false );
42 $opts->add( 'hidetrans', false );
43 $opts->add( 'hidelinks', false );
44 $opts->add( 'hideimages', false );
46 $opts->fetchValuesFromRequest( $wgRequest );
47 $opts->validateIntBounds( 'limit', 0, 5000 );
49 // Give precedence to subpage syntax
51 $opts->setValue( 'target', $par );
54 // Bind to member variable
57 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
58 if( !$this->target
) {
59 $wgOut->addHTML( $this->whatlinkshereForm() );
63 $this->selfTitle
= SpecialPage
::getTitleFor( 'Whatlinkshere', $this->target
->getPrefixedDBkey() );
65 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
66 $wgOut->setSubtitle( wfMsg( 'whatlinkshere-backlink', $this->skin
->link( $this->target
, $this->target
->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
68 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
69 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
73 * @param $level int Recursion level
74 * @param $target Title Target title
75 * @param $limit int Number of entries to display
76 * @param $from Title Display from this article ID
77 * @param $back Title Display from this article ID at backwards scrolling
80 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
81 global $wgOut, $wgMaxRedirectLinksRetrieved;
82 $dbr = wfGetDB( DB_SLAVE
);
85 $hidelinks = $this->opts
->getValue( 'hidelinks' );
86 $hideredirs = $this->opts
->getValue( 'hideredirs' );
87 $hidetrans = $this->opts
->getValue( 'hidetrans' );
88 $hideimages = $target->getNamespace() != NS_FILE ||
$this->opts
->getValue( 'hideimages' );
90 $fetchlinks = (!$hidelinks ||
!$hideredirs);
95 'pl_namespace' => $target->getNamespace(),
96 'pl_title' => $target->getDBkey(),
99 $plConds['page_is_redirect'] = 0;
100 } elseif( $hidelinks ) {
101 $plConds['page_is_redirect'] = 1;
106 'tl_namespace' => $target->getNamespace(),
107 'tl_title' => $target->getDBkey(),
112 'il_to' => $target->getDBkey(),
115 $namespace = $this->opts
->getValue( 'namespace' );
116 if ( is_int($namespace) ) {
117 $plConds['page_namespace'] = $namespace;
118 $tlConds['page_namespace'] = $namespace;
119 $ilConds['page_namespace'] = $namespace;
123 $tlConds[] = "tl_from >= $from";
124 $plConds[] = "pl_from >= $from";
125 $ilConds[] = "il_from >= $from";
128 // Read an extra row as an at-end check
129 $queryLimit = $limit +
1;
131 // Enforce join order, sometimes namespace selector may
132 // trigger filesorts which are far less efficient than scanning many entries
133 $options[] = 'STRAIGHT_JOIN';
135 $options['LIMIT'] = $queryLimit;
136 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
139 $options['ORDER BY'] = 'pl_from';
140 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
141 $plConds, __METHOD__
, $options );
145 $options['ORDER BY'] = 'tl_from';
146 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
147 $tlConds, __METHOD__
, $options );
151 $options['ORDER BY'] = 'il_from';
152 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
153 $ilConds, __METHOD__
, $options );
156 if( ( !$fetchlinks ||
!$dbr->numRows($plRes) ) && ( $hidetrans ||
!$dbr->numRows($tlRes) ) && ( $hideimages ||
!$dbr->numRows($ilRes) ) ) {
158 $wgOut->addHTML( $this->whatlinkshereForm() );
160 // Show filters only if there are links
161 if( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages )
162 $wgOut->addHTML( $this->getFilterPanel() );
164 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
165 $wgOut->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
170 // Read the rows into an array and remove duplicates
171 // templatelinks comes second so that the templatelinks row overwrites the
172 // pagelinks row, so we get (inclusion) rather than nothing
174 while ( $row = $dbr->fetchObject( $plRes ) ) {
175 $row->is_template
= 0;
177 $rows[$row->page_id
] = $row;
179 $dbr->freeResult( $plRes );
183 while ( $row = $dbr->fetchObject( $tlRes ) ) {
184 $row->is_template
= 1;
186 $rows[$row->page_id
] = $row;
188 $dbr->freeResult( $tlRes );
191 while ( $row = $dbr->fetchObject( $ilRes ) ) {
192 $row->is_template
= 0;
194 $rows[$row->page_id
] = $row;
196 $dbr->freeResult( $ilRes );
199 // Sort by key and then change the keys to 0-based indices
201 $rows = array_values( $rows );
203 $numRows = count( $rows );
205 // Work out the start and end IDs, for prev/next links
206 if ( $numRows > $limit ) {
207 // More rows available after these ones
208 // Get the ID from the last row in the result set
209 $nextId = $rows[$limit]->page_id
;
210 // Remove undisplayed rows
211 $rows = array_slice( $rows, 0, $limit );
213 // No more rows after
219 $wgOut->addHTML( $this->whatlinkshereForm() );
220 $wgOut->addHTML( $this->getFilterPanel() );
221 $wgOut->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
223 $prevnext = $this->getPrevNext( $prevId, $nextId );
224 $wgOut->addHTML( $prevnext );
227 $wgOut->addHTML( $this->listStart() );
228 foreach ( $rows as $row ) {
229 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
231 if ( $row->page_is_redirect
&& $level < 2 ) {
232 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
233 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
234 $wgOut->addHTML( Xml
::closeElement( 'li' ) );
236 $wgOut->addHTML( $this->listItem( $row, $nt ) );
240 $wgOut->addHTML( $this->listEnd() );
243 $wgOut->addHTML( $prevnext );
247 protected function listStart() {
248 return Xml
::openElement( 'ul', array ( 'id' => 'mw-whatlinkshere-list' ) );
251 protected function listItem( $row, $nt, $notClose = false ) {
252 # local message cache
253 static $msgcache = null;
254 if ( $msgcache === null ) {
255 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
256 'whatlinkshere-links', 'isimage' );
258 foreach ( $msgs as $msg ) {
259 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
263 if( $row->page_is_redirect
) {
264 $query = array( 'redirect' => 'no' );
269 $link = $this->skin
->linkKnown(
276 // Display properties (redirect or template)
279 if ( $row->page_is_redirect
)
280 $props[] = $msgcache['isredirect'];
281 if ( $row->is_template
)
282 $props[] = $msgcache['istemplate'];
284 $props[] = $msgcache['isimage'];
286 if ( count( $props ) ) {
287 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
290 # Space for utilities links, with a what-links-here link provided
291 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
292 $wlh = Xml
::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
295 Xml
::openElement( 'li' ) . "$link $propsText $wlh\n" :
296 Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
299 protected function listEnd() {
300 return Xml
::closeElement( 'ul' );
303 protected function wlhLink( Title
$target, $text ) {
304 static $title = null;
305 if ( $title === null )
306 $title = SpecialPage
::getTitleFor( 'Whatlinkshere' );
308 return $this->skin
->linkKnown(
312 array( 'target' => $target->getPrefixedText() )
316 function makeSelfLink( $text, $query ) {
317 return $this->skin
->linkKnown(
325 function getPrevNext( $prevId, $nextId ) {
327 $currentLimit = $this->opts
->getValue( 'limit' );
328 $fmtLimit = $wgLang->formatNum( $currentLimit );
329 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
330 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
332 $changed = $this->opts
->getChangedValues();
333 unset($changed['target']); // Already in the request title
335 if ( 0 != $prevId ) {
336 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
337 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
339 if ( 0 != $nextId ) {
340 $overrides = array( 'from' => $nextId, 'back' => $prevId );
341 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
344 $limitLinks = array();
345 foreach ( $this->limits
as $limit ) {
346 $prettyLimit = $wgLang->formatNum( $limit );
347 $overrides = array( 'limit' => $limit );
348 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
351 $nums = $wgLang->pipeList( $limitLinks );
353 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
356 function whatlinkshereForm() {
359 // We get nicer value from the title object
360 $this->opts
->consumeValue( 'target' );
361 // Reset these for new requests
362 $this->opts
->consumeValues( array( 'back', 'from' ) );
364 $target = $this->target ?
$this->target
->getPrefixedText() : '';
365 $namespace = $this->opts
->consumeValue( 'namespace' );
368 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
370 # Values that should not be forgotten
371 $f .= Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Whatlinkshere' )->getPrefixedText() );
372 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
373 $f .= Xml
::hidden( $name, $value );
376 $f .= Xml
::fieldset( wfMsg( 'whatlinkshere' ) );
379 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
380 'mw-whatlinkshere-target', 40, $target );
385 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
386 Xml
::namespaceSelector( $namespace, '' );
391 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
394 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
399 function getFilterPanel() {
401 $show = wfMsgHtml( 'show' );
402 $hide = wfMsgHtml( 'hide' );
404 $changed = $this->opts
->getChangedValues();
405 unset($changed['target']); // Already in the request title
408 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
409 if( $this->target
->getNamespace() == NS_FILE
)
410 $types[] = 'hideimages';
411 foreach( $types as $type ) {
412 $chosen = $this->opts
->getValue( $type );
413 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ?
$show : $hide );
414 $overrides = array( $type => !$chosen );
415 $links[] = $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) );
417 return Xml
::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );