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