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