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