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