3 * Special handling for category description pages
4 * Modelled after ImagePage.php
9 if( !defined( 'MEDIAWIKI' ) )
12 global $wgCategoryMagicGallery;
17 class CategoryPage
extends Article
{
20 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
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 function closeShowCategory() {
42 global $wgOut, $wgRequest;
43 $from = $wgRequest->getVal( 'from' );
44 $until = $wgRequest->getVal( 'until' );
45 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
49 * Format the category data list.
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
56 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();
65 if( $wgCategoryMagicGallery ) {
66 $ig = new ImageGallery();
69 $dbr =& wfGetDB( DB_SLAVE
);
71 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
73 } elseif( $until != '' ) {
74 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
77 $pageCondition = '1 = 1';
80 $limit = $wgCategoryPagingLimit;
82 array( 'page', 'categorylinks' ),
83 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
84 array( $pageCondition,
86 'cl_to' => $this->mTitle
->getDBKey()),
87 #'page_is_redirect' => 0),
90 array( 'ORDER BY' => $flip ?
'cl_sortkey DESC' : 'cl_sortkey',
91 'LIMIT' => $limit +
1 ) );
93 $sk =& $wgUser->getSkin();
94 $r = "<br style=\"clear:both;\"/>\n";
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
;
105 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
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() ) ) );
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...
116 if( $title->getPrefixedText() == $x->cl_sortkey
) {
117 $sortkey=$wgContLang->firstChar( $x->page_title
);
119 $sortkey=$wgContLang->firstChar( $x->cl_sortkey
);
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
125 $ig->insert( Image
::newFromTitle( $title ) );
127 $ig->add( Image
::newFromTitle( $title ) );
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
) ) );
135 $dbr->freeResult( $res );
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 );
145 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
146 } elseif( $nextPage != '' ||
$from != '' ) {
147 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
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 );
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 );
164 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
169 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
170 } elseif( $nextPage != '' ||
$from != '' ) {
171 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
174 wfProfileOut( $fname );
179 * Format a list of articles chunked by letter, either as a
180 * bullet list or a columnar format, depending on the length.
182 * @param array $articles
183 * @param array $articles_start_char
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 );
199 * Format a list of articles chunked by letter in a three-column
200 * list, ordered vertically.
202 * @param array $articles
203 * @param array $articles_start_char
207 function columnList( $articles, $articles_start_char ) {
208 // divide list into three equal chunks
209 $chunk = (int) (count ( $articles ) / 3);
211 // get and display header
212 $r = '<table width="100%"><tr valign="top">';
214 $prev_start_char = 'none';
216 // loop through the chunks
217 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
219 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
224 // output all articles in category
225 for ($index = $startChunk ;
226 $index < $endChunk && $index < count($articles);
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]) )
235 $atColumnTop = false;
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];
246 $r .= "<li>{$articles[$index]}</li>";
248 if( !$atColumnTop ) {
255 $r .= '</tr></table>';
260 * Format a list of articles chunked by letter in a bullet list.
261 * @param array $articles
262 * @param array $articles_start_char
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++
)
271 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
273 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
276 $r .= "<li>{$articles[$index]}</li>";
283 * @param Title $title
284 * @param string $first
285 * @param string $last
287 * @param array $query - additional query options to pass
291 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
292 global $wgUser, $wgLang;
293 $sk =& $wgUser->getSkin();
294 $limitText = $wgLang->formatNum( $limit );
296 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
298 $prevLink = $sk->makeLinkObj( $title, $prevLink,
299 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
301 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
303 $nextLink = $sk->makeLinkObj( $title, $nextLink,
304 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
307 return "($prevLink) ($nextLink)";