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