* (bug 829) Fix URL-escaping on block success
[lhc/web/wiklou.git] / includes / SpecialAllpages.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * Entry point : initialise variables and call subfunctions.
10 * @param string $par ????? (default NULL)
11 */
12 function wfSpecialAllpages( $par=NULL ) {
13 global $indexMaxperpage, $toplevelMaxperpage, $wgRequest, $wgOut, $wgContLang;
14 $indexMaxperpage = 480;
15 $toplevelMaxperpage = 50;
16 $from = $wgRequest->getVal( 'from' );
17 $namespace = $wgRequest->getInt( 'namespace' );
18 $names = $wgContLang->getNamespaces();
19 if( !isset( $names[$namespace] ) ) {
20 $namespace = 0;
21 }
22 $wgOut->setPagetitle ( $namespace > 0 ? wfMsg ( 'allpagesnamespace', $names[$namespace] )
23 : wfMsg ( 'allarticles' ) );
24
25 if ( $par ) {
26 indexShowChunk( $par, $namespace );
27 } elseif ( $from ) {
28 indexShowChunk( $from, $namespace );
29 } else {
30 indexShowToplevel ( $namespace );
31 }
32 }
33
34 /**
35 * HTML for the top form
36 * @param integer $namespace A namespace constant (default NS_MAIN).
37 * @param string $from Article name we are starting listing at.
38 */
39 function namespaceForm ( $namespace = NS_MAIN, $from = '' ) {
40 global $wgContLang, $wgScript;
41
42 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
43
44 $namespaceselect = '<select name="namespace">';
45 $arr = $wgContLang->getNamespaces();
46 for ( $i = 0; $i < 14; $i++ ) {
47 $namespacename = str_replace ( '_', ' ', $arr[$i] );
48 $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
49 $sel = ($i == $namespace) ? ' selected="selected"' : '';
50 $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
51 }
52 $namespaceselect .= '</select>';
53
54 $frombox = '<input type="text" size="20" name="from" value="'
55 . htmlspecialchars ( $from ) . '"/>';
56 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
57
58 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
59 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
60 $out .= wfMsg ( 'allpagesformtext1', $frombox ) . '<br />';
61 $out .= wfMsg ( 'allpagesformtext2', $namespaceselect, $submitbutton );
62 $out .= '</form></div>';
63 return $out;
64 }
65
66 /**
67 * @todo Document
68 * @param integer $namespace (default NS_MAIN)
69 */
70 function indexShowToplevel ( $namespace = NS_MAIN ) {
71 global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgContLang, $wgRequest, $wgUser;
72 $sk = $wgUser->getSkin();
73 $fname = "indexShowToplevel";
74 $namespace = intval ($namespace);
75
76 # TODO: Either make this *much* faster or cache the title index points
77 # in the querycache table.
78
79 $dbr =& wfGetDB( DB_SLAVE );
80 $page = $dbr->tableName( 'page' );
81 $fromwhere = "FROM $page WHERE page_namespace=$namespace";
82 $order_arr = array ( 'ORDER BY' => 'page_title' );
83 $order_str = 'ORDER BY page_title';
84 $out = "";
85 $where = array( 'page_namespace' => $namespace );
86
87 $count = $dbr->selectField( 'page', 'COUNT(*)', $where, $fname );
88 $sections = ceil( $count / $indexMaxperpage );
89
90 if ( $sections < 3 ) {
91 # If there are only two or less sections, don't even display them.
92 # Instead, display the first section directly.
93 indexShowChunk( '', $namespace );
94 return;
95 }
96
97 # We want to display $toplevelMaxperpage lines starting at $offset.
98 # NOTICE: $offset starts at 0
99 $offset = intval ( $wgRequest->getVal( 'offset' ) );
100 if ( $offset < 0 ) { $offset = 0; }
101 if ( $offset >= $sections ) { $offset = $sections - 1; }
102
103 # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
104 # we're displaying only the very last section, we still need two DB queries to find the titles
105 $stopat = ( $offset + $toplevelMaxperpage < $sections )
106 ? $offset + $toplevelMaxperpage : $sections ;
107
108 # This array is going to hold the page_titles in order.
109 $lines = array();
110
111 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
112 for ( $i = $offset; $i <= $stopat; $i++ ) {
113 if ( $i == $sections ) # if we're displaying the last section, we need to
114 $from = $count-1; # find the last page_title in the DB
115 else if ( $i > $offset )
116 $from = $i * $indexMaxperpage - 1;
117 else
118 $from = $i * $indexMaxperpage;
119 $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
120 $sql = "SELECT page_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
121 $res = $dbr->query( $sql, $fname );
122 $s = $dbr->fetchObject( $res );
123 array_push ( $lines, $s->page_title );
124 if ( $s = $dbr->fetchObject( $res ) ) {
125 array_push ( $lines, $s->page_title );
126 }
127 $dbr->freeResult( $res );
128 }
129
130 # At this point, $lines should contain an even number of elements.
131 $out .= "<table style='background: inherit;'>";
132 while ( count ( $lines ) > 0 ) {
133 $inpoint = array_shift ( $lines );
134 $outpoint = array_shift ( $lines );
135 $out .= indexShowline ( $inpoint, $outpoint, $namespace );
136 }
137 $out .= '</table>';
138
139 $nsForm = namespaceForm ( $namespace );
140
141 # Is there more?
142 $morelinks = '';
143 if ( $offset > 0 ) {
144 $morelinks = $sk->makeKnownLink (
145 $wgContLang->specialPage ( 'Allpages' ),
146 wfMsg ( 'allpagesprev' ),
147 ( $offset > $toplevelMaxperpage ) ? 'offset='.($offset-$toplevelMaxperpage) : ''
148 );
149 }
150 if ( $stopat < $sections-1 ) {
151 if ( $morelinks != '' ) { $morelinks .= " | "; }
152 $morelinks .= $sk->makeKnownLink (
153 $wgContLang->specialPage ( 'Allpages' ),
154 wfMsg ( 'allpagesnext' ),
155 'offset=' . ($offset + $toplevelMaxperpage)
156 );
157 }
158
159 if ( $morelinks != '' ) {
160 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
161 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
162 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
163 $out2 .= $morelinks . '</td></tr></table><hr />';
164 } else {
165 $out2 = $nsForm . '<hr />';
166 }
167
168 $wgOut->addHtml( $out2 . $out );
169 }
170
171 /**
172 * @todo Document
173 * @param string $from
174 * @param integer $namespace (Default NS_MAIN)
175 */
176 function indexShowline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
177 global $wgOut, $wgLang, $wgUser;
178 $sk = $wgUser->getSkin();
179 $dbr =& wfGetDB( DB_SLAVE );
180
181 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
182 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
183 $queryparams = $namespace ? ('namespace='.intval($namespace)) : '';
184 $special = Title::makeTitle( NS_SPECIAL, 'Allpages/' . $inpoint );
185 $link = $special->escapeLocalUrl( $queryparams );
186
187 $out = wfMsg(
188 'alphaindexline',
189 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
190 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
191 );
192 return '<tr><td align="right">'.$out.'</td></tr>';
193 }
194
195 function indexShowChunk( $from, $namespace = NS_MAIN ) {
196 global $wgOut, $wgUser, $indexMaxperpage, $wgContLang;
197 $sk = $wgUser->getSkin();
198 $maxPlusOne = $indexMaxperpage + 1;
199 $namespacee = intval($namespace);
200
201 $out = '';
202 $dbr =& wfGetDB( DB_SLAVE );
203 $page = $dbr->tableName( 'page' );
204
205 $fromTitle = Title::newFromURL( $from );
206 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
207
208 $sql = "SELECT page_title FROM $page WHERE page_namespace=$namespacee" .
209 " AND page_title >= ". $dbr->addQuotes( $fromKey ) .
210 " ORDER BY page_title LIMIT " . $maxPlusOne;
211 $res = $dbr->query( $sql, 'indexShowChunk' );
212
213 ### FIXME: side link to previous
214
215 $n = 0;
216 $out = '<table style="background: inherit;" border="0" width="100%">';
217 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
218 $t = Title::makeTitle( $namespacee, $s->page_title );
219 if( $t ) {
220 $link = $sk->makeKnownLinkObj( $t, $t->getText() );
221 } else {
222 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
223 }
224 if( $n % 3 == 0 ) {
225 $out .= '<tr>';
226 }
227 $out .= "<td>$link</td>";
228 $n++;
229 if( $n % 3 == 0 ) {
230 $out .= '</tr>';
231 }
232 }
233 if( ($n % 3) != 0 ) {
234 $out .= '</tr>';
235 }
236 $out .= '</table>';
237
238 $nsForm = namespaceForm ( $namespace, $from );
239 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
240 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
241 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
242 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
243 wfMsg ( 'allpages' ) );
244 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
245 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
246 $out2 .= " | " . $sk->makeKnownLink(
247 $wgContLang->specialPage( "Allpages" ),
248 wfMsg ( 'nextpage', $s->page_title ),
249 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam );
250 }
251 $out2 .= "</td></tr></table><hr />";
252
253 $wgOut->addHtml( $out2 . $out );
254 }
255
256 ?>