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 $suppressRedirect = $row->page_is_redirect ?
'redirect=no' : '';
262 $link = $this->skin
->makeKnownLinkObj( $nt, '', $suppressRedirect );
264 // Display properties (redirect or template)
267 if ( $row->page_is_redirect
)
268 $props[] = $msgcache['isredirect'];
269 if ( $row->is_template
)
270 $props[] = $msgcache['istemplate'];
272 $props[] = $msgcache['isimage'];
274 if ( count( $props ) ) {
275 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
278 # Space for utilities links, with a what-links-here link provided
279 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
280 $wlh = Xml
::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
283 Xml
::openElement( 'li' ) . "$link $propsText $wlh\n" :
284 Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
287 protected function listEnd() {
288 return Xml
::closeElement( 'ul' );
291 protected function wlhLink( Title
$target, $text ) {
292 static $title = null;
293 if ( $title === null )
294 $title = SpecialPage
::getTitleFor( 'Whatlinkshere' );
296 $targetText = $target->getPrefixedUrl();
297 return $this->skin
->makeKnownLinkObj( $title, $text, 'target=' . $targetText );
300 function makeSelfLink( $text, $query ) {
301 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
304 function getPrevNext( $prevId, $nextId ) {
306 $currentLimit = $this->opts
->getValue( 'limit' );
307 $fmtLimit = $wgLang->formatNum( $currentLimit );
308 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
309 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
311 $changed = $this->opts
->getChangedValues();
312 unset($changed['target']); // Already in the request title
314 if ( 0 != $prevId ) {
315 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
316 $prev = $this->makeSelfLink( $prev, wfArrayToCGI( $overrides, $changed ) );
318 if ( 0 != $nextId ) {
319 $overrides = array( 'from' => $nextId, 'back' => $prevId );
320 $next = $this->makeSelfLink( $next, wfArrayToCGI( $overrides, $changed ) );
323 $limitLinks = array();
324 foreach ( $this->limits
as $limit ) {
325 $prettyLimit = $wgLang->formatNum( $limit );
326 $overrides = array( 'limit' => $limit );
327 $limitLinks[] = $this->makeSelfLink( $prettyLimit, wfArrayToCGI( $overrides, $changed ) );
330 $nums = $wgLang->pipeList( $limitLinks );
332 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
335 function whatlinkshereForm() {
338 // We get nicer value from the title object
339 $this->opts
->consumeValue( 'target' );
340 // Reset these for new requests
341 $this->opts
->consumeValues( array( 'back', 'from' ) );
343 $target = $this->target ?
$this->target
->getPrefixedText() : '';
344 $namespace = $this->opts
->consumeValue( 'namespace' );
347 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
349 # Values that should not be forgotten
350 $f .= Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Whatlinkshere' )->getPrefixedText() );
351 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
352 $f .= Xml
::hidden( $name, $value );
355 $f .= Xml
::fieldset( wfMsg( 'whatlinkshere' ) );
358 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
359 'mw-whatlinkshere-target', 40, $target );
364 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
365 Xml
::namespaceSelector( $namespace, '' );
370 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
373 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
378 function getFilterPanel() {
380 $show = wfMsgHtml( 'show' );
381 $hide = wfMsgHtml( 'hide' );
383 $changed = $this->opts
->getChangedValues();
384 unset($changed['target']); // Already in the request title
387 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
388 if( $this->target
->getNamespace() == NS_FILE
)
389 $types[] = 'hideimages';
390 foreach( $types as $type ) {
391 $chosen = $this->opts
->getValue( $type );
392 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ?
$show : $hide );
393 $overrides = array( $type => !$chosen );
394 $links[] = $this->makeSelfLink( $msg, wfArrayToCGI( $overrides, $changed ) );
396 return Xml
::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );