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