Merge "(bug 56849) Deprecate dangerous edittime-based content update functions"
[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 global $wgQueryPageDefaultLimit;
46 $out = $this->getOutput();
47
48 $this->setHeaders();
49 $this->outputHeader();
50
51 $opts = new FormOptions();
52
53 $opts->add( 'target', '' );
54 $opts->add( 'namespace', '', FormOptions::INTNULL );
55 $opts->add( 'limit', $wgQueryPageDefaultLimit );
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
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 global $wgMaxRedirectLinksRetrieved;
105 $out = $this->getOutput();
106 $dbr = wfGetDB( DB_SLAVE );
107 $options = array();
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 // Make the query
117 $plConds = array(
118 'page_id=pl_from',
119 'pl_namespace' => $target->getNamespace(),
120 'pl_title' => $target->getDBkey(),
121 );
122 if ( $hideredirs ) {
123 $plConds['rd_from'] = null;
124 } elseif ( $hidelinks ) {
125 $plConds[] = 'rd_from is NOT NULL';
126 }
127
128 $tlConds = array(
129 'page_id=tl_from',
130 'tl_namespace' => $target->getNamespace(),
131 'tl_title' => $target->getDBkey(),
132 );
133
134 $ilConds = array(
135 'page_id=il_from',
136 'il_to' => $target->getDBkey(),
137 );
138
139 $namespace = $this->opts->getValue( 'namespace' );
140 if ( is_int( $namespace ) ) {
141 $plConds['page_namespace'] = $namespace;
142 $tlConds['page_namespace'] = $namespace;
143 $ilConds['page_namespace'] = $namespace;
144 }
145
146 if ( $from ) {
147 $tlConds[] = "tl_from >= $from";
148 $plConds[] = "pl_from >= $from";
149 $ilConds[] = "il_from >= $from";
150 }
151
152 // Read an extra row as an at-end check
153 $queryLimit = $limit + 1;
154
155 $options['LIMIT'] = $queryLimit;
156 $fields = array( 'page_id', 'page_namespace', 'page_title', 'rd_from' );
157
158 $joinConds = array( 'redirect' => array( 'LEFT JOIN', array(
159 'rd_from = page_id',
160 'rd_namespace' => $target->getNamespace(),
161 'rd_title' => $target->getDBkey(),
162 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'
163 ) ) );
164
165 if ( $fetchlinks ) {
166 $options['ORDER BY'] = 'pl_from';
167 $plRes = $dbr->select( array( 'pagelinks', 'page', 'redirect' ), $fields,
168 $plConds, __METHOD__, $options,
169 $joinConds
170 );
171 }
172
173 if ( !$hidetrans ) {
174 $options['ORDER BY'] = 'tl_from';
175 $tlRes = $dbr->select( array( 'templatelinks', 'page', 'redirect' ), $fields,
176 $tlConds, __METHOD__, $options,
177 $joinConds
178 );
179 }
180
181 if ( !$hideimages ) {
182 $options['ORDER BY'] = 'il_from';
183 $ilRes = $dbr->select( array( 'imagelinks', 'page', 'redirect' ), $fields,
184 $ilConds, __METHOD__, $options,
185 $joinConds
186 );
187 }
188
189 if ( ( !$fetchlinks || !$plRes->numRows() )
190 && ( $hidetrans || !$tlRes->numRows() )
191 && ( $hideimages || !$ilRes->numRows() )
192 ) {
193 if ( 0 == $level ) {
194 if ( !$this->including() ) {
195 $out->addHTML( $this->whatlinkshereForm() );
196
197 // Show filters only if there are links
198 if ( $hidelinks || $hidetrans || $hideredirs || $hideimages ) {
199 $out->addHTML( $this->getFilterPanel() );
200 }
201 $errMsg = is_int( $namespace ) ? 'nolinkshere-ns' : 'nolinkshere';
202 $out->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
203 }
204 }
205
206 return;
207 }
208
209 // Read the rows into an array and remove duplicates
210 // templatelinks comes second so that the templatelinks row overwrites the
211 // pagelinks row, so we get (inclusion) rather than nothing
212 if ( $fetchlinks ) {
213 foreach ( $plRes as $row ) {
214 $row->is_template = 0;
215 $row->is_image = 0;
216 $rows[$row->page_id] = $row;
217 }
218 }
219 if ( !$hidetrans ) {
220 foreach ( $tlRes as $row ) {
221 $row->is_template = 1;
222 $row->is_image = 0;
223 $rows[$row->page_id] = $row;
224 }
225 }
226 if ( !$hideimages ) {
227 foreach ( $ilRes as $row ) {
228 $row->is_template = 0;
229 $row->is_image = 1;
230 $rows[$row->page_id] = $row;
231 }
232 }
233
234 // Sort by key and then change the keys to 0-based indices
235 ksort( $rows );
236 $rows = array_values( $rows );
237
238 $numRows = count( $rows );
239
240 // Work out the start and end IDs, for prev/next links
241 if ( $numRows > $limit ) {
242 // More rows available after these ones
243 // Get the ID from the last row in the result set
244 $nextId = $rows[$limit]->page_id;
245 // Remove undisplayed rows
246 $rows = array_slice( $rows, 0, $limit );
247 } else {
248 // No more rows after
249 $nextId = false;
250 }
251 $prevId = $from;
252
253 if ( $level == 0 ) {
254 if ( !$this->including() ) {
255 $out->addHTML( $this->whatlinkshereForm() );
256 $out->addHTML( $this->getFilterPanel() );
257 $out->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
258
259 $prevnext = $this->getPrevNext( $prevId, $nextId );
260 $out->addHTML( $prevnext );
261 }
262 }
263 $out->addHTML( $this->listStart( $level ) );
264 foreach ( $rows as $row ) {
265 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
266
267 if ( $row->rd_from && $level < 2 ) {
268 $out->addHTML( $this->listItem( $row, $nt, $target, true ) );
269 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
270 $out->addHTML( Xml::closeElement( 'li' ) );
271 } else {
272 $out->addHTML( $this->listItem( $row, $nt, $target ) );
273 }
274 }
275
276 $out->addHTML( $this->listEnd() );
277
278 if ( $level == 0 ) {
279 if ( !$this->including() ) {
280 $out->addHTML( $prevnext );
281 }
282 }
283 }
284
285 protected function listStart( $level ) {
286 return Xml::openElement( 'ul', ( $level ? array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
287 }
288
289 protected function listItem( $row, $nt, $target, $notClose = false ) {
290 $dirmark = $this->getLanguage()->getDirMark();
291
292 # local message cache
293 static $msgcache = null;
294 if ( $msgcache === null ) {
295 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
296 'whatlinkshere-links', 'isimage' );
297 $msgcache = array();
298 foreach ( $msgs as $msg ) {
299 $msgcache[$msg] = $this->msg( $msg )->escaped();
300 }
301 }
302
303 if ( $row->rd_from ) {
304 $query = array( 'redirect' => 'no' );
305 } else {
306 $query = array();
307 }
308
309 $link = Linker::linkKnown(
310 $nt,
311 null,
312 array(),
313 $query
314 );
315
316 // Display properties (redirect or template)
317 $propsText = '';
318 $props = array();
319 if ( $row->rd_from ) {
320 $props[] = $msgcache['isredirect'];
321 }
322 if ( $row->is_template ) {
323 $props[] = $msgcache['istemplate'];
324 }
325 if ( $row->is_image ) {
326 $props[] = $msgcache['isimage'];
327 }
328
329 wfRunHooks( 'WhatLinksHereProps', array( $row, $nt, $target, &$props ) );
330
331 if ( count( $props ) ) {
332 $propsText = $this->msg( 'parentheses' )
333 ->rawParams( implode( $msgcache['semicolon-separator'], $props ) )->escaped();
334 }
335
336 # Space for utilities links, with a what-links-here link provided
337 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
338 $wlh = Xml::wrapClass(
339 $this->msg( 'parentheses' )->rawParams( $wlhLink )->escaped(),
340 'mw-whatlinkshere-tools'
341 );
342
343 return $notClose ?
344 Xml::openElement( 'li' ) . "$link $propsText $dirmark $wlh\n" :
345 Xml::tags( 'li', null, "$link $propsText $dirmark $wlh" ) . "\n";
346 }
347
348 protected function listEnd() {
349 return Xml::closeElement( 'ul' );
350 }
351
352 protected function wlhLink( Title $target, $text ) {
353 static $title = null;
354 if ( $title === null ) {
355 $title = $this->getPageTitle();
356 }
357
358 return Linker::linkKnown(
359 $title,
360 $text,
361 array(),
362 array( 'target' => $target->getPrefixedText() )
363 );
364 }
365
366 function makeSelfLink( $text, $query ) {
367 return Linker::linkKnown(
368 $this->selfTitle,
369 $text,
370 array(),
371 $query
372 );
373 }
374
375 function getPrevNext( $prevId, $nextId ) {
376 $currentLimit = $this->opts->getValue( 'limit' );
377 $prev = $this->msg( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
378 $next = $this->msg( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
379
380 $changed = $this->opts->getChangedValues();
381 unset( $changed['target'] ); // Already in the request title
382
383 if ( 0 != $prevId ) {
384 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
385 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
386 }
387 if ( 0 != $nextId ) {
388 $overrides = array( 'from' => $nextId, 'back' => $prevId );
389 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
390 }
391
392 $limitLinks = array();
393 $lang = $this->getLanguage();
394 foreach ( $this->limits as $limit ) {
395 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
396 $overrides = array( 'limit' => $limit );
397 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
398 }
399
400 $nums = $lang->pipeList( $limitLinks );
401
402 return $this->msg( 'viewprevnext' )->rawParams( $prev, $next, $nums )->escaped();
403 }
404
405 function whatlinkshereForm() {
406 global $wgScript;
407
408 // We get nicer value from the title object
409 $this->opts->consumeValue( 'target' );
410 // Reset these for new requests
411 $this->opts->consumeValues( array( 'back', 'from' ) );
412
413 $target = $this->target ? $this->target->getPrefixedText() : '';
414 $namespace = $this->opts->consumeValue( 'namespace' );
415
416 # Build up the form
417 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
418
419 # Values that should not be forgotten
420 $f .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
421 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
422 $f .= Html::hidden( $name, $value );
423 }
424
425 $f .= Xml::fieldset( $this->msg( 'whatlinkshere' )->text() );
426
427 # Target input
428 $f .= Xml::inputLabel( $this->msg( 'whatlinkshere-page' )->text(), 'target',
429 'mw-whatlinkshere-target', 40, $target );
430
431 $f .= ' ';
432
433 # Namespace selector
434 $f .= Html::namespaceSelector(
435 array(
436 'selected' => $namespace,
437 'all' => '',
438 'label' => $this->msg( 'namespace' )->text()
439 ), array(
440 'name' => 'namespace',
441 'id' => 'namespace',
442 'class' => 'namespaceselector',
443 )
444 );
445
446 $f .= ' ';
447
448 # Submit
449 $f .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
450
451 # Close
452 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
453
454 return $f;
455 }
456
457 /**
458 * Create filter panel
459 *
460 * @return string HTML fieldset and filter panel with the show/hide links
461 */
462 function getFilterPanel() {
463 $show = $this->msg( 'show' )->escaped();
464 $hide = $this->msg( 'hide' )->escaped();
465
466 $changed = $this->opts->getChangedValues();
467 unset( $changed['target'] ); // Already in the request title
468
469 $links = array();
470 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
471 if ( $this->target->getNamespace() == NS_FILE ) {
472 $types[] = 'hideimages';
473 }
474
475 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans',
476 // 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
477 // To be sure they will be found by grep
478 foreach ( $types as $type ) {
479 $chosen = $this->opts->getValue( $type );
480 $msg = $chosen ? $show : $hide;
481 $overrides = array( $type => !$chosen );
482 $links[] = $this->msg( "whatlinkshere-{$type}" )->rawParams(
483 $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) )->escaped();
484 }
485
486 return Xml::fieldset(
487 $this->msg( 'whatlinkshere-filters' )->text(),
488 $this->getLanguage()->pipeList( $links )
489 );
490 }
491
492 protected function getGroupName() {
493 return 'pagetools';
494 }
495 }