little cleanup
[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 ? '1=1'
114 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
115 $sql = "SELECT page_title $fromwhere AND $chunk $order_str " .
116 $dbr->limitResult( 2, $this->topLevelMax - 1 );
117 $res = $dbr->query( $sql, $fname );
118 if ( $s = $dbr->fetchObject( $res ) ) {
119 array_push( $lines, $s->page_title );
120 } else {
121 // Final chunk, but ended prematurely. Go back and find the end.
122 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
123 array(
124 'page_namespace' => $namespace,
125 $chunk
126 ), $fname );
127 array_push( $lines, $endTitle );
128 $done = true;
129 }
130 if( $s = $dbr->fetchObject( $res ) ) {
131 array_push( $lines, $s->page_title );
132 $lastTitle = $s->page_title;
133 } else {
134 // This was a final chunk and ended exactly at the limit.
135 // Rare but convenient!
136 $done = true;
137 }
138 $dbr->freeResult( $res );
139 }
140 $wgMemc->add( $key, $lines, 3600 );
141 }
142
143 // If there are only two or less sections, don't even display them.
144 // Instead, display the first section directly.
145 if( count( $lines ) <= 2 ) {
146 $this->showChunk( $namespace, '', false, $including );
147 return;
148 }
149
150 # At this point, $lines should contain an even number of elements.
151 $out .= "<table style='background: inherit;'>";
152 while ( count ( $lines ) > 0 ) {
153 $inpoint = array_shift ( $lines );
154 $outpoint = array_shift ( $lines );
155 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
156 }
157 $out .= '</table>';
158 $nsForm = $this->namespaceForm ( $namespace, '', false );
159
160 # Is there more?
161 if ( $including ) {
162 $out2 = '';
163 } else {
164 $morelinks = '';
165 if ( $morelinks != '' ) {
166 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
167 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
168 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
169 $out2 .= $morelinks . '</td></tr></table><hr />';
170 } else {
171 $out2 = $nsForm . '<hr />';
172 }
173 }
174
175 $wgOut->addHtml( $out2 . $out );
176 }
177
178 /**
179 * @todo Document
180 * @param string $from
181 * @param integer $namespace (Default NS_MAIN)
182 */
183 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
184 global $wgOut, $wgLang, $wgUser;
185 $sk = $wgUser->getSkin();
186 $dbr =& wfGetDB( DB_SLAVE );
187
188 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
189 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
190 $queryparams = ($namespace ? "namespace=$namespace" : '');
191 $special = Title::makeTitle( NS_SPECIAL, 'Allpages/' . $inpoint );
192 $link = $special->escapeLocalUrl( $queryparams );
193
194 $out = wfMsgHtml(
195 'alphaindexline',
196 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
197 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
198 );
199 return '<tr><td align="right">'.$out.'</td></tr>';
200 }
201
202 /**
203 * @param integer $namespace (Default NS_MAIN)
204 * @param string $from list all pages from this name (default FALSE)
205 */
206 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
207 global $wgOut, $wgUser, $wgContLang;
208
209 $fname = 'indexShowChunk';
210
211 $sk = $wgUser->getSkin();
212
213 $fromTitle = null;
214 if ($from!="") {
215 $fromTitle = Title::newFromURL( $from );
216 $fromNS = $fromTitle->getNamespace();
217 if ($namespace == NS_MAIN)
218 $namespace = $fromNS;
219 }
220 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
221
222 $dbr =& wfGetDB( DB_SLAVE );
223 $res = $dbr->select( 'page',
224 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
225 array(
226 'page_namespace' => $namespace,
227 'page_title >= ' . $dbr->addQuotes( $fromKey )
228 ),
229 $fname,
230 array(
231 'ORDER BY' => 'page_title',
232 'LIMIT' => $this->maxPerPage + 1,
233 'USE INDEX' => 'name_title',
234 )
235 );
236
237 ### FIXME: side link to previous
238
239 $n = 0;
240 $out = '<table style="background: inherit;" border="0" width="100%">';
241
242 $namespaces = $wgContLang->getFormattedNamespaces();
243 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
244 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
245 if( $t ) {
246 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
247 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
248 ($s->page_is_redirect ? '</div>' : '' );
249 } else {
250 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
251 }
252 if( $n % 3 == 0 ) {
253 $out .= '<tr>';
254 }
255 $out .= "<td>$link</td>";
256 $n++;
257 if( $n % 3 == 0 ) {
258 $out .= '</tr>';
259 }
260 }
261 if( ($n % 3) != 0 ) {
262 $out .= '</tr>';
263 }
264 $out .= '</table>';
265
266 if ( $including ) {
267 $out2 = '';
268 } else {
269 $nsForm = $this->namespaceForm ( $namespace, $from );
270 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
271 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
272 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
273 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
274 wfMsgHtml ( 'allpages' ) );
275 if ( ($n == $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
276 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
277 $out2 .= " | " . $sk->makeKnownLink(
278 $wgContLang->specialPage( "Allpages" ),
279 wfMsgHtml ( 'nextpage', $s->page_title ),
280 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam );
281 }
282 $out2 .= "</td></tr></table><hr />";
283 }
284
285 $wgOut->addHtml( $out2 . $out );
286 }
287 }
288
289 ?>