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 ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
23 $this->openShowCategory();
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
32 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
33 $this->closeShowCategory();
37 function openShowCategory() {
41 # generate a list of subcategories and pages for a category
42 # depending on wfMsg("usenewcategorypage") it either calls the new
43 # or the old code. The new code will not work properly for some
44 # languages due to sorting issues, so they might want to turn it
47 function closeShowCategory() {
48 global $wgOut, $wgRequest;
49 $pageConditions = array();
50 $from = $wgRequest->getVal( 'from' );
51 $until = $wgRequest->getVal( 'until' );
52 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
56 * Format the category data list.
58 * @param string $from -- return only sort keys from this item on
59 * @param string $until -- don't return keys after this point.
60 * @return string HTML output
63 function doCategoryMagic( $from = '', $until = '' ) {
64 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
65 $fname = 'CategoryPage::doCategoryMagic';
66 wfProfileIn( $fname );
69 $articles_start_char = array();
71 $children_start_char = array();
73 if( $wgCategoryMagicGallery ) {
74 $ig = new ImageGallery();
77 $dbr =& wfGetDB( DB_SLAVE
);
79 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
81 } elseif( $until != '' ) {
82 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
90 array( 'page', 'categorylinks' ),
91 array( 'page_title', 'page_namespace', 'cl_sortkey' ),
92 array( $pageCondition,
94 'cl_to' => $this->mTitle
->getDBKey()),
95 #'page_is_redirect' => 0),
98 array( 'ORDER BY' => $flip ?
'cl_sortkey DESC' : 'cl_sortkey',
99 'LIMIT' => $limit +
1 ) );
101 $sk =& $wgUser->getSkin();
102 $r = "<br style=\"clear:both;\"/>\n";
105 while( $x = $dbr->fetchObject ( $res ) ) {
106 if( ++
$count > $limit ) {
107 // We've reached the one extra which shows that there are
108 // additional pages to be had. Stop here...
109 $nextPage = $x->cl_sortkey
;
113 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
115 if( $title->getNamespace() == NS_CATEGORY
) {
116 // Subcategory; strip the 'Category' namespace from the link text.
117 array_push( $children, $sk->makeKnownLinkObj( $title, $title->getText() ) );
119 // If there's a link from Category:A to Category:B, the sortkey of the resulting
120 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
121 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
122 // else use sortkey...
123 if( $title->getPrefixedText() == $x->cl_sortkey
) {
124 array_push( $children_start_char, $wgContLang->firstChar( $x->page_title
) );
126 array_push( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
128 } elseif( $wgCategoryMagicGallery && $title->getNamespace() == NS_IMAGE
) {
129 // Show thumbnails of categorized images, in a separate chunk
131 $ig->insert( Image
::newFromTitle( $title ) );
133 $ig->add( Image
::newFromTitle( $title ) );
136 // Page in this category
137 array_push( $articles, $sk->makeKnownLinkObj( $title ) ) ;
138 array_push( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey
) ) ;
141 $dbr->freeResult( $res );
144 $children = array_reverse( $children );
145 $children_start_char = array_reverse( $children_start_char );
146 $articles = array_reverse( $articles );
147 $articles_start_char = array_reverse( $articles_start_char );
151 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
152 } elseif( $nextPage != '' ||
$from != '' ) {
153 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
156 # Don't show subcategories section if there are none.
157 if( count( $children ) > 0 ) {
158 # Showing subcategories
159 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
160 $r .= $this->formatCount( $children, 'subcategorycount' );
161 $r .= $this->formatList( $children, $children_start_char );
164 # Showing articles in this category
165 $ti = htmlspecialchars( $this->mTitle
->getText() );
166 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
167 $r .= $this->formatCount( $articles, 'categoryarticlecount' );
168 $r .= $this->formatList( $articles, $articles_start_char );
170 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
174 wfProfileOut( $fname );
179 * @param array $articles
180 * @param string $message
184 function formatCount( $articles, $message ) {
186 $numart = count( $articles );
188 # Slightly different message to avoid silly plural
191 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
194 * Format a list of articles chunked by letter, either as a
195 * bullet list or a columnar format, depending on the length.
197 * @param array $articles
198 * @param array $articles_start_char
203 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
204 if ( count ( $articles ) > $cutoff ) {
205 return $this->columnList( $articles, $articles_start_char );
206 } elseif ( count($articles) > 0) {
207 // for short lists of articles in categories.
208 return $this->shortList( $articles, $articles_start_char );
214 * Format a list of articles chunked by letter in a three-column
215 * list, ordered vertically.
217 * @param array $articles
218 * @param array $articles_start_char
222 function columnList( $articles, $articles_start_char ) {
223 // divide list into three equal chunks
224 $chunk = (int) (count ( $articles ) / 3);
226 // get and display header
227 $r = '<table width="100%"><tr valign="top">';
229 $prev_start_char = 'none';
231 // loop through the chunks
232 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
234 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
239 // output all articles in category
240 for ($index = $startChunk ;
241 $index < $endChunk && $index < count($articles);
244 // check for change of starting letter or begining of chunk
245 if ( ($index == $startChunk) ||
246 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
250 $atColumnTop = false;
255 if ( $articles_start_char[$index] == $prev_start_char )
256 $cont_msg = wfMsg('listingcontinuesabbrev');
257 $r .= "<h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
258 $prev_start_char = $articles_start_char[$index];
261 $r .= "<li>{$articles[$index]}</li>";
263 if( !$atColumnTop ) {
270 $r .= '</tr></table>';
275 * Format a list of articles chunked by letter in a bullet list.
276 * @param array $articles
277 * @param array $articles_start_char
281 function shortList( $articles, $articles_start_char ) {
282 $r = '<h3>'.$articles_start_char[0]."</h3>\n";
283 $r .= '<ul><li>'.$articles[0].'</li>';
284 for ($index = 1; $index < count($articles); $index++
)
286 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
288 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
291 $r .= "<li>{$articles[$index]}</li>";
298 * @param Title $title
299 * @param string $first
300 * @param string $last
302 * @param array $query - additional query options to pass
306 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
307 global $wgUser, $wgLang;
308 $sk =& $wgUser->getSkin();
309 $limitText = $wgLang->formatNum( $limit );
311 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
313 $prevLink = $sk->makeLinkObj( $title, $prevLink,
314 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
316 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
318 $nextLink = $sk->makeLinkObj( $title, $nextLink,
319 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
322 return "($prevLink) ($nextLink)";