API bug 10046: incorrect action produces invalid response format
[lhc/web/wiklou.git] / includes / SpecialAllpages.php
1 <?php
2 /**
3 * @addtogroup SpecialPage
4 */
5
6 /**
7 * Entry point : initialise variables and call subfunctions.
8 * @param $par String: becomes "FOO" when called like Special:Allpages/FOO (default NULL)
9 * @param $specialPage See the SpecialPage object.
10 */
11 function wfSpecialAllpages( $par=NULL, $specialPage ) {
12 global $wgRequest, $wgOut, $wgContLang;
13
14 # GET values
15 $from = $wgRequest->getVal( 'from' );
16 $namespace = $wgRequest->getInt( 'namespace' );
17
18 $namespaces = $wgContLang->getNamespaces();
19
20 $indexPage = new SpecialAllpages();
21
22 if( !in_array($namespace, array_keys($namespaces)) )
23 $namespace = 0;
24
25 $wgOut->setPagetitle( $namespace > 0 ?
26 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
27 wfMsg( 'allarticles' )
28 );
29
30 if ( isset($par) ) {
31 $indexPage->showChunk( $namespace, $par, $specialPage->including() );
32 } elseif ( isset($from) ) {
33 $indexPage->showChunk( $namespace, $from, $specialPage->including() );
34 } else {
35 $indexPage->showToplevel ( $namespace, $specialPage->including() );
36 }
37 }
38
39 /**
40 * Implements Special:Allpages
41 * @addtogroup SpecialPage
42 */
43 class SpecialAllpages {
44 var $maxPerPage=960;
45 var $topLevelMax=50;
46 var $name='Allpages';
47 # Determines, which message describes the input field 'nsfrom' (->SpecialPrefixindex.php)
48 var $nsfromMsg='allpagesfrom';
49
50 /**
51 * HTML for the top form
52 * @param integer $namespace A namespace constant (default NS_MAIN).
53 * @param string $from Article name we are starting listing at.
54 */
55 function namespaceForm ( $namespace = NS_MAIN, $from = '' ) {
56 global $wgScript;
57 $t = SpecialPage::getTitleFor( $this->name );
58
59 $namespaceselect = HTMLnamespaceselector($namespace, null);
60
61 $frombox = "<input type='text' size='20' name='from' id='nsfrom' value=\""
62 . htmlspecialchars ( $from ) . '"/>';
63 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" />';
64
65 $out = "<div class='namespaceoptions'><form method='get' action='{$wgScript}'>";
66 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
67 $out .= "
68 <table id='nsselect' class='allpages'>
69 <tr>
70 <td align='right'>" . wfMsgHtml($this->nsfromMsg) . "</td>
71 <td align='left'><label for='nsfrom'>$frombox</label></td>
72 </tr>
73 <tr>
74 <td align='right'><label for='namespace'>" . wfMsgHtml('namespace') . "</label></td>
75 <td align='left'>
76 $namespaceselect $submitbutton
77 </td>
78 </tr>
79 </table>
80 ";
81 $out .= '</form></div>';
82 return $out;
83 }
84
85 /**
86 * @param integer $namespace (default NS_MAIN)
87 */
88 function showToplevel ( $namespace = NS_MAIN, $including = false ) {
89 global $wgOut;
90 $fname = "indexShowToplevel";
91
92 # TODO: Either make this *much* faster or cache the title index points
93 # in the querycache table.
94
95 $dbr = wfGetDB( DB_SLAVE );
96 $out = "";
97 $where = array( 'page_namespace' => $namespace );
98
99 global $wgMemc;
100 $key = wfMemcKey( 'allpages', 'ns', $namespace );
101 $lines = $wgMemc->get( $key );
102
103 if( !is_array( $lines ) ) {
104 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, array( 'LIMIT' => 1 ) );
105 $lastTitle = $firstTitle;
106
107 # This array is going to hold the page_titles in order.
108 $lines = array( $firstTitle );
109
110 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
111 $done = false;
112 for( $i = 0; !$done; ++$i ) {
113 // Fetch the last title of this chunk and the first of the next
114 $chunk = is_null( $lastTitle )
115 ? ''
116 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
117 $res = $dbr->select(
118 'page', /* FROM */
119 'page_title', /* WHAT */
120 $where + array( $chunk),
121 $fname,
122 array ('LIMIT' => 2, 'OFFSET' => $this->maxPerPage - 1, 'ORDER BY' => 'page_title') );
123
124 if ( $s = $dbr->fetchObject( $res ) ) {
125 array_push( $lines, $s->page_title );
126 } else {
127 // Final chunk, but ended prematurely. Go back and find the end.
128 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
129 array(
130 'page_namespace' => $namespace,
131 $chunk
132 ), $fname );
133 array_push( $lines, $endTitle );
134 $done = true;
135 }
136 if( $s = $dbr->fetchObject( $res ) ) {
137 array_push( $lines, $s->page_title );
138 $lastTitle = $s->page_title;
139 } else {
140 // This was a final chunk and ended exactly at the limit.
141 // Rare but convenient!
142 $done = true;
143 }
144 $dbr->freeResult( $res );
145 }
146 $wgMemc->add( $key, $lines, 3600 );
147 }
148
149 // If there are only two or less sections, don't even display them.
150 // Instead, display the first section directly.
151 if( count( $lines ) <= 2 ) {
152 $this->showChunk( $namespace, '', $including );
153 return;
154 }
155
156 # At this point, $lines should contain an even number of elements.
157 $out .= "<table class='allpageslist' style='background: inherit;'>";
158 while ( count ( $lines ) > 0 ) {
159 $inpoint = array_shift ( $lines );
160 $outpoint = array_shift ( $lines );
161 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
162 }
163 $out .= '</table>';
164 $nsForm = $this->namespaceForm ( $namespace, '', false );
165
166 # Is there more?
167 if ( $including ) {
168 $out2 = '';
169 } else {
170 $morelinks = '';
171 if ( $morelinks != '' ) {
172 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
173 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
174 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
175 $out2 .= $morelinks . '</td></tr></table><hr />';
176 } else {
177 $out2 = $nsForm . '<hr />';
178 }
179 }
180
181 $wgOut->addHtml( $out2 . $out );
182 }
183
184 /**
185 * @todo Document
186 * @param string $from
187 * @param integer $namespace (Default NS_MAIN)
188 */
189 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
190 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
191 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
192 $queryparams = ($namespace ? "namespace=$namespace" : '');
193 $special = SpecialPage::getTitleFor( $this->name, $inpoint );
194 $link = $special->escapeLocalUrl( $queryparams );
195
196 $out = wfMsgHtml(
197 'alphaindexline',
198 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
199 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
200 );
201 return '<tr><td align="right">'.$out.'</td></tr>';
202 }
203
204 /**
205 * @param integer $namespace (Default NS_MAIN)
206 * @param string $from list all pages from this name (default FALSE)
207 */
208 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
209 global $wgOut, $wgUser, $wgContLang;
210
211 $fname = 'indexShowChunk';
212
213 $sk = $wgUser->getSkin();
214
215 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
216 $n = 0;
217
218 if ( !$fromList ) {
219 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
220 } else {
221 list( $namespace, $fromKey, $from ) = $fromList;
222
223 $dbr = wfGetDB( DB_SLAVE );
224 $res = $dbr->select( 'page',
225 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
226 array(
227 'page_namespace' => $namespace,
228 'page_title >= ' . $dbr->addQuotes( $fromKey )
229 ),
230 $fname,
231 array(
232 'ORDER BY' => 'page_title',
233 'LIMIT' => $this->maxPerPage + 1,
234 'USE INDEX' => 'name_title',
235 )
236 );
237
238 $out = '<table style="background: inherit;" border="0" width="100%">';
239
240 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
241 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
242 if( $t ) {
243 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
244 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
245 ($s->page_is_redirect ? '</div>' : '' );
246 } else {
247 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
248 }
249 if( $n % 3 == 0 ) {
250 $out .= '<tr>';
251 }
252 $out .= "<td>$link</td>";
253 $n++;
254 if( $n % 3 == 0 ) {
255 $out .= '</tr>';
256 }
257 }
258 if( ($n % 3) != 0 ) {
259 $out .= '</tr>';
260 }
261 $out .= '</table>';
262 }
263
264 if ( $including ) {
265 $out2 = '';
266 } else {
267 if( $from == '' ) {
268 // First chunk; no previous link.
269 $prevTitle = null;
270 } else {
271 # Get the last title from previous chunk
272 $dbr = wfGetDB( DB_SLAVE );
273 $res_prev = $dbr->select(
274 'page',
275 'page_title',
276 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
277 $fname,
278 array( 'ORDER BY' => 'page_title DESC', 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 ) )
279 );
280
281 # Get first title of previous complete chunk
282 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
283 $pt = $dbr->fetchObject( $res_prev );
284 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
285 } else {
286 # The previous chunk is not complete, need to link to the very first title
287 # available in the database
288 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title', array( 'page_namespace' => $namespace ), $fname, array( 'LIMIT' => 1) );
289
290 # Show the previous link if it s not the current requested chunk
291 if( $from != $reallyFirstPage_title ) {
292 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
293 } else {
294 $prevTitle = null;
295 }
296 }
297 }
298
299 $nsForm = $this->namespaceForm ( $namespace, $from );
300 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
301 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
302 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
303 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
304 wfMsgHtml ( 'allpages' ) );
305
306 $self = SpecialPage::getTitleFor( 'Allpages' );
307
308 # Do we put a previous link ?
309 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
310 $q = 'from=' . $prevTitle->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
311 $prevLink = $sk->makeKnownLinkObj( $self, wfMsgHTML( 'prevpage', $pt ), $q );
312 $out2 .= ' | ' . $prevLink;
313 }
314
315 if( $n == $this->maxPerPage && $s = $dbr->fetchObject($res) ) {
316 # $s is the first link of the next chunk
317 $t = Title::MakeTitle($namespace, $s->page_title);
318 $q = 'from=' . $t->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
319 $nextLink = $sk->makeKnownLinkObj( $self, wfMsgHtml( 'nextpage', $t->getText() ), $q );
320 $out2 .= ' | ' . $nextLink;
321 }
322 $out2 .= "</td></tr></table><hr />";
323 }
324
325 $wgOut->addHtml( $out2 . $out );
326 if( isset($prevLink) or isset($nextLink) ) {
327 $wgOut->addHtml( '<hr/><p style="font-size: smaller; float: right;">' );
328 if( isset( $prevLink ) )
329 $wgOut->addHTML( $prevLink . ' | ');
330 if( isset( $nextLink ) )
331 $wgOut->addHTML( $nextLink );
332 $wgOut->addHTML( '</p>' );
333
334 }
335
336 }
337
338 /**
339 * @param int $ns the namespace of the article
340 * @param string $text the name of the article
341 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
342 * @static (sort of)
343 * @access private
344 */
345 function getNamespaceKeyAndText ($ns, $text) {
346 if ( $text == '' )
347 return array( $ns, '', '' ); # shortcut for common case
348
349 $t = Title::makeTitleSafe($ns, $text);
350 if ( $t && $t->isLocal() ) {
351 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
352 } else if ( $t ) {
353 return NULL;
354 }
355
356 # try again, in case the problem was an empty pagename
357 $text = preg_replace('/(#|$)/', 'X$1', $text);
358 $t = Title::makeTitleSafe($ns, $text);
359 if ( $t && $t->isLocal() ) {
360 return array( $t->getNamespace(), '', '' );
361 } else {
362 return NULL;
363 }
364 }
365 }
366
367 ?>