Introduce 'FetchChangesList' hook; see docs/hooks.txt for more information
[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', $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 = Title::makeTitle( NS_SPECIAL, $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, $wgUser;
87 $sk = $wgUser->getSkin();
88 $fname = "indexShowToplevel";
89
90 # TODO: Either make this *much* faster or cache the title index points
91 # in the querycache table.
92
93 $dbr =& wfGetDB( DB_SLAVE );
94 $page = $dbr->tableName( 'page' );
95 $fromwhere = "FROM $page WHERE page_namespace=$namespace";
96 $order_arr = array ( 'ORDER BY' => 'page_title' );
97 $order_str = 'ORDER BY page_title';
98 $out = "";
99 $where = array( 'page_namespace' => $namespace );
100
101 global $wgMemc, $wgDBname;
102 $key = "$wgDBname:allpages:ns:$namespace";
103 $lines = $wgMemc->get( $key );
104
105 if( !is_array( $lines ) ) {
106 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, array( 'LIMIT' => 1 ) );
107 $lastTitle = $firstTitle;
108
109 # This array is going to hold the page_titles in order.
110 $lines = array( $firstTitle );
111
112 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
113 $done = false;
114 for( $i = 0; !$done; ++$i ) {
115 // Fetch the last title of this chunk and the first of the next
116 $chunk = is_null( $lastTitle )
117 ? ''
118 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
119 $res = $dbr->select(
120 'page', /* FROM */
121 'page_title', /* WHAT */
122 $where + array( $chunk),
123 $fname,
124 array ('LIMIT' => 2, 'OFFSET' => $this->maxPerPage - 1, 'ORDER BY' => 'page_title') );
125
126 if ( $s = $dbr->fetchObject( $res ) ) {
127 array_push( $lines, $s->page_title );
128 } else {
129 // Final chunk, but ended prematurely. Go back and find the end.
130 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
131 array(
132 'page_namespace' => $namespace,
133 $chunk
134 ), $fname );
135 array_push( $lines, $endTitle );
136 $done = true;
137 }
138 if( $s = $dbr->fetchObject( $res ) ) {
139 array_push( $lines, $s->page_title );
140 $lastTitle = $s->page_title;
141 } else {
142 // This was a final chunk and ended exactly at the limit.
143 // Rare but convenient!
144 $done = true;
145 }
146 $dbr->freeResult( $res );
147 }
148 $wgMemc->add( $key, $lines, 3600 );
149 }
150
151 // If there are only two or less sections, don't even display them.
152 // Instead, display the first section directly.
153 if( count( $lines ) <= 2 ) {
154 $this->showChunk( $namespace, '', $including );
155 return;
156 }
157
158 # At this point, $lines should contain an even number of elements.
159 $out .= "<table class='allpageslist' style='background: inherit;'>";
160 while ( count ( $lines ) > 0 ) {
161 $inpoint = array_shift ( $lines );
162 $outpoint = array_shift ( $lines );
163 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
164 }
165 $out .= '</table>';
166 $nsForm = $this->namespaceForm ( $namespace, '', false );
167
168 # Is there more?
169 if ( $including ) {
170 $out2 = '';
171 } else {
172 $morelinks = '';
173 if ( $morelinks != '' ) {
174 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
175 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
176 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
177 $out2 .= $morelinks . '</td></tr></table><hr />';
178 } else {
179 $out2 = $nsForm . '<hr />';
180 }
181 }
182
183 $wgOut->addHtml( $out2 . $out );
184 }
185
186 /**
187 * @todo Document
188 * @param string $from
189 * @param integer $namespace (Default NS_MAIN)
190 */
191 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
192 global $wgUser;
193 $sk = $wgUser->getSkin();
194 $dbr =& wfGetDB( DB_SLAVE );
195
196 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
197 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
198 $queryparams = ($namespace ? "namespace=$namespace" : '');
199 $special = Title::makeTitle( NS_SPECIAL, $this->name . '/' . $inpoint );
200 $link = $special->escapeLocalUrl( $queryparams );
201
202 $out = wfMsgHtml(
203 'alphaindexline',
204 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
205 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
206 );
207 return '<tr><td align="right">'.$out.'</td></tr>';
208 }
209
210 /**
211 * @param integer $namespace (Default NS_MAIN)
212 * @param string $from list all pages from this name (default FALSE)
213 */
214 function showChunk( $namespace = NS_MAIN, $from, $including = false ) {
215 global $wgOut, $wgUser, $wgContLang;
216
217 $fname = 'indexShowChunk';
218
219 $sk = $wgUser->getSkin();
220
221 $fromTitle = null;
222 if ($from!="") {
223 $fromTitle = Title::newFromURL( $from );
224 if (!$fromTitle) {
225 return;
226 }
227 $fromNS = $fromTitle->getNamespace();
228 if ($namespace == NS_MAIN)
229 $namespace = $fromNS;
230 }
231 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
232
233 $dbr =& wfGetDB( DB_SLAVE );
234 $res = $dbr->select( 'page',
235 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
236 array(
237 'page_namespace' => $namespace,
238 'page_title >= ' . $dbr->addQuotes( $fromKey )
239 ),
240 $fname,
241 array(
242 'ORDER BY' => 'page_title',
243 'LIMIT' => $this->maxPerPage + 1,
244 'USE INDEX' => 'name_title',
245 )
246 );
247
248 ### FIXME: side link to previous
249
250 $n = 0;
251 $out = '<table style="background: inherit;" border="0" width="100%">';
252
253 $namespaces = $wgContLang->getFormattedNamespaces();
254 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
255 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
256 if( $t ) {
257 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
258 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
259 ($s->page_is_redirect ? '</div>' : '' );
260 } else {
261 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
262 }
263 if( $n % 3 == 0 ) {
264 $out .= '<tr>';
265 }
266 $out .= "<td>$link</td>";
267 $n++;
268 if( $n % 3 == 0 ) {
269 $out .= '</tr>';
270 }
271 }
272 if( ($n % 3) != 0 ) {
273 $out .= '</tr>';
274 }
275 $out .= '</table>';
276
277
278 if ( $including ) {
279 $out2 = '';
280 } else {
281 $nsForm = $this->namespaceForm ( $namespace, $from );
282 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
283 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
284 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
285 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
286 wfMsgHtml ( 'allpages' ) );
287 if ( ($n == $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
288 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
289 $out2 .= " | " . $sk->makeKnownLink(
290 $wgContLang->specialPage( "Allpages" ),
291 wfMsgHtml ( 'nextpage', $s->page_title ),
292 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam );
293 }
294 $out2 .= "</td></tr></table><hr />";
295 }
296
297 $wgOut->addHtml( $out2 . $out );
298 }
299 }
300
301 ?>