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