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