* The gotcha here was that wfMsgHtml does not escape parameters
[lhc/web/wiklou.git] / includes / SpecialWhatlinkshere.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * Entry point
9 * @param string $par An article name ??
10 */
11 function wfSpecialWhatlinkshere($par = NULL) {
12 global $wgRequest;
13 $page = new WhatLinksHerePage( $wgRequest, $par );
14 $page->execute();
15 }
16
17 /**
18 * implements Special:Whatlinkshere
19 * @addtogroup SpecialPage
20 */
21 class WhatLinksHerePage {
22 var $request, $par;
23 var $limit, $from, $back, $target;
24 var $selfTitle, $skin;
25 var $hideredirs, $hidetrans, $hidelinks, $fetchlinks;
26
27 private $namespace;
28
29 function WhatLinksHerePage( $request, $par = null ) {
30 global $wgUser;
31 $this->request = $request;
32 $this->skin = $wgUser->getSkin();
33 $this->par = $par;
34 }
35
36 function execute() {
37 global $wgOut;
38
39 $this->limit = min( $this->request->getInt( 'limit', 50 ), 5000 );
40 if ( $this->limit <= 0 ) {
41 $this->limit = 50;
42 }
43 $this->from = $this->request->getInt( 'from' );
44 $this->back = $this->request->getInt( 'back' );
45
46 $this->hideredirs = $this->request->getInt( 'hideredirs' );
47 $this->hidetrans = $this->request->getInt( 'hidetrans' );
48 $this->hidelinks = $this->request->getInt( 'hidelinks' );
49 $this->fetchlinks = !$this->hidelinks || !$this->hideredirs;
50
51 $targetString = isset($this->par) ? $this->par : $this->request->getVal( 'target' );
52
53 if ( is_null( $targetString ) ) {
54 $wgOut->addHTML( $this->whatlinkshereForm() );
55 return;
56 }
57
58 $this->target = Title::newFromURL( $targetString );
59 if( !$this->target ) {
60 $wgOut->addHTML( $this->whatlinkshereForm() );
61 return;
62 }
63 $this->selfTitle = Title::makeTitleSafe( NS_SPECIAL,
64 'Whatlinkshere/' . $this->target->getPrefixedDBkey() );
65
66 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
67 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
68
69 $wgOut->addHTML( wfMsgExt( 'whatlinkshere-barrow', array( 'escapenoentities') ) . ' ' .$this->skin->makeLinkObj($this->target, '', 'redirect=no' )."<br />\n");
70
71 $this->showIndirectLinks( 0, $this->target, $this->limit, $this->from, $this->back );
72 }
73
74 /**
75 * @param int $level Recursion level
76 * @param Title $target Target title
77 * @param int $limit Number of entries to display
78 * @param Title $from Display from this article ID
79 * @param Title $back Display from this article ID at backwards scrolling
80 * @private
81 */
82 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
83 global $wgOut, $wgMaxRedirectLinksRetrieved;
84 $fname = 'WhatLinksHerePage::showIndirectLinks';
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, $fname, $options );
134 }
135
136 if( !$this->hidetrans ) {
137 $options['ORDER BY'] = 'tl_from';
138 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
139 $tlConds, $fname, $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( '<ul>' );
222 foreach ( $rows as $row ) {
223 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
224
225 if( $this->hideredirs && $row->page_is_redirect )
226 continue;
227 if( $this->hidelinks && ( !$row->page_is_redirect && !$row->is_template ) )
228 continue;
229
230 if ( $row->page_is_redirect ) {
231 $extra = 'redirect=no';
232 } else {
233 $extra = '';
234 }
235
236 $link = $this->skin->makeKnownLinkObj( $nt, '', $extra );
237 $wgOut->addHTML( '<li>'.$link );
238
239 // Display properties (redirect or template)
240 $props = array();
241 if ( $row->page_is_redirect ) {
242 $props[] = wfMsgHtml( 'isredirect' );
243 }
244 if ( $row->is_template ) {
245 $props[] = wfMsgHtml( 'istemplate' );
246 }
247 if ( count( $props ) ) {
248 $list = implode( wfMsgHtml( 'semicolon-separator' ), $props );
249 $wgOut->addHTML( " ($list) " );
250 }
251
252 # Space for utilities links, with a what-links-here link provided
253 $wlh = $this->skin->makeKnownLinkObj(
254 SpecialPage::getTitleFor( 'Whatlinkshere' ),
255 wfMsgHtml( 'whatlinkshere-links' ),
256 'target=' . $nt->getPrefixedUrl()
257 );
258 $wgOut->addHtml( ' <span class="mw-whatlinkshere-tools">(' . $wlh . ')</span>' );
259
260 if ( $row->page_is_redirect ) {
261 if ( $level < 2 ) {
262 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
263 }
264 }
265 $wgOut->addHTML( "</li>\n" );
266 }
267 $wgOut->addHTML( "</ul>\n" );
268
269 if( $level == 0 ) {
270 $wgOut->addHTML( $prevnext );
271 }
272 }
273
274 function makeSelfLink( $text, $query ) {
275 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
276 }
277
278 function getPrevNext( $limit, $prevId, $nextId ) {
279 global $wgLang;
280 $fmtLimit = $wgLang->formatNum( $limit );
281 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
282 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
283
284 $nsText = '';
285 if( is_int($this->namespace) ) {
286 $nsText = "&namespace={$this->namespace}";
287 }
288
289 if ( 0 != $prevId ) {
290 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}{$nsText}" );
291 } else {
292 $prevLink = $prev;
293 }
294 if ( 0 != $nextId ) {
295 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}{$nsText}" );
296 } else {
297 $nextLink = $next;
298 }
299 $nums = $this->numLink( 20, $prevId ) . ' | ' .
300 $this->numLink( 50, $prevId ) . ' | ' .
301 $this->numLink( 100, $prevId ) . ' | ' .
302 $this->numLink( 250, $prevId ) . ' | ' .
303 $this->numLink( 500, $prevId );
304
305 return wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $nums );
306 }
307
308 function numLink( $limit, $from, $ns = null ) {
309 global $wgLang;
310 $query = "limit={$limit}&from={$from}";
311 if( is_int($this->namespace) ) { $query .= "&namespace={$this->namespace}";}
312 $fmtLimit = $wgLang->formatNum( $limit );
313 return $this->makeSelfLink( $fmtLimit, $query );
314 }
315
316 function whatlinkshereForm( $options = array( 'target' => '', 'namespace' => '' ) ) {
317 global $wgScript, $wgTitle;
318
319 $options['title'] = $wgTitle->getPrefixedText();
320
321 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
322 Xml::openElement( 'fieldset' ) .
323 Xml::element( 'legend', array(), wfMsg( 'whatlinkshere' ) ) .
324 Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target', 'mw-whatlinkshere-target', 40, $options['target'] ) . ' ';
325
326 foreach ( $options as $name => $value ) {
327 if( $name === 'namespace' || $name === 'target' )
328 continue;
329 $f .= "\t" . Xml::hidden( $name, $value ). "\n";
330 }
331
332 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
333 Xml::namespaceSelector( $options['namespace'], '' ) .
334 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
335 Xml::closeElement( 'fieldset' ) .
336 Xml::closeElement( 'form' ) . "\n";
337
338 return $f;
339 }
340
341 /** Set the namespace we are filtering on */
342 private function setNamespace( $ns ) {
343 $this->namespace = $ns;
344 }
345
346 function getFilterPanel() {
347 $show = wfMsgHtml( 'show' );
348 $hide = wfMsgHtml( 'hide' );
349 $links = array();
350 foreach( array( 'hidetrans', 'hidelinks', 'hideredirs' ) as $type ) {
351 $chosen = $this->$type;
352 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ? $show : $hide );
353 $url = $this->request->appendQueryValue( $type, intval( !$chosen ), true );
354 $links[] = $this->makeSelfLink( $msg, $url );
355 }
356 return '<p>' . implode( '&nbsp;|&nbsp;', $links ) . '</p>';
357 }
358
359 }