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