* Removed messages in English, they'll be inherited from the parent.
[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 $names = $wgContLang->getNamespaces();
22
23 if( !isset( $names[$namespace] ) ) {
24 $namespace = 0;
25 }
26
27 if ($invert) {
28 $wgOut->setPagetitle( $namespace > 0 ?
29 wfMsg( 'allnotinnamespace', $names[$namespace] ) :
30 wfMsg( 'allnonarticles' )
31 );
32 } else {
33 $wgOut->setPagetitle( $namespace > 0 ?
34 wfMsg( 'allinnamespace', $names[$namespace] ) :
35 wfMsg( 'allarticles' )
36 );
37 }
38
39 if ( $par ) {
40 indexShowChunk( $namespace, $par, $invert );
41 } elseif ( $from ) {
42 indexShowChunk( $namespace, $from, $invert );
43 } else {
44 indexShowToplevel ( $namespace, $invert );
45 }
46 }
47
48 /**
49 * HTML for the top form
50 * @param integer $namespace A namespace constant (default NS_MAIN).
51 * @param string $from Article name we are starting listing at.
52 * @param bool $invert true if we want the namespaces inverted (default false)
53 */
54 function namespaceForm ( $namespace = NS_MAIN, $from = '', $invert ) {
55 global $wgContLang, $wgScript;
56 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
57
58 $namespaceselect = '<select name="namespace">';
59 $arr = $wgContLang->getNamespaces();
60 foreach ( $arr as $ns => $name ) {
61 if ( $ns < NS_MAIN ) continue;
62 $namespacename = str_replace ( '_', ' ', $name);
63 $n = ($ns == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
64 $sel = ($ns == $namespace) ? ' selected="selected"' : '';
65 $namespaceselect .= "<option value='{$ns}'{$sel}>{$n}</option>";
66 }
67 $namespaceselect .= '</select>';
68
69 $frombox = '<input type="text" size="20" name="from" value="'
70 . htmlspecialchars ( $from ) . '"/>';
71 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
72
73 $invertbox = "<input type='checkbox' name='invert' value='1'" . ( $invert ? ' checked="checked"' : '' ) . ' />';
74
75 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
76 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
77 $out .= wfMsg ( 'allpagesformtext', $frombox, $namespaceselect, $submitbutton, $invertbox );
78 $out .= '</form></div>';
79 return $out;
80 }
81
82 /**
83 * @param integer $namespace (default NS_MAIN)
84 * @param bool $invert true if we want the namespaces inverted (default false)
85 */
86 function indexShowToplevel ( $namespace = NS_MAIN, $invert ) {
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" .
97 ($invert ? '!' : '') . "=$namespace";
98 $order_arr = array ( 'ORDER BY' => 'page_title' );
99 $order_str = 'ORDER BY page_title';
100 $out = "";
101 $where = array( 'page_namespace' => $namespace );
102
103 $count = $dbr->selectField( 'page', 'COUNT(*)', $where, $fname );
104 $sections = ceil( $count / $indexMaxperpage );
105
106 if ( $sections < 3 ) {
107 # If there are only two or less sections, don't even display them.
108 # Instead, display the first section directly.
109 indexShowChunk( $namespace, '', $invert );
110 return;
111 }
112
113 # We want to display $toplevelMaxperpage lines starting at $offset.
114 # NOTICE: $offset starts at 0
115 $offset = intval ( $wgRequest->getVal( 'offset' ) );
116 if ( $offset < 0 ) { $offset = 0; }
117 if ( $offset >= $sections ) { $offset = $sections - 1; }
118
119 # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
120 # we're displaying only the very last section, we still need two DB queries to find the titles
121 $stopat = ( $offset + $toplevelMaxperpage < $sections )
122 ? $offset + $toplevelMaxperpage : $sections ;
123
124 # This array is going to hold the page_titles in order.
125 $lines = array();
126
127 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
128 for ( $i = $offset; $i <= $stopat; ++$i ) {
129 if ( $i == $sections ) # if we're displaying the last section, we need to
130 $from = $count-1; # find the last page_title in the DB
131 else if ( $i > $offset )
132 $from = $i * $indexMaxperpage - 1;
133 else
134 $from = $i * $indexMaxperpage;
135 $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
136 $sql = "SELECT page_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
137 $res = $dbr->query( $sql, $fname );
138 if ( $s = $dbr->fetchObject( $res ) ) {
139 array_push ( $lines, $s->page_title );
140 if ( $s = $dbr->fetchObject( $res ) ) {
141 array_push ( $lines, $s->page_title );
142 }
143 }
144 $dbr->freeResult( $res );
145 }
146
147 # At this point, $lines should contain an even number of elements.
148 $out .= "<table style='background: inherit;'>";
149 while ( count ( $lines ) > 0 ) {
150 $inpoint = array_shift ( $lines );
151 $outpoint = array_shift ( $lines );
152 $out .= indexShowline ( $inpoint, $outpoint, $namespace, $invert );
153 }
154 $out .= '</table>';
155
156 $nsForm = namespaceForm ( $namespace, '', $invert );
157
158 # Is there more?
159 $morelinks = '';
160 if ( $offset > 0 ) {
161 $morelinks = $sk->makeKnownLink (
162 $wgContLang->specialPage ( 'Allpages' ),
163 wfMsg ( 'allpagesprev' ),
164 ( $offset > $toplevelMaxperpage ) ? 'offset='.($offset-$toplevelMaxperpage) : ''
165 );
166 }
167 if ( $stopat < $sections-1 ) {
168 if ( $morelinks != '' ) { $morelinks .= " | "; }
169 $morelinks .= $sk->makeKnownLink (
170 $wgContLang->specialPage ( 'Allpages' ),
171 wfMsg ( 'allpagesnext' ),
172 'offset=' . ($offset + $toplevelMaxperpage)
173 );
174 }
175
176 if ( $morelinks != '' ) {
177 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
178 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
179 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
180 $out2 .= $morelinks . '</td></tr></table><hr />';
181 } else {
182 $out2 = $nsForm . '<hr />';
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 * @param bool $invert true if we want the namespaces inverted (default false)
193 */
194 function indexShowline( $inpoint, $outpoint, $namespace = NS_MAIN, $invert ) {
195 global $wgOut, $wgLang, $wgUser;
196 $sk = $wgUser->getSkin();
197 $dbr =& wfGetDB( DB_SLAVE );
198
199 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
200 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
201 $queryparams = ($namespace ? "namespace=$namespace" : '') . ($invert ? "&invert=$invert" : '');
202 $special = Title::makeTitle( NS_SPECIAL, 'Allpages/' . $inpoint );
203 $link = $special->escapeLocalUrl( $queryparams );
204
205 $out = wfMsg(
206 'alphaindexline',
207 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
208 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
209 );
210 return '<tr><td align="right">'.$out.'</td></tr>';
211 }
212
213 /**
214 * @param integer $namespace (Default NS_MAIN)
215 * @param string $from list all pages from this name (default FALSE)
216 * @param bool $invert true if we want the namespaces inverted (default false)
217 */
218 function indexShowChunk( $namespace = NS_MAIN, $from, $invert ) {
219 global $wgOut, $wgUser, $indexMaxperpage, $wgContLang;
220 $sk = $wgUser->getSkin();
221 $maxPlusOne = $indexMaxperpage + 1;
222
223 $out = '';
224 $dbr =& wfGetDB( DB_SLAVE );
225 $page = $dbr->tableName( 'page' );
226
227 $fromTitle = Title::newFromURL( $from );
228 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
229
230 $sql = "SELECT page_namespace,page_title FROM $page WHERE page_namespace" .
231 ($invert ? '!' : '') . "=$namespace" .
232 " AND page_title >= ". $dbr->addQuotes( $fromKey ) .
233 " ORDER BY page_title LIMIT " . $maxPlusOne;
234 $res = $dbr->query( $sql, 'indexShowChunk' );
235
236 ### FIXME: side link to previous
237
238 $n = 0;
239 $out = '<table style="background: inherit;" border="0" width="100%">';
240
241 $namespaces = $wgContLang->getNamespaces();
242 foreach($namespaces as $key => $ns) {
243 $namespaces[$key] = str_replace('_', ' ', $namespaces[$key]);
244 }
245 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
246 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
247 if( $t ) {
248 $ns = $s->page_namespace;
249 $s = $invert && $namespaces[$ns] != $wgContLang->getNsText(NS_MAIN) ? ':' : '';
250 $n = $invert ? $namespaces[$ns] : '';
251 $link = $sk->makeKnownLinkObj( $t, $t->getText(), false, false, $n . $s );
252 } else {
253 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
254 }
255 if( $n % 3 == 0 ) {
256 $out .= '<tr>';
257 }
258 $out .= "<td>$link</td>";
259 $n++;
260 if( $n % 3 == 0 ) {
261 $out .= '</tr>';
262 }
263 }
264 if( ($n % 3) != 0 ) {
265 $out .= '</tr>';
266 }
267 $out .= '</table>';
268
269 $nsForm = namespaceForm ( $namespace, $from, $invert );
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 $invertparam = $invert ? "&invert=$invert" : '';
278 $out2 .= " | " . $sk->makeKnownLink(
279 $wgContLang->specialPage( "Allpages" ),
280 wfMsg ( 'nextpage', $s->page_title ),
281 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam . $invertparam );
282 }
283 $out2 .= "</td></tr></table><hr />";
284
285 $wgOut->addHtml( $out2 . $out );
286 }
287
288 ?>