]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/SpecialAllpages.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.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                 $options = array( 'LIMIT' => 1 );
105                 if ( ! $dbr->implicitOrderby() ) {
106                         $options['ORDER BY'] = 'page_title';
107                 }
108                 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, $options );
109                 $lastTitle = $firstTitle;
110
111                 # This array is going to hold the page_titles in order.
112                 $lines = array( $firstTitle );
113
114                 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
115                 $done = false;
116                 for( $i = 0; !$done; ++$i ) {
117                         // Fetch the last title of this chunk and the first of the next
118                         $chunk = is_null( $lastTitle )
119                                 ? ''
120                                 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
121                         $res = $dbr->select(
122                                 'page', /* FROM */
123                                 'page_title', /* WHAT */
124                                 $where + array( $chunk),
125                                 $fname,
126                                 array ('LIMIT' => 2, 'OFFSET' => $this->maxPerPage - 1, 'ORDER BY' => 'page_title') );
127
128                         if ( $s = $dbr->fetchObject( $res ) ) {
129                                 array_push( $lines, $s->page_title );
130                         } else {
131                                 // Final chunk, but ended prematurely. Go back and find the end.
132                                 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
133                                         array(
134                                                 'page_namespace' => $namespace,
135                                                 $chunk
136                                         ), $fname );
137                                 array_push( $lines, $endTitle );
138                                 $done = true;
139                         }
140                         if( $s = $dbr->fetchObject( $res ) ) {
141                                 array_push( $lines, $s->page_title );
142                                 $lastTitle = $s->page_title;
143                         } else {
144                                 // This was a final chunk and ended exactly at the limit.
145                                 // Rare but convenient!
146                                 $done = true;
147                         }
148                         $dbr->freeResult( $res );
149                 }
150                 $wgMemc->add( $key, $lines, 3600 );
151         }
152
153         // If there are only two or less sections, don't even display them.
154         // Instead, display the first section directly.
155         if( count( $lines ) <= 2 ) {
156                 $this->showChunk( $namespace, '', $including );
157                 return;
158         }
159
160         # At this point, $lines should contain an even number of elements.
161         $out .= "<table class='allpageslist' style='background: inherit;'>";
162         while ( count ( $lines ) > 0 ) {
163                 $inpoint = array_shift ( $lines );
164                 $outpoint = array_shift ( $lines );
165                 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
166         }
167         $out .= '</table>';
168         $nsForm = $this->namespaceForm ( $namespace, '', false );
169
170         # Is there more?
171         if ( $including ) {
172                 $out2 = '';
173         } else {
174                 $morelinks = '';
175                 if ( $morelinks != '' ) {
176                         $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
177                         $out2 .= '<tr valign="top"><td>' . $nsForm;
178                         $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">';
179                         $out2 .= $morelinks . '</td></tr></table><hr />';
180                 } else {
181                         $out2 = $nsForm . '<hr />';
182                 }
183         }
184
185         $wgOut->addHtml( $out2 . $out );
186 }
187
188 /**
189  * @todo Document
190  * @param string $from
191  * @param integer $namespace (Default NS_MAIN)
192  */
193 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
194         global $wgContLang;
195         $align = $wgContLang->isRtl() ? 'left' : 'right';
196         $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
197         $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
198         $queryparams = ($namespace ? "namespace=$namespace" : '');
199         $special = SpecialPage::getTitleFor( $this->name, $inpoint );
200         $link = $special->escapeLocalUrl( $queryparams );
201
202         $out = wfMsgHtml(
203                 'alphaindexline',
204                 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
205                 "</a></td><td><a href=\"$link\">$outpointf</a>"
206         );
207         return '<tr><td align="' . $align . '">'.$out.'</td></tr>';
208 }
209
210 /**
211  * @param integer $namespace (Default NS_MAIN)
212  * @param string $from list all pages from this name (default FALSE)
213  */
214 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
215         global $wgOut, $wgUser, $wgContLang;
216
217         $fname = 'indexShowChunk';
218         $sk = $wgUser->getSkin();
219
220         $fromList = $this->getNamespaceKeyAndText($namespace, $from);
221         $namespaces = $wgContLang->getNamespaces();
222         $align = $wgContLang->isRtl() ? 'left' : 'right';
223
224         $n = 0;
225
226         if ( !$fromList ) {
227                 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
228         } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
229                 // Show errormessage and reset to NS_MAIN
230                 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
231                 $namespace = NS_MAIN;
232         } else {
233                 list( $namespace, $fromKey, $from ) = $fromList;
234
235                 $dbr = wfGetDB( DB_SLAVE );
236                 $res = $dbr->select( 'page',
237                         array( 'page_namespace', 'page_title', 'page_is_redirect' ),
238                         array(
239                                 'page_namespace' => $namespace,
240                                 'page_title >= ' . $dbr->addQuotes( $fromKey )
241                         ),
242                         $fname,
243                         array(
244                                 'ORDER BY'  => 'page_title',
245                                 'LIMIT'     => $this->maxPerPage + 1,
246                                 'USE INDEX' => 'name_title',
247                         )
248                 );
249
250                 $out = '<table style="background: inherit;" border="0" width="100%">';
251
252                 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
253                         $t = Title::makeTitle( $s->page_namespace, $s->page_title );
254                         if( $t ) {
255                                 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
256                                         $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
257                                         ($s->page_is_redirect ? '</div>' : '' );
258                         } else {
259                                 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
260                         }
261                         if( $n % 3 == 0 ) {
262                                 $out .= '<tr>';
263                         }
264                         $out .= "<td>$link</td>";
265                         $n++;
266                         if( $n % 3 == 0 ) {
267                                 $out .= '</tr>';
268                         }
269                 }
270                 if( ($n % 3) != 0 ) {
271                         $out .= '</tr>';
272                 }
273                 $out .= '</table>';
274         }
275
276         if ( $including ) {
277                 $out2 = '';
278         } else {
279                 if( $from == '' ) {
280                         // First chunk; no previous link.
281                         $prevTitle = null;
282                 } else {
283                         # Get the last title from previous chunk
284                         $dbr = wfGetDB( DB_SLAVE );
285                         $res_prev = $dbr->select(
286                                 'page',
287                                 'page_title',
288                                 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
289                                 $fname,
290                                 array( 'ORDER BY' => 'page_title DESC', 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 ) )
291                         );
292
293                         # Get first title of previous complete chunk
294                         if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
295                                 $pt = $dbr->fetchObject( $res_prev );
296                                 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
297                         } else {
298                                 # The previous chunk is not complete, need to link to the very first title
299                                 # available in the database
300                                 $options = array( 'LIMIT' => 1 );
301                                 if ( ! $dbr->implicitOrderby() ) {
302                                         $options['ORDER BY'] = 'page_title';
303                                 }
304                                 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title', array( 'page_namespace' => $namespace ), $fname, $options );
305                                 # Show the previous link if it s not the current requested chunk
306                                 if( $from != $reallyFirstPage_title ) {
307                                         $prevTitle =  Title::makeTitle( $namespace, $reallyFirstPage_title );
308                                 } else {
309                                         $prevTitle = null;
310                                 }
311                         }
312                 }
313
314                 $nsForm = $this->namespaceForm ( $namespace, $from );
315                 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
316                 $out2 .= '<tr valign="top"><td>' . $nsForm;
317                 $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">' .
318                                 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
319                                         wfMsgHtml ( 'allpages' ) );
320
321                 $self = SpecialPage::getTitleFor( 'Allpages' );
322
323                 # Do we put a previous link ?
324                 if( isset( $prevTitle ) &&  $pt = $prevTitle->getText() ) {
325                         $q = 'from=' . $prevTitle->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
326                         $prevLink = $sk->makeKnownLinkObj( $self, wfMsgHTML( 'prevpage', $pt ), $q );
327                         $out2 .= ' | ' . $prevLink;
328                 }
329
330                 if( $n == $this->maxPerPage && $s = $dbr->fetchObject($res) ) {
331                         # $s is the first link of the next chunk
332                         $t = Title::MakeTitle($namespace, $s->page_title);
333                         $q = 'from=' . $t->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
334                         $nextLink = $sk->makeKnownLinkObj( $self, wfMsgHtml( 'nextpage', $t->getText() ), $q );
335                         $out2 .= ' | ' . $nextLink;
336                 }
337                 $out2 .= "</td></tr></table><hr />";
338         }
339
340         $wgOut->addHtml( $out2 . $out );
341         if( isset($prevLink) or isset($nextLink) ) {
342                 $wgOut->addHtml( '<hr /><p style="font-size: smaller; float: ' . $align . '">' );
343                 if( isset( $prevLink ) ) {
344                         $wgOut->addHTML( $prevLink );
345                 }
346                 if( isset( $prevLink ) && isset( $nextLink ) ) {
347                         $wgOut->addHTML( ' | ' );
348                 }
349                 if( isset( $nextLink ) ) {
350                         $wgOut->addHTML( $nextLink );
351                 }
352                 $wgOut->addHTML( '</p>' );
353
354         }
355         
356 }
357         
358 /**
359  * @param int $ns the namespace of the article
360  * @param string $text the name of the article
361  * @return array( int namespace, string dbkey, string pagename ) or NULL on error
362  * @static (sort of)
363  * @access private
364  */
365 function getNamespaceKeyAndText ($ns, $text) {
366         if ( $text == '' )
367                 return array( $ns, '', '' ); # shortcut for common case
368
369         $t = Title::makeTitleSafe($ns, $text);
370         if ( $t && $t->isLocal() ) {
371                 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
372         } else if ( $t ) {
373                 return NULL;
374         }
375
376         # try again, in case the problem was an empty pagename
377         $text = preg_replace('/(#|$)/', 'X$1', $text);
378         $t = Title::makeTitleSafe($ns, $text);
379         if ( $t && $t->isLocal() ) {
380                 return array( $t->getNamespace(), '', '' );
381         } else {
382                 return NULL;
383         }
384 }
385 }
386
387 ?>