* Fixed unclosed <p> tag
[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 $indexMaxperpage, $toplevelMaxperpage, $wgRequest, $wgOut, $wgContLang;
13 # Config
14 $indexMaxperpage = 960;
15 $toplevelMaxperpage = 50;
16 # GET values
17 $from = $wgRequest->getVal( 'from' );
18 $namespace = $wgRequest->getInt( 'namespace' );
19
20 $namespaces = $wgContLang->getNamespaces();
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 indexShowChunk( $namespace, $par, $specialPage->including() );
32 } elseif ( isset($from) ) {
33 indexShowChunk( $namespace, $from, $specialPage->including() );
34 } else {
35 indexShowToplevel ( $namespace, $specialPage->including() );
36 }
37 }
38
39 /**
40 * HTML for the top form
41 * @param integer $namespace A namespace constant (default NS_MAIN).
42 * @param string $from Article name we are starting listing at.
43 */
44 function indexNamespaceForm ( $namespace = NS_MAIN, $from = '' ) {
45 global $wgContLang, $wgScript;
46 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
47
48 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
49 $arr = $wgContLang->getFormattedNamespaces();
50 foreach ( $arr as $ns => $name ) {
51 if ($ns < NS_MAIN)
52 continue;
53 $n = $ns == 0 ? wfMsg ( 'blanknamespace' ) : $name;
54 $sel = $ns == $namespace ? ' selected="selected"' : '';
55 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
56 }
57 $namespaceselect .= '</select>';
58
59 $frombox = "<input type='text' size='20' name='from' id='nsfrom' value=\""
60 . htmlspecialchars ( $from ) . '"/>';
61 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
62
63 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
64 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
65 $out .= "
66 <table id='nsselect' class='allpages'>
67 <tr>
68 <td align='right'>" . wfMsg('allpagesfrom') . "</td>
69 <td align='left'><label for='nsfrom'>$frombox</label></td>
70 </tr>
71 <tr>
72 <td align='right'><label for='nsselectbox'>" . wfMsg('namespace') . "</label></td>
73 <td align='left'>
74 $namespaceselect $submitbutton
75 </td>
76 </tr>
77 </table>
78 ";
79 $out .= '</form></div>';
80 return $out;
81 }
82
83 /**
84 * @param integer $namespace (default NS_MAIN)
85 */
86 function indexShowToplevel ( $namespace = NS_MAIN, $including = false ) {
87 global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgContLang, $wgRequest, $wgUser;
88 $sk = $wgUser->getSkin();
89 $fname = "indexShowToplevel";
90
91 # TODO: Either make this *much* faster or cache the title index points
92 # in the querycache table.
93
94 $dbr =& wfGetDB( DB_SLAVE );
95 $page = $dbr->tableName( 'page' );
96 $fromwhere = "FROM $page WHERE page_namespace=$namespace";
97 $order_arr = array ( 'ORDER BY' => 'page_title' );
98 $order_str = 'ORDER BY page_title';
99 $out = "";
100 $where = array( 'page_namespace' => $namespace );
101
102 global $wgMemc, $wgDBname;
103 $key = "$wgDBname:allpages:ns:$namespace";
104 $lines = $wgMemc->get( $key );
105
106 if( !is_array( $lines ) ) {
107 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, array( 'LIMIT' => 1 ) );
108 $lastTitle = $firstTitle;
109
110 # This array is going to hold the page_titles in order.
111 $lines = array( $firstTitle );
112
113 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
114 $done = false;
115 for( $i = 0; !$done; ++$i ) {
116 // Fetch the last title of this chunk and the first of the next
117 $chunk = is_null( $lastTitle )
118 ? '1=1'
119 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
120 $sql = "SELECT page_title $fromwhere AND $chunk $order_str " .
121 $dbr->limitResult( 2, $indexMaxperpage - 1 );
122 $res = $dbr->query( $sql, $fname );
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 indexShowChunk( $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 .= indexShowline ( $inpoint, $outpoint, $namespace, false );
161 }
162 $out .= '</table>';
163
164 $nsForm = indexNamespaceForm ( $namespace, '', false );
165
166 # Is there more?
167 if ( $including ) {
168 $out2 = '';
169 } else {
170 $morelinks = '';
171 if ( $morelinks != '' ) {
172 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
173 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
174 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
175 $out2 .= $morelinks . '</td></tr></table><hr />';
176 } else {
177 $out2 = $nsForm . '<hr />';
178 }
179 }
180
181 $wgOut->addHtml( $out2 . $out );
182 }
183
184 /**
185 * @todo Document
186 * @param string $from
187 * @param integer $namespace (Default NS_MAIN)
188 */
189 function indexShowline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
190 global $wgOut, $wgLang, $wgUser;
191 $sk = $wgUser->getSkin();
192 $dbr =& wfGetDB( DB_SLAVE );
193
194 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
195 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
196 $queryparams = ($namespace ? "namespace=$namespace" : '');
197 $special = Title::makeTitle( NS_SPECIAL, 'Allpages/' . $inpoint );
198 $link = $special->escapeLocalUrl( $queryparams );
199
200 $out = wfMsg(
201 'alphaindexline',
202 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
203 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
204 );
205 return '<tr><td align="right">'.$out.'</td></tr>';
206 }
207
208 /**
209 * @param integer $namespace (Default NS_MAIN)
210 * @param string $from list all pages from this name (default FALSE)
211 */
212 function indexShowChunk( $namespace = NS_MAIN, $from, $including = false ) {
213 global $wgOut, $wgUser, $indexMaxperpage, $wgContLang;
214
215 $fname = 'indexShowChunk';
216
217 $sk = $wgUser->getSkin();
218
219 $fromTitle = Title::newFromURL( $from );
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' => $indexMaxperpage + 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 < $indexMaxperpage) && ($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 = indexNamespaceForm ( $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 wfMsg ( 'allpages' ) );
275 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
276 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
277 $out2 .= " | " . $sk->makeKnownLink(
278 $wgContLang->specialPage( "Allpages" ),
279 wfMsg ( '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 ?>