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