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