* (bug 12553) Fixed invalid XHTML in edit conflict screen
[lhc/web/wiklou.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 /**
42 * Maximum number of pages to show on single subpage.
43 */
44 protected $maxPerPage = 960;
45
46 /**
47 * Name of this special page. Used to make title objects that reference back
48 * to this page.
49 */
50 protected $name = 'Allpages';
51
52 /**
53 * Determines, which message describes the input field 'nsfrom'.
54 */
55 protected $nsfromMsg = 'allpagesfrom';
56
57 /**
58 * HTML for the top form
59 * @param integer $namespace A namespace constant (default NS_MAIN).
60 * @param string $from Article name we are starting listing at.
61 */
62 function namespaceForm ( $namespace = NS_MAIN, $from = '' ) {
63 global $wgScript, $wgContLang;
64 $t = SpecialPage::getTitleFor( $this->name );
65 $align = $wgContLang->isRtl() ? 'left' : 'right';
66
67 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
68 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
69 $out .= Xml::hidden( 'title', $t->getPrefixedText() );
70 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
71 $out .= "<tr>
72 <td align='$align'>" .
73 Xml::label( wfMsg( $this->nsfromMsg ), 'nsfrom' ) .
74 "</td>
75 <td>" .
76 Xml::input( 'from', 20, htmlspecialchars ( $from ), array( 'id' => 'nsfrom' ) ) .
77 "</td>
78 </tr>
79 <tr>
80 <td align='$align'>" .
81 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
82 "</td>
83 <td>" .
84 Xml::namespaceSelector( $namespace, null ) .
85 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
86 "</td>
87 </tr>";
88 $out .= Xml::closeElement( 'table' );
89 $out .= Xml::closeElement( 'form' );
90 $out .= Xml::closeElement( 'div' );
91 return $out;
92 }
93
94 /**
95 * @param integer $namespace (default NS_MAIN)
96 */
97 function showToplevel ( $namespace = NS_MAIN, $including = false ) {
98 global $wgOut, $wgContLang;
99 $align = $wgContLang->isRtl() ? 'left' : 'right';
100
101 # TODO: Either make this *much* faster or cache the title index points
102 # in the querycache table.
103
104 $dbr = wfGetDB( DB_SLAVE );
105 $out = "";
106 $where = array( 'page_namespace' => $namespace );
107
108 global $wgMemc;
109 $key = wfMemcKey( 'allpages', 'ns', $namespace );
110 $lines = $wgMemc->get( $key );
111
112 if( !is_array( $lines ) ) {
113 $options = array( 'LIMIT' => 1 );
114 if ( ! $dbr->implicitOrderby() ) {
115 $options['ORDER BY'] = 'page_title';
116 }
117 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
118 $lastTitle = $firstTitle;
119
120 # This array is going to hold the page_titles in order.
121 $lines = array( $firstTitle );
122
123 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
124 $done = false;
125 for( $i = 0; !$done; ++$i ) {
126 // Fetch the last title of this chunk and the first of the next
127 $chunk = is_null( $lastTitle )
128 ? ''
129 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
130 $res = $dbr->select(
131 'page', /* FROM */
132 'page_title', /* WHAT */
133 $where + array( $chunk),
134 __METHOD__,
135 array ('LIMIT' => 2, 'OFFSET' => $this->maxPerPage - 1, 'ORDER BY' => 'page_title') );
136
137 if ( $s = $dbr->fetchObject( $res ) ) {
138 array_push( $lines, $s->page_title );
139 } else {
140 // Final chunk, but ended prematurely. Go back and find the end.
141 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
142 array(
143 'page_namespace' => $namespace,
144 $chunk
145 ), __METHOD__ );
146 array_push( $lines, $endTitle );
147 $done = true;
148 }
149 if( $s = $dbr->fetchObject( $res ) ) {
150 array_push( $lines, $s->page_title );
151 $lastTitle = $s->page_title;
152 } else {
153 // This was a final chunk and ended exactly at the limit.
154 // Rare but convenient!
155 $done = true;
156 }
157 $dbr->freeResult( $res );
158 }
159 $wgMemc->add( $key, $lines, 3600 );
160 }
161
162 // If there are only two or less sections, don't even display them.
163 // Instead, display the first section directly.
164 if( count( $lines ) <= 2 ) {
165 $this->showChunk( $namespace, '', $including );
166 return;
167 }
168
169 # At this point, $lines should contain an even number of elements.
170 $out .= "<table class='allpageslist' style='background: inherit;'>";
171 while ( count ( $lines ) > 0 ) {
172 $inpoint = array_shift ( $lines );
173 $outpoint = array_shift ( $lines );
174 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
175 }
176 $out .= '</table>';
177 $nsForm = $this->namespaceForm ( $namespace, '', false );
178
179 # Is there more?
180 if ( $including ) {
181 $out2 = '';
182 } else {
183 $morelinks = '';
184 if ( $morelinks != '' ) {
185 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
186 $out2 .= '<tr valign="top"><td>' . $nsForm;
187 $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">';
188 $out2 .= $morelinks . '</td></tr></table><hr />';
189 } else {
190 $out2 = $nsForm . '<hr />';
191 }
192 }
193
194 $wgOut->addHtml( $out2 . $out );
195 }
196
197 /**
198 * @todo Document
199 * @param string $from
200 * @param integer $namespace (Default NS_MAIN)
201 */
202 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
203 global $wgContLang;
204 $align = $wgContLang->isRtl() ? 'left' : 'right';
205 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
206 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
207 $queryparams = ($namespace ? "namespace=$namespace" : '');
208 $special = SpecialPage::getTitleFor( $this->name, $inpoint );
209 $link = $special->escapeLocalUrl( $queryparams );
210
211 $out = wfMsgHtml(
212 'alphaindexline',
213 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
214 "</a></td><td><a href=\"$link\">$outpointf</a>"
215 );
216 return '<tr><td align="' . $align . '">'.$out.'</td></tr>';
217 }
218
219 /**
220 * @param integer $namespace (Default NS_MAIN)
221 * @param string $from list all pages from this name (default FALSE)
222 */
223 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
224 global $wgOut, $wgUser, $wgContLang;
225
226 $sk = $wgUser->getSkin();
227
228 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
229 $namespaces = $wgContLang->getNamespaces();
230 $align = $wgContLang->isRtl() ? 'left' : 'right';
231
232 $n = 0;
233
234 if ( !$fromList ) {
235 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
236 } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
237 // Show errormessage and reset to NS_MAIN
238 $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
239 $namespace = NS_MAIN;
240 } else {
241 list( $namespace, $fromKey, $from ) = $fromList;
242
243 $dbr = wfGetDB( DB_SLAVE );
244 $res = $dbr->select( 'page',
245 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
246 array(
247 'page_namespace' => $namespace,
248 'page_title >= ' . $dbr->addQuotes( $fromKey )
249 ),
250 __METHOD__,
251 array(
252 'ORDER BY' => 'page_title',
253 'LIMIT' => $this->maxPerPage + 1,
254 'USE INDEX' => 'name_title',
255 )
256 );
257
258 $out = '<table style="background: inherit;" border="0" width="100%">';
259
260 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
261 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
262 if( $t ) {
263 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
264 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
265 ($s->page_is_redirect ? '</div>' : '' );
266 } else {
267 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
268 }
269 if( $n % 3 == 0 ) {
270 $out .= '<tr>';
271 }
272 $out .= "<td width=\"33%\">$link</td>";
273 $n++;
274 if( $n % 3 == 0 ) {
275 $out .= '</tr>';
276 }
277 }
278 if( ($n % 3) != 0 ) {
279 $out .= '</tr>';
280 }
281 $out .= '</table>';
282 }
283
284 if ( $including ) {
285 $out2 = '';
286 } else {
287 if( $from == '' ) {
288 // First chunk; no previous link.
289 $prevTitle = null;
290 } else {
291 # Get the last title from previous chunk
292 $dbr = wfGetDB( DB_SLAVE );
293 $res_prev = $dbr->select(
294 'page',
295 'page_title',
296 array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
297 __METHOD__,
298 array( 'ORDER BY' => 'page_title DESC', 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 ) )
299 );
300
301 # Get first title of previous complete chunk
302 if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
303 $pt = $dbr->fetchObject( $res_prev );
304 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
305 } else {
306 # The previous chunk is not complete, need to link to the very first title
307 # available in the database
308 $options = array( 'LIMIT' => 1 );
309 if ( ! $dbr->implicitOrderby() ) {
310 $options['ORDER BY'] = 'page_title';
311 }
312 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title', array( 'page_namespace' => $namespace ), __METHOD__, $options );
313 # Show the previous link if it s not the current requested chunk
314 if( $from != $reallyFirstPage_title ) {
315 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
316 } else {
317 $prevTitle = null;
318 }
319 }
320 }
321
322 $nsForm = $this->namespaceForm ( $namespace, $from );
323 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
324 $out2 .= '<tr valign="top"><td>' . $nsForm;
325 $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">' .
326 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
327 wfMsgHtml ( 'allpages' ) );
328
329 $self = SpecialPage::getTitleFor( 'Allpages' );
330
331 # Do we put a previous link ?
332 if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
333 $q = 'from=' . $prevTitle->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
334 $prevLink = $sk->makeKnownLinkObj( $self, wfMsgHTML( 'prevpage', $pt ), $q );
335 $out2 .= ' | ' . $prevLink;
336 }
337
338 if( $n == $this->maxPerPage && $s = $dbr->fetchObject($res) ) {
339 # $s is the first link of the next chunk
340 $t = Title::MakeTitle($namespace, $s->page_title);
341 $q = 'from=' . $t->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
342 $nextLink = $sk->makeKnownLinkObj( $self, wfMsgHtml( 'nextpage', $t->getText() ), $q );
343 $out2 .= ' | ' . $nextLink;
344 }
345 $out2 .= "</td></tr></table><hr />";
346 }
347
348 $wgOut->addHtml( $out2 . $out );
349 if( isset($prevLink) or isset($nextLink) ) {
350 $wgOut->addHtml( '<hr /><p style="font-size: smaller; float: ' . $align . '">' );
351 if( isset( $prevLink ) ) {
352 $wgOut->addHTML( $prevLink );
353 }
354 if( isset( $prevLink ) && isset( $nextLink ) ) {
355 $wgOut->addHTML( ' | ' );
356 }
357 if( isset( $nextLink ) ) {
358 $wgOut->addHTML( $nextLink );
359 }
360 $wgOut->addHTML( '</p>' );
361
362 }
363
364 }
365
366 /**
367 * @param int $ns the namespace of the article
368 * @param string $text the name of the article
369 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
370 * @static (sort of)
371 * @access private
372 */
373 function getNamespaceKeyAndText ($ns, $text) {
374 if ( $text == '' )
375 return array( $ns, '', '' ); # shortcut for common case
376
377 $t = Title::makeTitleSafe($ns, $text);
378 if ( $t && $t->isLocal() ) {
379 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
380 } else if ( $t ) {
381 return NULL;
382 }
383
384 # try again, in case the problem was an empty pagename
385 $text = preg_replace('/(#|$)/', 'X$1', $text);
386 $t = Title::makeTitleSafe($ns, $text);
387 if ( $t && $t->isLocal() ) {
388 return array( $t->getNamespace(), '', '' );
389 } else {
390 return NULL;
391 }
392 }
393 }
394
395 ?>