Remove last of $wgEnablePersistentCookies. Use > 0 checks on $wgCookieExpiration...
[lhc/web/wiklou.git] / includes / specials / SpecialAllpages.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 * Note that Special:Prefixindex.php depends on this
6 */
7
8 /**
9 * Entry point : initialise variables and call subfunctions.
10 * @param $par String: becomes "FOO" when called like Special:Allpages/FOO (default NULL)
11 * @param $specialPage See the SpecialPage object.
12 */
13 function wfSpecialAllpages( $par=NULL, $specialPage ) {
14 global $wgRequest, $wgOut, $wgContLang;
15
16 # GET values
17 $from = $wgRequest->getVal( 'from', '' );
18 $to = $wgRequest->getVal( 'to', '' );
19 $namespace = $wgRequest->getInt( 'namespace' );
20
21 $namespaces = $wgContLang->getNamespaces();
22
23 $wgOut->setPagetitle( ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
24 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
25 wfMsg( 'allarticles' )
26 );
27
28 if( isset($par) ) {
29 $indexPage = new SpecialPrefixIndex();
30 $indexPage->showPrefixChunk( $namespace, $par, $specialPage->including(), $from );
31 } else {
32 $indexPage = new SpecialAllpages();
33 $indexPage->showToplevel( $namespace, $from, $to, $specialPage->including() );
34 }
35 }
36
37 /**
38 * Implements Special:Allpages
39 * @ingroup SpecialPage
40 */
41 class SpecialAllpages {
42 /**
43 * Maximum number of pages to show on single list subpage.
44 */
45 protected $maxPerPage = 345;
46
47 /**
48 * Maximum number of pages to show on single index subpage.
49 */
50 protected $maxLineCount = 200;
51
52 /**
53 * Maximum number of chars to show for an entry.
54 */
55 protected $maxPageLength = 70;
56
57 /**
58 * Name of this special page. Used to make title objects that reference back
59 * to this page.
60 */
61 protected $name = 'Allpages';
62
63 /**
64 * HTML for the top form
65 * @param integer $namespace A namespace constant (default NS_MAIN).
66 * @param string $from dbKey we are starting listing at.
67 * @param string $to dbKey we are ending listing at.
68 */
69 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '' ) {
70 global $wgScript;
71 $t = SpecialPage::getTitleFor( $this->name );
72
73 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
74 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
75 $out .= Xml::hidden( 'title', $t->getPrefixedText() );
76 $out .= Xml::openElement( 'fieldset' );
77 $out .= Xml::element( 'legend', null, wfMsg( 'allpages' ) );
78 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
79 $out .= "<tr>
80 <td class='mw-label'>" .
81 Xml::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
82 "</td>
83 <td class='mw-input'>" .
84 Xml::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
85 "</td>
86 </tr>
87 <tr>
88 <td class='mw-label'>" .
89 Xml::label( wfMsg( 'allpagesto' ), 'nsto' ) .
90 "</td>
91 <td class='mw-input'>" .
92 Xml::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
93 "</td>
94 </tr>
95 <tr>
96 <td class='mw-label'>" .
97 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
98 "</td>
99 <td class='mw-input'>" .
100 Xml::namespaceSelector( $namespace, null ) . ' ' .
101 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
102 "</td>
103 </tr>";
104 $out .= Xml::closeElement( 'table' );
105 $out .= Xml::closeElement( 'fieldset' );
106 $out .= Xml::closeElement( 'form' );
107 $out .= Xml::closeElement( 'div' );
108 return $out;
109 }
110
111 /**
112 * @param integer $namespace (default NS_MAIN)
113 */
114 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $including = false ) {
115 global $wgOut, $wgContLang;
116 $align = $wgContLang->isRtl() ? 'left' : 'right';
117
118 # TODO: Either make this *much* faster or cache the title index points
119 # in the querycache table.
120
121 $dbr = wfGetDB( DB_SLAVE );
122 $out = "";
123 $where = array( 'page_namespace' => $namespace );
124
125 $from = Title::makeTitleSafe( $namespace, $from );
126 $to = Title::makeTitleSafe( $namespace, $to );
127 $from = ( $from && $from->isLocal() ) ? $from->getDBKey() : null;
128 $to = ( $to && $to->isLocal() ) ? $to->getDBKey() : null;
129
130 if( isset($from) )
131 $where[] = 'page_title >= '.$dbr->addQuotes( $from );
132 if( isset($to) )
133 $where[] = 'page_title <= '.$dbr->addQuotes( $to );
134
135 global $wgMemc;
136 $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
137 $lines = $wgMemc->get( $key );
138
139 $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
140 $maxPerSubpage = intval($count/$this->maxLineCount);
141 $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage);
142
143 if( !is_array( $lines ) ) {
144 $options = array( 'LIMIT' => 1 );
145 $options['ORDER BY'] = 'page_title ASC';
146 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
147 $lastTitle = $firstTitle;
148 # This array is going to hold the page_titles in order.
149 $lines = array( $firstTitle );
150 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
151 $done = false;
152 while( !$done ) {
153 // Fetch the last title of this chunk and the first of the next
154 $chunk = is_null( $lastTitle )
155 ? ''
156 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
157 $chunk = array($chunk);
158 $res = $dbr->select( 'page', /* FROM */
159 'page_title', /* WHAT */
160 array_merge($where,$chunk),
161 __METHOD__,
162 array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
163 );
164
165 if( $s = $dbr->fetchObject( $res ) ) {
166 array_push( $lines, $s->page_title );
167 } else {
168 // Final chunk, but ended prematurely. Go back and find the end.
169 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
170 array_merge($where,$chunk),
171 __METHOD__ );
172 array_push( $lines, $endTitle );
173 $done = true;
174 }
175 if( $s = $dbr->fetchObject( $res ) ) {
176 array_push( $lines, $s->page_title );
177 $lastTitle = $s->page_title;
178 } else {
179 // This was a final chunk and ended exactly at the limit.
180 // Rare but convenient!
181 $done = true;
182 }
183 $dbr->freeResult( $res );
184 }
185 $wgMemc->add( $key, $lines, 3600 );
186 }
187
188 // If there are only two or less sections, don't even display them.
189 // Instead, display the first section directly.
190 if( count( $lines ) <= 2 ) {
191 if( !empty($lines) ) {
192 $this->showChunk( $namespace, $lines[0], $lines[count($lines)-1], $including );
193 } else {
194 $wgOut->addHtml( $this->namespaceForm( $namespace, $from, $to ) );
195 }
196 return;
197 }
198
199 # At this point, $lines should contain an even number of elements.
200 $out .= "<table class='allpageslist' style='background: inherit;'>";
201 while( count ( $lines ) > 0 ) {
202 $inpoint = array_shift( $lines );
203 $outpoint = array_shift( $lines );
204 $out .= $this->showline( $inpoint, $outpoint, $namespace );
205 }
206 $out .= '</table>';
207 $nsForm = $this->namespaceForm( $namespace, $from, $to );
208
209 # Is there more?
210 if( $including ) {
211 $out2 = '';
212 } else {
213 if( isset($from) || isset($to) ) {
214 global $wgUser;
215 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
216 $out2 .= '<tr valign="top"><td>' . $nsForm;
217 $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">' .
218 $wgUser->getSkin()->makeKnownLink( $wgContLang->specialPage( "Allpages" ), wfMsgHtml ( 'allpages' ) );
219 $out2 .= "</td></tr></table><hr />";
220 } else {
221 $out2 = $nsForm . '<hr />';
222 }
223 }
224 $wgOut->addHtml( $out2 . $out );
225 }
226
227 /**
228 * @todo Document
229 * @param string $from
230 * @param integer $namespace (Default NS_MAIN)
231 */
232 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
233 global $wgContLang;
234 $align = $wgContLang->isRtl() ? 'left' : 'right';
235 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
236 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
237 // Don't let the length runaway
238 $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength, '...' );
239 $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength, '...' );
240
241 $queryparams = $namespace ? "namespace=$namespace&" : '';
242 $special = SpecialPage::getTitleFor( $this->name );
243 $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
244
245 $out = wfMsgHtml( 'alphaindexline',
246 "<a href=\"$link\">$inpointf</a></td><td>",
247 "</td><td><a href=\"$link\">$outpointf</a>"
248 );
249 return '<tr><td align="' . $align . '">'.$out.'</td></tr>';
250 }
251
252 /**
253 * @param integer $namespace (Default NS_MAIN)
254 * @param string $from list all pages from this name (default FALSE)
255 * @param string $from list all pages to this name (default FALSE)
256 */
257 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $including = false ) {
258 global $wgOut, $wgUser, $wgContLang;
259
260 $sk = $wgUser->getSkin();
261
262 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
263 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
264
265 $namespaces = $wgContLang->getNamespaces();
266 $align = $wgContLang->isRtl() ? 'left' : 'right';
267
268 $n = 0;
269
270 if ( !$fromList || !$toList ) {
271 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
272 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
273 // Show errormessage and reset to NS_MAIN
274 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
275 $namespace = NS_MAIN;
276 } else {
277 list( $namespace, $fromKey, $from ) = $fromList;
278 list( $namespace, $toKey, $to ) = $toList;
279
280 $dbr = wfGetDB( DB_SLAVE );
281 $res = $dbr->select( 'page',
282 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
283 array(
284 'page_namespace' => $namespace,
285 'page_title >= ' . $dbr->addQuotes( $fromKey ),
286 'page_title <= ' . $dbr->addQuotes( $toKey ),
287 ),
288 __METHOD__,
289 array(
290 'ORDER BY' => 'page_title ASC',
291 'LIMIT' => $this->maxPerPage + 1,
292 'USE INDEX' => 'name_title',
293 )
294 );
295
296 if( $res->numRows() > 0 ) {
297 $out = '<table style="background: inherit;" border="0" width="100%">';
298
299 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
300 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
301 if( $t ) {
302 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
303 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
304 ($s->page_is_redirect ? '</div>' : '' );
305 } else {
306 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
307 }
308 if( $n % 3 == 0 ) {
309 $out .= '<tr>';
310 }
311 $out .= "<td width=\"33%\">$link</td>";
312 $n++;
313 if( $n % 3 == 0 ) {
314 $out .= '</tr>';
315 }
316 }
317 if( ($n % 3) != 0 ) {
318 $out .= '</tr>';
319 }
320 $out .= '</table>';
321 } else {
322 $out = '';
323 }
324 }
325
326 if ( $including ) {
327 $out2 = '';
328 } else {
329 $nsForm = $this->namespaceForm( $namespace, $from, $to );
330 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
331 $out2 .= '<tr valign="top"><td>' . $nsForm;
332 $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">' .
333 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ), wfMsgHtml ( 'allpages' ) );
334 $out2 .= "</td></tr></table><hr />";
335 }
336
337 $wgOut->addHtml( $out2 . $out );
338 }
339
340 /**
341 * @param int $ns the namespace of the article
342 * @param string $text the name of the article
343 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
344 * @static (sort of)
345 * @access private
346 */
347 function getNamespaceKeyAndText( $ns, $text ) {
348 if ( $text == '' )
349 return array( $ns, '', '' ); # shortcut for common case
350
351 $t = Title::makeTitleSafe($ns, $text);
352 if ( $t && $t->isLocal() ) {
353 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
354 } else if ( $t ) {
355 return NULL;
356 }
357
358 # try again, in case the problem was an empty pagename
359 $text = preg_replace('/(#|$)/', 'X$1', $text);
360 $t = Title::makeTitleSafe($ns, $text);
361 if ( $t && $t->isLocal() ) {
362 return array( $t->getNamespace(), '', '' );
363 } else {
364 return NULL;
365 }
366 }
367
368 }