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