99e457ff15fe8fb49a617a1717b1e4e68c11d9b1
[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 # Config
15 $indexMaxperpage = 480;
16 $toplevelMaxperpage = 50;
17 # GET values
18 $from = $wgRequest->getVal( 'from' );
19 $namespace = $wgRequest->getInt( 'namespace' );
20 $invert = $wgRequest->getInt( 'invert' );
21
22 $names = $wgContLang->getNamespaces();
23
24 if( !isset( $names[$namespace] ) ) {
25 $namespace = 0;
26 }
27
28 if ($invert) {
29 $wgOut->setPagetitle( $namespace > 0 ?
30 wfMsg( 'allnotinnamespace', $names[$namespace] ) :
31 wfMsg( 'allnonarticles' )
32 );
33 } else {
34 $wgOut->setPagetitle( $namespace > 0 ?
35 wfMsg( 'allinnamespace', $names[$namespace] ) :
36 wfMsg( 'allarticles' )
37 );
38 }
39
40 if ( $par ) {
41 indexShowChunk( $namespace, $par, $invert );
42 } elseif ( $from ) {
43 indexShowChunk( $namespace, $from, $invert );
44 } else {
45 indexShowToplevel ( $namespace, $invert );
46 }
47 }
48
49 /**
50 * HTML for the top form
51 * @param integer $namespace A namespace constant (default NS_MAIN).
52 * @param string $from Article name we are starting listing at.
53 */
54 function namespaceForm ( $namespace = NS_MAIN, $from = '', $invert = 0) {
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 == 1 ? ' 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 * @todo Document
84 * @param integer $namespace (default NS_MAIN)
85 */
86 function indexShowToplevel ( $namespace = NS_MAIN, $invert = 0 ) {
87 global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgContLang, $wgRequest, $wgUser;
88 $sk = $wgUser->getSkin();
89 $fname = "indexShowToplevel";
90 $namespace = intval ($namespace);
91
92 # TODO: Either make this *much* faster or cache the title index points
93 # in the querycache table.
94
95 $dbr =& wfGetDB( DB_SLAVE );
96 $page = $dbr->tableName( 'page' );
97 $invsql = ($invert) ? '!' : '';
98 $fromwhere = "FROM $page WHERE page_namespace$invsql=$namespace";
99 $order_arr = array ( 'ORDER BY' => 'page_title' );
100 $order_str = 'ORDER BY page_title';
101 $out = "";
102 $where = array( 'page_namespace' => $namespace );
103
104 $count = $dbr->selectField( 'page', 'COUNT(*)', $where, $fname );
105 $sections = ceil( $count / $indexMaxperpage );
106
107 if ( $sections < 3 ) {
108 # If there are only two or less sections, don't even display them.
109 # Instead, display the first section directly.
110 indexShowChunk( $namespace, '', $invert );
111 return;
112 }
113
114 # We want to display $toplevelMaxperpage lines starting at $offset.
115 # NOTICE: $offset starts at 0
116 $offset = intval ( $wgRequest->getVal( 'offset' ) );
117 if ( $offset < 0 ) { $offset = 0; }
118 if ( $offset >= $sections ) { $offset = $sections - 1; }
119
120 # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
121 # we're displaying only the very last section, we still need two DB queries to find the titles
122 $stopat = ( $offset + $toplevelMaxperpage < $sections )
123 ? $offset + $toplevelMaxperpage : $sections ;
124
125 # This array is going to hold the page_titles in order.
126 $lines = array();
127
128 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
129 for ( $i = $offset; $i <= $stopat; ++$i ) {
130 if ( $i == $sections ) # if we're displaying the last section, we need to
131 $from = $count-1; # find the last page_title in the DB
132 else if ( $i > $offset )
133 $from = $i * $indexMaxperpage - 1;
134 else
135 $from = $i * $indexMaxperpage;
136 $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
137 $sql = "SELECT page_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
138 $res = $dbr->query( $sql, $fname );
139 $s = $dbr->fetchObject( $res );
140 array_push ( $lines, $s->page_title );
141 if ( $s = $dbr->fetchObject( $res ) ) {
142 array_push ( $lines, $s->page_title );
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 );
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 */
193 function indexShowline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
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='.intval($namespace)) : '';
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 function indexShowChunk( $namespace = NS_MAIN, $from, $invert ) {
213 global $wgOut, $wgUser, $indexMaxperpage, $wgContLang;
214 $sk = $wgUser->getSkin();
215 $maxPlusOne = $indexMaxperpage + 1;
216 $namespacee = intval($namespace);
217
218 $out = '';
219 $dbr =& wfGetDB( DB_SLAVE );
220 $page = $dbr->tableName( 'page' );
221
222 $fromTitle = Title::newFromURL( $from );
223 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
224
225 $invsql = ($invert) ? '!' : '';
226 $sql = "SELECT page_title FROM $page WHERE page_namespace$invsql=$namespacee" .
227 " AND page_title >= ". $dbr->addQuotes( $fromKey ) .
228 " ORDER BY page_title LIMIT " . $maxPlusOne;
229 $res = $dbr->query( $sql, 'indexShowChunk' );
230
231 ### FIXME: side link to previous
232
233 $n = 0;
234 $out = '<table style="background: inherit;" border="0" width="100%">';
235 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
236 $t = Title::makeTitle( $namespacee, $s->page_title );
237 if( $t ) {
238 $link = $sk->makeKnownLinkObj( $t, $t->getText() );
239 } else {
240 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
241 }
242 if( $n % 3 == 0 ) {
243 $out .= '<tr>';
244 }
245 $out .= "<td>$link</td>";
246 $n++;
247 if( $n % 3 == 0 ) {
248 $out .= '</tr>';
249 }
250 }
251 if( ($n % 3) != 0 ) {
252 $out .= '</tr>';
253 }
254 $out .= '</table>';
255
256 $nsForm = namespaceForm ( $namespace, $from, $invert );
257 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
258 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
259 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
260 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
261 wfMsg ( 'allpages' ) );
262 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
263 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
264 $invertparam = $invert ? "&invert=$invert" : '';
265 $out2 .= " | " . $sk->makeKnownLink(
266 $wgContLang->specialPage( "Allpages" ),
267 wfMsg ( 'nextpage', $s->page_title ),
268 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam . $invertparam );
269 }
270 $out2 .= "</td></tr></table><hr />";
271
272 $wgOut->addHtml( $out2 . $out );
273 }
274
275 ?>