* (bug 24563) Entries on Special:WhatLinksHere now have a link to their history
[lhc/web/wiklou.git] / includes / specials / SpecialWhatlinkshere.php
1 <?php
2 /**
3 * @todo Use some variant of Pager or something; the pagination here is lousy.
4 * @file
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 */
21
22 /**
23 * implements Special:Whatlinkshere
24 * @ingroup SpecialPage
25 */
26 class SpecialWhatLinksHere extends SpecialPage {
27
28 // Stored objects
29 protected $opts, $target, $selfTitle;
30
31 // Stored globals
32 protected $skin;
33
34 protected $limits = array( 20, 50, 100, 250, 500 );
35
36 public function __construct() {
37 parent::__construct( 'Whatlinkshere' );
38 global $wgUser;
39 $this->skin = $wgUser->getSkin();
40 }
41
42 function execute( $par ) {
43 global $wgOut, $wgRequest;
44
45 $this->setHeaders();
46
47 $opts = new FormOptions();
48
49 $opts->add( 'target', '' );
50 $opts->add( 'namespace', '', FormOptions::INTNULL );
51 $opts->add( 'limit', 50 );
52 $opts->add( 'from', 0 );
53 $opts->add( 'back', 0 );
54 $opts->add( 'hideredirs', false );
55 $opts->add( 'hidetrans', false );
56 $opts->add( 'hidelinks', false );
57 $opts->add( 'hideimages', false );
58
59 $opts->fetchValuesFromRequest( $wgRequest );
60 $opts->validateIntBounds( 'limit', 0, 5000 );
61
62 // Give precedence to subpage syntax
63 if ( isset($par) ) {
64 $opts->setValue( 'target', $par );
65 }
66
67 // Bind to member variable
68 $this->opts = $opts;
69
70 $this->target = Title::newFromURL( $opts->getValue( 'target' ) );
71 if( !$this->target ) {
72 $wgOut->addHTML( $this->whatlinkshereForm() );
73 return;
74 }
75
76 $this->selfTitle = $this->getTitle( $this->target->getPrefixedDBkey() );
77
78 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
79 $wgOut->setSubtitle( wfMsg( 'whatlinkshere-backlink', $this->skin->link( $this->target, $this->target->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
80
81 $this->showIndirectLinks( 0, $this->target, $opts->getValue( 'limit' ),
82 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
83 }
84
85 /**
86 * @param $level int Recursion level
87 * @param $target Title Target title
88 * @param $limit int Number of entries to display
89 * @param $from Title Display from this article ID
90 * @param $back Title Display from this article ID at backwards scrolling
91 * @private
92 */
93 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
94 global $wgOut, $wgMaxRedirectLinksRetrieved;
95 $dbr = wfGetDB( DB_SLAVE );
96 $options = array();
97
98 $hidelinks = $this->opts->getValue( 'hidelinks' );
99 $hideredirs = $this->opts->getValue( 'hideredirs' );
100 $hidetrans = $this->opts->getValue( 'hidetrans' );
101 $hideimages = $target->getNamespace() != NS_FILE || $this->opts->getValue( 'hideimages' );
102
103 $fetchlinks = (!$hidelinks || !$hideredirs);
104
105 // Make the query
106 $plConds = array(
107 'page_id=pl_from',
108 'pl_namespace' => $target->getNamespace(),
109 'pl_title' => $target->getDBkey(),
110 );
111 if( $hideredirs ) {
112 $plConds['page_is_redirect'] = 0;
113 } elseif( $hidelinks ) {
114 $plConds['page_is_redirect'] = 1;
115 }
116
117 $tlConds = array(
118 'page_id=tl_from',
119 'tl_namespace' => $target->getNamespace(),
120 'tl_title' => $target->getDBkey(),
121 );
122
123 $ilConds = array(
124 'page_id=il_from',
125 'il_to' => $target->getDBkey(),
126 );
127
128 $namespace = $this->opts->getValue( 'namespace' );
129 if ( is_int($namespace) ) {
130 $plConds['page_namespace'] = $namespace;
131 $tlConds['page_namespace'] = $namespace;
132 $ilConds['page_namespace'] = $namespace;
133 }
134
135 if ( $from ) {
136 $tlConds[] = "tl_from >= $from";
137 $plConds[] = "pl_from >= $from";
138 $ilConds[] = "il_from >= $from";
139 }
140
141 // Read an extra row as an at-end check
142 $queryLimit = $limit + 1;
143
144 // Enforce join order, sometimes namespace selector may
145 // trigger filesorts which are far less efficient than scanning many entries
146 $options[] = 'STRAIGHT_JOIN';
147
148 $options['LIMIT'] = $queryLimit;
149 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
150
151 if( $fetchlinks ) {
152 $options['ORDER BY'] = 'pl_from';
153 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
154 $plConds, __METHOD__, $options );
155 }
156
157 if( !$hidetrans ) {
158 $options['ORDER BY'] = 'tl_from';
159 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
160 $tlConds, __METHOD__, $options );
161 }
162
163 if( !$hideimages ) {
164 $options['ORDER BY'] = 'il_from';
165 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
166 $ilConds, __METHOD__, $options );
167 }
168
169 if( ( !$fetchlinks || !$dbr->numRows($plRes) ) && ( $hidetrans || !$dbr->numRows($tlRes) ) && ( $hideimages || !$dbr->numRows($ilRes) ) ) {
170 if ( 0 == $level ) {
171 $wgOut->addHTML( $this->whatlinkshereForm() );
172
173 // Show filters only if there are links
174 if( $hidelinks || $hidetrans || $hideredirs || $hideimages )
175 $wgOut->addHTML( $this->getFilterPanel() );
176
177 $errMsg = is_int($namespace) ? 'nolinkshere-ns' : 'nolinkshere';
178 $wgOut->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
179 }
180 return;
181 }
182
183 // Read the rows into an array and remove duplicates
184 // templatelinks comes second so that the templatelinks row overwrites the
185 // pagelinks row, so we get (inclusion) rather than nothing
186 if( $fetchlinks ) {
187 while ( $row = $dbr->fetchObject( $plRes ) ) {
188 $row->is_template = 0;
189 $row->is_image = 0;
190 $rows[$row->page_id] = $row;
191 }
192 $dbr->freeResult( $plRes );
193
194 }
195 if( !$hidetrans ) {
196 while ( $row = $dbr->fetchObject( $tlRes ) ) {
197 $row->is_template = 1;
198 $row->is_image = 0;
199 $rows[$row->page_id] = $row;
200 }
201 $dbr->freeResult( $tlRes );
202 }
203 if( !$hideimages ) {
204 while ( $row = $dbr->fetchObject( $ilRes ) ) {
205 $row->is_template = 0;
206 $row->is_image = 1;
207 $rows[$row->page_id] = $row;
208 }
209 $dbr->freeResult( $ilRes );
210 }
211
212 // Sort by key and then change the keys to 0-based indices
213 ksort( $rows );
214 $rows = array_values( $rows );
215
216 $numRows = count( $rows );
217
218 // Work out the start and end IDs, for prev/next links
219 if ( $numRows > $limit ) {
220 // More rows available after these ones
221 // Get the ID from the last row in the result set
222 $nextId = $rows[$limit]->page_id;
223 // Remove undisplayed rows
224 $rows = array_slice( $rows, 0, $limit );
225 } else {
226 // No more rows after
227 $nextId = false;
228 }
229 $prevId = $from;
230
231 if ( $level == 0 ) {
232 $wgOut->addHTML( $this->whatlinkshereForm() );
233 $wgOut->addHTML( $this->getFilterPanel() );
234 $wgOut->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
235
236 $prevnext = $this->getPrevNext( $prevId, $nextId );
237 $wgOut->addHTML( $prevnext );
238 }
239
240 $wgOut->addHTML( $this->listStart() );
241 foreach ( $rows as $row ) {
242 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
243
244 if ( $row->page_is_redirect && $level < 2 ) {
245 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
246 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
247 $wgOut->addHTML( Xml::closeElement( 'li' ) );
248 } else {
249 $wgOut->addHTML( $this->listItem( $row, $nt ) );
250 }
251 }
252
253 $wgOut->addHTML( $this->listEnd() );
254
255 if( $level == 0 ) {
256 $wgOut->addHTML( $prevnext );
257 }
258 }
259
260 protected function listStart() {
261 return Xml::openElement( 'ul', array ( 'id' => 'mw-whatlinkshere-list' ) );
262 }
263
264 protected function listItem( $row, $nt, $notClose = false ) {
265 global $wgLang;
266
267 # local message cache
268 static $msgcache = null;
269 if ( $msgcache === null ) {
270 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
271 'whatlinkshere-links', 'isimage', 'hist' );
272 $msgcache = array();
273 foreach ( $msgs as $msg ) {
274 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
275 }
276 }
277
278 if( $row->page_is_redirect ) {
279 $query = array( 'redirect' => 'no' );
280 } else {
281 $query = array();
282 }
283
284 $link = $this->skin->linkKnown(
285 $nt,
286 null,
287 array(),
288 $query
289 );
290
291 // Display properties (redirect or template)
292 $propsText = '';
293 $props = array();
294 if ( $row->page_is_redirect )
295 $props[] = $msgcache['isredirect'];
296 if ( $row->is_template )
297 $props[] = $msgcache['istemplate'];
298 if( $row->is_image )
299 $props[] = $msgcache['isimage'];
300
301 if ( count( $props ) ) {
302 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
303 }
304
305 # Space for utilities links, with a what-links-here link provided
306 $tools = array();
307 $tools[] = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
308 $tools[] = $this->skin->linkKnown( $nt, $msgcache['hist'], array(), array( 'action' => 'history' ) );
309 $wlh = Xml::wrapClass( '(' . $wgLang->pipeList( $tools ) . ')', 'mw-whatlinkshere-tools' );
310
311 return $notClose ?
312 Xml::openElement( 'li' ) . "$link $propsText $wlh\n" :
313 Xml::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
314 }
315
316 protected function listEnd() {
317 return Xml::closeElement( 'ul' );
318 }
319
320 protected function wlhLink( Title $target, $text ) {
321 static $title = null;
322 if ( $title === null )
323 $title = $this->getTitle();
324
325 return $this->skin->linkKnown(
326 $title,
327 $text,
328 array(),
329 array( 'target' => $target->getPrefixedText() )
330 );
331 }
332
333 function makeSelfLink( $text, $query ) {
334 return $this->skin->linkKnown(
335 $this->selfTitle,
336 $text,
337 array(),
338 $query
339 );
340 }
341
342 function getPrevNext( $prevId, $nextId ) {
343 global $wgLang;
344 $currentLimit = $this->opts->getValue( 'limit' );
345 $fmtLimit = $wgLang->formatNum( $currentLimit );
346 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
347 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
348
349 $changed = $this->opts->getChangedValues();
350 unset($changed['target']); // Already in the request title
351
352 if ( 0 != $prevId ) {
353 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
354 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
355 }
356 if ( 0 != $nextId ) {
357 $overrides = array( 'from' => $nextId, 'back' => $prevId );
358 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
359 }
360
361 $limitLinks = array();
362 foreach ( $this->limits as $limit ) {
363 $prettyLimit = $wgLang->formatNum( $limit );
364 $overrides = array( 'limit' => $limit );
365 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
366 }
367
368 $nums = $wgLang->pipeList( $limitLinks );
369
370 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
371 }
372
373 function whatlinkshereForm() {
374 global $wgScript;
375
376 // We get nicer value from the title object
377 $this->opts->consumeValue( 'target' );
378 // Reset these for new requests
379 $this->opts->consumeValues( array( 'back', 'from' ) );
380
381 $target = $this->target ? $this->target->getPrefixedText() : '';
382 $namespace = $this->opts->consumeValue( 'namespace' );
383
384 # Build up the form
385 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
386
387 # Values that should not be forgotten
388 $f .= Xml::hidden( 'title', $this->getTitle()->getPrefixedText() );
389 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
390 $f .= Xml::hidden( $name, $value );
391 }
392
393 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
394
395 # Target input
396 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
397 'mw-whatlinkshere-target', 40, $target );
398
399 $f .= ' ';
400
401 # Namespace selector
402 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;' .
403 Xml::namespaceSelector( $namespace, '' );
404
405 $f .= ' ';
406
407 # Submit
408 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
409
410 # Close
411 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
412
413 return $f;
414 }
415
416 /**
417 * Create filter panel
418 *
419 * @return string HTML fieldset and filter panel with the show/hide links
420 */
421 function getFilterPanel() {
422 global $wgLang;
423 $show = wfMsgHtml( 'show' );
424 $hide = wfMsgHtml( 'hide' );
425
426 $changed = $this->opts->getChangedValues();
427 unset($changed['target']); // Already in the request title
428
429 $links = array();
430 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
431 if( $this->target->getNamespace() == NS_FILE )
432 $types[] = 'hideimages';
433
434 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
435 // To be sure they will be find by grep
436 foreach( $types as $type ) {
437 $chosen = $this->opts->getValue( $type );
438 $msg = $chosen ? $show : $hide;
439 $overrides = array( $type => !$chosen );
440 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
441 }
442 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );
443 }
444 }