convert "::1" and other pseudo-IPv6 addresses that Apache may throw at us to their...
[lhc/web/wiklou.git] / includes / SpecialAllpages.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 /**
8 * Entry point : initialise variables and call subfunctions.
9 * @param $par String: becomes "FOO" when called like Special:Allpages/FOO (default NULL)
10 * @param $specialPage @see SpecialPage object.
11 */
12 function wfSpecialAllpages( $par=NULL, $specialPage ) {
13 global $wgRequest, $wgOut, $wgContLang;
14
15 # GET values
16 $from = $wgRequest->getVal( 'from' );
17 $namespace = $wgRequest->getInt( 'namespace' );
18
19 $namespaces = $wgContLang->getNamespaces();
20
21 $indexPage = new SpecialAllpages();
22
23 if( !in_array($namespace, array_keys($namespaces)) )
24 $namespace = 0;
25
26 $wgOut->setPagetitle( $namespace > 0 ?
27 wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
28 wfMsg( 'allarticles' )
29 );
30
31 if ( isset($par) ) {
32 $indexPage->showChunk( $namespace, $par, $specialPage->including() );
33 } elseif ( isset($from) ) {
34 $indexPage->showChunk( $namespace, $from, $specialPage->including() );
35 } else {
36 $indexPage->showToplevel ( $namespace, $specialPage->including() );
37 }
38 }
39
40 class SpecialAllpages {
41 var $maxPerPage=960;
42 var $topLevelMax=50;
43 var $name='Allpages';
44 # Determines, which message describes the input field 'nsfrom' (->SpecialPrefixindex.php)
45 var $nsfromMsg='allpagesfrom';
46
47 /**
48 * HTML for the top form
49 * @param integer $namespace A namespace constant (default NS_MAIN).
50 * @param string $from Article name we are starting listing at.
51 */
52 function namespaceForm ( $namespace = NS_MAIN, $from = '' ) {
53 global $wgScript;
54 $t = SpecialPage::getTitleFor( $this->name );
55
56 $namespaceselect = HTMLnamespaceselector($namespace, null);
57
58 $frombox = "<input type='text' size='20' name='from' id='nsfrom' value=\""
59 . htmlspecialchars ( $from ) . '"/>';
60 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" />';
61
62 $out = "<div class='namespaceoptions'><form method='get' action='{$wgScript}'>";
63 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
64 $out .= "
65 <table id='nsselect' class='allpages'>
66 <tr>
67 <td align='right'>" . wfMsgHtml($this->nsfromMsg) . "</td>
68 <td align='left'><label for='nsfrom'>$frombox</label></td>
69 </tr>
70 <tr>
71 <td align='right'><label for='namespace'>" . wfMsgHtml('namespace') . "</label></td>
72 <td align='left'>
73 $namespaceselect $submitbutton
74 </td>
75 </tr>
76 </table>
77 ";
78 $out .= '</form></div>';
79 return $out;
80 }
81
82 /**
83 * @param integer $namespace (default NS_MAIN)
84 */
85 function showToplevel ( $namespace = NS_MAIN, $including = false ) {
86 global $wgOut;
87 $fname = "indexShowToplevel";
88
89 # TODO: Either make this *much* faster or cache the title index points
90 # in the querycache table.
91
92 $dbr =& wfGetDB( DB_SLAVE );
93 $out = "";
94 $where = array( 'page_namespace' => $namespace );
95
96 global $wgMemc;
97 $key = wfMemcKey( 'allpages', 'ns', $namespace );
98 $lines = $wgMemc->get( $key );
99
100 if( !is_array( $lines ) ) {
101 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, array( 'LIMIT' => 1 ) );
102 $lastTitle = $firstTitle;
103
104 # This array is going to hold the page_titles in order.
105 $lines = array( $firstTitle );
106
107 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
108 $done = false;
109 for( $i = 0; !$done; ++$i ) {
110 // Fetch the last title of this chunk and the first of the next
111 $chunk = is_null( $lastTitle )
112 ? ''
113 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
114 $res = $dbr->select(
115 'page', /* FROM */
116 'page_title', /* WHAT */
117 $where + array( $chunk),
118 $fname,
119 array ('LIMIT' => 2, 'OFFSET' => $this->maxPerPage - 1, 'ORDER BY' => 'page_title') );
120
121 if ( $s = $dbr->fetchObject( $res ) ) {
122 array_push( $lines, $s->page_title );
123 } else {
124 // Final chunk, but ended prematurely. Go back and find the end.
125 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
126 array(
127 'page_namespace' => $namespace,
128 $chunk
129 ), $fname );
130 array_push( $lines, $endTitle );
131 $done = true;
132 }
133 if( $s = $dbr->fetchObject( $res ) ) {
134 array_push( $lines, $s->page_title );
135 $lastTitle = $s->page_title;
136 } else {
137 // This was a final chunk and ended exactly at the limit.
138 // Rare but convenient!
139 $done = true;
140 }
141 $dbr->freeResult( $res );
142 }
143 $wgMemc->add( $key, $lines, 3600 );
144 }
145
146 // If there are only two or less sections, don't even display them.
147 // Instead, display the first section directly.
148 if( count( $lines ) <= 2 ) {
149 $this->showChunk( $namespace, '', $including );
150 return;
151 }
152
153 # At this point, $lines should contain an even number of elements.
154 $out .= "<table class='allpageslist' style='background: inherit;'>";
155 while ( count ( $lines ) > 0 ) {
156 $inpoint = array_shift ( $lines );
157 $outpoint = array_shift ( $lines );
158 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
159 }
160 $out .= '</table>';
161 $nsForm = $this->namespaceForm ( $namespace, '', false );
162
163 # Is there more?
164 if ( $including ) {
165 $out2 = '';
166 } else {
167 $morelinks = '';
168 if ( $morelinks != '' ) {
169 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
170 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
171 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
172 $out2 .= $morelinks . '</td></tr></table><hr />';
173 } else {
174 $out2 = $nsForm . '<hr />';
175 }
176 }
177
178 $wgOut->addHtml( $out2 . $out );
179 }
180
181 /**
182 * @todo Document
183 * @param string $from
184 * @param integer $namespace (Default NS_MAIN)
185 */
186 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
187 global $wgUser;
188
189 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
190 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
191 $queryparams = ($namespace ? "namespace=$namespace" : '');
192 $special = SpecialPage::getTitleFor( $this->name, $inpoint );
193 $link = $special->escapeLocalUrl( $queryparams );
194
195 $out = wfMsgHtml(
196 'alphaindexline',
197 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
198 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
199 );
200 return '<tr><td align="right">'.$out.'</td></tr>';
201 }
202
203 /**
204 * @param integer $namespace (Default NS_MAIN)
205 * @param string $from list all pages from this name (default FALSE)
206 */
207 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
208 global $wgOut, $wgUser, $wgContLang;
209
210 $fname = 'indexShowChunk';
211
212 $sk = $wgUser->getSkin();
213
214 $fromList = $this->getNamespaceKeyAndText($namespace, $from);
215
216 if ( !$fromList ) {
217 $out = wfMsgWikiHtml( 'allpagesbadtitle' );
218 } else {
219 list( $namespace, $fromKey, $from ) = $fromList;
220
221 $dbr =& wfGetDB( DB_SLAVE );
222 $res = $dbr->select( 'page',
223 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
224 array(
225 'page_namespace' => $namespace,
226 'page_title >= ' . $dbr->addQuotes( $fromKey )
227 ),
228 $fname,
229 array(
230 'ORDER BY' => 'page_title',
231 'LIMIT' => $this->maxPerPage + 1,
232 'USE INDEX' => 'name_title',
233 )
234 );
235
236 ### FIXME: side link to previous
237
238 $n = 0;
239 $out = '<table style="background: inherit;" border="0" width="100%">';
240
241 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
242 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
243 if( $t ) {
244 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
245 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
246 ($s->page_is_redirect ? '</div>' : '' );
247 } else {
248 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
249 }
250 if( $n % 3 == 0 ) {
251 $out .= '<tr>';
252 }
253 $out .= "<td>$link</td>";
254 $n++;
255 if( $n % 3 == 0 ) {
256 $out .= '</tr>';
257 }
258 }
259 if( ($n % 3) != 0 ) {
260 $out .= '</tr>';
261 }
262 $out .= '</table>';
263 }
264
265 if ( $including ) {
266 $out2 = '';
267 } else {
268 $nsForm = $this->namespaceForm ( $namespace, $from );
269 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
270 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
271 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
272 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
273 wfMsgHtml ( 'allpages' ) );
274 if ( isset($dbr) && $dbr && ($n == $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
275 $self = SpecialPage::getTitleFor( 'Allpages' );
276 $q = 'from=' . $t->getPartialUrl() . ( $namespace ? '&namespace=' . $namespace : '' );
277 $out2 .= ' | ' . $sk->makeKnownLinkObj( $self, wfMsgHtml( 'nextpage', $t->getText() ), $q );
278 }
279 $out2 .= "</td></tr></table><hr />";
280 }
281
282 $wgOut->addHtml( $out2 . $out );
283 }
284
285 /**
286 * @param int $ns the namespace of the article
287 * @param string $text the name of the article
288 * @return array( int namespace, string dbkey, string pagename ) or NULL on error
289 * @static (sort of)
290 * @access private
291 */
292 function getNamespaceKeyAndText ($ns, $text) {
293 if ( $text == '' )
294 return array( $ns, '', '' ); # shortcut for common case
295
296 $t = Title::makeTitleSafe($ns, $text);
297 if ( $t && $t->isLocal() ) {
298 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
299 } else if ( $t ) {
300 return NULL;
301 }
302
303 # try again, in case the problem was an empty pagename
304 $text = preg_replace('/(#|$)/', 'X$1', $text);
305 $t = Title::makeTitleSafe($ns, $text);
306 if ( $t && $t->isLocal() ) {
307 return array( $t->getNamespace(), '', '' );
308 } else {
309 return NULL;
310 }
311 }
312 }
313
314 ?>