Make Whatlinkshere subclass SpecialPage.
[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 $suppressRedirect = $row->page_is_redirect ? 'redirect=no' : '';
262 $link = $this->skin->makeKnownLinkObj( $nt, '', $suppressRedirect );
263
264 // Display properties (redirect or template)
265 $propsText = '';
266 $props = array();
267 if ( $row->page_is_redirect )
268 $props[] = $msgcache['isredirect'];
269 if ( $row->is_template )
270 $props[] = $msgcache['istemplate'];
271 if( $row->is_image )
272 $props[] = $msgcache['isimage'];
273
274 if ( count( $props ) ) {
275 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
276 }
277
278 # Space for utilities links, with a what-links-here link provided
279 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
280 $wlh = Xml::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
281
282 return $notClose ?
283 Xml::openElement( 'li' ) . "$link $propsText $wlh\n" :
284 Xml::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
285 }
286
287 protected function listEnd() {
288 return Xml::closeElement( 'ul' );
289 }
290
291 protected function wlhLink( Title $target, $text ) {
292 static $title = null;
293 if ( $title === null )
294 $title = SpecialPage::getTitleFor( 'Whatlinkshere' );
295
296 $targetText = $target->getPrefixedUrl();
297 return $this->skin->makeKnownLinkObj( $title, $text, 'target=' . $targetText );
298 }
299
300 function makeSelfLink( $text, $query ) {
301 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
302 }
303
304 function getPrevNext( $prevId, $nextId ) {
305 global $wgLang;
306 $currentLimit = $this->opts->getValue( 'limit' );
307 $fmtLimit = $wgLang->formatNum( $currentLimit );
308 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
309 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
310
311 $changed = $this->opts->getChangedValues();
312 unset($changed['target']); // Already in the request title
313
314 if ( 0 != $prevId ) {
315 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
316 $prev = $this->makeSelfLink( $prev, wfArrayToCGI( $overrides, $changed ) );
317 }
318 if ( 0 != $nextId ) {
319 $overrides = array( 'from' => $nextId, 'back' => $prevId );
320 $next = $this->makeSelfLink( $next, wfArrayToCGI( $overrides, $changed ) );
321 }
322
323 $limitLinks = array();
324 foreach ( $this->limits as $limit ) {
325 $prettyLimit = $wgLang->formatNum( $limit );
326 $overrides = array( 'limit' => $limit );
327 $limitLinks[] = $this->makeSelfLink( $prettyLimit, wfArrayToCGI( $overrides, $changed ) );
328 }
329
330 $nums = $wgLang->pipeList( $limitLinks );
331
332 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
333 }
334
335 function whatlinkshereForm() {
336 global $wgScript;
337
338 // We get nicer value from the title object
339 $this->opts->consumeValue( 'target' );
340 // Reset these for new requests
341 $this->opts->consumeValues( array( 'back', 'from' ) );
342
343 $target = $this->target ? $this->target->getPrefixedText() : '';
344 $namespace = $this->opts->consumeValue( 'namespace' );
345
346 # Build up the form
347 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
348
349 # Values that should not be forgotten
350 $f .= Xml::hidden( 'title', SpecialPage::getTitleFor( 'Whatlinkshere' )->getPrefixedText() );
351 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
352 $f .= Xml::hidden( $name, $value );
353 }
354
355 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
356
357 # Target input
358 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
359 'mw-whatlinkshere-target', 40, $target );
360
361 $f .= ' ';
362
363 # Namespace selector
364 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;' .
365 Xml::namespaceSelector( $namespace, '' );
366
367 $f .= ' ';
368
369 # Submit
370 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
371
372 # Close
373 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
374
375 return $f;
376 }
377
378 function getFilterPanel() {
379 global $wgLang;
380 $show = wfMsgHtml( 'show' );
381 $hide = wfMsgHtml( 'hide' );
382
383 $changed = $this->opts->getChangedValues();
384 unset($changed['target']); // Already in the request title
385
386 $links = array();
387 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
388 if( $this->target->getNamespace() == NS_FILE )
389 $types[] = 'hideimages';
390 foreach( $types as $type ) {
391 $chosen = $this->opts->getValue( $type );
392 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ? $show : $hide );
393 $overrides = array( $type => !$chosen );
394 $links[] = $this->makeSelfLink( $msg, wfArrayToCGI( $overrides, $changed ) );
395 }
396 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );
397 }
398 }