some of lufthansa bits ;-) allows reusal of object in other pagelists, some error...
[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 string $par Becomes "FOO" when called like Special:Allpages/FOO (default NULL)
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 if( !in_array($namespace, array_keys($namespaces)) )
23 $namespace = 0;
24
25 $wgOut->setPagetitle( $namespace > 0 ?
26 wfMsg( 'allinnamespace', $namespaces[$namespace] ) :
27 wfMsg( 'allarticles' )
28 );
29
30 if ( isset($par) ) {
31 $indexPage->showChunk( $namespace, $par, $specialPage->including() );
32 } elseif ( isset($from) ) {
33 $indexPage->showChunk( $namespace, $from, $specialPage->including() );
34 } else {
35 $indexPage->showToplevel ( $namespace, $specialPage->including() );
36 }
37 }
38
39 class SpecialAllpages {
40 var $maxPerPage=960;
41 var $topLevelMax=50;
42 var $name='Allpages';
43
44 /**
45 * HTML for the top form
46 * @param integer $namespace A namespace constant (default NS_MAIN).
47 * @param string $from Article name we are starting listing at.
48 */
49 function namespaceForm ( $namespace = NS_MAIN, $from = '' ) {
50 global $wgContLang, $wgScript;
51 $t = Title::makeTitle( NS_SPECIAL, $this->name );
52
53 $namespaceselect = HTMLnamespaceselector($namespace, null);
54
55 $frombox = "<input type='text' size='20' name='from' id='nsfrom' value=\""
56 . htmlspecialchars ( $from ) . '"/>';
57 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" />';
58
59 $out = "<div class='namespaceoptions'><form method='get' action='{$wgScript}'>";
60 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
61 $out .= "
62 <table id='nsselect' class='allpages'>
63 <tr>
64 <td align='right'>" . wfMsgHtml('allpagesfrom') . "</td>
65 <td align='left'><label for='nsfrom'>$frombox</label></td>
66 </tr>
67 <tr>
68 <td align='right'><label for='namespace'>" . wfMsgHtml('namespace') . "</label></td>
69 <td align='left'>
70 $namespaceselect $submitbutton
71 </td>
72 </tr>
73 </table>
74 ";
75 $out .= '</form></div>';
76 return $out;
77 }
78
79 /**
80 * @param integer $namespace (default NS_MAIN)
81 */
82 function showToplevel ( $namespace = NS_MAIN, $including = false ) {
83 global $wgOut, $wgContLang, $wgRequest, $wgUser;
84 $sk = $wgUser->getSkin();
85 $fname = "indexShowToplevel";
86
87 # TODO: Either make this *much* faster or cache the title index points
88 # in the querycache table.
89
90 $dbr =& wfGetDB( DB_SLAVE );
91 $page = $dbr->tableName( 'page' );
92 $fromwhere = "FROM $page WHERE page_namespace=$namespace";
93 $order_arr = array ( 'ORDER BY' => 'page_title' );
94 $order_str = 'ORDER BY page_title';
95 $out = "";
96 $where = array( 'page_namespace' => $namespace );
97
98 global $wgMemc, $wgDBname;
99 $key = "$wgDBname:allpages:ns:$namespace";
100 $lines = $wgMemc->get( $key );
101
102 if( !is_array( $lines ) ) {
103 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, array( 'LIMIT' => 1 ) );
104 $lastTitle = $firstTitle;
105
106 # This array is going to hold the page_titles in order.
107 $lines = array( $firstTitle );
108
109 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
110 $done = false;
111 for( $i = 0; !$done; ++$i ) {
112 // Fetch the last title of this chunk and the first of the next
113 $chunk = is_null( $lastTitle )
114 ? ''
115 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
116 $res = $dbr->select(
117 'page', /* FROM */
118 'page_title', /* WHAT */
119 $where + array( $chunk),
120 $fname,
121 array ('LIMIT' => 2, 'OFFSET' => $this->maxPerPage - 1, 'ORDER BY' => 'page_title') );
122
123 if ( $s = $dbr->fetchObject( $res ) ) {
124 array_push( $lines, $s->page_title );
125 } else {
126 // Final chunk, but ended prematurely. Go back and find the end.
127 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
128 array(
129 'page_namespace' => $namespace,
130 $chunk
131 ), $fname );
132 array_push( $lines, $endTitle );
133 $done = true;
134 }
135 if( $s = $dbr->fetchObject( $res ) ) {
136 array_push( $lines, $s->page_title );
137 $lastTitle = $s->page_title;
138 } else {
139 // This was a final chunk and ended exactly at the limit.
140 // Rare but convenient!
141 $done = true;
142 }
143 $dbr->freeResult( $res );
144 }
145 $wgMemc->add( $key, $lines, 3600 );
146 }
147
148 // If there are only two or less sections, don't even display them.
149 // Instead, display the first section directly.
150 if( count( $lines ) <= 2 ) {
151 $this->showChunk( $namespace, '', false, $including );
152 return;
153 }
154
155 # At this point, $lines should contain an even number of elements.
156 $out .= "<table style='background: inherit;'>";
157 while ( count ( $lines ) > 0 ) {
158 $inpoint = array_shift ( $lines );
159 $outpoint = array_shift ( $lines );
160 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
161 }
162 $out .= '</table>';
163 $nsForm = $this->namespaceForm ( $namespace, '', false );
164
165 # Is there more?
166 if ( $including ) {
167 $out2 = '';
168 } else {
169 $morelinks = '';
170 if ( $morelinks != '' ) {
171 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
172 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
173 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
174 $out2 .= $morelinks . '</td></tr></table><hr />';
175 } else {
176 $out2 = $nsForm . '<hr />';
177 }
178 }
179
180 $wgOut->addHtml( $out2 . $out );
181 }
182
183 /**
184 * @todo Document
185 * @param string $from
186 * @param integer $namespace (Default NS_MAIN)
187 */
188 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
189 global $wgOut, $wgLang, $wgUser;
190 $sk = $wgUser->getSkin();
191 $dbr =& wfGetDB( DB_SLAVE );
192
193 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
194 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
195 $queryparams = ($namespace ? "namespace=$namespace" : '');
196 $special = Title::makeTitle( NS_SPECIAL, $this->name . '/' . $inpoint );
197 $link = $special->escapeLocalUrl( $queryparams );
198
199 $out = wfMsgHtml(
200 'alphaindexline',
201 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
202 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
203 );
204 return '<tr><td align="right">'.$out.'</td></tr>';
205 }
206
207 /**
208 * @param integer $namespace (Default NS_MAIN)
209 * @param string $from list all pages from this name (default FALSE)
210 */
211 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
212 global $wgOut, $wgUser, $wgContLang;
213
214 $fname = 'indexShowChunk';
215
216 $sk = $wgUser->getSkin();
217
218 $fromTitle = null;
219 if ($from!="") {
220 $fromTitle = Title::newFromURL( $from );
221 if (!$fromTitle) {
222 return;
223 }
224 $fromNS = $fromTitle->getNamespace();
225 if ($namespace == NS_MAIN)
226 $namespace = $fromNS;
227 }
228 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
229
230 $dbr =& wfGetDB( DB_SLAVE );
231 $res = $dbr->select( 'page',
232 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
233 array(
234 'page_namespace' => $namespace,
235 'page_title >= ' . $dbr->addQuotes( $fromKey )
236 ),
237 $fname,
238 array(
239 'ORDER BY' => 'page_title',
240 'LIMIT' => $this->maxPerPage + 1,
241 'USE INDEX' => 'name_title',
242 )
243 );
244
245 ### FIXME: side link to previous
246
247 $n = 0;
248 $out = '<table style="background: inherit;" border="0" width="100%">';
249
250 $namespaces = $wgContLang->getFormattedNamespaces();
251 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
252 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
253 if( $t ) {
254 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
255 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
256 ($s->page_is_redirect ? '</div>' : '' );
257 } else {
258 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
259 }
260 if( $n % 3 == 0 ) {
261 $out .= '<tr>';
262 }
263 $out .= "<td>$link</td>";
264 $n++;
265 if( $n % 3 == 0 ) {
266 $out .= '</tr>';
267 }
268 }
269 if( ($n % 3) != 0 ) {
270 $out .= '</tr>';
271 }
272 $out .= '</table>';
273
274 if ( $including ) {
275 $out2 = '';
276 } else {
277 $nsForm = $this->namespaceForm ( $namespace, $from );
278 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
279 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
280 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
281 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
282 wfMsgHtml ( 'allpages' ) );
283 if ( ($n == $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
284 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
285 $out2 .= " | " . $sk->makeKnownLink(
286 $wgContLang->specialPage( "Allpages" ),
287 wfMsgHtml ( 'nextpage', $s->page_title ),
288 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam );
289 }
290 $out2 .= "</td></tr></table><hr />";
291 }
292
293 $wgOut->addHtml( $out2 . $out );
294 }
295 }
296
297 ?>