* Refactored namespace selector to it's own function
[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
43 /**
44 * HTML for the top form
45 * @param integer $namespace A namespace constant (default NS_MAIN).
46 * @param string $from Article name we are starting listing at.
47 */
48 function namespaceForm ( $namespace = NS_MAIN, $from = '' ) {
49 global $wgContLang, $wgScript;
50 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
51
52 $namespaceselect = HTMLnamespaceselector($namespace, null);
53
54 $frombox = "<input type='text' size='20' name='from' id='nsfrom' value=\""
55 . htmlspecialchars ( $from ) . '"/>';
56 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" />';
57
58 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
59 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
60 $out .= "
61 <table id='nsselect' class='allpages'>
62 <tr>
63 <td align='right'>" . wfMsgHtml('allpagesfrom') . "</td>
64 <td align='left'><label for='nsfrom'>$frombox</label></td>
65 </tr>
66 <tr>
67 <td align='right'><label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label></td>
68 <td align='left'>
69 $namespaceselect $submitbutton
70 </td>
71 </tr>
72 </table>
73 ";
74 $out .= '</form></div>';
75 return $out;
76 }
77
78 /**
79 * @param integer $namespace (default NS_MAIN)
80 */
81 function showToplevel ( $namespace = NS_MAIN, $including = false ) {
82 global $wgOut, $wgContLang, $wgRequest, $wgUser;
83 $sk = $wgUser->getSkin();
84 $fname = "indexShowToplevel";
85
86 # TODO: Either make this *much* faster or cache the title index points
87 # in the querycache table.
88
89 $dbr =& wfGetDB( DB_SLAVE );
90 $page = $dbr->tableName( 'page' );
91 $fromwhere = "FROM $page WHERE page_namespace=$namespace";
92 $order_arr = array ( 'ORDER BY' => 'page_title' );
93 $order_str = 'ORDER BY page_title';
94 $out = "";
95 $where = array( 'page_namespace' => $namespace );
96
97 global $wgMemc, $wgDBname;
98 $key = "$wgDBname:allpages:ns:$namespace";
99 $lines = $wgMemc->get( $key );
100
101 if( !is_array( $lines ) ) {
102 $firstTitle = $dbr->selectField( 'page', 'page_title', $where, $fname, array( 'LIMIT' => 1 ) );
103 $lastTitle = $firstTitle;
104
105 # This array is going to hold the page_titles in order.
106 $lines = array( $firstTitle );
107
108 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
109 $done = false;
110 for( $i = 0; !$done; ++$i ) {
111 // Fetch the last title of this chunk and the first of the next
112 $chunk = is_null( $lastTitle )
113 ? '1=1'
114 : 'page_title >= ' . $dbr->addQuotes( $lastTitle );
115 $sql = "SELECT page_title $fromwhere AND $chunk $order_str " .
116 $dbr->limitResult( 2, $this->topLevelMax - 1 );
117 $res = $dbr->query( $sql, $fname );
118 if ( $s = $dbr->fetchObject( $res ) ) {
119 array_push( $lines, $s->page_title );
120 } else {
121 // Final chunk, but ended prematurely. Go back and find the end.
122 $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
123 array(
124 'page_namespace' => $namespace,
125 $chunk
126 ), $fname );
127 array_push( $lines, $endTitle );
128 $done = true;
129 }
130 if( $s = $dbr->fetchObject( $res ) ) {
131 array_push( $lines, $s->page_title );
132 $lastTitle = $s->page_title;
133 } else {
134 // This was a final chunk and ended exactly at the limit.
135 // Rare but convenient!
136 $done = true;
137 }
138 $dbr->freeResult( $res );
139 }
140 $wgMemc->add( $key, $lines, 3600 );
141 }
142
143 // If there are only two or less sections, don't even display them.
144 // Instead, display the first section directly.
145 if( count( $lines ) <= 2 ) {
146 $this->showChunk( $namespace, '', false, $including );
147 return;
148 }
149
150 # At this point, $lines should contain an even number of elements.
151 $out .= "<table style='background: inherit;'>";
152 while ( count ( $lines ) > 0 ) {
153 $inpoint = array_shift ( $lines );
154 $outpoint = array_shift ( $lines );
155 $out .= $this->showline ( $inpoint, $outpoint, $namespace, false );
156 }
157 $out .= '</table>';
158
159 $nsForm = $this->namespaceForm ( $namespace, '', false );
160
161 # Is there more?
162 if ( $including ) {
163 $out2 = '';
164 } else {
165 $morelinks = '';
166 if ( $morelinks != '' ) {
167 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
168 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
169 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
170 $out2 .= $morelinks . '</td></tr></table><hr />';
171 } else {
172 $out2 = $nsForm . '<hr />';
173 }
174 }
175
176 $wgOut->addHtml( $out2 . $out );
177 }
178
179 /**
180 * @todo Document
181 * @param string $from
182 * @param integer $namespace (Default NS_MAIN)
183 */
184 function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
185 global $wgOut, $wgLang, $wgUser;
186 $sk = $wgUser->getSkin();
187 $dbr =& wfGetDB( DB_SLAVE );
188
189 $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
190 $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
191 $queryparams = ($namespace ? "namespace=$namespace" : '');
192 $special = Title::makeTitle( NS_SPECIAL, 'Allpages/' . $inpoint );
193 $link = $special->escapeLocalUrl( $queryparams );
194
195 $out = wfMsg(
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 $fromTitle = null;
215 if ($from!="") {
216 $fromTitle = Title::newFromURL( $from );
217 $fromNS = $fromTitle->getNamespace();
218 if ($namespace == NS_MAIN)
219 $namespace = $fromNS;
220 }
221 $fromKey = is_null( $fromTitle ) ? '' : $fromTitle->getDBkey();
222
223 $dbr =& wfGetDB( DB_SLAVE );
224 $res = $dbr->select( 'page',
225 array( 'page_namespace', 'page_title', 'page_is_redirect' ),
226 array(
227 'page_namespace' => $namespace,
228 'page_title >= ' . $dbr->addQuotes( $fromKey )
229 ),
230 $fname,
231 array(
232 'ORDER BY' => 'page_title',
233 'LIMIT' => $this->maxPerPage + 1,
234 'USE INDEX' => 'name_title',
235 )
236 );
237
238 ### FIXME: side link to previous
239
240 $n = 0;
241 $out = '<table style="background: inherit;" border="0" width="100%">';
242
243 $namespaces = $wgContLang->getFormattedNamespaces();
244 while( ($n < $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
245 $t = Title::makeTitle( $s->page_namespace, $s->page_title );
246 if( $t ) {
247 $link = ($s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
248 $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
249 ($s->page_is_redirect ? '</div>' : '' );
250 } else {
251 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
252 }
253 if( $n % 3 == 0 ) {
254 $out .= '<tr>';
255 }
256 $out .= "<td>$link</td>";
257 $n++;
258 if( $n % 3 == 0 ) {
259 $out .= '</tr>';
260 }
261 }
262 if( ($n % 3) != 0 ) {
263 $out .= '</tr>';
264 }
265 $out .= '</table>';
266
267 if ( $including ) {
268 $out2 = '';
269 } else {
270 $nsForm = $this->namespaceForm ( $namespace, $from );
271 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
272 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
273 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
274 $sk->makeKnownLink( $wgContLang->specialPage( "Allpages" ),
275 wfMsg ( 'allpages' ) );
276 if ( ($n == $this->maxPerPage) && ($s = $dbr->fetchObject( $res )) ) {
277 $namespaceparam = $namespace ? "&namespace=$namespace" : "";
278 $out2 .= " | " . $sk->makeKnownLink(
279 $wgContLang->specialPage( "Allpages" ),
280 wfMsg ( 'nextpage', $s->page_title ),
281 "from=" . wfUrlEncode ( $s->page_title ) . $namespaceparam );
282 }
283 $out2 .= "</td></tr></table><hr />";
284 }
285
286 $wgOut->addHtml( $out2 . $out );
287 }
288 }
289
290 ?>