Use pl_from and tl_from for offsets/sorting, instead of secondary joined table field...
[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->showErrorPage( 'notargettitle', 'notargettext' );
49 return;
50 }
51
52 $this->target = Title::newFromURL( $targetString );
53 if( !$this->target ) {
54 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
55 return;
56 }
57 $this->selfTitle = Title::makeTitleSafe( NS_SPECIAL,
58 'Whatlinkshere/' . $this->target->getPrefixedDBkey() );
59 $wgOut->setPagetitle( $this->target->getPrefixedText() );
60 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
61
62 $wgOut->addHTML( wfMsg( 'whatlinkshere-barrow' ) . ' ' .$this->skin->makeLinkObj($this->target, '', 'redirect=no' )."<br />\n");
63
64 $this->showIndirectLinks( 0, $this->target, $this->limit, $this->from, $this->back );
65 }
66
67 /**
68 * @param int $level Recursion level
69 * @param Title $target Target title
70 * @param int $limit Number of entries to display
71 * @param Title $from Display from this article ID
72 * @param Title $back Display from this article ID at backwards scrolling
73 * @private
74 */
75 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
76 global $wgOut;
77 $fname = 'WhatLinksHerePage::showIndirectLinks';
78 $dbr = wfGetDB( DB_READ );
79 $options = array();
80
81 $ns = $this->request->getIntOrNull( 'namespace' );
82 if ( isset( $ns ) ) {
83 $options['namespace'] = $ns;
84 $this->setNamespace( $options['namespace'] );
85 } else {
86 $options['namespace'] = '';
87 }
88
89 // Make the query
90 $plConds = array(
91 'page_id=pl_from',
92 'pl_namespace' => $target->getNamespace(),
93 'pl_title' => $target->getDBkey(),
94 );
95
96 $tlConds = array(
97 'page_id=tl_from',
98 'tl_namespace' => $target->getNamespace(),
99 'tl_title' => $target->getDBkey(),
100 );
101
102 if ( $this->namespace !== null ){
103 $plConds['page_namespace'] = (int)$this->namespace;
104 $tlConds['page_namespace'] = (int)$this->namespace;
105 }
106
107 if ( $from ) {
108 $from = (int)$from; // just in case
109 $tlConds[] = "tl_from >= $from";
110 $plConds[] = "pl_from >= $from";
111 }
112
113 // Read an extra row as an at-end check
114 $queryLimit = $limit + 1;
115 $options['LIMIT'] = $queryLimit;
116 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
117
118 $options['ORDER BY'] = 'pl_from';
119 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
120 $plConds, $fname, $options );
121
122 $options['ORDER BY'] = 'tl_from';
123 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
124 $tlConds, $fname, $options );
125
126 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
127 if ( 0 == $level && !isset( $this->namespace ) ) {
128 // really no links to here
129 $wgOut->addWikiText( wfMsg( 'nolinkshere', $this->target->getPrefixedText() ) );
130 } elseif ( 0 == $level && isset( $this->namespace ) ) {
131 // no links from requested namespace to here
132 $options = array(); // reinitialize for a further namespace search
133 $options['namespace'] = $this->namespace;
134 $options['target'] = $this->target->getPrefixedText();
135 list( $options['limit'], $options['offset']) = wfCheckLimits();
136 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
137 $wgOut->addWikiText( wfMsg( 'nolinkshere-ns', $this->target->getPrefixedText() ) );
138 }
139 return;
140 }
141
142 $options = array();
143 list( $options['limit'], $options['offset']) = wfCheckLimits();
144 if ( ( $ns = $this->request->getVal( 'namespace', null ) ) !== null && $ns !== '' && ctype_digit($ns) ) {
145 $options['namespace'] = intval( $ns );
146 $this->setNamespace( $options['namespace'] );
147 } else {
148 $options['namespace'] = '';
149 $this->setNamespace( null );
150 }
151 $options['offset'] = $this->request->getVal( 'offset' );
152 /* Offset must be an integral. */
153 if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) )
154 $options['offset'] = '';
155 $options['target'] = $this->target->getPrefixedDBkey();
156
157 // Read the rows into an array and remove duplicates
158 // templatelinks comes second so that the templatelinks row overwrites the
159 // pagelinks row, so we get (inclusion) rather than nothing
160 while ( $row = $dbr->fetchObject( $plRes ) ) {
161 $row->is_template = 0;
162 $rows[$row->page_id] = $row;
163 }
164 $dbr->freeResult( $plRes );
165 while ( $row = $dbr->fetchObject( $tlRes ) ) {
166 $row->is_template = 1;
167 $rows[$row->page_id] = $row;
168 }
169 $dbr->freeResult( $tlRes );
170
171 // Sort by key and then change the keys to 0-based indices
172 ksort( $rows );
173 $rows = array_values( $rows );
174
175 $numRows = count( $rows );
176
177 // Work out the start and end IDs, for prev/next links
178 if ( $numRows > $limit ) {
179 // More rows available after these ones
180 // Get the ID from the last row in the result set
181 $nextId = $rows[$limit]->page_id;
182 // Remove undisplayed rows
183 $rows = array_slice( $rows, 0, $limit );
184 } else {
185 // No more rows after
186 $nextId = false;
187 }
188 $prevId = $from;
189
190 if ( $level == 0 ) {
191 $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
192 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target->getPrefixedText() ) );
193 }
194 $isredir = wfMsg( 'isredirect' );
195 $istemplate = wfMsg( 'istemplate' );
196
197 if( $level == 0 ) {
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[] = $isredir;
219 }
220 if ( $row->is_template ) {
221 $props[] = $istemplate;
222 }
223 if ( count( $props ) ) {
224 // FIXME? Cultural assumption, hard-coded punctuation
225 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
226 }
227
228 if ( $row->page_is_redirect ) {
229 if ( $level < 2 ) {
230 $this->showIndirectLinks( $level + 1, $nt, 500 );
231 }
232 }
233 $wgOut->addHTML( "</li>\n" );
234 }
235 $wgOut->addHTML( "</ul>\n" );
236
237 if( $level == 0 ) {
238 $wgOut->addHTML( $prevnext );
239 }
240 }
241
242 function makeSelfLink( $text, $query ) {
243 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
244 }
245
246 function getPrevNext( $limit, $prevId, $nextId ) {
247 global $wgLang;
248 $fmtLimit = $wgLang->formatNum( $limit );
249 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
250 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
251
252 $nsText = '';
253 if( is_int($this->namespace) ) {
254 $nsText = "&namespace={$this->namespace}";
255 }
256
257 if ( 0 != $prevId ) {
258 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$this->back}{$nsText}" );
259 } else {
260 $prevLink = $prev;
261 }
262 if ( 0 != $nextId ) {
263 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}&back={$prevId}{$nsText}" );
264 } else {
265 $nextLink = $next;
266 }
267 $nums = $this->numLink( 20, $prevId ) . ' | ' .
268 $this->numLink( 50, $prevId ) . ' | ' .
269 $this->numLink( 100, $prevId ) . ' | ' .
270 $this->numLink( 250, $prevId ) . ' | ' .
271 $this->numLink( 500, $prevId );
272
273 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
274 }
275
276 function numLink( $limit, $from, $ns = null ) {
277 global $wgLang;
278 $query = "limit={$limit}&from={$from}";
279 if( is_int($this->namespace) ) { $query .= "&namespace={$this->namespace}";}
280 $fmtLimit = $wgLang->formatNum( $limit );
281 return $this->makeSelfLink( $fmtLimit, $query );
282 }
283
284 function whatlinkshereForm( $options ) {
285 global $wgScript, $wgTitle;
286
287 $options['title'] = $wgTitle->getPrefixedText();
288
289 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
290 '<fieldset>' .
291 Xml::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
292
293 foreach ( $options as $name => $value ) {
294 if( $name === 'namespace') continue;
295 $f .= "\t" . Xml::hidden( $name, $value ). "\n";
296 }
297
298 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
299 Xml::namespaceSelector( $options['namespace'], '' ) .
300 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
301 '</fieldset>' .
302 Xml::closeElement( 'form' ) . "\n";
303
304 return $f;
305 }
306
307 /** Set the namespace we are filtering on */
308 private function setNamespace( $ns ) {
309 $this->namespace = $ns;
310 }
311
312 }
313
314 ?>