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