Assignment in loop conditions suck
[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 // Stored objects
32 protected $opts, $target, $selfTitle;
33
34 // Stored globals
35 protected $skin;
36
37 protected $limits = array( 20, 50, 100, 250, 500 );
38
39 public function __construct() {
40 parent::__construct( 'Whatlinkshere' );
41 global $wgUser;
42 $this->skin = $wgUser->getSkin();
43 }
44
45 function execute( $par ) {
46 global $wgOut, $wgRequest;
47
48 $this->setHeaders();
49
50 $opts = new FormOptions();
51
52 $opts->add( 'target', '' );
53 $opts->add( 'namespace', '', FormOptions::INTNULL );
54 $opts->add( 'limit', 50 );
55 $opts->add( 'from', 0 );
56 $opts->add( 'back', 0 );
57 $opts->add( 'hideredirs', false );
58 $opts->add( 'hidetrans', false );
59 $opts->add( 'hidelinks', false );
60 $opts->add( 'hideimages', false );
61
62 $opts->fetchValuesFromRequest( $wgRequest );
63 $opts->validateIntBounds( 'limit', 0, 5000 );
64
65 // Give precedence to subpage syntax
66 if ( isset($par) ) {
67 $opts->setValue( 'target', $par );
68 }
69
70 // Bind to member variable
71 $this->opts = $opts;
72
73 $this->target = Title::newFromURL( $opts->getValue( 'target' ) );
74 if( !$this->target ) {
75 $wgOut->addHTML( $this->whatlinkshereForm() );
76 return;
77 }
78
79 $this->selfTitle = $this->getTitle( $this->target->getPrefixedDBkey() );
80
81 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
82 $wgOut->setSubtitle( wfMsg( 'whatlinkshere-backlink', $this->skin->link( $this->target, $this->target->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
83
84 $this->showIndirectLinks( 0, $this->target, $opts->getValue( 'limit' ),
85 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
86 }
87
88 /**
89 * @param $level int Recursion level
90 * @param $target Title Target title
91 * @param $limit int Number of entries to display
92 * @param $from Title Display from this article ID
93 * @param $back Title Display from this article ID at backwards scrolling
94 * @private
95 */
96 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
97 global $wgOut, $wgMaxRedirectLinksRetrieved;
98 $dbr = wfGetDB( DB_SLAVE );
99 $options = array();
100
101 $hidelinks = $this->opts->getValue( 'hidelinks' );
102 $hideredirs = $this->opts->getValue( 'hideredirs' );
103 $hidetrans = $this->opts->getValue( 'hidetrans' );
104 $hideimages = $target->getNamespace() != NS_FILE || $this->opts->getValue( 'hideimages' );
105
106 $fetchlinks = (!$hidelinks || !$hideredirs);
107
108 // Make the query
109 $plConds = array(
110 'page_id=pl_from',
111 'pl_namespace' => $target->getNamespace(),
112 'pl_title' => $target->getDBkey(),
113 );
114 if( $hideredirs ) {
115 $plConds['page_is_redirect'] = 0;
116 } elseif( $hidelinks ) {
117 $plConds['page_is_redirect'] = 1;
118 }
119
120 $tlConds = array(
121 'page_id=tl_from',
122 'tl_namespace' => $target->getNamespace(),
123 'tl_title' => $target->getDBkey(),
124 );
125
126 $ilConds = array(
127 'page_id=il_from',
128 'il_to' => $target->getDBkey(),
129 );
130
131 $namespace = $this->opts->getValue( 'namespace' );
132 if ( is_int($namespace) ) {
133 $plConds['page_namespace'] = $namespace;
134 $tlConds['page_namespace'] = $namespace;
135 $ilConds['page_namespace'] = $namespace;
136 }
137
138 if ( $from ) {
139 $tlConds[] = "tl_from >= $from";
140 $plConds[] = "pl_from >= $from";
141 $ilConds[] = "il_from >= $from";
142 }
143
144 // Read an extra row as an at-end check
145 $queryLimit = $limit + 1;
146
147 // Enforce join order, sometimes namespace selector may
148 // trigger filesorts which are far less efficient than scanning many entries
149 $options[] = 'STRAIGHT_JOIN';
150
151 $options['LIMIT'] = $queryLimit;
152 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
153
154 if( $fetchlinks ) {
155 $options['ORDER BY'] = 'pl_from';
156 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
157 $plConds, __METHOD__, $options );
158 }
159
160 if( !$hidetrans ) {
161 $options['ORDER BY'] = 'tl_from';
162 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
163 $tlConds, __METHOD__, $options );
164 }
165
166 if( !$hideimages ) {
167 $options['ORDER BY'] = 'il_from';
168 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
169 $ilConds, __METHOD__, $options );
170 }
171
172 if( ( !$fetchlinks || !$dbr->numRows($plRes) ) && ( $hidetrans || !$dbr->numRows($tlRes) ) && ( $hideimages || !$dbr->numRows($ilRes) ) ) {
173 if ( 0 == $level ) {
174 $wgOut->addHTML( $this->whatlinkshereForm() );
175
176 // Show filters only if there are links
177 if( $hidelinks || $hidetrans || $hideredirs || $hideimages )
178 $wgOut->addHTML( $this->getFilterPanel() );
179
180 $errMsg = is_int($namespace) ? 'nolinkshere-ns' : 'nolinkshere';
181 $wgOut->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
182 }
183 return;
184 }
185
186 // Read the rows into an array and remove duplicates
187 // templatelinks comes second so that the templatelinks row overwrites the
188 // pagelinks row, so we get (inclusion) rather than nothing
189 if( $fetchlinks ) {
190 foreach ( $plRes as $row ) {
191 $row->is_template = 0;
192 $row->is_image = 0;
193 $rows[$row->page_id] = $row;
194 }
195 }
196 if( !$hidetrans ) {
197 foreach ( $tlRes as $row ) {
198 $row->is_template = 1;
199 $row->is_image = 0;
200 $rows[$row->page_id] = $row;
201 }
202 }
203 if( !$hideimages ) {
204 foreach ( $ilRes as $row ) {
205 $row->is_template = 0;
206 $row->is_image = 1;
207 $rows[$row->page_id] = $row;
208 }
209 }
210
211 // Sort by key and then change the keys to 0-based indices
212 ksort( $rows );
213 $rows = array_values( $rows );
214
215 $numRows = count( $rows );
216
217 // Work out the start and end IDs, for prev/next links
218 if ( $numRows > $limit ) {
219 // More rows available after these ones
220 // Get the ID from the last row in the result set
221 $nextId = $rows[$limit]->page_id;
222 // Remove undisplayed rows
223 $rows = array_slice( $rows, 0, $limit );
224 } else {
225 // No more rows after
226 $nextId = false;
227 }
228 $prevId = $from;
229
230 if ( $level == 0 ) {
231 $wgOut->addHTML( $this->whatlinkshereForm() );
232 $wgOut->addHTML( $this->getFilterPanel() );
233 $wgOut->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
234
235 $prevnext = $this->getPrevNext( $prevId, $nextId );
236 $wgOut->addHTML( $prevnext );
237 }
238
239 $wgOut->addHTML( $this->listStart() );
240 foreach ( $rows as $row ) {
241 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
242
243 if ( $row->page_is_redirect && $level < 2 ) {
244 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
245 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
246 $wgOut->addHTML( Xml::closeElement( 'li' ) );
247 } else {
248 $wgOut->addHTML( $this->listItem( $row, $nt ) );
249 }
250 }
251
252 $wgOut->addHTML( $this->listEnd() );
253
254 if( $level == 0 ) {
255 $wgOut->addHTML( $prevnext );
256 }
257 }
258
259 protected function listStart() {
260 return Xml::openElement( 'ul', array ( 'id' => 'mw-whatlinkshere-list' ) );
261 }
262
263 protected function listItem( $row, $nt, $notClose = false ) {
264 global $wgLang;
265
266 # local message cache
267 static $msgcache = null;
268 if ( $msgcache === null ) {
269 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
270 'whatlinkshere-links', 'isimage', 'hist' );
271 $msgcache = array();
272 foreach ( $msgs as $msg ) {
273 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
274 }
275 }
276
277 if( $row->page_is_redirect ) {
278 $query = array( 'redirect' => 'no' );
279 } else {
280 $query = array();
281 }
282
283 $link = $this->skin->linkKnown(
284 $nt,
285 null,
286 array(),
287 $query
288 );
289
290 // Display properties (redirect or template)
291 $propsText = '';
292 $props = array();
293 if ( $row->page_is_redirect )
294 $props[] = $msgcache['isredirect'];
295 if ( $row->is_template )
296 $props[] = $msgcache['istemplate'];
297 if( $row->is_image )
298 $props[] = $msgcache['isimage'];
299
300 if ( count( $props ) ) {
301 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
302 }
303
304 # Space for utilities links, with a what-links-here link provided
305 $tools = array();
306 $tools[] = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
307 $tools[] = $this->skin->linkKnown( $nt, $msgcache['hist'], array(), array( 'action' => 'history' ) );
308 $wlh = Xml::wrapClass( '(' . $wgLang->pipeList( $tools ) . ')', 'mw-whatlinkshere-tools' );
309
310 return $notClose ?
311 Xml::openElement( 'li' ) . "$link $propsText $wlh\n" :
312 Xml::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
313 }
314
315 protected function listEnd() {
316 return Xml::closeElement( 'ul' );
317 }
318
319 protected function wlhLink( Title $target, $text ) {
320 static $title = null;
321 if ( $title === null )
322 $title = $this->getTitle();
323
324 return $this->skin->linkKnown(
325 $title,
326 $text,
327 array(),
328 array( 'target' => $target->getPrefixedText() )
329 );
330 }
331
332 function makeSelfLink( $text, $query ) {
333 return $this->skin->linkKnown(
334 $this->selfTitle,
335 $text,
336 array(),
337 $query
338 );
339 }
340
341 function getPrevNext( $prevId, $nextId ) {
342 global $wgLang;
343 $currentLimit = $this->opts->getValue( 'limit' );
344 $fmtLimit = $wgLang->formatNum( $currentLimit );
345 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
346 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
347
348 $changed = $this->opts->getChangedValues();
349 unset($changed['target']); // Already in the request title
350
351 if ( 0 != $prevId ) {
352 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
353 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
354 }
355 if ( 0 != $nextId ) {
356 $overrides = array( 'from' => $nextId, 'back' => $prevId );
357 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
358 }
359
360 $limitLinks = array();
361 foreach ( $this->limits as $limit ) {
362 $prettyLimit = $wgLang->formatNum( $limit );
363 $overrides = array( 'limit' => $limit );
364 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
365 }
366
367 $nums = $wgLang->pipeList( $limitLinks );
368
369 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
370 }
371
372 function whatlinkshereForm() {
373 global $wgScript;
374
375 // We get nicer value from the title object
376 $this->opts->consumeValue( 'target' );
377 // Reset these for new requests
378 $this->opts->consumeValues( array( 'back', 'from' ) );
379
380 $target = $this->target ? $this->target->getPrefixedText() : '';
381 $namespace = $this->opts->consumeValue( 'namespace' );
382
383 # Build up the form
384 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
385
386 # Values that should not be forgotten
387 $f .= Xml::hidden( 'title', $this->getTitle()->getPrefixedText() );
388 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
389 $f .= Xml::hidden( $name, $value );
390 }
391
392 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
393
394 # Target input
395 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
396 'mw-whatlinkshere-target', 40, $target );
397
398 $f .= ' ';
399
400 # Namespace selector
401 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;' .
402 Xml::namespaceSelector( $namespace, '' );
403
404 $f .= ' ';
405
406 # Submit
407 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
408
409 # Close
410 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
411
412 return $f;
413 }
414
415 /**
416 * Create filter panel
417 *
418 * @return string HTML fieldset and filter panel with the show/hide links
419 */
420 function getFilterPanel() {
421 global $wgLang;
422 $show = wfMsgHtml( 'show' );
423 $hide = wfMsgHtml( 'hide' );
424
425 $changed = $this->opts->getChangedValues();
426 unset($changed['target']); // Already in the request title
427
428 $links = array();
429 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
430 if( $this->target->getNamespace() == NS_FILE )
431 $types[] = 'hideimages';
432
433 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
434 // To be sure they will be find by grep
435 foreach( $types as $type ) {
436 $chosen = $this->opts->getValue( $type );
437 $msg = $chosen ? $show : $hide;
438 $overrides = array( $type => !$chosen );
439 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
440 }
441 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );
442 }
443 }