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