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();
72 $dbr =& wfGetDB( DB_SLAVE
);
74 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
76 } elseif( $until != '' ) {
77 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
80 $pageCondition = '1 = 1';
83 $limit = $wgCategoryPagingLimit;
85 array( 'page', 'categorylinks' ),
86 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
87 array( $pageCondition,
89 'cl_to' => $this->mTitle
->getDBKey()),
90 #'page_is_redirect' => 0),
93 array( 'ORDER BY' => $flip ?
'cl_sortkey DESC' : 'cl_sortkey',
94 'LIMIT' => $limit +
1 ) );
96 $sk =& $wgUser->getSkin();
97 $r = "<br style=\"clear:both;\"/>\n";
100 while( $x = $dbr->fetchObject ( $res ) ) {
101 if( ++
$count > $limit ) {
102 // We've reached the one extra which shows that there are
103 // additional pages to be had. Stop here...
104 $nextPage = $x->cl_sortkey
;
108 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
110 if( $title->getNamespace() == NS_CATEGORY
) {
111 // Subcategory; strip the 'Category' namespace from the link text.
112 array_push( $children, $sk->makeKnownLinkObj( $title, $wgContLang->convertHtml( $title->getText() ) ) );
114 // If there's a link from Category:A to Category:B, the sortkey of the resulting
115 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
116 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
117 // else use sortkey...
119 if( $title->getPrefixedText() == $x->cl_sortkey
) {
120 $sortkey=$wgContLang->firstChar( $x->page_title
);
122 $sortkey=$wgContLang->firstChar( $x->cl_sortkey
);
124 array_push( $children_start_char, $wgContLang->convert( $sortkey ) ) ;
125 } elseif( $showGallery && $title->getNamespace() == NS_IMAGE
) {
126 // Show thumbnails of categorized images, in a separate chunk
127 $anImage = new Image($title);
129 $ig->insert( $anImage );
131 $ig->add( $anImage );
135 // Page in this category
136 array_push( $articles, $sk->makeSizeLinkObj( $x->page_len
, $title, $wgContLang->convert( $title->getPrefixedText() ) ) ) ;
137 array_push( $articles_start_char, $wgContLang->convert( $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 .= wfMsgExt( 'subcategorycount', array( 'parse' ), count( $children) );
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 .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), count( $articles) );
167 $r .= $this->formatList( $articles, $articles_start_char );
169 if( $showGallery && ! $ig->isEmpty() ) {
174 $r .= $this->pagingLinks( $this->mTitle
, $nextPage, $until, $limit );
175 } elseif( $nextPage != '' ||
$from != '' ) {
176 $r .= $this->pagingLinks( $this->mTitle
, $from, $nextPage, $limit );
179 wfProfileOut( $fname );
184 * Format a list of articles chunked by letter, either as a
185 * bullet list or a columnar format, depending on the length.
187 * @param array $articles
188 * @param array $articles_start_char
193 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
194 if ( count ( $articles ) > $cutoff ) {
195 return $this->columnList( $articles, $articles_start_char );
196 } elseif ( count($articles) > 0) {
197 // for short lists of articles in categories.
198 return $this->shortList( $articles, $articles_start_char );
204 * Format a list of articles chunked by letter in a three-column
205 * list, ordered vertically.
207 * @param array $articles
208 * @param array $articles_start_char
212 function columnList( $articles, $articles_start_char ) {
213 // divide list into three equal chunks
214 $chunk = (int) (count ( $articles ) / 3);
216 // get and display header
217 $r = '<table width="100%"><tr valign="top">';
219 $prev_start_char = 'none';
221 // loop through the chunks
222 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
224 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
229 // output all articles in category
230 for ($index = $startChunk ;
231 $index < $endChunk && $index < count($articles);
234 // check for change of starting letter or begining of chunk
235 if ( ($index == $startChunk) ||
236 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
240 $atColumnTop = false;
245 if ( $articles_start_char[$index] == $prev_start_char )
246 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
247 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
248 $prev_start_char = $articles_start_char[$index];
251 $r .= "<li>{$articles[$index]}</li>";
253 if( !$atColumnTop ) {
260 $r .= '</tr></table>';
265 * Format a list of articles chunked by letter in a bullet list.
266 * @param array $articles
267 * @param array $articles_start_char
271 function shortList( $articles, $articles_start_char ) {
272 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
273 $r .= '<ul><li>'.$articles[0].'</li>';
274 for ($index = 1; $index < count($articles); $index++
)
276 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
278 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
281 $r .= "<li>{$articles[$index]}</li>";
288 * @param Title $title
289 * @param string $first
290 * @param string $last
292 * @param array $query - additional query options to pass
296 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
297 global $wgUser, $wgLang;
298 $sk =& $wgUser->getSkin();
299 $limitText = $wgLang->formatNum( $limit );
301 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
303 $prevLink = $sk->makeLinkObj( $title, $prevLink,
304 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
306 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
308 $nextLink = $sk->makeLinkObj( $title, $nextLink,
309 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
312 return "($prevLink) ($nextLink)";