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;
32 $opts = new FormOptions();
34 $opts->add( 'target', '' );
35 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
36 $opts->add( 'limit', 50 );
37 $opts->add( 'from', 0 );
38 $opts->add( 'back', 0 );
39 $opts->add( 'hideredirs', false );
40 $opts->add( 'hidetrans', false );
41 $opts->add( 'hidelinks', false );
42 $opts->add( 'hideimages', false );
44 $opts->fetchValuesFromRequest( $wgRequest );
45 $opts->validateIntBounds( 'limit', 0, 5000 );
47 // Give precedence to subpage syntax
49 $opts->setValue( 'target', $par );
52 // Bind to member variable
55 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
56 if( !$this->target
) {
57 $wgOut->addHTML( $this->whatlinkshereForm() );
61 $this->selfTitle
= SpecialPage
::getTitleFor( 'Whatlinkshere', $this->target
->getPrefixedDBkey() );
63 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
64 $wgOut->setSubtitle( wfMsg( 'whatlinkshere-backlink', $this->skin
->link( $this->target
, $this->target
->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
66 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
67 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
71 * @param $level int Recursion level
72 * @param $target Title Target title
73 * @param $limit int Number of entries to display
74 * @param $from Title Display from this article ID
75 * @param $back Title Display from this article ID at backwards scrolling
78 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
79 global $wgOut, $wgMaxRedirectLinksRetrieved;
80 $dbr = wfGetDB( DB_SLAVE
);
83 $hidelinks = $this->opts
->getValue( 'hidelinks' );
84 $hideredirs = $this->opts
->getValue( 'hideredirs' );
85 $hidetrans = $this->opts
->getValue( 'hidetrans' );
86 $hideimages = $target->getNamespace() != NS_FILE ||
$this->opts
->getValue( 'hideimages' );
88 $fetchlinks = (!$hidelinks ||
!$hideredirs);
93 'pl_namespace' => $target->getNamespace(),
94 'pl_title' => $target->getDBkey(),
97 $plConds['page_is_redirect'] = 0;
98 } elseif( $hidelinks ) {
99 $plConds['page_is_redirect'] = 1;
104 'tl_namespace' => $target->getNamespace(),
105 'tl_title' => $target->getDBkey(),
110 'il_to' => $target->getDBkey(),
113 $namespace = $this->opts
->getValue( 'namespace' );
114 if ( is_int($namespace) ) {
115 $plConds['page_namespace'] = $namespace;
116 $tlConds['page_namespace'] = $namespace;
117 $ilConds['page_namespace'] = $namespace;
121 $tlConds[] = "tl_from >= $from";
122 $plConds[] = "pl_from >= $from";
123 $ilConds[] = "il_from >= $from";
126 // Read an extra row as an at-end check
127 $queryLimit = $limit +
1;
129 // Enforce join order, sometimes namespace selector may
130 // trigger filesorts which are far less efficient than scanning many entries
131 $options[] = 'STRAIGHT_JOIN';
133 $options['LIMIT'] = $queryLimit;
134 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
137 $options['ORDER BY'] = 'pl_from';
138 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
139 $plConds, __METHOD__
, $options );
143 $options['ORDER BY'] = 'tl_from';
144 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
145 $tlConds, __METHOD__
, $options );
149 $options['ORDER BY'] = 'il_from';
150 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
151 $ilConds, __METHOD__
, $options );
154 if( ( !$fetchlinks ||
!$dbr->numRows($plRes) ) && ( $hidetrans ||
!$dbr->numRows($tlRes) ) && ( $hideimages ||
!$dbr->numRows($ilRes) ) ) {
156 $wgOut->addHTML( $this->whatlinkshereForm() );
158 // Show filters only if there are links
159 if( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages )
160 $wgOut->addHTML( $this->getFilterPanel() );
162 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
163 $wgOut->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
168 // Read the rows into an array and remove duplicates
169 // templatelinks comes second so that the templatelinks row overwrites the
170 // pagelinks row, so we get (inclusion) rather than nothing
172 while ( $row = $dbr->fetchObject( $plRes ) ) {
173 $row->is_template
= 0;
175 $rows[$row->page_id
] = $row;
177 $dbr->freeResult( $plRes );
181 while ( $row = $dbr->fetchObject( $tlRes ) ) {
182 $row->is_template
= 1;
184 $rows[$row->page_id
] = $row;
186 $dbr->freeResult( $tlRes );
189 while ( $row = $dbr->fetchObject( $ilRes ) ) {
190 $row->is_template
= 0;
192 $rows[$row->page_id
] = $row;
194 $dbr->freeResult( $ilRes );
197 // Sort by key and then change the keys to 0-based indices
199 $rows = array_values( $rows );
201 $numRows = count( $rows );
203 // Work out the start and end IDs, for prev/next links
204 if ( $numRows > $limit ) {
205 // More rows available after these ones
206 // Get the ID from the last row in the result set
207 $nextId = $rows[$limit]->page_id
;
208 // Remove undisplayed rows
209 $rows = array_slice( $rows, 0, $limit );
211 // No more rows after
217 $wgOut->addHTML( $this->whatlinkshereForm() );
218 $wgOut->addHTML( $this->getFilterPanel() );
219 $wgOut->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
221 $prevnext = $this->getPrevNext( $prevId, $nextId );
222 $wgOut->addHTML( $prevnext );
225 $wgOut->addHTML( $this->listStart() );
226 foreach ( $rows as $row ) {
227 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
229 if ( $row->page_is_redirect
&& $level < 2 ) {
230 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
231 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
232 $wgOut->addHTML( Xml
::closeElement( 'li' ) );
234 $wgOut->addHTML( $this->listItem( $row, $nt ) );
238 $wgOut->addHTML( $this->listEnd() );
241 $wgOut->addHTML( $prevnext );
245 protected function listStart() {
246 return Xml
::openElement( 'ul', array ( 'id' => 'mw-whatlinkshere-list' ) );
249 protected function listItem( $row, $nt, $notClose = false ) {
250 # local message cache
251 static $msgcache = null;
252 if ( $msgcache === null ) {
253 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
254 'whatlinkshere-links', 'isimage' );
256 foreach ( $msgs as $msg ) {
257 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
261 if( $row->page_is_redirect
) {
262 $query = array( 'redirect' => 'no' );
267 $link = $this->skin
->linkKnown(
274 // Display properties (redirect or template)
277 if ( $row->page_is_redirect
)
278 $props[] = $msgcache['isredirect'];
279 if ( $row->is_template
)
280 $props[] = $msgcache['istemplate'];
282 $props[] = $msgcache['isimage'];
284 if ( count( $props ) ) {
285 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
288 # Space for utilities links, with a what-links-here link provided
289 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
290 $wlh = Xml
::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
293 Xml
::openElement( 'li' ) . "$link $propsText $wlh\n" :
294 Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
297 protected function listEnd() {
298 return Xml
::closeElement( 'ul' );
301 protected function wlhLink( Title
$target, $text ) {
302 static $title = null;
303 if ( $title === null )
304 $title = SpecialPage
::getTitleFor( 'Whatlinkshere' );
306 $targetText = $target->getPrefixedUrl();
307 return $this->skin
->linkKnown(
311 array( 'target' => $targetText )
315 function makeSelfLink( $text, $query ) {
316 return $this->skin
->linkKnown(
324 function getPrevNext( $prevId, $nextId ) {
326 $currentLimit = $this->opts
->getValue( 'limit' );
327 $fmtLimit = $wgLang->formatNum( $currentLimit );
328 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
329 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
331 $changed = $this->opts
->getChangedValues();
332 unset($changed['target']); // Already in the request title
334 if ( 0 != $prevId ) {
335 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
336 $prev = $this->makeSelfLink( $prev, array_merge( $overrides, $changed ) );
338 if ( 0 != $nextId ) {
339 $overrides = array( 'from' => $nextId, 'back' => $prevId );
340 $next = $this->makeSelfLink( $next, array_merge( $overrides, $changed ) );
343 $limitLinks = array();
344 foreach ( $this->limits
as $limit ) {
345 $prettyLimit = $wgLang->formatNum( $limit );
346 $overrides = array( 'limit' => $limit );
347 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $overrides, $changed ) );
350 $nums = $wgLang->pipeList( $limitLinks );
352 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
355 function whatlinkshereForm() {
358 // We get nicer value from the title object
359 $this->opts
->consumeValue( 'target' );
360 // Reset these for new requests
361 $this->opts
->consumeValues( array( 'back', 'from' ) );
363 $target = $this->target ?
$this->target
->getPrefixedText() : '';
364 $namespace = $this->opts
->consumeValue( 'namespace' );
367 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
369 # Values that should not be forgotten
370 $f .= Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Whatlinkshere' )->getPrefixedText() );
371 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
372 $f .= Xml
::hidden( $name, $value );
375 $f .= Xml
::fieldset( wfMsg( 'whatlinkshere' ) );
378 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
379 'mw-whatlinkshere-target', 40, $target );
384 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
385 Xml
::namespaceSelector( $namespace, '' );
390 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
393 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
398 function getFilterPanel() {
400 $show = wfMsgHtml( 'show' );
401 $hide = wfMsgHtml( 'hide' );
403 $changed = $this->opts
->getChangedValues();
404 unset($changed['target']); // Already in the request title
407 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
408 if( $this->target
->getNamespace() == NS_FILE
)
409 $types[] = 'hideimages';
410 foreach( $types as $type ) {
411 $chosen = $this->opts
->getValue( $type );
412 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ?
$show : $hide );
413 $overrides = array( $type => !$chosen );
414 $links[] = $this->makeSelfLink( $msg, array_merge( $overrides, $changed ) );
416 return Xml
::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );