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