d51e13ad826942c493de0def25c0506649c3ecf9
3 * @TODO: Use some variant of Pager or something; the pagination here is lousy.
5 * @addtogroup SpecialPage
10 * @param string $par An article name ??
12 function wfSpecialWhatlinkshere($par = NULL) {
14 $page = new WhatLinksHerePage( $wgRequest, $par );
19 * implements Special:Whatlinkshere
20 * @addtogroup SpecialPage
22 class WhatLinksHerePage
{
27 protected $opts, $target, $selfTitle;
30 protected $skin, $request;
32 protected $limits = array( 20, 50, 100, 250, 500 );
34 function WhatLinksHerePage( $request, $par = null ) {
36 $this->request
= $request;
37 $this->skin
= $wgUser->getSkin();
44 $opts = new FormOptions();
46 $opts->add( 'target', '' );
47 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
48 $opts->add( 'limit', 50 );
49 $opts->add( 'from', 0 );
50 $opts->add( 'back', 0 );
51 $opts->add( 'hideredirs', false );
52 $opts->add( 'hidetrans', false );
53 $opts->add( 'hidelinks', false );
54 $opts->add( 'hideimages', false );
56 $opts->fetchValuesFromRequest( $this->request
);
57 $opts->validateIntBounds( 'limit', 0, 5000 );
58 if ( isset($this->par
) ) {
59 $opts->setValue( 'target', $this->par
);
62 // Bind to member variable
65 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
66 if( !$this->target
) {
67 $wgOut->addHTML( $this->whatlinkshereForm() );
70 $this->selfTitle
= Title
::makeTitleSafe( NS_SPECIAL
,
71 'Whatlinkshere/' . $this->target
->getPrefixedDBkey() );
73 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
74 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
76 $wgOut->addHTML( wfMsgExt( 'whatlinkshere-barrow', array( 'escapenoentities') ) . ' ' .$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
78 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
79 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
83 * @param int $level Recursion level
84 * @param Title $target Target title
85 * @param int $limit Number of entries to display
86 * @param Title $from Display from this article ID
87 * @param Title $back Display from this article ID at backwards scrolling
90 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
91 global $wgOut, $wgMaxRedirectLinksRetrieved;
92 $dbr = wfGetDB( DB_SLAVE
);
95 $hidelinks = $this->opts
->getValue( 'hidelinks' );
96 $hideredirs = $this->opts
->getValue( 'hideredirs' );
97 $hidetrans = $this->opts
->getValue( 'hidetrans' );
98 $hideimages = $target->getNamespace() != NS_IMAGE ||
$this->opts
->getValue( 'hideimages' );
100 $fetchlinks = !$hidelinks ||
!$hideredirs;
105 'pl_namespace' => $target->getNamespace(),
106 'pl_title' => $target->getDBkey(),
109 $plConds['page_is_redirect'] = 0;
110 } elseif( $hidelinks ) {
111 $plConds['page_is_redirect'] = 1;
116 'tl_namespace' => $target->getNamespace(),
117 'tl_title' => $target->getDBkey(),
122 'il_to' => $target->getDBkey(),
125 $namespace = $this->opts
->getValue( 'namespace' );
126 if ( is_int($namespace) ){
127 $plConds['page_namespace'] = $namespace;
128 $tlConds['page_namespace'] = $namespace;
132 $tlConds[] = "tl_from >= $from";
133 $plConds[] = "pl_from >= $from";
136 // Read an extra row as an at-end check
137 $queryLimit = $limit +
1;
139 // enforce join order, sometimes namespace selector may
140 // trigger filesorts which are far less efficient than scanning many entries
141 $options[] = 'STRAIGHT_JOIN';
143 $options['LIMIT'] = $queryLimit;
144 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
147 $options['ORDER BY'] = 'pl_from';
148 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
149 $plConds, __METHOD__
, $options );
153 $options['ORDER BY'] = 'tl_from';
154 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
155 $tlConds, __METHOD__
, $options );
159 $options['ORDER BY'] = 'il_from';
160 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
161 $ilConds, __METHOD__
, $options );
164 if( ( !$fetchlinks ||
!$dbr->numRows( $plRes ) ) && ( $hidetrans ||
!$dbr->numRows( $tlRes ) ) && ( $hideimages ||
!$dbr->numRows( $ilRes ) ) ) {
166 $wgOut->addHTML( $this->whatlinkshereForm() );
167 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
168 $wgOut->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
169 // Show filters only if there are links
170 if( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages )
171 $wgOut->addHTML( $this->getFilterPanel() );
176 // Read the rows into an array and remove duplicates
177 // templatelinks comes second so that the templatelinks row overwrites the
178 // pagelinks row, so we get (inclusion) rather than nothing
180 while ( $row = $dbr->fetchObject( $plRes ) ) {
181 $row->is_template
= 0;
183 $rows[$row->page_id
] = $row;
185 $dbr->freeResult( $plRes );
189 while ( $row = $dbr->fetchObject( $tlRes ) ) {
190 $row->is_template
= 1;
192 $rows[$row->page_id
] = $row;
194 $dbr->freeResult( $tlRes );
197 while ( $row = $dbr->fetchObject( $ilRes ) ) {
198 $row->is_template
= 0;
200 $rows[$row->page_id
] = $row;
202 $dbr->freeResult( $ilRes );
205 // Sort by key and then change the keys to 0-based indices
207 $rows = array_values( $rows );
209 $numRows = count( $rows );
211 // Work out the start and end IDs, for prev/next links
212 if ( $numRows > $limit ) {
213 // More rows available after these ones
214 // Get the ID from the last row in the result set
215 $nextId = $rows[$limit]->page_id
;
216 // Remove undisplayed rows
217 $rows = array_slice( $rows, 0, $limit );
219 // No more rows after
225 $wgOut->addHTML( $this->whatlinkshereForm( ) );
226 $wgOut->addHTML( $this->getFilterPanel() );
227 $wgOut->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
229 $prevnext = $this->getPrevNext( $prevId, $nextId );
230 $wgOut->addHTML( $prevnext );
233 $wgOut->addHTML( $this->listStart() );
234 foreach ( $rows as $row ) {
235 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
237 if ( $row->page_is_redirect
&& $level < 2 ) {
238 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
239 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
240 $wgOut->addHTML( Xml
::closeElement( 'li' ) );
242 $wgOut->addHTML( $this->listItem( $row, $nt ) );
246 $wgOut->addHTML( $this->listEnd() );
249 $wgOut->addHTML( $prevnext );
253 protected function listStart() {
254 return Xml
::openElement( 'ul' );
257 protected function listItem( $row, $nt, $notClose = false ) {
258 # local message cache
259 static $msgcache = null;
260 if ( $msgcache === null ) {
261 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
262 'whatlinkshere-links', 'isimage' );
264 foreach ( $msgs as $msg ) {
265 $msgcache[$msg] = wfMsgHtml( $msg );
269 $suppressRedirect = $row->page_is_redirect ?
'redirect=no' : '';
270 $link = $this->skin
->makeKnownLinkObj( $nt, '', $suppressRedirect );
272 // Display properties (redirect or template)
275 if ( $row->page_is_redirect
)
276 $props[] = $msgcache['isredirect'];
277 if ( $row->is_template
)
278 $props[] = $msgcache['istemplate'];
280 $props[] = $msgcache['isimage'];
282 if ( count( $props ) ) {
283 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
286 # Space for utilities links, with a what-links-here link provided
287 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
288 $wlh = Xml
::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
291 Xml
::openElement( 'li' ) . "$link $propsText $wlh\n" :
292 Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
295 protected function listEnd() {
296 return Xml
::closeElement( 'ul' );
299 protected function wlhLink( Title
$target, $text ) {
300 static $title = null;
301 if ( $title === null )
302 $title = SpecialPage
::getTitleFor( 'Whatlinkshere' );
304 $targetText = $target->getPrefixedUrl();
305 return $this->skin
->makeKnownLinkObj( $title, $text, 'target=' . $targetText );
308 function makeSelfLink( $text, $query ) {
309 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
312 function getPrevNext( $prevId, $nextId ) {
314 $currentLimit = $this->opts
->getValue( 'limit' );
315 $fmtLimit = $wgLang->formatNum( $currentLimit );
316 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
317 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
319 $changed = $this->opts
->getChangedValues();
320 unset($changed['target']); // Already in the request title
322 if ( 0 != $prevId ) {
323 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
324 $prev = $this->makeSelfLink( $prev, wfArrayToCGI( $overrides, $changed ) );
326 if ( 0 != $nextId ) {
327 $overrides = array( 'from' => $nextId, 'back' => $prevId );
328 $next = $this->makeSelfLink( $next, wfArrayToCGI( $overrides, $changed ) );
331 $limitLinks = array();
332 foreach ( $this->limits
as $limit ) {
333 $prettyLimit = $wgLang->formatNum( $limit );
334 $overrides = array( 'limit' => $limit );
335 $limitLinks[] = $this->makeSelfLink( $prettyLimit, wfArrayToCGI( $overrides, $changed ) );
338 $nums = implode ( ' | ', $limitLinks );
340 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
343 function whatlinkshereForm() {
344 global $wgScript, $wgTitle;
346 // We get nicer value from the title object
347 $this->opts
->consumeValue( 'target' );
348 // Reset these for new requests
349 $this->opts
->consumeValues( array( 'back', 'from' ) );
351 $target = $this->target ?
$this->target
->getPrefixedText() : '';
352 $namespace = $this->opts
->consumeValue( 'namespace' );
355 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
357 # Values that should not be forgotten
358 $f .= Xml
::hidden( 'title', $wgTitle->getPrefixedText() );
359 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
360 $f .= Xml
::hidden( $name, $value );
363 $f .= Xml
::openElement( 'fieldset' );
364 $f .= Xml
::element( 'legend', null, wfMsg( 'whatlinkshere' ) );
367 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
368 'mw-whatlinkshere-target', 40, $target );
373 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
374 Xml
::namespaceSelector( $namespace, '' );
377 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
380 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
385 function getFilterPanel() {
386 $show = wfMsgHtml( 'show' );
387 $hide = wfMsgHtml( 'hide' );
389 $changed = $this->opts
->getChangedValues();
390 unset($changed['target']); // Already in the request title
393 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
394 if( $this->target
->getNamespace() == NS_IMAGE
)
395 $types[] = 'hideimages';
396 foreach( $types as $type ) {
397 $chosen = $this->opts
->getValue( $type );
398 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ?
$show : $hide );
399 $overrides = array( $type => !$chosen );
400 $links[] = $this->makeSelfLink( $msg, wfArrayToCGI( $overrides, $changed ) );
402 return Xml
::tags( 'fieldset', null,
403 Xml
::element( 'legend', null, wfMsg( 'whatlinkshere-filters' ) ) .
404 implode( ' | ', $links )