Don't throw a fatal error if somebody access Special:Whatlinkshere without target...
[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 $wgOut->addHTML( $this->listItem( $row, $nt ) );
216
217 if ( $row->page_is_redirect && $level < 2 ) {
218 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
219 }
220 }
221
222 $wgOut->addHTML( $this->listEnd() );
223
224 if( $level == 0 ) {
225 $wgOut->addHTML( $prevnext );
226 }
227 }
228
229 protected function listStart() {
230 return Xml::openElement( 'ul' );
231 }
232
233 protected function listItem( $row, $nt ) {
234 # local message cache
235 static $msgcache = null;
236 if ( $msgcache === null ) {
237 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
238 'whatlinkshere-links' );
239 $msgcache = array();
240 foreach ( $msgs as $msg ) {
241 $msgcache[$msg] = wfMsgHtml( $msg );
242 }
243 }
244
245 $suppressRedirect = $row->page_is_redirect ? 'redirect=no' : '';
246 $link = $this->skin->makeKnownLinkObj( $nt, '', $suppressRedirect );
247
248 // Display properties (redirect or template)
249 $propsText = '';
250 $props = array();
251 if ( $row->page_is_redirect )
252 $props[] = $msgcache['isredirect'];
253 if ( $row->is_template )
254 $props[] = $msgcache['istemplate'];
255
256 if ( count( $props ) ) {
257 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
258 }
259
260 # Space for utilities links, with a what-links-here link provided
261 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
262 $wlh = Xml::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
263
264 return Xml::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
265 }
266
267 protected function listEnd() {
268 return Xml::closeElement( 'ul' );
269 }
270
271 protected function wlhLink( Title $target, $text ) {
272 static $title = null;
273 if ( $title === null )
274 $title = SpecialPage::getTitleFor( 'Whatlinkshere' );
275
276 $targetText = $target->getPrefixedUrl();
277 return $this->skin->makeKnownLinkObj( $title, $text, 'target=' . $targetText );
278 }
279
280 function makeSelfLink( $text, $query ) {
281 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
282 }
283
284 function getPrevNext( $prevId, $nextId ) {
285 global $wgLang;
286 $currentLimit = $this->opts->getValue( 'limit' );
287 $fmtLimit = $wgLang->formatNum( $currentLimit );
288 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
289 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
290
291 $changed = $this->opts->getChangedValues();
292 unset($changed['target']); // Already in the request title
293
294 if ( 0 != $prevId ) {
295 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
296 $prev = $this->makeSelfLink( $prev, wfArrayToCGI( $overrides, $changed ) );
297 }
298 if ( 0 != $nextId ) {
299 $overrides = array( 'from' => $nextId, 'back' => $prevId );
300 $next = $this->makeSelfLink( $next, wfArrayToCGI( $overrides, $changed ) );
301 }
302
303 $limitLinks = array();
304 foreach ( $this->limits as $limit ) {
305 $prettyLimit = $wgLang->formatNum( $limit );
306 $overrides = array( 'limit' => $limit );
307 $limitLinks[] = $this->makeSelfLink( $prettyLimit, wfArrayToCGI( $overrides, $changed ) );
308 }
309
310 $nums = implode ( ' | ', $limitLinks );
311
312 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
313 }
314
315 function whatlinkshereForm() {
316 global $wgScript, $wgTitle;
317
318 // We get nicer value from the title object
319 $this->opts->consumeValue( 'target' );
320 // Reset these for new requests
321 $this->opts->consumeValues( array( 'back', 'from' ) );
322
323 $target = $this->target ? $this->target->getPrefixedText() : '';
324 $namespace = $this->opts->consumeValue( 'namespace' );
325
326 # Build up the form
327 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
328
329 # Values that should not be forgotten
330 $f .= Xml::hidden( 'title', $wgTitle->getPrefixedText() );
331 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
332 $f .= Xml::hidden( $name, $value );
333 }
334
335 $f .= Xml::openElement( 'fieldset' );
336 $f .= Xml::element( 'legend', null, wfMsg( 'whatlinkshere' ) );
337
338 # Target input
339 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
340 'mw-whatlinkshere-target', 40, $target );
341
342 $f .= ' ';
343
344 # Namespace selector
345 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;' .
346 Xml::namespaceSelector( $namespace, '' );
347
348 # Submit
349 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
350
351 # Close
352 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
353
354 return $f;
355 }
356
357 function getFilterPanel() {
358 $show = wfMsgHtml( 'show' );
359 $hide = wfMsgHtml( 'hide' );
360
361 $changed = $this->opts->getChangedValues();
362 unset($changed['target']); // Already in the request title
363
364 $links = array();
365 foreach( array( 'hidetrans', 'hidelinks', 'hideredirs' ) as $type ) {
366 $chosen = $this->opts->getValue( $type );
367 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ? $show : $hide );
368 $overrides = array( $type => !$chosen );
369 $links[] = $this->makeSelfLink( $msg, wfArrayToCGI( $overrides, $changed ) );
370 }
371 return Xml::tags( 'fieldset', null,
372 Xml::element( 'legend', null, wfMsg( 'whatlinkshere-filters' ) ) .
373 implode( '&nbsp;|&nbsp;', $links )
374 );
375 }
376 }