Revert accidental commit of experimentation
[lhc/web/wiklou.git] / includes / CategoryPage.php
1 <?php
2 /**
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
5 *
6 * @package MediaWiki
7 */
8
9 if( !defined( 'MEDIAWIKI' ) )
10 die( 1 );
11
12 /**
13 * @package MediaWiki
14 */
15 class CategoryPage extends Article {
16
17 function view() {
18 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
19
20 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
21 $this->openShowCategory();
22 }
23
24 Article::view();
25
26 # If the article we've just shown is in the "Image" namespace,
27 # follow it with the history list and link list for the image
28 # it describes.
29
30 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
31 $this->closeShowCategory();
32 }
33 }
34
35 function openShowCategory() {
36 # For overloading
37 }
38
39 function closeShowCategory() {
40 global $wgOut, $wgRequest;
41 $from = $wgRequest->getVal( 'from' );
42 $until = $wgRequest->getVal( 'until' );
43
44 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
45 }
46
47 /**
48 * Format the category data list.
49 *
50 * @param string $from -- return only sort keys from this item on
51 * @param string $until -- don't return keys after this point.
52 * @return string HTML output
53 * @private
54 */
55 function doCategoryMagic( $from = '', $until = '' ) {
56 global $wgOut;
57 global $wgContLang,$wgUser, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
58 $fname = 'CategoryPage::doCategoryMagic';
59 wfProfileIn( $fname );
60
61 $articles = array();
62 $articles_start_char = array();
63 $children = array();
64 $children_start_char = array();
65
66 $showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
67 if( $showGallery ) {
68 $ig = new ImageGallery();
69 $ig->setParsing();
70 }
71
72 $dbr =& wfGetDB( DB_SLAVE );
73 if( $from != '' ) {
74 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
75 $flip = false;
76 } elseif( $until != '' ) {
77 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
78 $flip = true;
79 } else {
80 $pageCondition = '1 = 1';
81 $flip = false;
82 }
83 $limit = $wgCategoryPagingLimit;
84 $res = $dbr->select(
85 array( 'page', 'categorylinks' ),
86 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
87 array( $pageCondition,
88 'cl_from = page_id',
89 'cl_to' => $this->mTitle->getDBKey()),
90 #'page_is_redirect' => 0),
91 #+ $pageCondition,
92 $fname,
93 array( 'ORDER BY' => $flip ? 'cl_sortkey DESC' : 'cl_sortkey',
94 'LIMIT' => $limit + 1 ) );
95
96 $sk =& $wgUser->getSkin();
97 $r = "<br style=\"clear:both;\"/>\n";
98 $count = 0;
99 $nextPage = null;
100 while( $x = $dbr->fetchObject ( $res ) ) {
101 if( ++$count > $limit ) {
102 // We've reached the one extra which shows that there are
103 // additional pages to be had. Stop here...
104 $nextPage = $x->cl_sortkey;
105 break;
106 }
107
108 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
109
110 if( $title->getNamespace() == NS_CATEGORY ) {
111 // Subcategory; strip the 'Category' namespace from the link text.
112 array_push( $children, $sk->makeKnownLinkObj( $title, $wgContLang->convertHtml( $title->getText() ) ) );
113
114 // If there's a link from Category:A to Category:B, the sortkey of the resulting
115 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
116 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
117 // else use sortkey...
118 $sortkey='';
119 if( $title->getPrefixedText() == $x->cl_sortkey ) {
120 $sortkey=$wgContLang->firstChar( $x->page_title );
121 } else {
122 $sortkey=$wgContLang->firstChar( $x->cl_sortkey );
123 }
124 array_push( $children_start_char, $wgContLang->convert( $sortkey ) ) ;
125 } elseif( $showGallery && $title->getNamespace() == NS_IMAGE ) {
126 // Show thumbnails of categorized images, in a separate chunk
127 if( $flip ) {
128 $ig->insert( Image::newFromTitle( $title ) );
129 } else {
130 $ig->add( Image::newFromTitle( $title ) );
131 }
132 } else {
133 // Page in this category
134 array_push( $articles, $sk->makeSizeLinkObj( $x->page_len, $title, $wgContLang->convert( $title->getPrefixedText() ) ) ) ;
135 array_push( $articles_start_char, $wgContLang->convert( $wgContLang->firstChar( $x->cl_sortkey ) ) );
136 }
137 }
138 $dbr->freeResult( $res );
139
140 if( $flip ) {
141 $children = array_reverse( $children );
142 $children_start_char = array_reverse( $children_start_char );
143 $articles = array_reverse( $articles );
144 $articles_start_char = array_reverse( $articles_start_char );
145 }
146
147 if( $until != '' ) {
148 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
149 } elseif( $nextPage != '' || $from != '' ) {
150 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
151 }
152
153 # Don't show subcategories section if there are none.
154 if( count( $children ) > 0 ) {
155 # Showing subcategories
156 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
157 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), count( $children) );
158 $r .= $this->formatList( $children, $children_start_char );
159 }
160
161 # Showing articles in this category
162 $ti = htmlspecialchars( $this->mTitle->getText() );
163 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
164 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), count( $articles) );
165 $r .= $this->formatList( $articles, $articles_start_char );
166
167 if( $showGallery && ! $ig->isEmpty() ) {
168 $r.= $ig->toHTML();
169 }
170
171 if( $until != '' ) {
172 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
173 } elseif( $nextPage != '' || $from != '' ) {
174 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
175 }
176
177 wfProfileOut( $fname );
178 return $r;
179 }
180
181 /**
182 * Format a list of articles chunked by letter, either as a
183 * bullet list or a columnar format, depending on the length.
184 *
185 * @param array $articles
186 * @param array $articles_start_char
187 * @param int $cutoff
188 * @return string
189 * @private
190 */
191 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
192 if ( count ( $articles ) > $cutoff ) {
193 return $this->columnList( $articles, $articles_start_char );
194 } elseif ( count($articles) > 0) {
195 // for short lists of articles in categories.
196 return $this->shortList( $articles, $articles_start_char );
197 }
198 return '';
199 }
200
201 /**
202 * Format a list of articles chunked by letter in a three-column
203 * list, ordered vertically.
204 *
205 * @param array $articles
206 * @param array $articles_start_char
207 * @return string
208 * @private
209 */
210 function columnList( $articles, $articles_start_char ) {
211 // divide list into three equal chunks
212 $chunk = (int) (count ( $articles ) / 3);
213
214 // get and display header
215 $r = '<table width="100%"><tr valign="top">';
216
217 $prev_start_char = 'none';
218
219 // loop through the chunks
220 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
221 $chunkIndex < 3;
222 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
223 {
224 $r .= "<td>\n";
225 $atColumnTop = true;
226
227 // output all articles in category
228 for ($index = $startChunk ;
229 $index < $endChunk && $index < count($articles);
230 $index++ )
231 {
232 // check for change of starting letter or begining of chunk
233 if ( ($index == $startChunk) ||
234 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
235
236 {
237 if( $atColumnTop ) {
238 $atColumnTop = false;
239 } else {
240 $r .= "</ul>\n";
241 }
242 $cont_msg = "";
243 if ( $articles_start_char[$index] == $prev_start_char )
244 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
245 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
246 $prev_start_char = $articles_start_char[$index];
247 }
248
249 $r .= "<li>{$articles[$index]}</li>";
250 }
251 if( !$atColumnTop ) {
252 $r .= "</ul>\n";
253 }
254 $r .= "</td>\n";
255
256
257 }
258 $r .= '</tr></table>';
259 return $r;
260 }
261
262 /**
263 * Format a list of articles chunked by letter in a bullet list.
264 * @param array $articles
265 * @param array $articles_start_char
266 * @return string
267 * @private
268 */
269 function shortList( $articles, $articles_start_char ) {
270 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
271 $r .= '<ul><li>'.$articles[0].'</li>';
272 for ($index = 1; $index < count($articles); $index++ )
273 {
274 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
275 {
276 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
277 }
278
279 $r .= "<li>{$articles[$index]}</li>";
280 }
281 $r .= '</ul>';
282 return $r;
283 }
284
285 /**
286 * @param Title $title
287 * @param string $first
288 * @param string $last
289 * @param int $limit
290 * @param array $query - additional query options to pass
291 * @return string
292 * @private
293 */
294 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
295 global $wgUser, $wgLang;
296 $sk =& $wgUser->getSkin();
297 $limitText = $wgLang->formatNum( $limit );
298
299 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
300 if( $first != '' ) {
301 $prevLink = $sk->makeLinkObj( $title, $prevLink,
302 wfArrayToCGI( $query + array( 'until' => $first ) ) );
303 }
304 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
305 if( $last != '' ) {
306 $nextLink = $sk->makeLinkObj( $title, $nextLink,
307 wfArrayToCGI( $query + array( 'from' => $last ) ) );
308 }
309
310 return "($prevLink) ($nextLink)";
311 }
312 }
313
314
315 ?>