API: Requesting a token you aren't allowed to request no longer dies with an error...
[lhc/web/wiklou.git] / includes / 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( wfMsgExt( 'whatlinkshere-title', 'escape', $this->target->getPrefixedText() ) );
77 $wgOut->setSubtitle( wfMsgHtml( 'linklistsub' ) );
78
79 $wgOut->addHTML( wfMsgExt( 'whatlinkshere-barrow', array( 'escapenoentities') ) . ' ' .$this->skin->makeLinkObj($this->target, '', 'redirect=no' )."<br />\n");
80
81 $this->showIndirectLinks( 0, $this->target, $opts->getValue( 'limit' ),
82 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
83 }
84
85 /**
86 * @param int $level Recursion level
87 * @param Title $target Target title
88 * @param int $limit Number of entries to display
89 * @param Title $from Display from this article ID
90 * @param Title $back 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_IMAGE || $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 }
133
134 if ( $from ) {
135 $tlConds[] = "tl_from >= $from";
136 $plConds[] = "pl_from >= $from";
137 $ilConds[] = "il_from >= $from";
138 }
139
140 // Read an extra row as an at-end check
141 $queryLimit = $limit + 1;
142
143 // Enforce join order, sometimes namespace selector may
144 // trigger filesorts which are far less efficient than scanning many entries
145 $options[] = 'STRAIGHT_JOIN';
146
147 $options['LIMIT'] = $queryLimit;
148 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
149
150 if( $fetchlinks ) {
151 $options['ORDER BY'] = 'pl_from';
152 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
153 $plConds, __METHOD__, $options );
154 }
155
156 if( !$hidetrans ) {
157 $options['ORDER BY'] = 'tl_from';
158 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
159 $tlConds, __METHOD__, $options );
160 }
161
162 if( !$hideimages ) {
163 $options['ORDER BY'] = 'il_from';
164 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
165 $ilConds, __METHOD__, $options );
166 }
167
168 if( ( !$fetchlinks || !$dbr->numRows($plRes) ) && ( $hidetrans || !$dbr->numRows($tlRes) ) && ( $hideimages || !$dbr->numRows($ilRes) ) ) {
169 if ( 0 == $level ) {
170 $wgOut->addHTML( $this->whatlinkshereForm() );
171 $errMsg = is_int($namespace) ? 'nolinkshere-ns' : 'nolinkshere';
172 $wgOut->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
173 // Show filters only if there are links
174 if( $hidelinks || $hidetrans || $hideredirs || $hideimages )
175 $wgOut->addHTML( $this->getFilterPanel() );
176 }
177 return;
178 }
179
180 // Read the rows into an array and remove duplicates
181 // templatelinks comes second so that the templatelinks row overwrites the
182 // pagelinks row, so we get (inclusion) rather than nothing
183 if( $fetchlinks ) {
184 while ( $row = $dbr->fetchObject( $plRes ) ) {
185 $row->is_template = 0;
186 $row->is_image = 0;
187 $rows[$row->page_id] = $row;
188 }
189 $dbr->freeResult( $plRes );
190
191 }
192 if( !$hidetrans ) {
193 while ( $row = $dbr->fetchObject( $tlRes ) ) {
194 $row->is_template = 1;
195 $row->is_image = 0;
196 $rows[$row->page_id] = $row;
197 }
198 $dbr->freeResult( $tlRes );
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 $dbr->freeResult( $ilRes );
207 }
208
209 // Sort by key and then change the keys to 0-based indices
210 ksort( $rows );
211 $rows = array_values( $rows );
212
213 $numRows = count( $rows );
214
215 // Work out the start and end IDs, for prev/next links
216 if ( $numRows > $limit ) {
217 // More rows available after these ones
218 // Get the ID from the last row in the result set
219 $nextId = $rows[$limit]->page_id;
220 // Remove undisplayed rows
221 $rows = array_slice( $rows, 0, $limit );
222 } else {
223 // No more rows after
224 $nextId = false;
225 }
226 $prevId = $from;
227
228 if ( $level == 0 ) {
229 $wgOut->addHTML( $this->whatlinkshereForm() );
230 $wgOut->addHTML( $this->getFilterPanel() );
231 $wgOut->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
232
233 $prevnext = $this->getPrevNext( $prevId, $nextId );
234 $wgOut->addHTML( $prevnext );
235 }
236
237 $wgOut->addHTML( $this->listStart() );
238 foreach ( $rows as $row ) {
239 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
240
241 if ( $row->page_is_redirect && $level < 2 ) {
242 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
243 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
244 $wgOut->addHTML( Xml::closeElement( 'li' ) );
245 } else {
246 $wgOut->addHTML( $this->listItem( $row, $nt ) );
247 }
248 }
249
250 $wgOut->addHTML( $this->listEnd() );
251
252 if( $level == 0 ) {
253 $wgOut->addHTML( $prevnext );
254 }
255 }
256
257 protected function listStart() {
258 return Xml::openElement( 'ul' );
259 }
260
261 protected function listItem( $row, $nt, $notClose = false ) {
262 # local message cache
263 static $msgcache = null;
264 if ( $msgcache === null ) {
265 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
266 'whatlinkshere-links', 'isimage' );
267 $msgcache = array();
268 foreach ( $msgs as $msg ) {
269 $msgcache[$msg] = wfMsgHtml( $msg );
270 }
271 }
272
273 $suppressRedirect = $row->page_is_redirect ? 'redirect=no' : '';
274 $link = $this->skin->makeKnownLinkObj( $nt, '', $suppressRedirect );
275
276 // Display properties (redirect or template)
277 $propsText = '';
278 $props = array();
279 if ( $row->page_is_redirect )
280 $props[] = $msgcache['isredirect'];
281 if ( $row->is_template )
282 $props[] = $msgcache['istemplate'];
283 if( $row->is_image )
284 $props[] = $msgcache['isimage'];
285
286 if ( count( $props ) ) {
287 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
288 }
289
290 # Space for utilities links, with a what-links-here link provided
291 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
292 $wlh = Xml::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
293
294 return $notClose ?
295 Xml::openElement( 'li' ) . "$link $propsText $wlh\n" :
296 Xml::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
297 }
298
299 protected function listEnd() {
300 return Xml::closeElement( 'ul' );
301 }
302
303 protected function wlhLink( Title $target, $text ) {
304 static $title = null;
305 if ( $title === null )
306 $title = SpecialPage::getTitleFor( 'Whatlinkshere' );
307
308 $targetText = $target->getPrefixedUrl();
309 return $this->skin->makeKnownLinkObj( $title, $text, 'target=' . $targetText );
310 }
311
312 function makeSelfLink( $text, $query ) {
313 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
314 }
315
316 function getPrevNext( $prevId, $nextId ) {
317 global $wgLang;
318 $currentLimit = $this->opts->getValue( 'limit' );
319 $fmtLimit = $wgLang->formatNum( $currentLimit );
320 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
321 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
322
323 $changed = $this->opts->getChangedValues();
324 unset($changed['target']); // Already in the request title
325
326 if ( 0 != $prevId ) {
327 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
328 $prev = $this->makeSelfLink( $prev, wfArrayToCGI( $overrides, $changed ) );
329 }
330 if ( 0 != $nextId ) {
331 $overrides = array( 'from' => $nextId, 'back' => $prevId );
332 $next = $this->makeSelfLink( $next, wfArrayToCGI( $overrides, $changed ) );
333 }
334
335 $limitLinks = array();
336 foreach ( $this->limits as $limit ) {
337 $prettyLimit = $wgLang->formatNum( $limit );
338 $overrides = array( 'limit' => $limit );
339 $limitLinks[] = $this->makeSelfLink( $prettyLimit, wfArrayToCGI( $overrides, $changed ) );
340 }
341
342 $nums = implode ( ' | ', $limitLinks );
343
344 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
345 }
346
347 function whatlinkshereForm() {
348 global $wgScript, $wgTitle;
349
350 // We get nicer value from the title object
351 $this->opts->consumeValue( 'target' );
352 // Reset these for new requests
353 $this->opts->consumeValues( array( 'back', 'from' ) );
354
355 $target = $this->target ? $this->target->getPrefixedText() : '';
356 $namespace = $this->opts->consumeValue( 'namespace' );
357
358 # Build up the form
359 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
360
361 # Values that should not be forgotten
362 $f .= Xml::hidden( 'title', $wgTitle->getPrefixedText() );
363 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
364 $f .= Xml::hidden( $name, $value );
365 }
366
367 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
368
369 # Target input
370 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
371 'mw-whatlinkshere-target', 40, $target );
372
373 $f .= ' ';
374
375 # Namespace selector
376 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;' .
377 Xml::namespaceSelector( $namespace, '' );
378
379 # Submit
380 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
381
382 # Close
383 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
384
385 return $f;
386 }
387
388 function getFilterPanel() {
389 $show = wfMsgHtml( 'show' );
390 $hide = wfMsgHtml( 'hide' );
391
392 $changed = $this->opts->getChangedValues();
393 unset($changed['target']); // Already in the request title
394
395 $links = array();
396 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
397 if( $this->target->getNamespace() == NS_IMAGE )
398 $types[] = 'hideimages';
399 foreach( $types as $type ) {
400 $chosen = $this->opts->getValue( $type );
401 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ? $show : $hide );
402 $overrides = array( $type => !$chosen );
403 $links[] = $this->makeSelfLink( $msg, wfArrayToCGI( $overrides, $changed ) );
404 }
405 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), implode( '&nbsp;|&nbsp;', $links ) );
406 }
407 }