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