3 * Special handling for category description pages
4 * Modelled after ImagePage.php
9 if( !defined( 'MEDIAWIKI' ) )
12 if( $wgCategoryMagicGallery )
14 require_once('ImageGallery.php');
19 class CategoryPage
extends Article
{
22 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
24 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
25 $this->openShowCategory();
30 # If the article we've just shown is in the "Image" namespace,
31 # follow it with the history list and link list for the image
34 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
35 $this->closeShowCategory();
39 function openShowCategory() {
43 # generate a list of subcategories and pages for a category
44 # depending on wfMsg("usenewcategorypage") it either calls the new
45 # or the old code. The new code will not work properly for some
46 # languages due to sorting issues, so they might want to turn it
49 function closeShowCategory() {
50 global $wgOut, $wgRequest;
51 $pageConditions = array();
52 $from = $wgRequest->getVal( 'from' );
53 $until = $wgRequest->getVal( 'until' );
54 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
58 * Format the category data list.
60 * @param string $from -- return only sort keys from this item on
61 * @param string $until -- don't return keys after this point.
62 * @return string HTML output
65 function doCategoryMagic( $from = '', $until = '' ) {
66 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
67 $fname = 'CategoryPage::doCategoryMagic';
68 wfProfileIn( $fname );
71 $articles_start_char = array();
73 $children_start_char = array();
75 if( $wgCategoryMagicGallery ) {
76 $ig = new ImageGallery();
79 $dbr =& wfGetDB( DB_SLAVE
);
81 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
83 } elseif( $until != '' ) {
84 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
92 array( 'page', 'categorylinks' ),
93 array( 'page_title', 'page_namespace', 'cl_sortkey' ),
94 array( $pageCondition,
96 'cl_to' => $this->mTitle
->getDBKey()),
97 #'page_is_redirect' => 0),
100 array( 'ORDER BY' => $flip ?
'cl_sortkey DESC' : 'cl_sortkey',
101 'LIMIT' => $limit +
1 ) );
103 $sk =& $wgUser->getSkin();
104 $r = "<br style=\"clear:both;\"/>\n";
107 while( $x = $dbr->fetchObject ( $res ) ) {
108 if( ++
$count > $limit ) {
109 // We've reached the one extra which shows that there are
110 // additional pages to be had. Stop here...
111 $nextPage = $x->cl_sortkey
;
115 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
117 if( $title->getNamespace() == NS_CATEGORY
) {
118 // Subcategory; strip the 'Category' namespace from the link text.
119 array_push( $children, $sk->makeKnownLinkObj( $title, $title->getText() ) );
121 // If there's a link from Category:A to Category:B, the sortkey of the resulting
122 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
123 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
124 // else use sortkey...
125 if( $title->getPrefixedText() == $x->cl_sortkey
) {
126 array_push( $children_start_char, $wgContLang->firstChar( $x->page_title
) );
128 array_push( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
130 } elseif( $wgCategoryMagicGallery && $title->getNamespace() == NS_IMAGE
) {
131 // Show thumbnails of categorized images, in a separate chunk
133 $ig->insert( Image
::newFromTitle( $title ) );
135 $ig->add( Image
::newFromTitle( $title ) );
138 // Page in this category
139 array_push( $articles, $sk->makeKnownLinkObj( $title ) ) ;
140 array_push( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
143 $dbr->freeResult( $res );
146 $children = array_reverse( $children );
147 $children_start_char = array_reverse( $children_start_char );
148 $articles = array_reverse( $articles );
149 $articles_start_char = array_reverse( $articles_start_char );
153 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
154 } elseif( $nextPage != '' ||
$from != '' ) {
155 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
158 # Don't show subcategories section if there are none.
159 if( count( $children ) > 0 ) {
160 # Showing subcategories
161 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
162 $r .= $this->formatCount( $children, 'subcategorycount' );
163 $r .= $this->formatList( $children, $children_start_char );
166 # Showing articles in this category
167 $ti = htmlspecialchars( $this->mTitle
->getText() );
168 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
169 $r .= $this->formatCount( $articles, 'categoryarticlecount' );
170 $r .= $this->formatList( $articles, $articles_start_char );
172 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
176 wfProfileOut( $fname );
181 * @param array $articles
182 * @param string $message
186 function formatCount( $articles, $message ) {
188 $numart = count( $articles );
190 # Slightly different message to avoid silly plural
193 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
196 * Format a list of articles chunked by letter, either as a
197 * bullet list or a columnar format, depending on the length.
199 * @param array $articles
200 * @param array $articles_start_char
205 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
206 if ( count ( $articles ) > $cutoff ) {
207 return $this->columnList( $articles, $articles_start_char );
208 } elseif ( count($articles) > 0) {
209 // for short lists of articles in categories.
210 return $this->shortList( $articles, $articles_start_char );
216 * Format a list of articles chunked by letter in a three-column
217 * list, ordered vertically.
219 * @param array $articles
220 * @param array $articles_start_char
224 function columnList( $articles, $articles_start_char ) {
225 // divide list into three equal chunks
226 $chunk = (int) (count ( $articles ) / 3);
228 // get and display header
229 $r = '<table width="100%"><tr valign="top">';
231 $prev_start_char = 'none';
233 // loop through the chunks
234 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
236 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
241 // output all articles in category
242 for ($index = $startChunk ;
243 $index < $endChunk && $index < count($articles);
246 // check for change of starting letter or begining of chunk
247 if ( ($index == $startChunk) ||
248 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
252 $atColumnTop = false;
257 if ( $articles_start_char[$index] == $prev_start_char )
258 $cont_msg = wfMsg('listingcontinuesabbrev');
259 $r .= "<h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
260 $prev_start_char = $articles_start_char[$index];
263 $r .= "<li>{$articles[$index]}</li>";
265 if( !$atColumnTop ) {
272 $r .= '</tr></table>';
277 * Format a list of articles chunked by letter in a bullet list.
278 * @param array $articles
279 * @param array $articles_start_char
283 function shortList( $articles, $articles_start_char ) {
284 $r = '<h3>'.$articles_start_char[0]."</h3>\n";
285 $r .= '<ul><li>'.$articles[0].'</li>';
286 for ($index = 1; $index < count($articles); $index++
)
288 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
290 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
293 $r .= "<li>{$articles[$index]}</li>";
300 * @param Title $title
301 * @param string $first
302 * @param string $last
304 * @param array $query - additional query options to pass
308 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
309 global $wgUser, $wgLang;
310 $sk =& $wgUser->getSkin();
311 $limitText = $wgLang->formatNum( $limit );
313 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
315 $prevLink = $sk->makeLinkObj( $title, $prevLink,
316 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
318 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
320 $nextLink = $sk->makeLinkObj( $title, $nextLink,
321 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
324 return "($prevLink) ($nextLink)";