3 * Special handling for category description pages
4 * Modelled after ImagePage.php
9 if( !defined( 'MEDIAWIKI' ) )
15 class CategoryPage
extends Article
{
17 global $wgRequest, $wgUser;
19 $diff = $wgRequest->getVal( 'diff' );
20 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
22 if ( isset( $diff ) && $diffOnly )
23 return Article
::view();
25 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
27 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
28 $this->openShowCategory();
33 # If the article we've just shown is in the "Image" namespace,
34 # follow it with the history list and link list for the image
37 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
38 $this->closeShowCategory();
42 function openShowCategory() {
46 function closeShowCategory() {
47 global $wgOut, $wgRequest;
48 $from = $wgRequest->getVal( 'from' );
49 $until = $wgRequest->getVal( 'until' );
51 $viewer = new CategoryViewer( $this->mTitle
, $from, $until );
52 $wgOut->addHTML( $viewer->getHTML() );
56 class CategoryViewer
{
57 var $title, $limit, $from, $until,
58 $articles, $articles_start_char,
59 $children, $children_start_char,
60 $showGallery, $gallery,
63 function __construct( $title, $from = '', $until = '' ) {
64 global $wgCategoryPagingLimit;
65 $this->title
= $title;
67 $this->until
= $until;
68 $this->limit
= $wgCategoryPagingLimit;
72 * Format the category data list.
74 * @param string $from -- return only sort keys from this item on
75 * @param string $until -- don't return keys after this point.
76 * @return string HTML output
80 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
81 wfProfileIn( __METHOD__
);
83 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
85 $this->clearCategoryState();
86 $this->doCategoryQuery();
87 $this->finaliseCategoryState();
89 $r = $this->getCategoryTop() .
90 $this->getSubcategorySection() .
91 $this->getPagesSection() .
92 $this->getImageSection() .
93 $this->getCategoryBottom();
95 wfProfileOut( __METHOD__
);
99 function clearCategoryState() {
100 $this->articles
= array();
101 $this->articles_start_char
= array();
102 $this->children
= array();
103 $this->children_start_char
= array();
104 if( $this->showGallery
) {
105 $this->gallery
= new ImageGallery();
106 $this->gallery
->setParsing();
111 if ( !$this->skin
) {
113 $this->skin
= $wgUser->getSkin();
119 * Add a subcategory to the internal lists
121 function addSubcategory( $title, $sortkey, $pageLength ) {
123 // Subcategory; strip the 'Category' namespace from the link text.
124 $this->children
[] = $this->getSkin()->makeKnownLinkObj(
125 $title, $wgContLang->convertHtml( $title->getText() ) );
127 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
131 * Get the character to be used for sorting subcategories.
132 * If there's a link from Category:A to Category:B, the sortkey of the resulting
133 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
134 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
135 * else use sortkey...
137 function getSubcategorySortChar( $title, $sortkey ) {
140 if( $title->getPrefixedText() == $sortkey ) {
141 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
143 $firstChar = $wgContLang->firstChar( $sortkey );
146 return $wgContLang->convert( $firstChar );
150 * Add a page in the image namespace
152 function addImage( $title, $sortkey, $pageLength ) {
153 if ( $this->showGallery
) {
154 $image = new Image( $title );
156 $this->gallery
->insert( $image );
158 $this->gallery
->add( $image );
161 $this->addPage( $title, $sortkey, $pageLength );
166 * Add a miscellaneous page
168 function addPage( $title, $sortkey, $pageLength ) {
170 $this->articles
[] = $this->getSkin()->makeSizeLinkObj(
171 $pageLength, $title, $wgContLang->convert( $title->getPrefixedText() )
173 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
176 function finaliseCategoryState() {
178 $this->children
= array_reverse( $this->children
);
179 $this->children_start_char
= array_reverse( $this->children_start_char
);
180 $this->articles
= array_reverse( $this->articles
);
181 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
185 function doCategoryQuery() {
186 $dbr =& wfGetDB( DB_SLAVE
);
187 if( $this->from
!= '' ) {
188 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
190 } elseif( $this->until
!= '' ) {
191 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
194 $pageCondition = '1 = 1';
198 array( 'page', 'categorylinks' ),
199 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
200 array( $pageCondition,
202 'cl_to' => $this->title
->getDBKey()),
203 #'page_is_redirect' => 0),
206 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
207 'LIMIT' => $this->limit +
1 ) );
210 $this->nextPage
= null;
211 while( $x = $dbr->fetchObject ( $res ) ) {
212 if( ++
$count > $this->limit
) {
213 // We've reached the one extra which shows that there are
214 // additional pages to be had. Stop here...
215 $this->nextPage
= $x->cl_sortkey
;
219 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
221 if( $title->getNamespace() == NS_CATEGORY
) {
222 $this->addSubcategory( $title, $x->cl_sortkey
, $x->page_len
);
223 } elseif( $title->getNamespace() == NS_IMAGE
) {
224 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
);
226 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
);
229 $dbr->freeResult( $res );
232 function getCategoryTop() {
233 $r = "<br style=\"clear:both;\"/>\n";
234 if( $this->until
!= '' ) {
235 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
236 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
237 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
242 function getSubcategorySection() {
243 # Don't show subcategories section if there are none.
245 if( count( $this->children
) > 0 ) {
246 # Showing subcategories
247 $r .= "<div id=\"mw-subcategories\">\n";
248 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
249 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), count( $this->children
) );
250 $r .= $this->formatList( $this->children
, $this->children_start_char
);
256 function getPagesSection() {
257 $ti = htmlspecialchars( $this->title
->getText() );
258 $r = "<div id=\"mw-pages\">\n";
259 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
260 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), count( $this->articles
) );
261 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
266 function getImageSection() {
267 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
268 return "<div id=\"mw-category-media\">\n" .
269 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title
->getText()) ) . "</h2>\n" .
270 wfMsgExt( 'category-media-count', array( 'parse' ), $this->gallery
->count() ) .
271 $this->gallery
->toHTML() . "\n</div>";
277 function getCategoryBottom() {
278 if( $this->until
!= '' ) {
279 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
280 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
281 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
288 * Format a list of articles chunked by letter, either as a
289 * bullet list or a columnar format, depending on the length.
291 * @param array $articles
292 * @param array $articles_start_char
297 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
298 if ( count ( $articles ) > $cutoff ) {
299 return $this->columnList( $articles, $articles_start_char );
300 } elseif ( count($articles) > 0) {
301 // for short lists of articles in categories.
302 return $this->shortList( $articles, $articles_start_char );
308 * Format a list of articles chunked by letter in a three-column
309 * list, ordered vertically.
311 * @param array $articles
312 * @param array $articles_start_char
316 function columnList( $articles, $articles_start_char ) {
317 // divide list into three equal chunks
318 $chunk = (int) (count ( $articles ) / 3);
320 // get and display header
321 $r = '<table width="100%"><tr valign="top">';
323 $prev_start_char = 'none';
325 // loop through the chunks
326 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
328 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
333 // output all articles in category
334 for ($index = $startChunk ;
335 $index < $endChunk && $index < count($articles);
338 // check for change of starting letter or begining of chunk
339 if ( ($index == $startChunk) ||
340 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
344 $atColumnTop = false;
349 if ( $articles_start_char[$index] == $prev_start_char )
350 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
351 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
352 $prev_start_char = $articles_start_char[$index];
355 $r .= "<li>{$articles[$index]}</li>";
357 if( !$atColumnTop ) {
364 $r .= '</tr></table>';
369 * Format a list of articles chunked by letter in a bullet list.
370 * @param array $articles
371 * @param array $articles_start_char
375 function shortList( $articles, $articles_start_char ) {
376 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
377 $r .= '<ul><li>'.$articles[0].'</li>';
378 for ($index = 1; $index < count($articles); $index++
)
380 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
382 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
385 $r .= "<li>{$articles[$index]}</li>";
392 * @param Title $title
393 * @param string $first
394 * @param string $last
396 * @param array $query - additional query options to pass
400 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
401 global $wgUser, $wgLang;
402 $sk =& $this->getSkin();
403 $limitText = $wgLang->formatNum( $limit );
405 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
407 $prevLink = $sk->makeLinkObj( $title, $prevLink,
408 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
410 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
412 $nextLink = $sk->makeLinkObj( $title, $nextLink,
413 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
416 return "($prevLink) ($nextLink)";