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