3 * Special handling for category description pages
4 * Modelled after ImagePage.php
9 if( !defined( 'MEDIAWIKI' ) )
15 class CategoryPage
extends Article
{
18 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
20 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
21 $this->openShowCategory();
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
30 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
31 $this->closeShowCategory();
35 function openShowCategory() {
39 function closeShowCategory() {
40 global $wgOut, $wgRequest;
41 $from = $wgRequest->getVal( 'from' );
42 $until = $wgRequest->getVal( 'until' );
44 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
48 * Format the category data list.
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
55 function doCategoryMagic( $from = '', $until = '' ) {
57 global $wgContLang,$wgUser, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
58 $fname = 'CategoryPage::doCategoryMagic';
59 wfProfileIn( $fname );
62 $articles_start_char = array();
64 $children_start_char = array();
66 $showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
68 $ig = new ImageGallery();
71 $dbr =& wfGetDB( DB_SLAVE
);
73 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
75 } elseif( $until != '' ) {
76 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
79 $pageCondition = '1 = 1';
82 $limit = $wgCategoryPagingLimit;
84 array( 'page', 'categorylinks' ),
85 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
86 array( $pageCondition,
88 'cl_to' => $this->mTitle
->getDBKey()),
89 #'page_is_redirect' => 0),
92 array( 'ORDER BY' => $flip ?
'cl_sortkey DESC' : 'cl_sortkey',
93 'LIMIT' => $limit +
1 ) );
95 $sk =& $wgUser->getSkin();
96 $r = "<br style=\"clear:both;\"/>\n";
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
;
107 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
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() ) ) );
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...
118 if( $title->getPrefixedText() == $x->cl_sortkey
) {
119 $sortkey=$wgContLang->firstChar( $x->page_title
);
121 $sortkey=$wgContLang->firstChar( $x->cl_sortkey
);
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
127 $ig->insert( Image
::newFromTitle( $title ) );
129 $ig->add( Image
::newFromTitle( $title ) );
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
) ) );
137 $dbr->freeResult( $res );
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 );
147 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
148 } elseif( $nextPage != '' ||
$from != '' ) {
149 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
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 );
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 );
166 if( $showGallery && ! $ig->isEmpty() ) {
171 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
172 } elseif( $nextPage != '' ||
$from != '' ) {
173 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
176 wfProfileOut( $fname );
181 * Format a list of articles chunked by letter, either as a
182 * bullet list or a columnar format, depending on the length.
184 * @param array $articles
185 * @param array $articles_start_char
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 );
201 * Format a list of articles chunked by letter in a three-column
202 * list, ordered vertically.
204 * @param array $articles
205 * @param array $articles_start_char
209 function columnList( $articles, $articles_start_char ) {
210 // divide list into three equal chunks
211 $chunk = (int) (count ( $articles ) / 3);
213 // get and display header
214 $r = '<table width="100%"><tr valign="top">';
216 $prev_start_char = 'none';
218 // loop through the chunks
219 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
221 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
226 // output all articles in category
227 for ($index = $startChunk ;
228 $index < $endChunk && $index < count($articles);
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]) )
237 $atColumnTop = false;
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];
248 $r .= "<li>{$articles[$index]}</li>";
250 if( !$atColumnTop ) {
257 $r .= '</tr></table>';
262 * Format a list of articles chunked by letter in a bullet list.
263 * @param array $articles
264 * @param array $articles_start_char
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++
)
273 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
275 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
278 $r .= "<li>{$articles[$index]}</li>";
285 * @param Title $title
286 * @param string $first
287 * @param string $last
289 * @param array $query - additional query options to pass
293 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
294 global $wgUser, $wgLang;
295 $sk =& $wgUser->getSkin();
296 $limitText = $wgLang->formatNum( $limit );
298 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
300 $prevLink = $sk->makeLinkObj( $title, $prevLink,
301 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
303 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
305 $nextLink = $sk->makeLinkObj( $title, $nextLink,
306 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
309 return "($prevLink) ($nextLink)";