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