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