3 * @addtogroup SpecialPage
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.
11 function wfSpecialAllpages( $par=NULL, $specialPage ) {
12 global $wgRequest, $wgOut, $wgContLang;
15 $from = $wgRequest->getVal( 'from' );
16 $namespace = $wgRequest->getInt( 'namespace' );
18 $namespaces = $wgContLang->getNamespaces();
20 $indexPage = new SpecialAllpages();
22 $wgOut->setPagetitle( ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
23 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
24 wfMsg( 'allarticles' )
28 $indexPage->showChunk( $namespace, $par, $specialPage->including() );
29 } elseif ( isset($from) ) {
30 $indexPage->showChunk( $namespace, $from, $specialPage->including() );
32 $indexPage->showToplevel ( $namespace, $specialPage->including() );
37 * Implements Special:Allpages
38 * @addtogroup SpecialPage
40 class SpecialAllpages
{
44 # Determines, which message describes the input field 'nsfrom' (->SpecialPrefixindex.php)
45 var $nsfromMsg='allpagesfrom';
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.
52 function namespaceForm ( $namespace = NS_MAIN
, $from = '' ) {
53 global $wgScript, $wgContLang;
54 $t = SpecialPage
::getTitleFor( $this->name
);
55 $align = $wgContLang->isRtl() ?
'left' : 'right';
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' ) );
62 <td align='$align'>" .
63 Xml
::label( wfMsg( $this->nsfromMsg
), 'nsfrom' ) .
66 Xml
::input( 'from', 20, htmlspecialchars ( $from ), array( 'id' => 'nsfrom' ) ) .
70 <td align='$align'>" .
71 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
74 Xml
::namespaceSelector( $namespace, null ) .
75 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
78 $out .= Xml
::closeElement( 'table' );
79 $out .= Xml
::closeElement( 'form' );
80 $out .= Xml
::closeElement( 'div' );
85 * @param integer $namespace (default NS_MAIN)
87 function showToplevel ( $namespace = NS_MAIN
, $including = false ) {
88 global $wgOut, $wgContLang;
89 $align = $wgContLang->isRtl() ?
'left' : 'right';
91 # TODO: Either make this *much* faster or cache the title index points
92 # in the querycache table.
94 $dbr = wfGetDB( DB_SLAVE
);
96 $where = array( 'page_namespace' => $namespace );
99 $key = wfMemcKey( 'allpages', 'ns', $namespace );
100 $lines = $wgMemc->get( $key );
102 if( !is_array( $lines ) ) {
103 $options = array( 'LIMIT' => 1 );
104 if ( ! $dbr->implicitOrderby() ) {
105 $options['ORDER BY'] = 'page_title';
107 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__
, $options );
108 $lastTitle = $firstTitle;
110 # This array is going to hold the page_titles in order.
111 $lines = array( $firstTitle );
113 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
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 )
119 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
122 'page_title', /* WHAT */
123 $where +
array( $chunk),
125 array ('LIMIT' => 2, 'OFFSET' => $this->maxPerPage
- 1, 'ORDER BY' => 'page_title') );
127 if ( $s = $dbr->fetchObject( $res ) ) {
128 array_push( $lines, $s->page_title
);
130 // Final chunk, but ended prematurely. Go back and find the end.
131 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
133 'page_namespace' => $namespace,
136 array_push( $lines, $endTitle );
139 if( $s = $dbr->fetchObject( $res ) ) {
140 array_push( $lines, $s->page_title
);
141 $lastTitle = $s->page_title
;
143 // This was a final chunk and ended exactly at the limit.
144 // Rare but convenient!
147 $dbr->freeResult( $res );
149 $wgMemc->add( $key, $lines, 3600 );
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 );
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 );
167 $nsForm = $this->namespaceForm ( $namespace, '', false );
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 />';
180 $out2 = $nsForm . '<hr />';
184 $wgOut->addHtml( $out2 . $out );
189 * @param string $from
190 * @param integer $namespace (Default NS_MAIN)
192 function showline( $inpoint, $outpoint, $namespace = NS_MAIN
) {
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 );
203 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
204 "</a></td><td><a href=\"$link\">$outpointf</a>"
206 return '<tr><td align="' . $align . '">'.$out.'</td></tr>';
210 * @param integer $namespace (Default NS_MAIN)
211 * @param string $from list all pages from this name (default FALSE)
213 function showChunk( $namespace = NS_MAIN
, $from, $including = false ) {
214 global $wgOut, $wgUser, $wgContLang;
216 $sk = $wgUser->getSkin();
218 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
219 $namespaces = $wgContLang->getNamespaces();
220 $align = $wgContLang->isRtl() ?
'left' : 'right';
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
;
231 list( $namespace, $fromKey, $from ) = $fromList;
233 $dbr = wfGetDB( DB_SLAVE
);
234 $res = $dbr->select( 'page',
235 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
237 'page_namespace' => $namespace,
238 'page_title >= ' . $dbr->addQuotes( $fromKey )
242 'ORDER BY' => 'page_title',
243 'LIMIT' => $this->maxPerPage +
1,
244 'USE INDEX' => 'name_title',
248 $out = '<table style="background: inherit;" border="0" width="100%">';
250 while( ($n < $this->maxPerPage
) && ($s = $dbr->fetchObject( $res )) ) {
251 $t = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
253 $link = ($s->page_is_redirect ?
'<div class="allpagesredirect">' : '' ) .
254 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
255 ($s->page_is_redirect ?
'</div>' : '' );
257 $link = '[[' . htmlspecialchars( $s->page_title
) . ']]';
262 $out .= "<td>$link</td>";
268 if( ($n %
3) != 0 ) {
278 // First chunk; no previous link.
281 # Get the last title from previous chunk
282 $dbr = wfGetDB( DB_SLAVE
);
283 $res_prev = $dbr->select(
286 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
288 array( 'ORDER BY' => 'page_title DESC', 'LIMIT' => $this->maxPerPage
, 'OFFSET' => ($this->maxPerPage
- 1 ) )
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
);
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';
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 );
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' ) );
319 $self = SpecialPage
::getTitleFor( 'Allpages' );
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;
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;
335 $out2 .= "</td></tr></table><hr />";
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 );
344 if( isset( $prevLink ) && isset( $nextLink ) ) {
345 $wgOut->addHTML( ' | ' );
347 if( isset( $nextLink ) ) {
348 $wgOut->addHTML( $nextLink );
350 $wgOut->addHTML( '</p>' );
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
363 function getNamespaceKeyAndText ($ns, $text) {
365 return array( $ns, '', '' ); # shortcut for common case
367 $t = Title
::makeTitleSafe($ns, $text);
368 if ( $t && $t->isLocal() ) {
369 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
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(), '', '' );