In the allpages index, make the whole line links to hopefully
[lhc/web/wiklou.git] / includes / SpecialAllpages.php
1 <?php
2
3 function wfSpecialAllpages( $par=NULL )
4 {
5 global $indexMaxperpage, $toplevelMaxperpage, $wgRequest, $wgOut, $wgLang;
6 $indexMaxperpage = 480;
7 $toplevelMaxperpage = 50;
8 $from = $wgRequest->getVal( 'from' );
9 $namespace = $wgRequest->getInt( 'namespace' );
10 $names = $wgLang->getNamespaces();
11 if( !isset( $names[$namespace] ) ) {
12 $namespace = 0;
13 }
14 $wgOut->setPagetitle ( $namespace > 0 ? wfMsg ( 'allpagesnamespace', $names[$namespace] )
15 : wfMsg ( 'allarticles' ) );
16
17 if ( $par ) {
18 indexShowChunk( $par, $namespace );
19 } elseif ( $from ) {
20 indexShowChunk( $from, $namespace );
21 } else {
22 indexShowToplevel ( $namespace );
23 }
24 }
25
26 function namespaceForm ( $namespace = 0, $from = "" )
27 {
28 global $wgLang, $wgScript;
29
30 $t = Title::makeTitle( NS_SPECIAL, "Allpages" );
31
32 $namespaceselect = '<select name="namespace">';
33 $arr = $wgLang->getNamespaces();
34 for ( $i = 0; $i < 14; $i++ ) {
35 $namespacename = str_replace ( "_", " ", $arr[$i] );
36 $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
37 $sel = ($i == $namespace) ? ' selected="selected"' : '';
38 $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
39 }
40 $namespaceselect .= '</select>';
41
42 $frombox = '<input type="text" size="20" name="from" value="'
43 . htmlspecialchars ( $from ) . '"/>';
44 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
45
46 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
47 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
48 $out .= wfMsg ( 'allpagesformtext', $frombox, $namespaceselect, $submitbutton );
49 $out .= '</form></div>';
50 return $out;
51 }
52
53 function indexShowToplevel ( $namespace = 0 )
54 {
55 global $wgOut, $indexMaxperpage, $toplevelMaxperpage, $wgLang, $wgRequest, $wgUser;
56 $sk = $wgUser->getSkin();
57 $fname = "indexShowToplevel";
58 $namespace = intval ($namespace);
59
60 # TODO: Either make this *much* faster or cache the title index points
61 # in the querycache table.
62
63 $dbr =& wfGetDB( DB_SLAVE );
64 $cur = $dbr->tableName( 'cur' );
65 $fromwhere = "FROM $cur WHERE cur_namespace=$namespace";
66 $order_arr = array ( 'ORDER BY' => 'cur_title' );
67 $order_str = 'ORDER BY cur_title';
68 $out = "";
69 $where = array( 'cur_namespace' => $namespace );
70
71 $count = $dbr->selectField( 'cur', 'COUNT(*)', $where, $fname );
72 $sections = ceil( $count / $indexMaxperpage );
73
74 if ( $sections < 3 ) {
75 # If there are only two or less sections, don't even display them.
76 # Instead, display the first section directly.
77 indexShowChunk( '', $namespace );
78 return;
79 }
80
81 # We want to display $toplevelMaxperpage lines starting at $offset.
82 # NOTICE: $offset starts at 0
83 $offset = intval ( $wgRequest->getVal( 'offset' ) );
84 if ( $offset < 0 ) { $offset = 0; }
85 if ( $offset >= $sections ) { $offset = $sections - 1; }
86
87 # Where to stop? Notice that this can take the value of $sections, but $offset can't, because if
88 # we're displaying only the very last section, we still need two DB queries to find the titles
89 $stopat = ( $offset + $toplevelMaxperpage < $sections )
90 ? $offset + $toplevelMaxperpage : $sections ;
91
92 # This array is going to hold the cur_titles in order.
93 $lines = array();
94
95 # If we are going to show n rows, we need n+1 queries to find the relevant titles.
96 for ( $i = $offset; $i <= $stopat; $i++ ) {
97 if ( $i == $sections ) # if we're displaying the last section, we need to
98 $from = $count-1; # find the last cur_title in the DB
99 else if ( $i > $offset )
100 $from = $i * $indexMaxperpage - 1;
101 else
102 $from = $i * $indexMaxperpage;
103 $limit = ( $i == $offset || $i == $stopat ) ? 1 : 2;
104 $sql = "SELECT cur_title $fromwhere $order_str " . $dbr->limitResult ( $limit, $from );
105 $res = $dbr->query( $sql, $fname );
106 $s = $dbr->fetchObject( $res );
107 array_push ( $lines, $s->cur_title );
108 if ( $s = $dbr->fetchObject( $res ) ) {
109 array_push ( $lines, $s->cur_title );
110 }
111 $dbr->freeResult( $res );
112 }
113
114 # At this point, $lines should contain an even number of elements.
115 $out .= "<table style='background: inherit;'>";
116 while ( count ( $lines ) > 0 ) {
117 $inpoint = array_shift ( $lines );
118 $outpoint = array_shift ( $lines );
119 $out .= indexShowline ( $inpoint, $outpoint, $namespace );
120 }
121 $out .= "</table>";
122
123 $nsForm = namespaceForm ( $namespace );
124
125 # Is there more?
126 $morelinks = "";
127 if ( $offset > 0 ) {
128 $morelinks = $sk->makeKnownLink (
129 $wgLang->specialPage ( "Allpages" ),
130 wfMsg ( 'allpagesprev' ),
131 ( $offset > $toplevelMaxperpage ) ? 'offset='.($offset-$toplevelMaxperpage) : ''
132 );
133 }
134 if ( $stopat < $sections-1 ) {
135 if ( $morelinks != "" ) { $morelinks .= " | "; }
136 $morelinks .= $sk->makeKnownLink (
137 $wgLang->specialPage ( "Allpages" ),
138 wfMsg ( 'allpagesnext' ),
139 'offset=' . ($offset + $toplevelMaxperpage)
140 );
141 }
142
143 if ( $morelinks != "" ) {
144 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
145 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
146 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">';
147 $out2 .= $morelinks . '</td></tr></table><hr />';
148 } else {
149 $out2 = $nsForm . '<hr />';
150 }
151
152 $wgOut->addHtml( $out2 . $out );
153 }
154
155 function indexShowline( $inpoint, $outpoint, $namespace = 0 )
156 {
157 global $wgOut, $wgLang, $wgUser;
158 $sk = $wgUser->getSkin();
159 $dbr =& wfGetDB( DB_SLAVE );
160
161 $inpointf = htmlspecialchars( str_replace( "_", " ", $inpoint ) );
162 $outpointf = htmlspecialchars( str_replace( "_", " ", $outpoint ) );
163 $queryparams = $namespace ? ('namespace='.intval($namespace)) : '';
164 $special = Title::makeTitle( NS_SPECIAL, 'Allpages/' . $inpoint );
165 $link = $special->escapeLocalUrl( $queryparams );
166
167 $out = wfMsg(
168 'alphaindexline',
169 "<a href=\"$link\">$inpointf</a></td><td><a href=\"$link\">",
170 "</a></td><td align=\"left\"><a href=\"$link\">$outpointf</a>"
171 );
172 return '<tr><td align="right">'.$out.'</td></tr>';
173 }
174
175 function indexShowChunk( $from, $namespace = 0 )
176 {
177 global $wgOut, $wgUser, $indexMaxperpage, $wgLang;
178 $sk = $wgUser->getSkin();
179 $maxPlusOne = $indexMaxperpage + 1;
180 $namespacee = intval($namespace);
181
182 $out = "";
183 $dbr =& wfGetDB( DB_SLAVE );
184 $cur = $dbr->tableName( 'cur' );
185 $sql = "SELECT cur_title FROM $cur WHERE cur_namespace=$namespacee AND cur_title >= '"
186 . $dbr->strencode( $from ) . "' ORDER BY cur_title LIMIT " . $maxPlusOne;
187 $res = $dbr->query( $sql, "indexShowChunk" );
188
189 ### FIXME: side link to previous
190
191 $n = 0;
192 $out = '<table style="background: inherit;" border="0" width="100%">';
193 while( ($n < $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
194 $t = Title::makeTitle( $namespacee, $s->cur_title );
195 if( $t ) {
196 $link = $sk->makeKnownLinkObj( $t, $t->getText() );
197 } else {
198 $link = '[[' . htmlspecialchars( $s->cur_title ) . ']]';
199 }
200 if( $n % 3 == 0 ) {
201 $out .= '<tr>';
202 }
203 $out .= "<td>$link</td>";
204 $n++;
205 if( $n % 3 == 0 ) {
206 $out .= '</tr>';
207 }
208 }
209 if( ($n % 3) != 0 ) {
210 $out .= '</tr>';
211 }
212 $out .= '</table>';
213
214 $nsForm = namespaceForm ( $namespace, $from );
215 $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
216 $out2 .= '<tr valign="top"><td align="left">' . $nsForm;
217 $out2 .= '</td><td align="right" style="font-size: smaller; margin-bottom: 1em;">' .
218 $sk->makeKnownLink( $wgLang->specialPage( "Allpages" ),
219 wfMsg ( 'allpages' ) );
220 if ( ($n == $indexMaxperpage) && ($s = $dbr->fetchObject( $res )) ) {
221 $out2 .= " | " . $sk->makeKnownLink(
222 $wgLang->specialPage( "Allpages" ),
223 wfMsg ( 'nextpage', $s->cur_title ),
224 "from=" . wfUrlEncode ( $s->cur_title ) );
225 }
226 $out2 .= "</td></tr></table><hr />";
227
228 $wgOut->addHtml( $out2 . $out );
229 }
230
231 ?>