Set context earlier in ImageListPager::__construct
[lhc/web/wiklou.git] / includes / specials / SpecialWhatlinkshere.php
1 <?php
2 /**
3 * Implements Special:Whatlinkshere
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @todo Use some variant of Pager or something; the pagination here is lousy.
22 */
23
24 /**
25 * Implements Special:Whatlinkshere
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialWhatLinksHere extends IncludableSpecialPage {
30 /** @var FormOptions */
31 protected $opts;
32
33 protected $selfTitle;
34
35 /** @var Title */
36 protected $target;
37
38 protected $limits = array( 20, 50, 100, 250, 500 );
39
40 public function __construct() {
41 parent::__construct( 'Whatlinkshere' );
42 }
43
44 function execute( $par ) {
45 $out = $this->getOutput();
46
47 $this->setHeaders();
48 $this->outputHeader();
49
50 $opts = new FormOptions();
51
52 $opts->add( 'target', '' );
53 $opts->add( 'namespace', '', FormOptions::INTNULL );
54 $opts->add( 'limit', $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
55 $opts->add( 'from', 0 );
56 $opts->add( 'back', 0 );
57 $opts->add( 'hideredirs', false );
58 $opts->add( 'hidetrans', false );
59 $opts->add( 'hidelinks', false );
60 $opts->add( 'hideimages', false );
61 $opts->add( 'invert', false );
62
63 $opts->fetchValuesFromRequest( $this->getRequest() );
64 $opts->validateIntBounds( 'limit', 0, 5000 );
65
66 // Give precedence to subpage syntax
67 if ( $par !== null ) {
68 $opts->setValue( 'target', $par );
69 }
70
71 // Bind to member variable
72 $this->opts = $opts;
73
74 $this->target = Title::newFromURL( $opts->getValue( 'target' ) );
75 if ( !$this->target ) {
76 $out->addHTML( $this->whatlinkshereForm() );
77
78 return;
79 }
80
81 $this->getSkin()->setRelevantTitle( $this->target );
82
83 $this->selfTitle = $this->getPageTitle( $this->target->getPrefixedDBkey() );
84
85 $out->setPageTitle( $this->msg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
86 $out->addBacklinkSubtitle( $this->target );
87 $this->showIndirectLinks(
88 0,
89 $this->target,
90 $opts->getValue( 'limit' ),
91 $opts->getValue( 'from' ),
92 $opts->getValue( 'back' )
93 );
94 }
95
96 /**
97 * @param int $level Recursion level
98 * @param Title $target Target title
99 * @param int $limit Number of entries to display
100 * @param int $from Display from this article ID (default: 0)
101 * @param int $back Display from this article ID at backwards scrolling (default: 0)
102 */
103 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
104 $out = $this->getOutput();
105 $dbr = wfGetDB( DB_SLAVE );
106
107 $hidelinks = $this->opts->getValue( 'hidelinks' );
108 $hideredirs = $this->opts->getValue( 'hideredirs' );
109 $hidetrans = $this->opts->getValue( 'hidetrans' );
110 $hideimages = $target->getNamespace() != NS_FILE || $this->opts->getValue( 'hideimages' );
111
112 $fetchlinks = ( !$hidelinks || !$hideredirs );
113
114 // Build query conds in concert for all three tables...
115 $conds['pagelinks'] = array(
116 'pl_namespace' => $target->getNamespace(),
117 'pl_title' => $target->getDBkey(),
118 );
119 $conds['templatelinks'] = array(
120 'tl_namespace' => $target->getNamespace(),
121 'tl_title' => $target->getDBkey(),
122 );
123 $conds['imagelinks'] = array(
124 'il_to' => $target->getDBkey(),
125 );
126
127 $useLinkNamespaceDBFields = $this->getConfig()->get( 'UseLinkNamespaceDBFields' );
128 $namespace = $this->opts->getValue( 'namespace' );
129 $invert = $this->opts->getValue( 'invert' );
130 $nsComparison = ( $invert ? '!= ' : '= ' ) . $dbr->addQuotes( $namespace );
131 if ( is_int( $namespace ) ) {
132 if ( $useLinkNamespaceDBFields ) {
133 $conds['pagelinks'][] = "pl_from_namespace $nsComparison";
134 $conds['templatelinks'][] = "tl_from_namespace $nsComparison";
135 $conds['imagelinks'][] = "il_from_namespace $nsComparison";
136 } else {
137 $conds['pagelinks'][] = "page_namespace $nsComparison";
138 $conds['templatelinks'][] = "page_namespace $nsComparison";
139 $conds['imagelinks'][] = "page_namespace $nsComparison";
140 }
141 }
142
143 if ( $from ) {
144 $conds['templatelinks'][] = "tl_from >= $from";
145 $conds['pagelinks'][] = "pl_from >= $from";
146 $conds['imagelinks'][] = "il_from >= $from";
147 }
148
149 if ( $hideredirs ) {
150 $conds['pagelinks']['rd_from'] = null;
151 } elseif ( $hidelinks ) {
152 $conds['pagelinks'][] = 'rd_from is NOT NULL';
153 }
154
155 $queryFunc = function ( $dbr, $table, $fromCol ) use (
156 $conds, $target, $limit, $useLinkNamespaceDBFields
157 ) {
158 // Read an extra row as an at-end check
159 $queryLimit = $limit + 1;
160 $on = array(
161 "rd_from = $fromCol",
162 'rd_title' => $target->getDBkey(),
163 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'
164 );
165 if ( $useLinkNamespaceDBFields ) { // migration check
166 $on['rd_namespace'] = $target->getNamespace();
167 }
168 // Inner LIMIT is 2X in case of stale backlinks with wrong namespaces
169 $subQuery = $dbr->selectSqlText(
170 array( $table, 'page', 'redirect' ),
171 array( $fromCol, 'rd_from' ),
172 $conds[$table],
173 __CLASS__ . '::showIndirectLinks',
174 array( 'ORDER BY' => $fromCol, 'LIMIT' => 2 * $queryLimit ),
175 array(
176 'page' => array( 'INNER JOIN', "$fromCol = page_id" ),
177 'redirect' => array( 'LEFT JOIN', $on )
178 )
179 );
180 return $dbr->select(
181 array( 'page', 'temp_backlink_range' => "($subQuery)" ),
182 array( 'page_id', 'page_namespace', 'page_title', 'rd_from', 'page_is_redirect' ),
183 array(),
184 __CLASS__ . '::showIndirectLinks',
185 array( 'ORDER BY' => 'page_id', 'LIMIT' => $queryLimit ),
186 array( 'page' => array( 'INNER JOIN', "$fromCol = page_id" ) )
187 );
188 };
189
190 if ( $fetchlinks ) {
191 $plRes = $queryFunc( $dbr, 'pagelinks', 'pl_from' );
192 }
193
194 if ( !$hidetrans ) {
195 $tlRes = $queryFunc( $dbr, 'templatelinks', 'tl_from' );
196 }
197
198 if ( !$hideimages ) {
199 $ilRes = $queryFunc( $dbr, 'imagelinks', 'il_from' );
200 }
201
202 if ( ( !$fetchlinks || !$plRes->numRows() )
203 && ( $hidetrans || !$tlRes->numRows() )
204 && ( $hideimages || !$ilRes->numRows() )
205 ) {
206 if ( 0 == $level ) {
207 if ( !$this->including() ) {
208 $out->addHTML( $this->whatlinkshereForm() );
209
210 // Show filters only if there are links
211 if ( $hidelinks || $hidetrans || $hideredirs || $hideimages ) {
212 $out->addHTML( $this->getFilterPanel() );
213 }
214 $errMsg = is_int( $namespace ) ? 'nolinkshere-ns' : 'nolinkshere';
215 $out->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
216 $out->setStatusCode( 404 );
217 }
218 }
219
220 return;
221 }
222
223 // Read the rows into an array and remove duplicates
224 // templatelinks comes second so that the templatelinks row overwrites the
225 // pagelinks row, so we get (inclusion) rather than nothing
226 if ( $fetchlinks ) {
227 foreach ( $plRes as $row ) {
228 $row->is_template = 0;
229 $row->is_image = 0;
230 $rows[$row->page_id] = $row;
231 }
232 }
233 if ( !$hidetrans ) {
234 foreach ( $tlRes as $row ) {
235 $row->is_template = 1;
236 $row->is_image = 0;
237 $rows[$row->page_id] = $row;
238 }
239 }
240 if ( !$hideimages ) {
241 foreach ( $ilRes as $row ) {
242 $row->is_template = 0;
243 $row->is_image = 1;
244 $rows[$row->page_id] = $row;
245 }
246 }
247
248 // Sort by key and then change the keys to 0-based indices
249 ksort( $rows );
250 $rows = array_values( $rows );
251
252 $numRows = count( $rows );
253
254 // Work out the start and end IDs, for prev/next links
255 if ( $numRows > $limit ) {
256 // More rows available after these ones
257 // Get the ID from the last row in the result set
258 $nextId = $rows[$limit]->page_id;
259 // Remove undisplayed rows
260 $rows = array_slice( $rows, 0, $limit );
261 } else {
262 // No more rows after
263 $nextId = false;
264 }
265 $prevId = $from;
266
267 if ( $level == 0 ) {
268 if ( !$this->including() ) {
269 $out->addHTML( $this->whatlinkshereForm() );
270 $out->addHTML( $this->getFilterPanel() );
271 $out->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
272
273 $prevnext = $this->getPrevNext( $prevId, $nextId );
274 $out->addHTML( $prevnext );
275 }
276 }
277 $out->addHTML( $this->listStart( $level ) );
278 foreach ( $rows as $row ) {
279 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
280
281 if ( $row->rd_from && $level < 2 ) {
282 $out->addHTML( $this->listItem( $row, $nt, $target, true ) );
283 $this->showIndirectLinks(
284 $level + 1,
285 $nt,
286 $this->getConfig()->get( 'MaxRedirectLinksRetrieved' )
287 );
288 $out->addHTML( Xml::closeElement( 'li' ) );
289 } else {
290 $out->addHTML( $this->listItem( $row, $nt, $target ) );
291 }
292 }
293
294 $out->addHTML( $this->listEnd() );
295
296 if ( $level == 0 ) {
297 if ( !$this->including() ) {
298 $out->addHTML( $prevnext );
299 }
300 }
301 }
302
303 protected function listStart( $level ) {
304 return Xml::openElement( 'ul', ( $level ? array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
305 }
306
307 protected function listItem( $row, $nt, $target, $notClose = false ) {
308 $dirmark = $this->getLanguage()->getDirMark();
309
310 # local message cache
311 static $msgcache = null;
312 if ( $msgcache === null ) {
313 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
314 'whatlinkshere-links', 'isimage' );
315 $msgcache = array();
316 foreach ( $msgs as $msg ) {
317 $msgcache[$msg] = $this->msg( $msg )->escaped();
318 }
319 }
320
321 if ( $row->rd_from ) {
322 $query = array( 'redirect' => 'no' );
323 } else {
324 $query = array();
325 }
326
327 $link = Linker::linkKnown(
328 $nt,
329 null,
330 $row->page_is_redirect ? array( 'class' => 'mw-redirect' ) : array(),
331 $query
332 );
333
334 // Display properties (redirect or template)
335 $propsText = '';
336 $props = array();
337 if ( $row->rd_from ) {
338 $props[] = $msgcache['isredirect'];
339 }
340 if ( $row->is_template ) {
341 $props[] = $msgcache['istemplate'];
342 }
343 if ( $row->is_image ) {
344 $props[] = $msgcache['isimage'];
345 }
346
347 Hooks::run( 'WhatLinksHereProps', array( $row, $nt, $target, &$props ) );
348
349 if ( count( $props ) ) {
350 $propsText = $this->msg( 'parentheses' )
351 ->rawParams( implode( $msgcache['semicolon-separator'], $props ) )->escaped();
352 }
353
354 # Space for utilities links, with a what-links-here link provided
355 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
356 $wlh = Xml::wrapClass(
357 $this->msg( 'parentheses' )->rawParams( $wlhLink )->escaped(),
358 'mw-whatlinkshere-tools'
359 );
360
361 return $notClose ?
362 Xml::openElement( 'li' ) . "$link $propsText $dirmark $wlh\n" :
363 Xml::tags( 'li', null, "$link $propsText $dirmark $wlh" ) . "\n";
364 }
365
366 protected function listEnd() {
367 return Xml::closeElement( 'ul' );
368 }
369
370 protected function wlhLink( Title $target, $text ) {
371 static $title = null;
372 if ( $title === null ) {
373 $title = $this->getPageTitle();
374 }
375
376 return Linker::linkKnown(
377 $title,
378 $text,
379 array(),
380 array( 'target' => $target->getPrefixedText() )
381 );
382 }
383
384 function makeSelfLink( $text, $query ) {
385 return Linker::linkKnown(
386 $this->selfTitle,
387 $text,
388 array(),
389 $query
390 );
391 }
392
393 function getPrevNext( $prevId, $nextId ) {
394 $currentLimit = $this->opts->getValue( 'limit' );
395 $prev = $this->msg( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
396 $next = $this->msg( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
397
398 $changed = $this->opts->getChangedValues();
399 unset( $changed['target'] ); // Already in the request title
400
401 if ( 0 != $prevId ) {
402 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
403 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
404 }
405 if ( 0 != $nextId ) {
406 $overrides = array( 'from' => $nextId, 'back' => $prevId );
407 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
408 }
409
410 $limitLinks = array();
411 $lang = $this->getLanguage();
412 foreach ( $this->limits as $limit ) {
413 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
414 $overrides = array( 'limit' => $limit );
415 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
416 }
417
418 $nums = $lang->pipeList( $limitLinks );
419
420 return $this->msg( 'viewprevnext' )->rawParams( $prev, $next, $nums )->escaped();
421 }
422
423 function whatlinkshereForm() {
424 // We get nicer value from the title object
425 $this->opts->consumeValue( 'target' );
426 // Reset these for new requests
427 $this->opts->consumeValues( array( 'back', 'from' ) );
428
429 $target = $this->target ? $this->target->getPrefixedText() : '';
430 $namespace = $this->opts->consumeValue( 'namespace' );
431 $nsinvert = $this->opts->consumeValue( 'invert' );
432
433 # Build up the form
434 $f = Xml::openElement( 'form', array( 'action' => wfScript() ) );
435
436 # Values that should not be forgotten
437 $f .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
438 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
439 $f .= Html::hidden( $name, $value );
440 }
441
442 $f .= Xml::fieldset( $this->msg( 'whatlinkshere' )->text() );
443
444 # Target input (.mw-searchInput enables suggestions)
445 $f .= Xml::inputLabel( $this->msg( 'whatlinkshere-page' )->text(), 'target',
446 'mw-whatlinkshere-target', 40, $target, array( 'class' => 'mw-searchInput' ) );
447
448 $f .= ' ';
449
450 # Namespace selector
451 $f .= Html::namespaceSelector(
452 array(
453 'selected' => $namespace,
454 'all' => '',
455 'label' => $this->msg( 'namespace' )->text()
456 ), array(
457 'name' => 'namespace',
458 'id' => 'namespace',
459 'class' => 'namespaceselector',
460 )
461 );
462
463 $f .= '&#160;' .
464 Xml::checkLabel(
465 $this->msg( 'invert' )->text(),
466 'invert',
467 'nsinvert',
468 $nsinvert,
469 array( 'title' => $this->msg( 'tooltip-whatlinkshere-invert' )->text() )
470 );
471
472 $f .= ' ';
473
474 # Submit
475 $f .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
476
477 # Close
478 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
479
480 return $f;
481 }
482
483 /**
484 * Create filter panel
485 *
486 * @return string HTML fieldset and filter panel with the show/hide links
487 */
488 function getFilterPanel() {
489 $show = $this->msg( 'show' )->escaped();
490 $hide = $this->msg( 'hide' )->escaped();
491
492 $changed = $this->opts->getChangedValues();
493 unset( $changed['target'] ); // Already in the request title
494
495 $links = array();
496 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
497 if ( $this->target->getNamespace() == NS_FILE ) {
498 $types[] = 'hideimages';
499 }
500
501 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans',
502 // 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
503 // To be sure they will be found by grep
504 foreach ( $types as $type ) {
505 $chosen = $this->opts->getValue( $type );
506 $msg = $chosen ? $show : $hide;
507 $overrides = array( $type => !$chosen );
508 $links[] = $this->msg( "whatlinkshere-{$type}" )->rawParams(
509 $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) )->escaped();
510 }
511
512 return Xml::fieldset(
513 $this->msg( 'whatlinkshere-filters' )->text(),
514 $this->getLanguage()->pipeList( $links )
515 );
516 }
517
518 /**
519 * Return an array of subpages beginning with $search that this special page will accept.
520 *
521 * @param string $search Prefix to search for
522 * @param int $limit Maximum number of results to return (usually 10)
523 * @param int $offset Number of results to skip (usually 0)
524 * @return string[] Matching subpages
525 */
526 public function prefixSearchSubpages( $search, $limit, $offset ) {
527 if ( $search === '' ) {
528 return array();
529 }
530 // Autocomplete subpage the same as a normal search
531 $prefixSearcher = new StringPrefixSearch;
532 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
533 return $result;
534 }
535
536 protected function getGroupName() {
537 return 'pagetools';
538 }
539 }