* Using getFormattedNamespaces() instead of getNamespaces() for faster
[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 ) {
12 global $indexMaxperpage, $toplevelMaxperpage, $wgRequest, $wgOut, $wgContLang;
13 # Config
14 $indexMaxperpage = 480;
15 $toplevelMaxperpage = 50;
16 # GET values
17 $from = $wgRequest->getVal( 'from' );
18 $namespace = $wgRequest->getInt( 'namespace' );
19 $invert = $wgRequest->getBool( 'invert' );
20
21 $namespaces = array_keys($wgContLang->getNamespaces());
22
23 if( !in_array($namespace, $namespaces) )
24 $namespace = 0;
25
26 if ($invert) {
27 $wgOut->setPagetitle( $namespace > 0 ?
28 wfMsg( 'allnotinnamespace', $names[$namespace] ) :
29 wfMsg( 'allnonarticles' )
30 );
31 } else {
32 $wgOut->setPagetitle( $namespace > 0 ?
33 wfMsg( 'allinnamespace', $names[$namespace] ) :
34 wfMsg( 'allarticles' )
35 );
36 }
37
38 if ( $par ) {
39 indexShowChunk( $namespace, $par, $invert );
40 } elseif ( $from ) {
41 indexShowChunk( $namespace, $from, $invert );
42 } else {
43 indexShowToplevel ( $namespace, $invert );
44 }
45 }
46
47 /**
48 * HTML for the top form
49 * @param integer $namespace A namespace constant (default NS_MAIN).
50 * @param string $from Article name we are starting listing at.
51 * @param bool $invert true if we want the namespaces inverted (default false)
52 */
53 function namespaceForm ( $namespace = NS_MAIN, $from = '', $invert ) {
54 global $wgContLang, $wgScript;
55 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
56
57 $namespaceselect = '<select name="namespace">';
58 $arr = $wgContLang->getFormattedNamespaces();
59 foreach ( $arr as $ns => $name ) {
60 if ($ns < NS_MAIN)
61 continue;
62 $n = $ns == 0 ? wfMsg ( 'blanknamespace' ) : $name;
63 $sel = $ns == $namespace ? ' selected="selected"' : '';
64 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
65 }
66 $namespaceselect .= '</select>';
67
68 $frombox = '<input type="text" size="20" name="from" value="'
69 . htmlspecialchars ( $from ) . '"/>';
70 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
71
72 $invertbox = "<input type='checkbox' name='invert' value='1'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
73
74 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
75 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
76 $out .= wfMsg ( 'allpagesformtext', $frombox, $namespaceselect, $submitbutton, $invertbox );
77 $out .= '</form></div>';
78 return $out;
79 }
80
81 /**
82 * @param integer $namespace (default NS_MAIN)
83 * @param bool $invert true if we want the namespaces inverted (default false)
84 */
85 function indexShowToplevel ( $namespace = NS_MAIN, $invert ) {
86 global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgContLang, $wgRequest, $wgUser;
87 $sk = $wgUser->getSkin();
88 $fname = "indexShowToplevel";
89
90 # TODO: Either make this *much* faster or cache the title index points
91 # in the querycache table.
92
93 $dbr =& wfGetDB( DB_SLAVE );
94 $page = $dbr->tableName( 'page' );
95 $fromwhere = "FROM $page WHERE page_namespace" .
96 ($invert ? '!' : '') . "=$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 $count = $dbr->selectField( 'page', 'COUNT(*)', $where, $fname );
103 $sections = ceil( $count / $indexMaxperpage );
104
105 if ( $sections < 3 ) {
106 # If there are only two or less sections, don't even display them.
107 # Instead, display the first section directly.
108 indexShowChunk( $namespace, '', $invert );
109 return;
110 }
111
112 # We want to display $toplevelMaxperpage lines starting at $offset.
113 # NOTICE: $offset starts at 0
114 $offset = intval ( $wgRequest->getVal( 'offset' ) );
115 if ( $offset < 0 ) { $offset = 0; }
116 if ( $offset >= $sections ) { $offset = $sections - 1; }
117
118 # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
119 # we're displaying only the very last section, we still need two DB queries to find the titles
120 $stopat = ( $offset + $toplevelMaxperpage < $sections )
121 ? $offset + $toplevelMaxperpage : $sections ;
122
123 # This array is going to hold the page_titles in order.
124 $lines = array();
125
126 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
127 for ( $i = $offset; $i <= $stopat; ++$i ) {
128 if ( $i == $sections ) # if we're displaying the last section, we need to
129 $from = $count-1; # find the last page_title in the DB
130 else if ( $i > $offset )
131 $from = $i * $indexMaxperpage - 1;
132 else
133 $from = $i * $indexMaxperpage;
134 $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
135 $sql = "SELECT page_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
136 $res = $dbr->query( $sql, $fname );
137 if ( $s = $dbr->fetchObject( $res ) ) {
138 array_push ( $lines, $s->page_title );
139 if ( $s = $dbr->fetchObject( $res ) ) {
140 array_push ( $lines, $s->page_title );
141 }
142 }
143 $dbr->freeResult( $res );
144 }
145
146 # At this point, $lines should contain an even number of elements.
147 $out .= "<table style='background: inherit;'>";
148 while ( count ( $lines ) > 0 ) {
149 $inpoint = array_shift ( $lines );
150 $outpoint = array_shift ( $lines );
151 $out .= indexShowline ( $inpoint, $outpoint, $namespace, $invert );
152 }
153 $out .= '</table>';
154
155 $nsForm = namespaceForm ( $namespace, '', $invert );
156
157 # Is there more?
158 $morelinks = '';
159 if ( $offset > 0 ) {
160 $morelinks = $sk->makeKnownLink (
161 $wgContLang->specialPage ( 'Allpages' ),
162 wfMsg ( 'allpagesprev' ),
163 ( $offset > $toplevelMaxperpage ) ? 'offset='.($offset-$toplevelMaxperpage) : ''
164 );
165 }
166 if ( $stopat < $sections-1 ) {
167 if ( $morelinks != '' ) { $morelinks .= " | "; }
168 $morelinks .= $sk->makeKnownLink (
169 $wgContLang->specialPage ( 'Allpages' ),
170 wfMsg ( 'allpagesnext' ),
171 'offset=' . ($offset + $toplevelMaxperpage)
172 );
173 }
174
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 $wgOut->addHtml( $out2 . $out );
185 }
186
187 /**
188 * @todo Document
189 * @param string $from
190 * @param integer $namespace (Default NS_MAIN)
191 * @param bool $invert true if we want the namespaces inverted (default false)
192 */
193 function indexShowline( $inpoint, $outpoint, $namespace = NS_MAIN, $invert ) {
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" : '') . ($invert ? "&invert=$invert" : '');
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 * @param bool $invert true if we want the namespaces inverted (default false)
216 */
217 function indexShowChunk( $namespace = NS_MAIN, $from, $invert ) {
218 global $wgOut, $wgUser, $indexMaxperpage, $wgContLang;
219 $sk = $wgUser->getSkin();
220 $maxPlusOne = $indexMaxperpage + 1;
221
222 $out = '';
223 $dbr =& wfGetDB( DB_SLAVE );
224 $page = $dbr->tableName( 'page' );
225
226 $fromTitle = Title::newFromURL( $from );
227 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
228
229 $sql = "SELECT page_namespace,page_title FROM $page WHERE page_namespace" .
230 ($invert ? '!' : '') . "=$namespace" .
231 " AND page_title >= ". $dbr->addQuotes( $fromKey ) .
232 " ORDER BY page_title LIMIT " . $maxPlusOne;
233 $res = $dbr->query( $sql, 'indexShowChunk' );
234
235 ### FIXME: side link to previous
236
237 $n = 0;
238 $out = '<table style="background: inherit;" border="0" width="100%">';
239
240 $namespaces = $wgContLang->getFormattedNamespaces();
241 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
242 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
243 if( $t ) {
244 $ns = $s->page_namespace;
245 $s = $invert && $namespaces[$ns] != $wgContLang->getNsText(NS_MAIN) ? ':' : '';
246 $n = $invert ? $namespaces[$ns] : '';
247 $link = $sk->makeKnownLinkObj( $t, $t->getText(), false, false, $n . $s );
248 } else {
249 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
250 }
251 if( $n % 3 == 0 ) {
252 $out .= '<tr>';
253 }
254 $out .= "<td>$link</td>";
255 $n++;
256 if( $n % 3 == 0 ) {
257 $out .= '</tr>';
258 }
259 }
260 if( ($n % 3) != 0 ) {
261 $out .= '</tr>';
262 }
263 $out .= '</table>';
264
265 $nsForm = namespaceForm ( $namespace, $from, $invert );
266 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
267 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
268 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
269 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
270 wfMsg ( 'allpages' ) );
271 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
272 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
273 $invertparam = $invert ? "&invert=$invert" : '';
274 $out2 .= " | " . $sk->makeKnownLink(
275 $wgContLang->specialPage( "Allpages" ),
276 wfMsg ( 'nextpage', $s->page_title ),
277 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam . $invertparam );
278 }
279 $out2 .= "</td></tr></table><hr />";
280
281 $wgOut->addHtml( $out2 . $out );
282 }
283
284 ?>