3 * @todo Use some variant of Pager or something; the pagination here is lousy.
11 * @param $par String: An article name ??
13 function wfSpecialWhatlinkshere($par = NULL) {
15 $page = new WhatLinksHerePage( $wgRequest, $par );
20 * implements Special:Whatlinkshere
21 * @ingroup SpecialPage
23 class WhatLinksHerePage
{
28 protected $opts, $target, $selfTitle;
31 protected $skin, $request;
33 protected $limits = array( 20, 50, 100, 250, 500 );
35 function WhatLinksHerePage( $request, $par = null ) {
37 $this->request
= $request;
38 $this->skin
= $wgUser->getSkin();
45 $opts = new FormOptions();
47 $opts->add( 'target', '' );
48 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
49 $opts->add( 'limit', 50 );
50 $opts->add( 'from', 0 );
51 $opts->add( 'back', 0 );
52 $opts->add( 'hideredirs', false );
53 $opts->add( 'hidetrans', false );
54 $opts->add( 'hidelinks', false );
55 $opts->add( 'hideimages', false );
57 $opts->fetchValuesFromRequest( $this->request
);
58 $opts->validateIntBounds( 'limit', 0, 5000 );
60 // Give precedence to subpage syntax
61 if ( isset($this->par
) ) {
62 $opts->setValue( 'target', $this->par
);
65 // Bind to member variable
68 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
69 if( !$this->target
) {
70 $wgOut->addHTML( $this->whatlinkshereForm() );
74 $this->selfTitle
= SpecialPage
::getTitleFor( 'Whatlinkshere', $this->target
->getPrefixedDBkey() );
76 $wgOut->setPageTitle( wfMsgExt( 'whatlinkshere-title', 'escape', $this->target
->getPrefixedText() ) );
77 $wgOut->setSubtitle( wfMsgHtml( 'linklistsub' ) );
79 $wgOut->addHTML( wfMsgExt( 'whatlinkshere-barrow', array( 'escapenoentities') ) . ' ' .$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
81 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
82 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
86 * @param int $level Recursion level
87 * @param Title $target Target title
88 * @param int $limit Number of entries to display
89 * @param Title $from Display from this article ID
90 * @param Title $back 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_IMAGE ||
$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;
135 $tlConds[] = "tl_from >= $from";
136 $plConds[] = "pl_from >= $from";
137 $ilConds[] = "il_from >= $from";
140 // Read an extra row as an at-end check
141 $queryLimit = $limit +
1;
143 // Enforce join order, sometimes namespace selector may
144 // trigger filesorts which are far less efficient than scanning many entries
145 $options[] = 'STRAIGHT_JOIN';
147 $options['LIMIT'] = $queryLimit;
148 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
151 $options['ORDER BY'] = 'pl_from';
152 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
153 $plConds, __METHOD__
, $options );
157 $options['ORDER BY'] = 'tl_from';
158 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
159 $tlConds, __METHOD__
, $options );
163 $options['ORDER BY'] = 'il_from';
164 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
165 $ilConds, __METHOD__
, $options );
168 if( ( !$fetchlinks ||
!$dbr->numRows($plRes) ) && ( $hidetrans ||
!$dbr->numRows($tlRes) ) && ( $hideimages ||
!$dbr->numRows($ilRes) ) ) {
170 $wgOut->addHTML( $this->whatlinkshereForm() );
171 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
172 $wgOut->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
173 // Show filters only if there are links
174 if( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages )
175 $wgOut->addHTML( $this->getFilterPanel() );
180 // Read the rows into an array and remove duplicates
181 // templatelinks comes second so that the templatelinks row overwrites the
182 // pagelinks row, so we get (inclusion) rather than nothing
184 while ( $row = $dbr->fetchObject( $plRes ) ) {
185 $row->is_template
= 0;
187 $rows[$row->page_id
] = $row;
189 $dbr->freeResult( $plRes );
193 while ( $row = $dbr->fetchObject( $tlRes ) ) {
194 $row->is_template
= 1;
196 $rows[$row->page_id
] = $row;
198 $dbr->freeResult( $tlRes );
201 while ( $row = $dbr->fetchObject( $ilRes ) ) {
202 $row->is_template
= 0;
204 $rows[$row->page_id
] = $row;
206 $dbr->freeResult( $ilRes );
209 // Sort by key and then change the keys to 0-based indices
211 $rows = array_values( $rows );
213 $numRows = count( $rows );
215 // Work out the start and end IDs, for prev/next links
216 if ( $numRows > $limit ) {
217 // More rows available after these ones
218 // Get the ID from the last row in the result set
219 $nextId = $rows[$limit]->page_id
;
220 // Remove undisplayed rows
221 $rows = array_slice( $rows, 0, $limit );
223 // No more rows after
229 $wgOut->addHTML( $this->whatlinkshereForm() );
230 $wgOut->addHTML( $this->getFilterPanel() );
231 $wgOut->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
233 $prevnext = $this->getPrevNext( $prevId, $nextId );
234 $wgOut->addHTML( $prevnext );
237 $wgOut->addHTML( $this->listStart() );
238 foreach ( $rows as $row ) {
239 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
241 if ( $row->page_is_redirect
&& $level < 2 ) {
242 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
243 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
244 $wgOut->addHTML( Xml
::closeElement( 'li' ) );
246 $wgOut->addHTML( $this->listItem( $row, $nt ) );
250 $wgOut->addHTML( $this->listEnd() );
253 $wgOut->addHTML( $prevnext );
257 protected function listStart() {
258 return Xml
::openElement( 'ul' );
261 protected function listItem( $row, $nt, $notClose = false ) {
262 # local message cache
263 static $msgcache = null;
264 if ( $msgcache === null ) {
265 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
266 'whatlinkshere-links', 'isimage' );
268 foreach ( $msgs as $msg ) {
269 $msgcache[$msg] = wfMsgHtml( $msg );
273 $suppressRedirect = $row->page_is_redirect ?
'redirect=no' : '';
274 $link = $this->skin
->makeKnownLinkObj( $nt, '', $suppressRedirect );
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 $targetText = $target->getPrefixedUrl();
309 return $this->skin
->makeKnownLinkObj( $title, $text, 'target=' . $targetText );
312 function makeSelfLink( $text, $query ) {
313 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
316 function getPrevNext( $prevId, $nextId ) {
318 $currentLimit = $this->opts
->getValue( 'limit' );
319 $fmtLimit = $wgLang->formatNum( $currentLimit );
320 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
321 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
323 $changed = $this->opts
->getChangedValues();
324 unset($changed['target']); // Already in the request title
326 if ( 0 != $prevId ) {
327 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
328 $prev = $this->makeSelfLink( $prev, wfArrayToCGI( $overrides, $changed ) );
330 if ( 0 != $nextId ) {
331 $overrides = array( 'from' => $nextId, 'back' => $prevId );
332 $next = $this->makeSelfLink( $next, wfArrayToCGI( $overrides, $changed ) );
335 $limitLinks = array();
336 foreach ( $this->limits
as $limit ) {
337 $prettyLimit = $wgLang->formatNum( $limit );
338 $overrides = array( 'limit' => $limit );
339 $limitLinks[] = $this->makeSelfLink( $prettyLimit, wfArrayToCGI( $overrides, $changed ) );
342 $nums = implode ( ' | ', $limitLinks );
344 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
347 function whatlinkshereForm() {
348 global $wgScript, $wgTitle;
350 // We get nicer value from the title object
351 $this->opts
->consumeValue( 'target' );
352 // Reset these for new requests
353 $this->opts
->consumeValues( array( 'back', 'from' ) );
355 $target = $this->target ?
$this->target
->getPrefixedText() : '';
356 $namespace = $this->opts
->consumeValue( 'namespace' );
359 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
361 # Values that should not be forgotten
362 $f .= Xml
::hidden( 'title', $wgTitle->getPrefixedText() );
363 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
364 $f .= Xml
::hidden( $name, $value );
367 $f .= Xml
::fieldset( wfMsg( 'whatlinkshere' ) );
370 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
371 'mw-whatlinkshere-target', 40, $target );
376 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
377 Xml
::namespaceSelector( $namespace, '' );
380 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
383 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
388 function getFilterPanel() {
389 $show = wfMsgHtml( 'show' );
390 $hide = wfMsgHtml( 'hide' );
392 $changed = $this->opts
->getChangedValues();
393 unset($changed['target']); // Already in the request title
396 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
397 if( $this->target
->getNamespace() == NS_IMAGE
)
398 $types[] = 'hideimages';
399 foreach( $types as $type ) {
400 $chosen = $this->opts
->getValue( $type );
401 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ?
$show : $hide );
402 $overrides = array( $type => !$chosen );
403 $links[] = $this->makeSelfLink( $msg, wfArrayToCGI( $overrides, $changed ) );
405 return Xml
::fieldset( wfMsg( 'whatlinkshere-filters' ), implode( ' | ', $links ) );