Clean up after r14751:
[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 }
70
71 $dbr =& wfGetDB( DB_SLAVE );
72 if( $from != '' ) {
73 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
74 $flip = false;
75 } elseif( $until != '' ) {
76 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
77 $flip = true;
78 } else {
79 $pageCondition = '1 = 1';
80 $flip = false;
81 }
82 $limit = $wgCategoryPagingLimit;
83 $res = $dbr->select(
84 array( 'page', 'categorylinks' ),
85 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
86 array( $pageCondition,
87 'cl_from = page_id',
88 'cl_to' => $this->mTitle->getDBKey()),
89 #'page_is_redirect' => 0),
90 #+ $pageCondition,
91 $fname,
92 array( 'ORDER BY' => $flip ? 'cl_sortkey DESC' : 'cl_sortkey',
93 'LIMIT' => $limit + 1 ) );
94
95 $sk =& $wgUser->getSkin();
96 $r = "<br style=\"clear:both;\"/>\n";
97 $count = 0;
98 $nextPage = null;
99 while( $x = $dbr->fetchObject ( $res ) ) {
100 if( ++$count > $limit ) {
101 // We've reached the one extra which shows that there are
102 // additional pages to be had. Stop here...
103 $nextPage = $x->cl_sortkey;
104 break;
105 }
106
107 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
108
109 if( $title->getNamespace() == NS_CATEGORY ) {
110 // Subcategory; strip the 'Category' namespace from the link text.
111 array_push( $children, $sk->makeKnownLinkObj( $title, $wgContLang->convertHtml( $title->getText() ) ) );
112
113 // If there's a link from Category:A to Category:B, the sortkey of the resulting
114 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
115 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
116 // else use sortkey...
117 $sortkey='';
118 if( $title->getPrefixedText() == $x->cl_sortkey ) {
119 $sortkey=$wgContLang->firstChar( $x->page_title );
120 } else {
121 $sortkey=$wgContLang->firstChar( $x->cl_sortkey );
122 }
123 array_push( $children_start_char, $wgContLang->convert( $sortkey ) ) ;
124 } elseif( $showGallery && $title->getNamespace() == NS_IMAGE ) {
125 // Show thumbnails of categorized images, in a separate chunk
126 if( $flip ) {
127 $ig->insert( Image::newFromTitle( $title ) );
128 } else {
129 $ig->add( Image::newFromTitle( $title ) );
130 }
131 } else {
132 // Page in this category
133 array_push( $articles, $sk->makeSizeLinkObj( $x->page_len, $title, $wgContLang->convert( $title->getPrefixedText() ) ) ) ;
134 array_push( $articles_start_char, $wgContLang->convert( $wgContLang->firstChar( $x->cl_sortkey ) ) );
135 }
136 }
137 $dbr->freeResult( $res );
138
139 if( $flip ) {
140 $children = array_reverse( $children );
141 $children_start_char = array_reverse( $children_start_char );
142 $articles = array_reverse( $articles );
143 $articles_start_char = array_reverse( $articles_start_char );
144 }
145
146 if( $until != '' ) {
147 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
148 } elseif( $nextPage != '' || $from != '' ) {
149 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
150 }
151
152 # Don't show subcategories section if there are none.
153 if( count( $children ) > 0 ) {
154 # Showing subcategories
155 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
156 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), count( $children) );
157 $r .= $this->formatList( $children, $children_start_char );
158 }
159
160 # Showing articles in this category
161 $ti = htmlspecialchars( $this->mTitle->getText() );
162 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
163 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), count( $articles) );
164 $r .= $this->formatList( $articles, $articles_start_char );
165
166 if( $showGallery && ! $ig->isEmpty() ) {
167 $r.= $ig->toHTML();
168 }
169
170 if( $until != '' ) {
171 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
172 } elseif( $nextPage != '' || $from != '' ) {
173 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
174 }
175
176 wfProfileOut( $fname );
177 return $r;
178 }
179
180 /**
181 * Format a list of articles chunked by letter, either as a
182 * bullet list or a columnar format, depending on the length.
183 *
184 * @param array $articles
185 * @param array $articles_start_char
186 * @param int $cutoff
187 * @return string
188 * @private
189 */
190 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
191 if ( count ( $articles ) > $cutoff ) {
192 return $this->columnList( $articles, $articles_start_char );
193 } elseif ( count($articles) > 0) {
194 // for short lists of articles in categories.
195 return $this->shortList( $articles, $articles_start_char );
196 }
197 return '';
198 }
199
200 /**
201 * Format a list of articles chunked by letter in a three-column
202 * list, ordered vertically.
203 *
204 * @param array $articles
205 * @param array $articles_start_char
206 * @return string
207 * @private
208 */
209 function columnList( $articles, $articles_start_char ) {
210 // divide list into three equal chunks
211 $chunk = (int) (count ( $articles ) / 3);
212
213 // get and display header
214 $r = '<table width="100%"><tr valign="top">';
215
216 $prev_start_char = 'none';
217
218 // loop through the chunks
219 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
220 $chunkIndex < 3;
221 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
222 {
223 $r .= "<td>\n";
224 $atColumnTop = true;
225
226 // output all articles in category
227 for ($index = $startChunk ;
228 $index < $endChunk && $index < count($articles);
229 $index++ )
230 {
231 // check for change of starting letter or begining of chunk
232 if ( ($index == $startChunk) ||
233 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
234
235 {
236 if( $atColumnTop ) {
237 $atColumnTop = false;
238 } else {
239 $r .= "</ul>\n";
240 }
241 $cont_msg = "";
242 if ( $articles_start_char[$index] == $prev_start_char )
243 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
244 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
245 $prev_start_char = $articles_start_char[$index];
246 }
247
248 $r .= "<li>{$articles[$index]}</li>";
249 }
250 if( !$atColumnTop ) {
251 $r .= "</ul>\n";
252 }
253 $r .= "</td>\n";
254
255
256 }
257 $r .= '</tr></table>';
258 return $r;
259 }
260
261 /**
262 * Format a list of articles chunked by letter in a bullet list.
263 * @param array $articles
264 * @param array $articles_start_char
265 * @return string
266 * @private
267 */
268 function shortList( $articles, $articles_start_char ) {
269 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
270 $r .= '<ul><li>'.$articles[0].'</li>';
271 for ($index = 1; $index < count($articles); $index++ )
272 {
273 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
274 {
275 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
276 }
277
278 $r .= "<li>{$articles[$index]}</li>";
279 }
280 $r .= '</ul>';
281 return $r;
282 }
283
284 /**
285 * @param Title $title
286 * @param string $first
287 * @param string $last
288 * @param int $limit
289 * @param array $query - additional query options to pass
290 * @return string
291 * @private
292 */
293 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
294 global $wgUser, $wgLang;
295 $sk =& $wgUser->getSkin();
296 $limitText = $wgLang->formatNum( $limit );
297
298 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
299 if( $first != '' ) {
300 $prevLink = $sk->makeLinkObj( $title, $prevLink,
301 wfArrayToCGI( $query + array( 'until' => $first ) ) );
302 }
303 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
304 if( $last != '' ) {
305 $nextLink = $sk->makeLinkObj( $title, $nextLink,
306 wfArrayToCGI( $query + array( 'from' => $last ) ) );
307 }
308
309 return "($prevLink) ($nextLink)";
310 }
311 }
312
313
314 ?>