Resolve fixme on r96249: can't use in static context
[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 $out = $this->getOutput();
51
52 $this->setHeaders();
53
54 $opts = new FormOptions();
55
56 $opts->add( 'target', '' );
57 $opts->add( 'namespace', '', FormOptions::INTNULL );
58 $opts->add( 'limit', 50 );
59 $opts->add( 'from', 0 );
60 $opts->add( 'back', 0 );
61 $opts->add( 'hideredirs', false );
62 $opts->add( 'hidetrans', false );
63 $opts->add( 'hidelinks', false );
64 $opts->add( 'hideimages', false );
65
66 $opts->fetchValuesFromRequest( $this->getRequest() );
67 $opts->validateIntBounds( 'limit', 0, 5000 );
68
69 // Give precedence to subpage syntax
70 if ( isset($par) ) {
71 $opts->setValue( 'target', $par );
72 }
73
74 // Bind to member variable
75 $this->opts = $opts;
76
77 $this->target = Title::newFromURL( $opts->getValue( 'target' ) );
78 if( !$this->target ) {
79 $out->addHTML( $this->whatlinkshereForm() );
80 return;
81 }
82
83 $this->getSkin()->setRelevantTitle( $this->target );
84
85
86 $this->selfTitle = $this->getTitle( $this->target->getPrefixedDBkey() );
87
88 $out->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
89 $out->setSubtitle( wfMsg( 'whatlinkshere-backlink', Linker::link( $this->target, $this->target->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
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['page_is_redirect'] = 0;
123 } elseif( $hidelinks ) {
124 $plConds['page_is_redirect'] = 1;
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', 'page_is_redirect' );
160
161 if( $fetchlinks ) {
162 $options['ORDER BY'] = 'pl_from';
163 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
164 $plConds, __METHOD__, $options );
165 }
166
167 if( !$hidetrans ) {
168 $options['ORDER BY'] = 'tl_from';
169 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
170 $tlConds, __METHOD__, $options );
171 }
172
173 if( !$hideimages ) {
174 $options['ORDER BY'] = 'il_from';
175 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
176 $ilConds, __METHOD__, $options );
177 }
178
179 if( ( !$fetchlinks || !$dbr->numRows($plRes) ) && ( $hidetrans || !$dbr->numRows($tlRes) ) && ( $hideimages || !$dbr->numRows($ilRes) ) ) {
180 if ( 0 == $level ) {
181 $out->addHTML( $this->whatlinkshereForm() );
182
183 // Show filters only if there are links
184 if( $hidelinks || $hidetrans || $hideredirs || $hideimages )
185 $out->addHTML( $this->getFilterPanel() );
186
187 $errMsg = is_int($namespace) ? 'nolinkshere-ns' : 'nolinkshere';
188 $out->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
189 }
190 return;
191 }
192
193 // Read the rows into an array and remove duplicates
194 // templatelinks comes second so that the templatelinks row overwrites the
195 // pagelinks row, so we get (inclusion) rather than nothing
196 if( $fetchlinks ) {
197 foreach ( $plRes as $row ) {
198 $row->is_template = 0;
199 $row->is_image = 0;
200 $rows[$row->page_id] = $row;
201 }
202 }
203 if( !$hidetrans ) {
204 foreach ( $tlRes as $row ) {
205 $row->is_template = 1;
206 $row->is_image = 0;
207 $rows[$row->page_id] = $row;
208 }
209 }
210 if( !$hideimages ) {
211 foreach ( $ilRes as $row ) {
212 $row->is_template = 0;
213 $row->is_image = 1;
214 $rows[$row->page_id] = $row;
215 }
216 }
217
218 // Sort by key and then change the keys to 0-based indices
219 ksort( $rows );
220 $rows = array_values( $rows );
221
222 $numRows = count( $rows );
223
224 // Work out the start and end IDs, for prev/next links
225 if ( $numRows > $limit ) {
226 // More rows available after these ones
227 // Get the ID from the last row in the result set
228 $nextId = $rows[$limit]->page_id;
229 // Remove undisplayed rows
230 $rows = array_slice( $rows, 0, $limit );
231 } else {
232 // No more rows after
233 $nextId = false;
234 }
235 $prevId = $from;
236
237 if ( $level == 0 ) {
238 $out->addHTML( $this->whatlinkshereForm() );
239 $out->addHTML( $this->getFilterPanel() );
240 $out->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
241
242 $prevnext = $this->getPrevNext( $prevId, $nextId );
243 $out->addHTML( $prevnext );
244 }
245
246 $out->addHTML( $this->listStart( $level ) );
247 foreach ( $rows as $row ) {
248 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
249
250 if ( $row->page_is_redirect && $level < 2 ) {
251 $out->addHTML( $this->listItem( $row, $nt, true ) );
252 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
253 $out->addHTML( Xml::closeElement( 'li' ) );
254 } else {
255 $out->addHTML( $this->listItem( $row, $nt ) );
256 }
257 }
258
259 $out->addHTML( $this->listEnd() );
260
261 if( $level == 0 ) {
262 $out->addHTML( $prevnext );
263 }
264 }
265
266 protected function listStart( $level ) {
267 return Xml::openElement( 'ul', ( $level ? array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
268 }
269
270 protected function listItem( $row, $nt, $notClose = false ) {
271 global $wgLang;
272 $dirmark = $wgLang->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 global $wgLang;
349 $currentLimit = $this->opts->getValue( 'limit' );
350 $fmtLimit = $wgLang->formatNum( $currentLimit );
351 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
352 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
353
354 $changed = $this->opts->getChangedValues();
355 unset($changed['target']); // Already in the request title
356
357 if ( 0 != $prevId ) {
358 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
359 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
360 }
361 if ( 0 != $nextId ) {
362 $overrides = array( 'from' => $nextId, 'back' => $prevId );
363 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
364 }
365
366 $limitLinks = array();
367 foreach ( $this->limits as $limit ) {
368 $prettyLimit = $wgLang->formatNum( $limit );
369 $overrides = array( 'limit' => $limit );
370 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
371 }
372
373 $nums = $wgLang->pipeList( $limitLinks );
374
375 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
376 }
377
378 function whatlinkshereForm() {
379 global $wgScript;
380
381 // We get nicer value from the title object
382 $this->opts->consumeValue( 'target' );
383 // Reset these for new requests
384 $this->opts->consumeValues( array( 'back', 'from' ) );
385
386 $target = $this->target ? $this->target->getPrefixedText() : '';
387 $namespace = $this->opts->consumeValue( 'namespace' );
388
389 # Build up the form
390 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
391
392 # Values that should not be forgotten
393 $f .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
394 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
395 $f .= Html::hidden( $name, $value );
396 }
397
398 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
399
400 # Target input
401 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
402 'mw-whatlinkshere-target', 40, $target );
403
404 $f .= ' ';
405
406 # Namespace selector
407 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;' .
408 Xml::namespaceSelector( $namespace, '' );
409
410 $f .= ' ';
411
412 # Submit
413 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
414
415 # Close
416 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
417
418 return $f;
419 }
420
421 /**
422 * Create filter panel
423 *
424 * @return string HTML fieldset and filter panel with the show/hide links
425 */
426 function getFilterPanel() {
427 global $wgLang;
428 $show = wfMsgHtml( 'show' );
429 $hide = wfMsgHtml( 'hide' );
430
431 $changed = $this->opts->getChangedValues();
432 unset($changed['target']); // Already in the request title
433
434 $links = array();
435 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
436 if( $this->target->getNamespace() == NS_FILE )
437 $types[] = 'hideimages';
438
439 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
440 // To be sure they will be find by grep
441 foreach( $types as $type ) {
442 $chosen = $this->opts->getValue( $type );
443 $msg = $chosen ? $show : $hide;
444 $overrides = array( $type => !$chosen );
445 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
446 }
447 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );
448 }
449 }