don't just assume we get a valid title object
[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 = 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 = $wgContLang->getNamespaces();
22
23 if( !in_array($namespace, array_keys($namespaces)) )
24 $namespace = 0;
25
26 if ($invert) {
27 $wgOut->setPagetitle( $namespace > 0 ?
28 wfMsg( 'allnotinnamespace', $namespaces[$namespace] ) :
29 wfMsg( 'allnonarticles' )
30 );
31 } else {
32 $wgOut->setPagetitle( $namespace > 0 ?
33 wfMsg( 'allinnamespace', $namespaces[$namespace] ) :
34 wfMsg( 'allarticles' )
35 );
36 }
37
38 if ( isset($par) ) {
39 indexShowChunk( $namespace, $par, $invert, $specialPage->including() );
40 } elseif ( isset($from) ) {
41 indexShowChunk( $namespace, $from, $invert, $specialPage->including() );
42 } else {
43 indexShowToplevel ( $namespace, $invert, $specialPage->including() );
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 indexNamespaceForm ( $namespace = NS_MAIN, $from = '', $invert = false ) {
54 global $wgContLang, $wgScript;
55 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
56
57 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
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' id='nsfrom' value=\""
69 . htmlspecialchars ( $from ) . '"/>';
70 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
71
72 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $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 .= "
77 <table id='nsselect' class='allpages'>
78 <tr>
79 <td align='right'>" . wfMsg('allpagesfrom') . "</td>
80 <td align='left'><label for='nsfrom'>$frombox</label></td>
81 </tr>
82 <tr>
83 <td align='right'><label for='nsselectbox'>" . wfMsg('namespace') . "</label></td>
84 <td align='left'>
85 $namespaceselect $submitbutton $invertbox
86 <label for='nsinvert'>" . wfMsg('invert') . "</label>
87 </td>
88 </tr>
89 </table>
90 ";
91 $out .= '</form></div>';
92 return $out;
93 }
94
95 /**
96 * @param integer $namespace (default NS_MAIN)
97 * @param bool $invert true if we want the namespaces inverted (default false)
98 */
99 function indexShowToplevel ( $namespace = NS_MAIN, $invert = false, $including = false ) {
100 global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgContLang, $wgRequest, $wgUser;
101 $sk = $wgUser->getSkin();
102 $fname = "indexShowToplevel";
103
104 # TODO: Either make this *much* faster or cache the title index points
105 # in the querycache table.
106
107 $dbr =& wfGetDB( DB_SLAVE );
108 $page = $dbr->tableName( 'page' );
109 $fromwhere = "FROM $page WHERE page_namespace" .
110 ($invert ? '!' : '') . "=$namespace";
111 $order_arr = array ( 'ORDER BY' => 'page_title' );
112 $order_str = 'ORDER BY page_title';
113 $out = "";
114 $where = array( 'page_namespace' => $namespace );
115
116 $count = $dbr->selectField( 'page', 'COUNT(*)', $where, $fname );
117 $sections = ceil( $count / $indexMaxperpage );
118
119 if ( $sections < 3 ) {
120 # If there are only two or less sections, don't even display them.
121 # Instead, display the first section directly.
122 indexShowChunk( $namespace, '', $invert, $including );
123 return;
124 }
125
126 # We want to display $toplevelMaxperpage lines starting at $offset.
127 # NOTICE: $offset starts at 0
128 $offset = intval ( $wgRequest->getVal( 'offset' ) );
129 if ( $offset < 0 ) { $offset = 0; }
130 if ( $offset >= $sections ) { $offset = $sections - 1; }
131
132 # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
133 # we're displaying only the very last section, we still need two DB queries to find the titles
134 $stopat = ( $offset + $toplevelMaxperpage < $sections )
135 ? $offset + $toplevelMaxperpage : $sections ;
136
137 # This array is going to hold the page_titles in order.
138 $lines = array();
139
140 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
141 for ( $i = $offset; $i <= $stopat; ++$i ) {
142 if ( $i == $sections ) # if we're displaying the last section, we need to
143 $from = $count-1; # find the last page_title in the DB
144 else if ( $i > $offset )
145 $from = $i * $indexMaxperpage - 1;
146 else
147 $from = $i * $indexMaxperpage;
148 $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
149 $sql = "SELECT page_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
150 $res = $dbr->query( $sql, $fname );
151 if ( $s = $dbr->fetchObject( $res ) ) {
152 array_push ( $lines, $s->page_title );
153 if ( $s = $dbr->fetchObject( $res ) ) {
154 array_push ( $lines, $s->page_title );
155 }
156 }
157 $dbr->freeResult( $res );
158 }
159
160 # At this point, $lines should contain an even number of elements.
161 $out .= "<table style='background: inherit;'>";
162 while ( count ( $lines ) > 0 ) {
163 $inpoint = array_shift ( $lines );
164 $outpoint = array_shift ( $lines );
165 $out .= indexShowline ( $inpoint, $outpoint, $namespace, $invert );
166 }
167 $out .= '</table>';
168
169 $nsForm = indexNamespaceForm ( $namespace, '', $invert );
170
171 # Is there more?
172 if ( $including ) {
173 $out2 = '';
174 } else {
175 $morelinks = '';
176 if ( $offset > 0 ) {
177 $morelinks = $sk->makeKnownLink (
178 $wgContLang->specialPage ( 'Allpages' ),
179 wfMsg ( 'allpagesprev' ),
180 ( $offset > $toplevelMaxperpage ) ? 'offset='.($offset-$toplevelMaxperpage) : ''
181 );
182 }
183 if ( $stopat < $sections-1 ) {
184 if ( $morelinks != '' ) { $morelinks .= " | "; }
185 $morelinks .= $sk->makeKnownLink (
186 $wgContLang->specialPage ( 'Allpages' ),
187 wfMsg ( 'allpagesnext' ),
188 'offset=' . ($offset + $toplevelMaxperpage)
189 );
190 }
191
192 if ( $morelinks != '' ) {
193 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
194 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
195 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
196 $out2 .= $morelinks . '</td></tr></table><hr />';
197 } else {
198 $out2 = $nsForm . '<hr />';
199 }
200 }
201
202 $wgOut->addHtml( $out2 . $out );
203 }
204
205 /**
206 * @todo Document
207 * @param string $from
208 * @param integer $namespace (Default NS_MAIN)
209 * @param bool $invert true if we want the namespaces inverted (default false)
210 */
211 function indexShowline( $inpoint, $outpoint, $namespace = NS_MAIN, $invert ) {
212 global $wgOut, $wgLang, $wgUser;
213 $sk = $wgUser->getSkin();
214 $dbr =& wfGetDB( DB_SLAVE );
215
216 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
217 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
218 $queryparams = ($namespace ? "namespace=$namespace" : '') . ($invert ? "&invert=$invert" : '');
219 $special = Title::makeTitle( NS_SPECIAL, 'Allpages/' . $inpoint );
220 $link = $special->escapeLocalUrl( $queryparams );
221
222 $out = wfMsg(
223 'alphaindexline',
224 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
225 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
226 );
227 return '<tr><td align="right">'.$out.'</td></tr>';
228 }
229
230 /**
231 * @param integer $namespace (Default NS_MAIN)
232 * @param string $from list all pages from this name (default FALSE)
233 * @param bool $invert true if we want the namespaces inverted (default false)
234 */
235 function indexShowChunk( $namespace = NS_MAIN, $from, $invert = false, $including = false ) {
236 global $wgOut, $wgUser, $indexMaxperpage, $wgContLang;
237 $sk = $wgUser->getSkin();
238 $maxPlusOne = $indexMaxperpage + 1;
239
240 $out = '';
241 $dbr =& wfGetDB( DB_SLAVE );
242 $page = $dbr->tableName( 'page' );
243
244 $fromTitle = Title::newFromURL( $from );
245 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
246
247 $sql = "SELECT page_namespace,page_title FROM $page WHERE page_namespace" .
248 ($invert ? '!' : '') . "=$namespace" .
249 " AND page_title >= ". $dbr->addQuotes( $fromKey ) .
250 " ORDER BY page_title LIMIT " . $maxPlusOne;
251 $res = $dbr->query( $sql, 'indexShowChunk' );
252
253 ### FIXME: side link to previous
254
255 $n = 0;
256 $out = '<table style="background: inherit;" border="0" width="100%">';
257
258 $namespaces = $wgContLang->getFormattedNamespaces();
259 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
260 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
261 if( $t ) {
262 $ns = $s->page_namespace;
263 $prefix = $invert ? $namespaces[$ns] : '';
264 $prefix .= $invert && $namespaces[$ns] != $wgContLang->getNsText(NS_MAIN) ? ':' : '';
265 $link = $sk->makeKnownLinkObj( $t, $t->getText(), false, false, $prefix );
266 } else {
267 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
268 }
269 if( $n % 3 == 0 ) {
270 $out .= '<tr>';
271 }
272 $out .= "<td>$link</td>";
273 $n++;
274 if( $n % 3 == 0 ) {
275 $out .= '</tr>';
276 }
277 }
278 if( ($n % 3) != 0 ) {
279 $out .= '</tr>';
280 }
281 $out .= '</table>';
282
283 if ( $including ) {
284 $out2 = '';
285 } else {
286 $nsForm = indexNamespaceForm ( $namespace, $from, $invert );
287 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
288 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
289 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
290 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
291 wfMsg ( 'allpages' ) );
292 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
293 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
294 $invertparam = $invert ? "&invert=$invert" : '';
295 $out2 .= " | " . $sk->makeKnownLink(
296 $wgContLang->specialPage( "Allpages" ),
297 wfMsg ( 'nextpage', $s->page_title ),
298 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam . $invertparam );
299 }
300 $out2 .= "</td></tr></table><hr />";
301 }
302
303 $wgOut->addHtml( $out2 . $out );
304 }
305
306 ?>