737e6834f41819bafecafac30bec4c9fd99523db
[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 $n = 0;
214
215 if ( !$fromList ) {
216 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
217 } else {
218 list( $namespace, $fromKey, $from ) = $fromList;
219
220 $dbr =& wfGetDB( DB_SLAVE );
221 $res = $dbr->select( 'page',
222 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
223 array(
224 'page_namespace' => $namespace,
225 'page_title >= ' . $dbr->addQuotes( $fromKey )
226 ),
227 $fname,
228 array(
229 'ORDER BY' => 'page_title',
230 'LIMIT' => $this->maxPerPage + 1,
231 'USE INDEX' => 'name_title',
232 )
233 );
234
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 $dbr =& wfGetDB( DB_SLAVE );
267 $res_prev = $dbr->select(
268 'page',
269 'page_title',
270 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
271 $fname,
272 array( 'ORDER BY' => 'page_title DESC', 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 ) )
273 );
274
275 # Get first title of previous complete chunk
276 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
277 $pt = $dbr->fetchObject( $res_prev );
278 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
279 } else {
280 # The previous chunk is not complete, need to link to the very first title
281 # available in the database
282 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title', array( 'page_namespace' => $namespace ), $fname, array( 'LIMIT' => 1) );
283
284 # Show the previous link if it s not the current requested chunk
285 if( $from != $reallyFirstPage_title ) {
286 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
287 } else {
288 $prevTitle = null;
289 }
290 }
291
292 $nsForm = $this->namespaceForm ( $namespace, $from );
293 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
294 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
295 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
296 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
297 wfMsgHtml ( 'allpages' ) );
298
299 $self = SpecialPage::getTitleFor( 'Allpages' );
300
301 # Do we put a previous link ?
302 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
303 $q = 'from=' . $prevTitle->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
304 $prevLink = $sk->makeKnownLinkObj( $self, wfMsgHTML( 'prevpage', $pt ), $q );
305 $out2 .= ' | ' . $prevLink;
306 }
307
308 if( $n == $this->maxPerPage && $s = $dbr->fetchObject($res) ) {
309 # $s is the first link of the next chunk
310 $t = Title::MakeTitle($namespace, $s->page_title);
311 $q = 'from=' . $t->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
312 $nextLink = $sk->makeKnownLinkObj( $self, wfMsgHtml( 'nextpage', $t->getText() ), $q );
313 $out2 .= ' | ' . $nextLink;
314 }
315 $out2 .= "</td></tr></table><hr />";
316 }
317
318 $wgOut->addHtml( $out2 . $out );
319 if( isset($prevLink) or isset($nextLink) ) {
320 $wgOut->addHtml( '<hr/><p style="font-size: smaller; float: right;">' );
321 if( isset( $prevLink ) )
322 $wgOut->addHTML( $prevLink . ' | ');
323 if( isset( $nextLink ) )
324 $wgOut->addHTML( $nextLink );
325 $wgOut->addHTML( '</p>' );
326
327 }
328
329 }
330
331 /**
332 * @param int $ns the namespace of the article
333 * @param string $text the name of the article
334 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
335 * @static (sort of)
336 * @access private
337 */
338 function getNamespaceKeyAndText ($ns, $text) {
339 if ( $text == '' )
340 return array( $ns, '', '' ); # shortcut for common case
341
342 $t = Title::makeTitleSafe($ns, $text);
343 if ( $t && $t->isLocal() ) {
344 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
345 } else if ( $t ) {
346 return NULL;
347 }
348
349 # try again, in case the problem was an empty pagename
350 $text = preg_replace('/(#|$)/', 'X$1', $text);
351 $t = Title::makeTitleSafe($ns, $text);
352 if ( $t && $t->isLocal() ) {
353 return array( $t->getNamespace(), '', '' );
354 } else {
355 return NULL;
356 }
357 }
358 }
359
360 ?>