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