Bug 32673: Keep the username in the input field if not existing
[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
55 $opts = new FormOptions();
56
57 $opts->add( 'target', '' );
58 $opts->add( 'namespace', '', FormOptions::INTNULL );
59 $opts->add( 'limit', $wgQueryPageDefaultLimit );
60 $opts->add( 'from', 0 );
61 $opts->add( 'back', 0 );
62 $opts->add( 'hideredirs', false );
63 $opts->add( 'hidetrans', false );
64 $opts->add( 'hidelinks', false );
65 $opts->add( 'hideimages', false );
66
67 $opts->fetchValuesFromRequest( $this->getRequest() );
68 $opts->validateIntBounds( 'limit', 0, 5000 );
69
70 // Give precedence to subpage syntax
71 if ( isset($par) ) {
72 $opts->setValue( 'target', $par );
73 }
74
75 // Bind to member variable
76 $this->opts = $opts;
77
78 $this->target = Title::newFromURL( $opts->getValue( 'target' ) );
79 if( !$this->target ) {
80 $out->addHTML( $this->whatlinkshereForm() );
81 return;
82 }
83
84 $this->getSkin()->setRelevantTitle( $this->target );
85
86 $this->selfTitle = $this->getTitle( $this->target->getPrefixedDBkey() );
87
88 $out->setPageTitle( $this->msg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
89 $out->addBacklinkSubtitle( $this->target );
90
91 $this->showIndirectLinks( 0, $this->target, $opts->getValue( 'limit' ),
92 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
93 }
94
95 /**
96 * @param $level int Recursion level
97 * @param $target Title Target title
98 * @param $limit int Number of entries to display
99 * @param $from Title Display from this article ID
100 * @param $back Title Display from this article ID at backwards scrolling
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 // Enforce join order, sometimes namespace selector may
155 // trigger filesorts which are far less efficient than scanning many entries
156 $options[] = 'STRAIGHT_JOIN';
157
158 $options['LIMIT'] = $queryLimit;
159 $fields = array( 'page_id', 'page_namespace', 'page_title', 'rd_from' );
160
161 $joinConds = array( 'redirect' => array( 'LEFT JOIN', array(
162 'rd_from = page_id',
163 'rd_namespace' => $target->getNamespace(),
164 'rd_title' => $target->getDBkey(),
165 '(rd_interwiki is NULL) or (rd_interwiki = \'\')'
166 )));
167
168 if( $fetchlinks ) {
169 $options['ORDER BY'] = 'pl_from';
170 $plRes = $dbr->select( array( 'pagelinks', 'page', 'redirect' ), $fields,
171 $plConds, __METHOD__, $options,
172 $joinConds);
173 }
174
175 if( !$hidetrans ) {
176 $options['ORDER BY'] = 'tl_from';
177 $tlRes = $dbr->select( array( 'templatelinks', 'page', 'redirect' ), $fields,
178 $tlConds, __METHOD__, $options,
179 $joinConds);
180 }
181
182 if( !$hideimages ) {
183 $options['ORDER BY'] = 'il_from';
184 $ilRes = $dbr->select( array( 'imagelinks', 'page', 'redirect' ), $fields,
185 $ilConds, __METHOD__, $options,
186 $joinConds);
187 }
188
189 if( ( !$fetchlinks || !$dbr->numRows($plRes) ) && ( $hidetrans || !$dbr->numRows($tlRes) ) && ( $hideimages || !$dbr->numRows($ilRes) ) ) {
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 $errMsg = is_int($namespace) ? 'nolinkshere-ns' : 'nolinkshere';
198 $out->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
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 $out->addHTML( $this->whatlinkshereForm() );
249 $out->addHTML( $this->getFilterPanel() );
250 $out->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
251
252 $prevnext = $this->getPrevNext( $prevId, $nextId );
253 $out->addHTML( $prevnext );
254 }
255
256 $out->addHTML( $this->listStart( $level ) );
257 foreach ( $rows as $row ) {
258 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
259
260 if ( $row->rd_from && $level < 2 ) {
261 $out->addHTML( $this->listItem( $row, $nt, true ) );
262 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
263 $out->addHTML( Xml::closeElement( 'li' ) );
264 } else {
265 $out->addHTML( $this->listItem( $row, $nt ) );
266 }
267 }
268
269 $out->addHTML( $this->listEnd() );
270
271 if( $level == 0 ) {
272 $out->addHTML( $prevnext );
273 }
274 }
275
276 protected function listStart( $level ) {
277 return Xml::openElement( 'ul', ( $level ? array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
278 }
279
280 protected function listItem( $row, $nt, $notClose = false ) {
281 $dirmark = $this->getLanguage()->getDirMark();
282
283 # local message cache
284 static $msgcache = null;
285 if ( $msgcache === null ) {
286 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
287 'whatlinkshere-links', 'isimage' );
288 $msgcache = array();
289 foreach ( $msgs as $msg ) {
290 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
291 }
292 }
293
294 if( $row->rd_from ) {
295 $query = array( 'redirect' => 'no' );
296 } else {
297 $query = array();
298 }
299
300 $link = Linker::linkKnown(
301 $nt,
302 null,
303 array(),
304 $query
305 );
306
307 // Display properties (redirect or template)
308 $propsText = '';
309 $props = array();
310 if ( $row->rd_from )
311 $props[] = $msgcache['isredirect'];
312 if ( $row->is_template )
313 $props[] = $msgcache['istemplate'];
314 if( $row->is_image )
315 $props[] = $msgcache['isimage'];
316
317 if ( count( $props ) ) {
318 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
319 }
320
321 # Space for utilities links, with a what-links-here link provided
322 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
323 $wlh = Xml::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
324
325 return $notClose ?
326 Xml::openElement( 'li' ) . "$link $propsText $dirmark $wlh\n" :
327 Xml::tags( 'li', null, "$link $propsText $dirmark $wlh" ) . "\n";
328 }
329
330 protected function listEnd() {
331 return Xml::closeElement( 'ul' );
332 }
333
334 protected function wlhLink( Title $target, $text ) {
335 static $title = null;
336 if ( $title === null )
337 $title = $this->getTitle();
338
339 return Linker::linkKnown(
340 $title,
341 $text,
342 array(),
343 array( 'target' => $target->getPrefixedText() )
344 );
345 }
346
347 function makeSelfLink( $text, $query ) {
348 return Linker::linkKnown(
349 $this->selfTitle,
350 $text,
351 array(),
352 $query
353 );
354 }
355
356 function getPrevNext( $prevId, $nextId ) {
357 $currentLimit = $this->opts->getValue( 'limit' );
358 $prev = wfMessage( 'whatlinkshere-prev' )->numParams( $currentLimit )->escaped();
359 $next = wfMessage( 'whatlinkshere-next' )->numParams( $currentLimit )->escaped();
360
361 $changed = $this->opts->getChangedValues();
362 unset($changed['target']); // Already in the request title
363
364 if ( 0 != $prevId ) {
365 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
366 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
367 }
368 if ( 0 != $nextId ) {
369 $overrides = array( 'from' => $nextId, 'back' => $prevId );
370 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
371 }
372
373 $limitLinks = array();
374 $lang = $this->getLanguage();
375 foreach ( $this->limits as $limit ) {
376 $prettyLimit = htmlspecialchars( $lang->formatNum( $limit ) );
377 $overrides = array( 'limit' => $limit );
378 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
379 }
380
381 $nums = $lang->pipeList( $limitLinks );
382
383 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
384 }
385
386 function whatlinkshereForm() {
387 global $wgScript;
388
389 // We get nicer value from the title object
390 $this->opts->consumeValue( 'target' );
391 // Reset these for new requests
392 $this->opts->consumeValues( array( 'back', 'from' ) );
393
394 $target = $this->target ? $this->target->getPrefixedText() : '';
395 $namespace = $this->opts->consumeValue( 'namespace' );
396
397 # Build up the form
398 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
399
400 # Values that should not be forgotten
401 $f .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
402 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
403 $f .= Html::hidden( $name, $value );
404 }
405
406 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
407
408 # Target input
409 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
410 'mw-whatlinkshere-target', 40, $target );
411
412 $f .= ' ';
413
414 # Namespace selector
415 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;' .
416 Xml::namespaceSelector( $namespace, '' );
417
418 $f .= ' ';
419
420 # Submit
421 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
422
423 # Close
424 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
425
426 return $f;
427 }
428
429 /**
430 * Create filter panel
431 *
432 * @return string HTML fieldset and filter panel with the show/hide links
433 */
434 function getFilterPanel() {
435 $show = wfMsgHtml( 'show' );
436 $hide = wfMsgHtml( 'hide' );
437
438 $changed = $this->opts->getChangedValues();
439 unset($changed['target']); // Already in the request title
440
441 $links = array();
442 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
443 if( $this->target->getNamespace() == NS_FILE )
444 $types[] = 'hideimages';
445
446 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
447 // To be sure they will be find by grep
448 foreach( $types as $type ) {
449 $chosen = $this->opts->getValue( $type );
450 $msg = $chosen ? $show : $hide;
451 $overrides = array( $type => !$chosen );
452 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
453 }
454 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), $this->getLanguage()->pipeList( $links ) );
455 }
456 }