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