Merge "Error Msg for missing db username & password when installing"
[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 ( isset( $par ) ) {
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, true ) );
269 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
270 $out->addHTML( Xml::closeElement( 'li' ) );
271 } else {
272 $out->addHTML( $this->listItem( $row, $nt ) );
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, $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 if ( count( $props ) ) {
330 $propsText = $this->msg( 'parentheses' )
331 ->rawParams( implode( $msgcache['semicolon-separator'], $props ) )->escaped();
332 }
333
334 # Space for utilities links, with a what-links-here link provided
335 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
336 $wlh = Xml::wrapClass(
337 $this->msg( 'parentheses' )->rawParams( $wlhLink )->escaped(),
338 'mw-whatlinkshere-tools'
339 );
340
341 return $notClose ?
342 Xml::openElement( 'li' ) . "$link $propsText $dirmark $wlh\n" :
343 Xml::tags( 'li', null, "$link $propsText $dirmark $wlh" ) . "\n";
344 }
345
346 protected function listEnd() {
347 return Xml::closeElement( 'ul' );
348 }
349
350 protected function wlhLink( Title $target, $text ) {
351 static $title = null;
352 if ( $title === null ) {
353 $title = $this->getPageTitle();
354 }
355
356 return Linker::linkKnown(
357 $title,
358 $text,
359 array(),
360 array( 'target' => $target->getPrefixedText() )
361 );
362 }
363
364 function makeSelfLink( $text, $query ) {
365 return Linker::linkKnown(
366 $this->selfTitle,
367 $text,
368 array(),
369 $query
370 );
371 }
372
373 function getPrevNext( $prevId, $nextId ) {
374 $currentLimit = $this->opts->getValue( 'limit' );
375 $prev = $this->msg( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
376 $next = $this->msg( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
377
378 $changed = $this->opts->getChangedValues();
379 unset( $changed['target'] ); // Already in the request title
380
381 if ( 0 != $prevId ) {
382 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
383 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
384 }
385 if ( 0 != $nextId ) {
386 $overrides = array( 'from' => $nextId, 'back' => $prevId );
387 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
388 }
389
390 $limitLinks = array();
391 $lang = $this->getLanguage();
392 foreach ( $this->limits as $limit ) {
393 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
394 $overrides = array( 'limit' => $limit );
395 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
396 }
397
398 $nums = $lang->pipeList( $limitLinks );
399
400 return $this->msg( 'viewprevnext' )->rawParams( $prev, $next, $nums )->escaped();
401 }
402
403 function whatlinkshereForm() {
404 global $wgScript;
405
406 // We get nicer value from the title object
407 $this->opts->consumeValue( 'target' );
408 // Reset these for new requests
409 $this->opts->consumeValues( array( 'back', 'from' ) );
410
411 $target = $this->target ? $this->target->getPrefixedText() : '';
412 $namespace = $this->opts->consumeValue( 'namespace' );
413
414 # Build up the form
415 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
416
417 # Values that should not be forgotten
418 $f .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
419 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
420 $f .= Html::hidden( $name, $value );
421 }
422
423 $f .= Xml::fieldset( $this->msg( 'whatlinkshere' )->text() );
424
425 # Target input
426 $f .= Xml::inputLabel( $this->msg( 'whatlinkshere-page' )->text(), 'target',
427 'mw-whatlinkshere-target', 40, $target );
428
429 $f .= ' ';
430
431 # Namespace selector
432 $f .= Html::namespaceSelector(
433 array(
434 'selected' => $namespace,
435 'all' => '',
436 'label' => $this->msg( 'namespace' )->text()
437 ), array(
438 'name' => 'namespace',
439 'id' => 'namespace',
440 'class' => 'namespaceselector',
441 )
442 );
443
444 $f .= ' ';
445
446 # Submit
447 $f .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
448
449 # Close
450 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
451
452 return $f;
453 }
454
455 /**
456 * Create filter panel
457 *
458 * @return string HTML fieldset and filter panel with the show/hide links
459 */
460 function getFilterPanel() {
461 $show = $this->msg( 'show' )->escaped();
462 $hide = $this->msg( 'hide' )->escaped();
463
464 $changed = $this->opts->getChangedValues();
465 unset( $changed['target'] ); // Already in the request title
466
467 $links = array();
468 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
469 if ( $this->target->getNamespace() == NS_FILE ) {
470 $types[] = 'hideimages';
471 }
472
473 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans',
474 // 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
475 // To be sure they will be found by grep
476 foreach ( $types as $type ) {
477 $chosen = $this->opts->getValue( $type );
478 $msg = $chosen ? $show : $hide;
479 $overrides = array( $type => !$chosen );
480 $links[] = $this->msg( "whatlinkshere-{$type}" )->rawParams(
481 $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) )->escaped();
482 }
483
484 return Xml::fieldset(
485 $this->msg( 'whatlinkshere-filters' )->text(),
486 $this->getLanguage()->pipeList( $links )
487 );
488 }
489
490 protected function getGroupName() {
491 return 'pagetools';
492 }
493 }