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