3 * Special handling for category description pages
4 * Modelled after ImagePage.php
8 if( !defined( 'MEDIAWIKI' ) )
13 class CategoryPage
extends Article
{
15 global $wgRequest, $wgUser;
17 $diff = $wgRequest->getVal( 'diff' );
18 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
20 if ( isset( $diff ) && $diffOnly )
21 return Article
::view();
23 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
25 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
26 $this->openShowCategory();
31 # If the article we've just shown is in the "Image" namespace,
32 # follow it with the history list and link list for the image
35 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
36 $this->closeShowCategory();
41 * This page should not be cached if 'from' or 'until' has been used
44 function isFileCacheable() {
47 return ( ! Article
::isFileCacheable()
48 ||
$wgRequest->getVal( 'from' )
49 ||
$wgRequest->getVal( 'until' )
53 function openShowCategory() {
57 function closeShowCategory() {
58 global $wgOut, $wgRequest;
59 $from = $wgRequest->getVal( 'from' );
60 $until = $wgRequest->getVal( 'until' );
62 $viewer = new CategoryViewer( $this->mTitle
, $from, $until );
63 $wgOut->addHTML( $viewer->getHTML() );
67 class CategoryViewer
{
68 var $title, $limit, $from, $until,
69 $articles, $articles_start_char,
70 $children, $children_start_char,
71 $showGallery, $gallery,
74 function __construct( $title, $from = '', $until = '' ) {
75 global $wgCategoryPagingLimit;
76 $this->title
= $title;
78 $this->until
= $until;
79 $this->limit
= $wgCategoryPagingLimit;
83 * Format the category data list.
85 * @param string $from -- return only sort keys from this item on
86 * @param string $until -- don't return keys after this point.
87 * @return string HTML output
91 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
92 wfProfileIn( __METHOD__
);
94 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
96 $this->clearCategoryState();
97 $this->doCategoryQuery();
98 $this->finaliseCategoryState();
100 $r = $this->getCategoryTop() .
101 $this->getSubcategorySection() .
102 $this->getPagesSection() .
103 $this->getImageSection() .
104 $this->getCategoryBottom();
106 // Give a proper message if category is empty
108 $r = wfMsgExt( 'category-empty', array( 'parse' ) );
111 wfProfileOut( __METHOD__
);
115 function clearCategoryState() {
116 $this->articles
= array();
117 $this->articles_start_char
= array();
118 $this->children
= array();
119 $this->children_start_char
= array();
120 if( $this->showGallery
) {
121 $this->gallery
= new ImageGallery();
122 $this->gallery
->setHideBadImages();
127 if ( !$this->skin
) {
129 $this->skin
= $wgUser->getSkin();
135 * Add a subcategory to the internal lists
137 function addSubcategory( $title, $sortkey, $pageLength ) {
139 // Subcategory; strip the 'Category' namespace from the link text.
140 $this->children
[] = $this->getSkin()->makeKnownLinkObj(
141 $title, $wgContLang->convertHtml( $title->getText() ) );
143 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
147 * Get the character to be used for sorting subcategories.
148 * If there's a link from Category:A to Category:B, the sortkey of the resulting
149 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
150 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
151 * else use sortkey...
153 function getSubcategorySortChar( $title, $sortkey ) {
156 if( $title->getPrefixedText() == $sortkey ) {
157 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
159 $firstChar = $wgContLang->firstChar( $sortkey );
162 return $wgContLang->convert( $firstChar );
166 * Add a page in the image namespace
168 function addImage( Title
$title, $sortkey, $pageLength, $isRedirect = false ) {
169 if ( $this->showGallery
) {
170 $image = new Image( $title );
172 $this->gallery
->insert( $image );
174 $this->gallery
->add( $image );
177 $this->addPage( $title, $sortkey, $pageLength, $isRedirect );
182 * Add a miscellaneous page
184 function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
186 $this->articles
[] = $isRedirect
187 ?
'<span class="redirect-in-category">' . $this->getSkin()->makeKnownLinkObj( $title ) . '</span>'
188 : $this->getSkin()->makeSizeLinkObj( $pageLength, $title );
189 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
192 function finaliseCategoryState() {
194 $this->children
= array_reverse( $this->children
);
195 $this->children_start_char
= array_reverse( $this->children_start_char
);
196 $this->articles
= array_reverse( $this->articles
);
197 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
201 function doCategoryQuery() {
202 $dbr = wfGetDB( DB_SLAVE
);
203 if( $this->from
!= '' ) {
204 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
206 } elseif( $this->until
!= '' ) {
207 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
210 $pageCondition = '1 = 1';
214 array( 'page', 'categorylinks' ),
215 array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey' ),
216 array( $pageCondition,
218 'cl_to' => $this->title
->getDBkey()),
219 #'page_is_redirect' => 0),
222 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
223 'USE INDEX' => 'cl_sortkey',
224 'LIMIT' => $this->limit +
1 ) );
227 $this->nextPage
= null;
228 while( $x = $dbr->fetchObject ( $res ) ) {
229 if( ++
$count > $this->limit
) {
230 // We've reached the one extra which shows that there are
231 // additional pages to be had. Stop here...
232 $this->nextPage
= $x->cl_sortkey
;
236 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
238 if( $title->getNamespace() == NS_CATEGORY
) {
239 $this->addSubcategory( $title, $x->cl_sortkey
, $x->page_len
);
240 } elseif( $this->showGallery
&& $title->getNamespace() == NS_IMAGE
) {
241 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
243 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
, $x->page_is_redirect
);
246 $dbr->freeResult( $res );
249 function getCategoryTop() {
251 if( $this->until
!= '' ) {
252 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
253 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
254 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
258 : "<br style=\"clear:both;\"/>\n" . $r;
261 function getSubcategorySection() {
262 # Don't show subcategories section if there are none.
264 $c = count( $this->children
);
266 # Showing subcategories
267 $r .= "<div id=\"mw-subcategories\">\n";
268 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
269 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), $c );
270 $r .= $this->formatList( $this->children
, $this->children_start_char
);
276 function getPagesSection() {
277 $ti = htmlspecialchars( $this->title
->getText() );
278 # Don't show articles section if there are none.
280 $c = count( $this->articles
);
282 $r = "<div id=\"mw-pages\">\n";
283 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
284 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), $c );
285 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
291 function getImageSection() {
292 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
293 return "<div id=\"mw-category-media\">\n" .
294 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title
->getText()) ) . "</h2>\n" .
295 wfMsgExt( 'category-media-count', array( 'parse' ), $this->gallery
->count() ) .
296 $this->gallery
->toHTML() . "\n</div>";
302 function getCategoryBottom() {
303 if( $this->until
!= '' ) {
304 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
305 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
306 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
313 * Format a list of articles chunked by letter, either as a
314 * bullet list or a columnar format, depending on the length.
316 * @param array $articles
317 * @param array $articles_start_char
322 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
323 if ( count ( $articles ) > $cutoff ) {
324 return $this->columnList( $articles, $articles_start_char );
325 } elseif ( count($articles) > 0) {
326 // for short lists of articles in categories.
327 return $this->shortList( $articles, $articles_start_char );
333 * Format a list of articles chunked by letter in a three-column
334 * list, ordered vertically.
336 * @param array $articles
337 * @param array $articles_start_char
341 function columnList( $articles, $articles_start_char ) {
342 // divide list into three equal chunks
343 $chunk = (int) (count ( $articles ) / 3);
345 // get and display header
346 $r = '<table width="100%"><tr valign="top">';
348 $prev_start_char = 'none';
350 // loop through the chunks
351 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
353 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
358 // output all articles in category
359 for ($index = $startChunk ;
360 $index < $endChunk && $index < count($articles);
363 // check for change of starting letter or begining of chunk
364 if ( ($index == $startChunk) ||
365 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
369 $atColumnTop = false;
374 if ( $articles_start_char[$index] == $prev_start_char )
375 $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
376 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
377 $prev_start_char = $articles_start_char[$index];
380 $r .= "<li>{$articles[$index]}</li>";
382 if( !$atColumnTop ) {
389 $r .= '</tr></table>';
394 * Format a list of articles chunked by letter in a bullet list.
395 * @param array $articles
396 * @param array $articles_start_char
400 function shortList( $articles, $articles_start_char ) {
401 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
402 $r .= '<ul><li>'.$articles[0].'</li>';
403 for ($index = 1; $index < count($articles); $index++
)
405 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
407 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
410 $r .= "<li>{$articles[$index]}</li>";
417 * @param Title $title
418 * @param string $first
419 * @param string $last
421 * @param array $query - additional query options to pass
425 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
427 $sk = $this->getSkin();
428 $limitText = $wgLang->formatNum( $limit );
430 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
432 $prevLink = $sk->makeLinkObj( $title, $prevLink,
433 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
435 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
437 $nextLink = $sk->makeLinkObj( $title, $nextLink,
438 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
441 return "($prevLink) ($nextLink)";