A few comment tag tweaks.
[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 $fname = "indexShowToplevel";
90 $align = $wgContLang->isRtl() ? 'left' : 'right';
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>' . $nsForm;
174 $out2 .= '</td><td align="' . $align . '" 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 global $wgContLang;
191 $align = $wgContLang->isRtl() ? 'left' : 'right';
192 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
193 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
194 $queryparams = ($namespace ? "namespace=$namespace" : '');
195 $special = SpecialPage::getTitleFor( $this->name, $inpoint );
196 $link = $special->escapeLocalUrl( $queryparams );
197
198 $out = wfMsgHtml(
199 'alphaindexline',
200 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
201 "</a></td><td><a href=\"$link\">$outpointf</a>"
202 );
203 return '<tr><td align="' . $align . '">'.$out.'</td></tr>';
204 }
205
206 /**
207 * @param integer $namespace (Default NS_MAIN)
208 * @param string $from list all pages from this name (default FALSE)
209 */
210 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
211 global $wgOut, $wgUser, $wgContLang;
212
213 $fname = 'indexShowChunk';
214 $sk = $wgUser->getSkin();
215
216 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
217 $namespaces = $wgContLang->getNamespaces();
218 $align = $wgContLang->isRtl() ? 'left' : 'right';
219
220 $n = 0;
221
222 if ( !$fromList ) {
223 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
224 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
225 // Show errormessage and reset to NS_MAIN
226 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
227 $namespace = NS_MAIN;
228 } else {
229 list( $namespace, $fromKey, $from ) = $fromList;
230
231 $dbr = wfGetDB( DB_SLAVE );
232 $res = $dbr->select( 'page',
233 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
234 array(
235 'page_namespace' => $namespace,
236 'page_title >= ' . $dbr->addQuotes( $fromKey )
237 ),
238 $fname,
239 array(
240 'ORDER BY' => 'page_title',
241 'LIMIT' => $this->maxPerPage + 1,
242 'USE INDEX' => 'name_title',
243 )
244 );
245
246 $out = '<table style="background: inherit;" border="0" width="100%">';
247
248 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
249 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
250 if( $t ) {
251 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
252 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
253 ($s->page_is_redirect ? '</div>' : '' );
254 } else {
255 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
256 }
257 if( $n % 3 == 0 ) {
258 $out .= '<tr>';
259 }
260 $out .= "<td>$link</td>";
261 $n++;
262 if( $n % 3 == 0 ) {
263 $out .= '</tr>';
264 }
265 }
266 if( ($n % 3) != 0 ) {
267 $out .= '</tr>';
268 }
269 $out .= '</table>';
270 }
271
272 if ( $including ) {
273 $out2 = '';
274 } else {
275 if( $from == '' ) {
276 // First chunk; no previous link.
277 $prevTitle = null;
278 } else {
279 # Get the last title from previous chunk
280 $dbr = wfGetDB( DB_SLAVE );
281 $res_prev = $dbr->select(
282 'page',
283 'page_title',
284 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
285 $fname,
286 array( 'ORDER BY' => 'page_title DESC', 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 ) )
287 );
288
289 # Get first title of previous complete chunk
290 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
291 $pt = $dbr->fetchObject( $res_prev );
292 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
293 } else {
294 # The previous chunk is not complete, need to link to the very first title
295 # available in the database
296 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title', array( 'page_namespace' => $namespace ), $fname, array( 'LIMIT' => 1) );
297
298 # Show the previous link if it s not the current requested chunk
299 if( $from != $reallyFirstPage_title ) {
300 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
301 } else {
302 $prevTitle = null;
303 }
304 }
305 }
306
307 $nsForm = $this->namespaceForm ( $namespace, $from );
308 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
309 $out2 .= '<tr valign="top"><td>' . $nsForm;
310 $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">' .
311 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
312 wfMsgHtml ( 'allpages' ) );
313
314 $self = SpecialPage::getTitleFor( 'Allpages' );
315
316 # Do we put a previous link ?
317 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
318 $q = 'from=' . $prevTitle->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
319 $prevLink = $sk->makeKnownLinkObj( $self, wfMsgHTML( 'prevpage', $pt ), $q );
320 $out2 .= ' | ' . $prevLink;
321 }
322
323 if( $n == $this->maxPerPage && $s = $dbr->fetchObject($res) ) {
324 # $s is the first link of the next chunk
325 $t = Title::MakeTitle($namespace, $s->page_title);
326 $q = 'from=' . $t->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
327 $nextLink = $sk->makeKnownLinkObj( $self, wfMsgHtml( 'nextpage', $t->getText() ), $q );
328 $out2 .= ' | ' . $nextLink;
329 }
330 $out2 .= "</td></tr></table><hr />";
331 }
332
333 $wgOut->addHtml( $out2 . $out );
334 if( isset($prevLink) or isset($nextLink) ) {
335 $wgOut->addHtml( '<hr /><p style="font-size: smaller; float: ' . $align . '">' );
336 if( isset( $prevLink ) ) {
337 $wgOut->addHTML( $prevLink );
338 }
339 if( isset( $prevLink ) && isset( $nextLink ) ) {
340 $wgOut->addHTML( ' | ' );
341 }
342 if( isset( $nextLink ) ) {
343 $wgOut->addHTML( $nextLink );
344 }
345 $wgOut->addHTML( '</p>' );
346
347 }
348
349 }
350
351 /**
352 * @param int $ns the namespace of the article
353 * @param string $text the name of the article
354 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
355 * @static (sort of)
356 * @access private
357 */
358 function getNamespaceKeyAndText ($ns, $text) {
359 if ( $text == '' )
360 return array( $ns, '', '' ); # shortcut for common case
361
362 $t = Title::makeTitleSafe($ns, $text);
363 if ( $t && $t->isLocal() ) {
364 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
365 } else if ( $t ) {
366 return NULL;
367 }
368
369 # try again, in case the problem was an empty pagename
370 $text = preg_replace('/(#|$)/', 'X$1', $text);
371 $t = Title::makeTitleSafe($ns, $text);
372 if ( $t && $t->isLocal() ) {
373 return array( $t->getNamespace(), '', '' );
374 } else {
375 return NULL;
376 }
377 }
378 }
379
380 ?>