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