Merge "Make MySQLi work with non-standard port"
[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->getPageTitle( $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 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() ) && ( $hidetrans || !$tlRes->numRows() ) && ( $hideimages || !$ilRes->numRows() ) ) {
190 if ( 0 == $level ) {
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
198 $errMsg = is_int( $namespace ) ? 'nolinkshere-ns' : 'nolinkshere';
199 $out->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
200 }
201 return;
202 }
203
204 // Read the rows into an array and remove duplicates
205 // templatelinks comes second so that the templatelinks row overwrites the
206 // pagelinks row, so we get (inclusion) rather than nothing
207 if ( $fetchlinks ) {
208 foreach ( $plRes as $row ) {
209 $row->is_template = 0;
210 $row->is_image = 0;
211 $rows[$row->page_id] = $row;
212 }
213 }
214 if ( !$hidetrans ) {
215 foreach ( $tlRes as $row ) {
216 $row->is_template = 1;
217 $row->is_image = 0;
218 $rows[$row->page_id] = $row;
219 }
220 }
221 if ( !$hideimages ) {
222 foreach ( $ilRes as $row ) {
223 $row->is_template = 0;
224 $row->is_image = 1;
225 $rows[$row->page_id] = $row;
226 }
227 }
228
229 // Sort by key and then change the keys to 0-based indices
230 ksort( $rows );
231 $rows = array_values( $rows );
232
233 $numRows = count( $rows );
234
235 // Work out the start and end IDs, for prev/next links
236 if ( $numRows > $limit ) {
237 // More rows available after these ones
238 // Get the ID from the last row in the result set
239 $nextId = $rows[$limit]->page_id;
240 // Remove undisplayed rows
241 $rows = array_slice( $rows, 0, $limit );
242 } else {
243 // No more rows after
244 $nextId = false;
245 }
246 $prevId = $from;
247
248 if ( $level == 0 ) {
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 $out->addHTML( $prevnext );
274 }
275 }
276
277 protected function listStart( $level ) {
278 return Xml::openElement( 'ul', ( $level ? array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
279 }
280
281 protected function listItem( $row, $nt, $notClose = false ) {
282 $dirmark = $this->getLanguage()->getDirMark();
283
284 # local message cache
285 static $msgcache = null;
286 if ( $msgcache === null ) {
287 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
288 'whatlinkshere-links', 'isimage' );
289 $msgcache = array();
290 foreach ( $msgs as $msg ) {
291 $msgcache[$msg] = $this->msg( $msg )->escaped();
292 }
293 }
294
295 if ( $row->rd_from ) {
296 $query = array( 'redirect' => 'no' );
297 } else {
298 $query = array();
299 }
300
301 $link = Linker::linkKnown(
302 $nt,
303 null,
304 array(),
305 $query
306 );
307
308 // Display properties (redirect or template)
309 $propsText = '';
310 $props = array();
311 if ( $row->rd_from ) {
312 $props[] = $msgcache['isredirect'];
313 }
314 if ( $row->is_template ) {
315 $props[] = $msgcache['istemplate'];
316 }
317 if ( $row->is_image ) {
318 $props[] = $msgcache['isimage'];
319 }
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->getPageTitle();
342 }
343
344 return Linker::linkKnown(
345 $title,
346 $text,
347 array(),
348 array( 'target' => $target->getPrefixedText() )
349 );
350 }
351
352 function makeSelfLink( $text, $query ) {
353 return Linker::linkKnown(
354 $this->selfTitle,
355 $text,
356 array(),
357 $query
358 );
359 }
360
361 function getPrevNext( $prevId, $nextId ) {
362 $currentLimit = $this->opts->getValue( 'limit' );
363 $prev = $this->msg( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
364 $next = $this->msg( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
365
366 $changed = $this->opts->getChangedValues();
367 unset( $changed['target'] ); // Already in the request title
368
369 if ( 0 != $prevId ) {
370 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
371 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
372 }
373 if ( 0 != $nextId ) {
374 $overrides = array( 'from' => $nextId, 'back' => $prevId );
375 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
376 }
377
378 $limitLinks = array();
379 $lang = $this->getLanguage();
380 foreach ( $this->limits as $limit ) {
381 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
382 $overrides = array( 'limit' => $limit );
383 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
384 }
385
386 $nums = $lang->pipeList( $limitLinks );
387
388 return $this->msg( 'viewprevnext' )->rawParams( $prev, $next, $nums )->escaped();
389 }
390
391 function whatlinkshereForm() {
392 global $wgScript;
393
394 // We get nicer value from the title object
395 $this->opts->consumeValue( 'target' );
396 // Reset these for new requests
397 $this->opts->consumeValues( array( 'back', 'from' ) );
398
399 $target = $this->target ? $this->target->getPrefixedText() : '';
400 $namespace = $this->opts->consumeValue( 'namespace' );
401
402 # Build up the form
403 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
404
405 # Values that should not be forgotten
406 $f .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
407 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
408 $f .= Html::hidden( $name, $value );
409 }
410
411 $f .= Xml::fieldset( $this->msg( 'whatlinkshere' )->text() );
412
413 # Target input
414 $f .= Xml::inputLabel( $this->msg( 'whatlinkshere-page' )->text(), 'target',
415 'mw-whatlinkshere-target', 40, $target );
416
417 $f .= ' ';
418
419 # Namespace selector
420 $f .= Html::namespaceSelector(
421 array(
422 'selected' => $namespace,
423 'all' => '',
424 'label' => $this->msg( 'namespace' )->text()
425 ), array(
426 'name' => 'namespace',
427 'id' => 'namespace',
428 'class' => 'namespaceselector',
429 )
430 );
431
432 $f .= ' ';
433
434 # Submit
435 $f .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
436
437 # Close
438 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
439
440 return $f;
441 }
442
443 /**
444 * Create filter panel
445 *
446 * @return string HTML fieldset and filter panel with the show/hide links
447 */
448 function getFilterPanel() {
449 $show = $this->msg( 'show' )->escaped();
450 $hide = $this->msg( 'hide' )->escaped();
451
452 $changed = $this->opts->getChangedValues();
453 unset( $changed['target'] ); // Already in the request title
454
455 $links = array();
456 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
457 if ( $this->target->getNamespace() == NS_FILE ) {
458 $types[] = 'hideimages';
459 }
460
461 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
462 // To be sure they will be found by grep
463 foreach ( $types as $type ) {
464 $chosen = $this->opts->getValue( $type );
465 $msg = $chosen ? $show : $hide;
466 $overrides = array( $type => !$chosen );
467 $links[] = $this->msg( "whatlinkshere-{$type}" )->rawParams(
468 $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) )->escaped();
469 }
470 return Xml::fieldset( $this->msg( 'whatlinkshere-filters' )->text(), $this->getLanguage()->pipeList( $links ) );
471 }
472
473 protected function getGroupName() {
474 return 'pagetools';
475 }
476 }