Fix the borked SpecialAllpages
[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 = HTMLnamespaceselector($namespace, null);
53
54 $frombox = "<input type='text' size='20' name='from' id='nsfrom' value=\""
55 . htmlspecialchars ( $from ) . '"/>';
56 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" />';
57
58 $out = "<div class='namespaceoptions'><form method='get' action='{$wgScript}'>";
59 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
60 $out .= "
61 <table id='nsselect' class='allpages'>
62 <tr>
63 <td align='right'>" . wfMsgHtml('allpagesfrom') . "</td>
64 <td align='left'><label for='nsfrom'>$frombox</label></td>
65 </tr>
66 <tr>
67 <td align='right'><label for='namespace'>" . wfMsgHtml('namespace') . "</label></td>
68 <td align='left'>
69 $namespaceselect $submitbutton
70 </td>
71 </tr>
72 </table>
73 ";
74 $out .= '</form></div>';
75 return $out;
76 }
77
78 /**
79 * @param integer $namespace (default NS_MAIN)
80 */
81 function showToplevel ( $namespace = NS_MAIN, $including = false ) {
82 global $wgOut, $wgContLang, $wgRequest, $wgUser;
83 $sk = $wgUser->getSkin();
84 $fname = "indexShowToplevel";
85
86 # TODO: Either make this *much* faster or cache the title index points
87 # in the querycache table.
88
89 $dbr =& wfGetDB( DB_SLAVE );
90 $page = $dbr->tableName( 'page' );
91 $fromwhere = "FROM $page WHERE page_namespace=$namespace";
92 $order_arr = array ( 'ORDER BY' => 'page_title' );
93 $order_str = 'ORDER BY page_title';
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, '', false, $including );
151 return;
152 }
153
154 # At this point, $lines should contain an even number of elements.
155 $out .= "<table 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 $wgOut, $wgLang, $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, 'Allpages/' . $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 $fromTitle = null;
218 if ($from!="") {
219 $fromTitle = Title::newFromURL( $from );
220 $fromNS = $fromTitle->getNamespace();
221 if ($namespace == NS_MAIN)
222 $namespace = $fromNS;
223 }
224 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
225
226 $dbr =& wfGetDB( DB_SLAVE );
227 $res = $dbr->select( 'page',
228 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
229 array(
230 'page_namespace' => $namespace,
231 'page_title >= ' . $dbr->addQuotes( $fromKey )
232 ),
233 $fname,
234 array(
235 'ORDER BY' => 'page_title',
236 'LIMIT' => $this->maxPerPage + 1,
237 'USE INDEX' => 'name_title',
238 )
239 );
240
241 ### FIXME: side link to previous
242
243 $n = 0;
244 $out = '<table style="background: inherit;" border="0" width="100%">';
245
246 $namespaces = $wgContLang->getFormattedNamespaces();
247 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
248 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
249 if( $t ) {
250 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
251 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
252 ($s->page_is_redirect ? '</div>' : '' );
253 } else {
254 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
255 }
256 if( $n % 3 == 0 ) {
257 $out .= '<tr>';
258 }
259 $out .= "<td>$link</td>";
260 $n++;
261 if( $n % 3 == 0 ) {
262 $out .= '</tr>';
263 }
264 }
265 if( ($n % 3) != 0 ) {
266 $out .= '</tr>';
267 }
268 $out .= '</table>';
269
270 if ( $including ) {
271 $out2 = '';
272 } else {
273 $nsForm = $this->namespaceForm ( $namespace, $from );
274 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
275 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
276 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
277 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
278 wfMsgHtml ( 'allpages' ) );
279 if ( ($n == $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
280 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
281 $out2 .= " | " . $sk->makeKnownLink(
282 $wgContLang->specialPage( "Allpages" ),
283 wfMsgHtml ( 'nextpage', $s->page_title ),
284 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam );
285 }
286 $out2 .= "</td></tr></table><hr />";
287 }
288
289 $wgOut->addHtml( $out2 . $out );
290 }
291 }
292
293 ?>