Implements bug #4673 : Special:Allpages should display "previous" link
[lhc/web/wiklou.git] / includes / SpecialAllpages.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 /**
8 * Entry point : initialise variables and call subfunctions.
9 * @param $par String: becomes "FOO" when called like Special:Allpages/FOO (default NULL)
10 * @param $specialPage @see SpecialPage object.
11 */
12 function wfSpecialAllpages( $par=NULL, $specialPage ) {
13 global $wgRequest, $wgOut, $wgContLang;
14
15 # GET values
16 $from = $wgRequest->getVal( 'from' );
17 $namespace = $wgRequest->getInt( 'namespace' );
18
19 $namespaces = $wgContLang->getNamespaces();
20
21 $indexPage = new SpecialAllpages();
22
23 if( !in_array($namespace, array_keys($namespaces)) )
24 $namespace = 0;
25
26 $wgOut->setPagetitle( $namespace > 0 ?
27 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
28 wfMsg( 'allarticles' )
29 );
30
31 if ( isset($par) ) {
32 $indexPage->showChunk( $namespace, $par, $specialPage->including() );
33 } elseif ( isset($from) ) {
34 $indexPage->showChunk( $namespace, $from, $specialPage->including() );
35 } else {
36 $indexPage->showToplevel ( $namespace, $specialPage->including() );
37 }
38 }
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;
54 $t = SpecialPage::getTitleFor( $this->name );
55
56 $namespaceselect = HTMLnamespaceselector($namespace, null);
57
58 $frombox = "<input type='text' size='20' name='from' id='nsfrom' value=\""
59 . htmlspecialchars ( $from ) . '"/>';
60 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" />';
61
62 $out = "<div class='namespaceoptions'><form method='get' action='{$wgScript}'>";
63 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
64 $out .= "
65 <table id='nsselect' class='allpages'>
66 <tr>
67 <td align='right'>" . wfMsgHtml($this->nsfromMsg) . "</td>
68 <td align='left'><label for='nsfrom'>$frombox</label></td>
69 </tr>
70 <tr>
71 <td align='right'><label for='namespace'>" . wfMsgHtml('namespace') . "</label></td>
72 <td align='left'>
73 $namespaceselect $submitbutton
74 </td>
75 </tr>
76 </table>
77 ";
78 $out .= '</form></div>';
79 return $out;
80 }
81
82 /**
83 * @param integer $namespace (default NS_MAIN)
84 */
85 function showToplevel ( $namespace = NS_MAIN, $including = false ) {
86 global $wgOut;
87 $fname = "indexShowToplevel";
88
89 # TODO: Either make this *much* faster or cache the title index points
90 # in the querycache table.
91
92 $dbr =& wfGetDB( DB_SLAVE );
93 $out = "";
94 $where = array( 'page_namespace' => $namespace );
95
96 global $wgMemc;
97 $key = wfMemcKey( 'allpages', 'ns', $namespace );
98 $lines = $wgMemc->get( $key );
99
100 if( !is_array( $lines ) ) {
101 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, array( 'LIMIT' => 1 ) );
102 $lastTitle = $firstTitle;
103
104 # This array is going to hold the page_titles in order.
105 $lines = array( $firstTitle );
106
107 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
108 $done = false;
109 for( $i = 0; !$done; ++$i ) {
110 // Fetch the last title of this chunk and the first of the next
111 $chunk = is_null( $lastTitle )
112 ? ''
113 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
114 $res = $dbr->select(
115 'page', /* FROM */
116 'page_title', /* WHAT */
117 $where + array( $chunk),
118 $fname,
119 array ('LIMIT' => 2, 'OFFSET' => $this->maxPerPage - 1, 'ORDER BY' => 'page_title') );
120
121 if ( $s = $dbr->fetchObject( $res ) ) {
122 array_push( $lines, $s->page_title );
123 } else {
124 // Final chunk, but ended prematurely. Go back and find the end.
125 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
126 array(
127 'page_namespace' => $namespace,
128 $chunk
129 ), $fname );
130 array_push( $lines, $endTitle );
131 $done = true;
132 }
133 if( $s = $dbr->fetchObject( $res ) ) {
134 array_push( $lines, $s->page_title );
135 $lastTitle = $s->page_title;
136 } else {
137 // This was a final chunk and ended exactly at the limit.
138 // Rare but convenient!
139 $done = true;
140 }
141 $dbr->freeResult( $res );
142 }
143 $wgMemc->add( $key, $lines, 3600 );
144 }
145
146 // If there are only two or less sections, don't even display them.
147 // Instead, display the first section directly.
148 if( count( $lines ) <= 2 ) {
149 $this->showChunk( $namespace, '', $including );
150 return;
151 }
152
153 # At this point, $lines should contain an even number of elements.
154 $out .= "<table class='allpageslist' style='background: inherit;'>";
155 while ( count ( $lines ) > 0 ) {
156 $inpoint = array_shift ( $lines );
157 $outpoint = array_shift ( $lines );
158 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
159 }
160 $out .= '</table>';
161 $nsForm = $this->namespaceForm ( $namespace, '', false );
162
163 # Is there more?
164 if ( $including ) {
165 $out2 = '';
166 } else {
167 $morelinks = '';
168 if ( $morelinks != '' ) {
169 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
170 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
171 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
172 $out2 .= $morelinks . '</td></tr></table><hr />';
173 } else {
174 $out2 = $nsForm . '<hr />';
175 }
176 }
177
178 $wgOut->addHtml( $out2 . $out );
179 }
180
181 /**
182 * @todo Document
183 * @param string $from
184 * @param integer $namespace (Default NS_MAIN)
185 */
186 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
187 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
188 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
189 $queryparams = ($namespace ? "namespace=$namespace" : '');
190 $special = SpecialPage::getTitleFor( $this->name, $inpoint );
191 $link = $special->escapeLocalUrl( $queryparams );
192
193 $out = wfMsgHtml(
194 'alphaindexline',
195 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
196 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
197 );
198 return '<tr><td align="right">'.$out.'</td></tr>';
199 }
200
201 /**
202 * @param integer $namespace (Default NS_MAIN)
203 * @param string $from list all pages from this name (default FALSE)
204 */
205 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
206 global $wgOut, $wgUser, $wgContLang;
207
208 $fname = 'indexShowChunk';
209
210 $sk = $wgUser->getSkin();
211
212 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
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 $n = 0;
235 $out = '<table style="background: inherit;" border="0" width="100%">';
236
237 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
238 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
239 if( $t ) {
240 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
241 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
242 ($s->page_is_redirect ? '</div>' : '' );
243 } else {
244 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
245 }
246 if( $n % 3 == 0 ) {
247 $out .= '<tr>';
248 }
249 $out .= "<td>$link</td>";
250 $n++;
251 if( $n % 3 == 0 ) {
252 $out .= '</tr>';
253 }
254 }
255 if( ($n % 3) != 0 ) {
256 $out .= '</tr>';
257 }
258 $out .= '</table>';
259 }
260
261 if ( $including ) {
262 $out2 = '';
263 } else {
264
265 # Get the last title from previous chunk
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 ?>