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