Merge "Use 1 processes instead of 4 for phan"
[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 = [ 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::newFromText( $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_REPLICA );
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'] = [
119 'pl_namespace' => $target->getNamespace(),
120 'pl_title' => $target->getDBkey(),
121 ];
122 $conds['templatelinks'] = [
123 'tl_namespace' => $target->getNamespace(),
124 'tl_title' => $target->getDBkey(),
125 ];
126 $conds['imagelinks'] = [
127 'il_to' => $target->getDBkey(),
128 ];
129
130 $namespace = $this->opts->getValue( 'namespace' );
131 $invert = $this->opts->getValue( 'invert' );
132 $nsComparison = ( $invert ? '!= ' : '= ' ) . $dbr->addQuotes( $namespace );
133 if ( is_int( $namespace ) ) {
134 $conds['pagelinks'][] = "pl_from_namespace $nsComparison";
135 $conds['templatelinks'][] = "tl_from_namespace $nsComparison";
136 $conds['imagelinks'][] = "il_from_namespace $nsComparison";
137 }
138
139 if ( $from ) {
140 $conds['templatelinks'][] = "tl_from >= $from";
141 $conds['pagelinks'][] = "pl_from >= $from";
142 $conds['imagelinks'][] = "il_from >= $from";
143 }
144
145 if ( $hideredirs ) {
146 $conds['pagelinks']['rd_from'] = null;
147 } elseif ( $hidelinks ) {
148 $conds['pagelinks'][] = 'rd_from is NOT NULL';
149 }
150
151 $queryFunc = function ( IDatabase $dbr, $table, $fromCol ) use (
152 $conds, $target, $limit
153 ) {
154 // Read an extra row as an at-end check
155 $queryLimit = $limit + 1;
156 $on = [
157 "rd_from = $fromCol",
158 'rd_title' => $target->getDBkey(),
159 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'
160 ];
161 $on['rd_namespace'] = $target->getNamespace();
162 // Inner LIMIT is 2X in case of stale backlinks with wrong namespaces
163 $subQuery = $dbr->selectSQLText(
164 [ $table, 'redirect', 'page' ],
165 [ $fromCol, 'rd_from' ],
166 $conds[$table],
167 __CLASS__ . '::showIndirectLinks',
168 // Force JOIN order per T106682 to avoid large filesorts
169 [ 'ORDER BY' => $fromCol, 'LIMIT' => 2 * $queryLimit, 'STRAIGHT_JOIN' ],
170 [
171 'page' => [ 'INNER JOIN', "$fromCol = page_id" ],
172 'redirect' => [ 'LEFT JOIN', $on ]
173 ]
174 );
175 return $dbr->select(
176 [ 'page', 'temp_backlink_range' => "($subQuery)" ],
177 [ 'page_id', 'page_namespace', 'page_title', 'rd_from', 'page_is_redirect' ],
178 [],
179 __CLASS__ . '::showIndirectLinks',
180 [ 'ORDER BY' => 'page_id', 'LIMIT' => $queryLimit ],
181 [ 'page' => [ 'INNER JOIN', "$fromCol = page_id" ] ]
182 );
183 };
184
185 if ( $fetchlinks ) {
186 $plRes = $queryFunc( $dbr, 'pagelinks', 'pl_from' );
187 }
188
189 if ( !$hidetrans ) {
190 $tlRes = $queryFunc( $dbr, 'templatelinks', 'tl_from' );
191 }
192
193 if ( !$hideimages ) {
194 $ilRes = $queryFunc( $dbr, 'imagelinks', 'il_from' );
195 }
196
197 if ( ( !$fetchlinks || !$plRes->numRows() )
198 && ( $hidetrans || !$tlRes->numRows() )
199 && ( $hideimages || !$ilRes->numRows() )
200 ) {
201 if ( 0 == $level ) {
202 if ( !$this->including() ) {
203 $out->addHTML( $this->whatlinkshereForm() );
204
205 // Show filters only if there are links
206 if ( $hidelinks || $hidetrans || $hideredirs || $hideimages ) {
207 $out->addHTML( $this->getFilterPanel() );
208 }
209 $errMsg = is_int( $namespace ) ? 'nolinkshere-ns' : 'nolinkshere';
210 $out->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
211 $out->setStatusCode( 404 );
212 }
213 }
214
215 return;
216 }
217
218 // Read the rows into an array and remove duplicates
219 // templatelinks comes second so that the templatelinks row overwrites the
220 // pagelinks row, so we get (inclusion) rather than nothing
221 if ( $fetchlinks ) {
222 foreach ( $plRes as $row ) {
223 $row->is_template = 0;
224 $row->is_image = 0;
225 $rows[$row->page_id] = $row;
226 }
227 }
228 if ( !$hidetrans ) {
229 foreach ( $tlRes as $row ) {
230 $row->is_template = 1;
231 $row->is_image = 0;
232 $rows[$row->page_id] = $row;
233 }
234 }
235 if ( !$hideimages ) {
236 foreach ( $ilRes as $row ) {
237 $row->is_template = 0;
238 $row->is_image = 1;
239 $rows[$row->page_id] = $row;
240 }
241 }
242
243 // Sort by key and then change the keys to 0-based indices
244 ksort( $rows );
245 $rows = array_values( $rows );
246
247 $numRows = count( $rows );
248
249 // Work out the start and end IDs, for prev/next links
250 if ( $numRows > $limit ) {
251 // More rows available after these ones
252 // Get the ID from the last row in the result set
253 $nextId = $rows[$limit]->page_id;
254 // Remove undisplayed rows
255 $rows = array_slice( $rows, 0, $limit );
256 } else {
257 // No more rows after
258 $nextId = false;
259 }
260 $prevId = $from;
261
262 // use LinkBatch to make sure, that all required data (associated with Titles)
263 // is loaded in one query
264 $lb = new LinkBatch();
265 foreach ( $rows as $row ) {
266 $lb->add( $row->page_namespace, $row->page_title );
267 }
268 $lb->execute();
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 ? [] : [ '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 = [ 'isredirect', 'istemplate', 'semicolon-separator',
317 'whatlinkshere-links', 'isimage', 'editlink' ];
318 $msgcache = [];
319 foreach ( $msgs as $msg ) {
320 $msgcache[$msg] = $this->msg( $msg )->escaped();
321 }
322 }
323
324 if ( $row->rd_from ) {
325 $query = [ 'redirect' => 'no' ];
326 } else {
327 $query = [];
328 }
329
330 $link = $this->getLinkRenderer()->makeKnownLink(
331 $nt,
332 null,
333 $row->page_is_redirect ? [ 'class' => 'mw-redirect' ] : [],
334 $query
335 );
336
337 // Display properties (redirect or template)
338 $propsText = '';
339 $props = [];
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', [ $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'], $msgcache['editlink'] );
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, $editText ) {
374 static $title = null;
375 if ( $title === null ) {
376 $title = $this->getPageTitle();
377 }
378
379 $linkRenderer = $this->getLinkRenderer();
380
381 // always show a "<- Links" link
382 $links = [
383 'links' => $linkRenderer->makeKnownLink(
384 $title,
385 new HtmlArmor( $text ),
386 [],
387 [ 'target' => $target->getPrefixedText() ]
388 ),
389 ];
390
391 // if the page is editable, add an edit link
392 if (
393 // check user permissions
394 $this->getUser()->isAllowed( 'edit' ) &&
395 // check, if the content model is editable through action=edit
396 ContentHandler::getForTitle( $target )->supportsDirectEditing()
397 ) {
398 $links['edit'] = $linkRenderer->makeKnownLink(
399 $target,
400 new HtmlArmor( $editText ),
401 [],
402 [ 'action' => 'edit' ]
403 );
404 }
405
406 // build the links html
407 return $this->getLanguage()->pipeList( $links );
408 }
409
410 function makeSelfLink( $text, $query ) {
411 return $this->getLinkRenderer()->makeKnownLink(
412 $this->selfTitle,
413 new HtmlArmor( $text ),
414 [],
415 $query
416 );
417 }
418
419 function getPrevNext( $prevId, $nextId ) {
420 $currentLimit = $this->opts->getValue( 'limit' );
421 $prev = $this->msg( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
422 $next = $this->msg( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
423
424 $changed = $this->opts->getChangedValues();
425 unset( $changed['target'] ); // Already in the request title
426
427 if ( 0 != $prevId ) {
428 $overrides = [ 'from' => $this->opts->getValue( 'back' ) ];
429 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
430 }
431 if ( 0 != $nextId ) {
432 $overrides = [ 'from' => $nextId, 'back' => $prevId ];
433 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
434 }
435
436 $limitLinks = [];
437 $lang = $this->getLanguage();
438 foreach ( $this->limits as $limit ) {
439 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
440 $overrides = [ 'limit' => $limit ];
441 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
442 }
443
444 $nums = $lang->pipeList( $limitLinks );
445
446 return $this->msg( 'viewprevnext' )->rawParams( $prev, $next, $nums )->escaped();
447 }
448
449 function whatlinkshereForm() {
450 // We get nicer value from the title object
451 $this->opts->consumeValue( 'target' );
452 // Reset these for new requests
453 $this->opts->consumeValues( [ 'back', 'from' ] );
454
455 $target = $this->target ? $this->target->getPrefixedText() : '';
456 $namespace = $this->opts->consumeValue( 'namespace' );
457 $nsinvert = $this->opts->consumeValue( 'invert' );
458
459 # Build up the form
460 $f = Xml::openElement( 'form', [ 'action' => wfScript() ] );
461
462 # Values that should not be forgotten
463 $f .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
464 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
465 $f .= Html::hidden( $name, $value );
466 }
467
468 $f .= Xml::fieldset( $this->msg( 'whatlinkshere' )->text() );
469
470 # Target input (.mw-searchInput enables suggestions)
471 $f .= Xml::inputLabel( $this->msg( 'whatlinkshere-page' )->text(), 'target',
472 'mw-whatlinkshere-target', 40, $target, [ 'class' => 'mw-searchInput' ] );
473
474 $f .= ' ';
475
476 # Namespace selector
477 $f .= Html::namespaceSelector(
478 [
479 'selected' => $namespace,
480 'all' => '',
481 'label' => $this->msg( 'namespace' )->text()
482 ], [
483 'name' => 'namespace',
484 'id' => 'namespace',
485 'class' => 'namespaceselector',
486 ]
487 );
488
489 $f .= '&#160;' .
490 Xml::checkLabel(
491 $this->msg( 'invert' )->text(),
492 'invert',
493 'nsinvert',
494 $nsinvert,
495 [ 'title' => $this->msg( 'tooltip-whatlinkshere-invert' )->text() ]
496 );
497
498 $f .= ' ';
499
500 # Submit
501 $f .= Xml::submitButton( $this->msg( 'whatlinkshere-submit' )->text() );
502
503 # Close
504 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
505
506 return $f;
507 }
508
509 /**
510 * Create filter panel
511 *
512 * @return string HTML fieldset and filter panel with the show/hide links
513 */
514 function getFilterPanel() {
515 $show = $this->msg( 'show' )->escaped();
516 $hide = $this->msg( 'hide' )->escaped();
517
518 $changed = $this->opts->getChangedValues();
519 unset( $changed['target'] ); // Already in the request title
520
521 $links = [];
522 $types = [ 'hidetrans', 'hidelinks', 'hideredirs' ];
523 if ( $this->target->getNamespace() == NS_FILE ) {
524 $types[] = 'hideimages';
525 }
526
527 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans',
528 // 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
529 // To be sure they will be found by grep
530 foreach ( $types as $type ) {
531 $chosen = $this->opts->getValue( $type );
532 $msg = $chosen ? $show : $hide;
533 $overrides = [ $type => !$chosen ];
534 $links[] = $this->msg( "whatlinkshere-{$type}" )->rawParams(
535 $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) )->escaped();
536 }
537
538 return Xml::fieldset(
539 $this->msg( 'whatlinkshere-filters' )->text(),
540 $this->getLanguage()->pipeList( $links )
541 );
542 }
543
544 /**
545 * Return an array of subpages beginning with $search that this special page will accept.
546 *
547 * @param string $search Prefix to search for
548 * @param int $limit Maximum number of results to return (usually 10)
549 * @param int $offset Number of results to skip (usually 0)
550 * @return string[] Matching subpages
551 */
552 public function prefixSearchSubpages( $search, $limit, $offset ) {
553 return $this->prefixSearchString( $search, $limit, $offset );
554 }
555
556 protected function getGroupName() {
557 return 'pagetools';
558 }
559 }