* Non-working API to facilitate dev collaboration. Do not enable this yet in localset...
[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', $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 = Title::makeTitle( NS_SPECIAL, $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, $wgUser;
87 $sk = $wgUser->getSkin();
88 $fname = "indexShowToplevel";
89
90 # TODO: Either make this *much* faster or cache the title index points
91 # in the querycache table.
92
93 $dbr =& wfGetDB( DB_SLAVE );
94 $out = "";
95 $where = array( 'page_namespace' => $namespace );
96
97 global $wgMemc, $wgDBname;
98 $key = "$wgDBname:allpages:ns:$namespace";
99 $lines = $wgMemc->get( $key );
100
101 if( !is_array( $lines ) ) {
102 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, array( 'LIMIT' => 1 ) );
103 $lastTitle = $firstTitle;
104
105 # This array is going to hold the page_titles in order.
106 $lines = array( $firstTitle );
107
108 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
109 $done = false;
110 for( $i = 0; !$done; ++$i ) {
111 // Fetch the last title of this chunk and the first of the next
112 $chunk = is_null( $lastTitle )
113 ? ''
114 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
115 $res = $dbr->select(
116 'page', /* FROM */
117 'page_title', /* WHAT */
118 $where + array( $chunk),
119 $fname,
120 array ('LIMIT' => 2, 'OFFSET' => $this->maxPerPage - 1, 'ORDER BY' => 'page_title') );
121
122 if ( $s = $dbr->fetchObject( $res ) ) {
123 array_push( $lines, $s->page_title );
124 } else {
125 // Final chunk, but ended prematurely. Go back and find the end.
126 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
127 array(
128 'page_namespace' => $namespace,
129 $chunk
130 ), $fname );
131 array_push( $lines, $endTitle );
132 $done = true;
133 }
134 if( $s = $dbr->fetchObject( $res ) ) {
135 array_push( $lines, $s->page_title );
136 $lastTitle = $s->page_title;
137 } else {
138 // This was a final chunk and ended exactly at the limit.
139 // Rare but convenient!
140 $done = true;
141 }
142 $dbr->freeResult( $res );
143 }
144 $wgMemc->add( $key, $lines, 3600 );
145 }
146
147 // If there are only two or less sections, don't even display them.
148 // Instead, display the first section directly.
149 if( count( $lines ) <= 2 ) {
150 $this->showChunk( $namespace, '', $including );
151 return;
152 }
153
154 # At this point, $lines should contain an even number of elements.
155 $out .= "<table class='allpageslist' style='background: inherit;'>";
156 while ( count ( $lines ) > 0 ) {
157 $inpoint = array_shift ( $lines );
158 $outpoint = array_shift ( $lines );
159 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
160 }
161 $out .= '</table>';
162 $nsForm = $this->namespaceForm ( $namespace, '', false );
163
164 # Is there more?
165 if ( $including ) {
166 $out2 = '';
167 } else {
168 $morelinks = '';
169 if ( $morelinks != '' ) {
170 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
171 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
172 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
173 $out2 .= $morelinks . '</td></tr></table><hr />';
174 } else {
175 $out2 = $nsForm . '<hr />';
176 }
177 }
178
179 $wgOut->addHtml( $out2 . $out );
180 }
181
182 /**
183 * @todo Document
184 * @param string $from
185 * @param integer $namespace (Default NS_MAIN)
186 */
187 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
188 global $wgUser;
189 $sk = $wgUser->getSkin();
190 $dbr =& wfGetDB( DB_SLAVE );
191
192 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
193 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
194 $queryparams = ($namespace ? "namespace=$namespace" : '');
195 $special = Title::makeTitle( NS_SPECIAL, $this->name . '/' . $inpoint );
196 $link = $special->escapeLocalUrl( $queryparams );
197
198 $out = wfMsgHtml(
199 'alphaindexline',
200 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
201 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
202 );
203 return '<tr><td align="right">'.$out.'</td></tr>';
204 }
205
206 /**
207 * @param integer $namespace (Default NS_MAIN)
208 * @param string $from list all pages from this name (default FALSE)
209 */
210 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
211 global $wgOut, $wgUser, $wgContLang;
212
213 $fname = 'indexShowChunk';
214
215 $sk = $wgUser->getSkin();
216
217 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
218
219 if ( !$fromList ) {
220 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
221 } else {
222 list( $namespace, $fromKey, $from ) = $fromList;
223
224 $dbr =& wfGetDB( DB_SLAVE );
225 $res = $dbr->select( 'page',
226 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
227 array(
228 'page_namespace' => $namespace,
229 'page_title >= ' . $dbr->addQuotes( $fromKey )
230 ),
231 $fname,
232 array(
233 'ORDER BY' => 'page_title',
234 'LIMIT' => $this->maxPerPage + 1,
235 'USE INDEX' => 'name_title',
236 )
237 );
238
239 ### FIXME: side link to previous
240
241 $n = 0;
242 $out = '<table style="background: inherit;" border="0" width="100%">';
243
244 $namespaces = $wgContLang->getFormattedNamespaces();
245 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
246 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
247 if( $t ) {
248 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
249 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
250 ($s->page_is_redirect ? '</div>' : '' );
251 } else {
252 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
253 }
254 if( $n % 3 == 0 ) {
255 $out .= '<tr>';
256 }
257 $out .= "<td>$link</td>";
258 $n++;
259 if( $n % 3 == 0 ) {
260 $out .= '</tr>';
261 }
262 }
263 if( ($n % 3) != 0 ) {
264 $out .= '</tr>';
265 }
266 $out .= '</table>';
267 }
268
269 if ( $including ) {
270 $out2 = '';
271 } else {
272 $nsForm = $this->namespaceForm ( $namespace, $from );
273 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
274 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
275 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
276 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
277 wfMsgHtml ( 'allpages' ) );
278 if ( isset($dbr) && $dbr && ($n == $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
279 $self = Title::makeTitle( NS_SPECIAL, 'Allpages' );
280 $q = 'from=' . $t->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
281 $out2 .= ' | ' . $sk->makeKnownLinkObj( $self, wfMsgHtml( 'nextpage', $t->getText() ), $q );
282 }
283 $out2 .= "</td></tr></table><hr />";
284 }
285
286 $wgOut->addHtml( $out2 . $out );
287 }
288
289 /**
290 * @param int $ns the namespace of the article
291 * @param string $text the name of the article
292 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
293 * @static (sort of)
294 * @access private
295 */
296 function getNamespaceKeyAndText ($ns, $text) {
297 if ( $text == '' )
298 return array( $ns, '', '' ); # shortcut for common case
299
300 $t = Title::makeTitleSafe($ns, $text);
301 if ( $t && $t->isLocal() )
302 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
303 else if ( $t )
304 return NULL;
305
306 # try again, in case the problem was an empty pagename
307 $text = preg_replace('/(#|$)/', 'X$1', $text);
308 $t = Title::makeTitleSafe($ns, $text);
309 if ( $t && $t->isLocal() )
310 return array( $t->getNamespace(), '', '' );
311 else
312 return NULL;
313 }
314 }
315
316 ?>