3 * Special handling for category description pages
4 * Modelled after ImagePage.php
9 if( !defined( 'MEDIAWIKI' ) )
12 if( $wgCategoryMagicGallery )
13 require_once('ImageGallery.php');
18 class CategoryPage
extends Article
{
21 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
22 $this->openShowCategory();
27 # If the article we've just shown is in the "Image" namespace,
28 # follow it with the history list and link list for the image
31 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
32 $this->closeShowCategory();
36 function openShowCategory() {
40 # generate a list of subcategories and pages for a category
41 # depending on wfMsg("usenewcategorypage") it either calls the new
42 # or the old code. The new code will not work properly for some
43 # languages due to sorting issues, so they might want to turn it
46 function closeShowCategory() {
47 global $wgOut, $wgRequest;
48 $pageConditions = array();
49 $from = $wgRequest->getVal( 'from' );
50 $until = $wgRequest->getVal( 'until' );
51 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
55 * Format the category data list.
57 * @param string $from -- return only sort keys from this item on
58 * @param string $until -- don't return keys after this point.
59 * @return string HTML output
62 function doCategoryMagic( $from = '', $until = '' ) {
63 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
64 $fname = 'CategoryPage::doCategoryMagic';
65 wfProfileIn( $fname );
68 $articles_start_char = array();
70 $children_start_char = array();
72 if( $wgCategoryMagicGallery ) {
73 $ig = new ImageGallery();
76 $dbr =& wfGetDB( DB_SLAVE
);
78 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
80 } elseif( $until != '' ) {
81 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
89 array( 'page', 'categorylinks' ),
90 array( 'page_title', 'page_namespace', 'cl_sortkey' ),
91 array( $pageCondition,
93 'cl_to' => $this->mTitle
->getDBKey(),
94 'page_is_redirect' => 0),
97 array( 'ORDER BY' => $flip ?
'cl_sortkey DESC' : 'cl_sortkey',
98 'LIMIT' => $limit +
1 ) );
100 $sk =& $wgUser->getSkin();
101 $r = "<br style=\"clear:both;\"/>\n";
104 while( $x = $dbr->fetchObject ( $res ) ) {
105 if( ++
$count > $limit ) {
106 // We've reached the one extra which shows that there are
107 // additional pages to be had. Stop here...
108 $nextPage = $x->cl_sortkey
;
112 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
114 if( $title->getNamespace() == NS_CATEGORY
) {
115 // Subcategory; strip the 'Category' namespace from the link text.
116 array_push( $children, $sk->makeKnownLinkObj( $title, $title->getText() ) );
118 // If there's a link from Category:A to Category:B, the sortkey of the resulting
119 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
120 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
121 // else use sortkey...
122 if( $title->getPrefixedText() == $x->cl_sortkey
) {
123 array_push( $children_start_char, $wgContLang->firstChar( $x->page_title
) );
125 array_push( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
127 } elseif( $wgCategoryMagicGallery && $title->getNamespace() == NS_IMAGE
) {
128 // Show thumbnails of categorized images, in a separate chunk
130 $ig->insert( Image
::newFromTitle( $title ) );
132 $ig->add( Image
::newFromTitle( $title ) );
135 // Page in this category
136 array_push( $articles, $sk->makeKnownLinkObj( $title ) ) ;
137 array_push( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
140 $dbr->freeResult( $res );
143 $children = array_reverse( $children );
144 $children_start_char = array_reverse( $children_start_char );
145 $articles = array_reverse( $articles );
146 $articles_start_char = array_reverse( $articles_start_char );
150 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
151 } elseif( $nextPage != '' ||
$from != '' ) {
152 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
155 # Don't show subcategories section if there are none.
156 if( count( $children ) > 0 ) {
157 # Showing subcategories
158 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
159 $r .= $this->formatCount( $children, 'subcategorycount' );
160 $r .= $this->formatList( $children, $children_start_char );
163 # Showing articles in this category
164 $ti = htmlspecialchars( $this->mTitle
->getText() );
165 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
166 $r .= $this->formatCount( $articles, 'categoryarticlecount' );
167 $r .= $this->formatList( $articles, $articles_start_char );
169 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
173 wfProfileOut( $fname );
178 * @param array $articles
179 * @param string $message
183 function formatCount( $articles, $message ) {
185 $numart = count( $articles );
187 # Slightly different message to avoid silly plural
190 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
193 * Format a list of articles chunked by letter, either as a
194 * bullet list or a columnar format, depending on the length.
196 * @param array $articles
197 * @param array $articles_start_char
202 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
203 if ( count ( $articles ) > $cutoff ) {
204 return $this->columnList( $articles, $articles_start_char );
205 } elseif ( count($articles) > 0) {
206 // for short lists of articles in categories.
207 return $this->shortList( $articles, $articles_start_char );
213 * Format a list of articles chunked by letter in a three-column
214 * list, ordered vertically.
216 * @param array $articles
217 * @param array $articles_start_char
221 function columnList( $articles, $articles_start_char ) {
222 // divide list into three equal chunks
223 $chunk = (int) (count ( $articles ) / 3);
225 // get and display header
226 $r = '<table width="100%"><tr valign="top">';
228 $prev_start_char = "";
230 // loop through the chunks
231 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
233 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
238 // output all articles in category
239 for ($index = $startChunk ;
240 $index < $endChunk && $index < count($articles);
243 // check for change of starting letter or begging of chunk
244 if ( ($index == $startChunk) ||
245 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
249 if($articles_start_char[$index] == $prev_start_char)
250 $cont_msg = wfMsg('listingcontinuesabbrev');
251 $r .= "</ul><h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
252 $prev_start_char = $articles_start_char[$index];
255 $r .= "<li>{$articles[$index]}</li>";
261 $r .= '</tr></table>';
266 * Format a list of articles chunked by letter in a bullet list.
267 * @param array $articles
268 * @param array $articles_start_char
272 function shortList( $articles, $articles_start_char ) {
273 $r = '<h3>'.$articles_start_char[0]."</h3>\n";
274 $r .= '<ul><li>'.$articles[0].'</li>';
275 for ($index = 1; $index < count($articles); $index++
)
277 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
279 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
282 $r .= "<li>{$articles[$index]}</li>";
289 * @param Title $title
290 * @param string $first
291 * @param string $last
293 * @param array $query - additional query options to pass
297 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
298 global $wgUser, $wgLang;
299 $sk =& $wgUser->getSkin();
300 $limitText = $wgLang->formatNum( $limit );
302 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
304 $prevLink = $sk->makeLinkObj( $title, $prevLink,
305 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
307 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
309 $nextLink = $sk->makeLinkObj( $title, $nextLink,
310 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
313 return "($prevLink) ($nextLink)";