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();
40 function openShowCategory() {
44 function closeShowCategory() {
45 global $wgOut, $wgRequest;
46 $from = $wgRequest->getVal( 'from' );
47 $until = $wgRequest->getVal( 'until' );
49 $viewer = new CategoryViewer( $this->mTitle
, $from, $until );
50 $wgOut->addHTML( $viewer->getHTML() );
54 class CategoryViewer
{
55 var $title, $limit, $from, $until,
56 $articles, $articles_start_char,
57 $children, $children_start_char,
58 $showGallery, $gallery,
61 function __construct( $title, $from = '', $until = '' ) {
62 global $wgCategoryPagingLimit;
63 $this->title
= $title;
65 $this->until
= $until;
66 $this->limit
= $wgCategoryPagingLimit;
70 * Format the category data list.
72 * @param string $from -- return only sort keys from this item on
73 * @param string $until -- don't return keys after this point.
74 * @return string HTML output
78 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
79 wfProfileIn( __METHOD__
);
81 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
83 $this->clearCategoryState();
84 $this->doCategoryQuery();
85 $this->finaliseCategoryState();
87 $r = $this->getCategoryTop() .
88 $this->getSubcategorySection() .
89 $this->getPagesSection() .
90 $this->getImageSection() .
91 $this->getCategoryBottom();
93 wfProfileOut( __METHOD__
);
97 function clearCategoryState() {
98 $this->articles
= array();
99 $this->articles_start_char
= array();
100 $this->children
= array();
101 $this->children_start_char
= array();
102 if( $this->showGallery
) {
103 $this->gallery
= new ImageGallery();
104 $this->gallery
->setParsing();
109 if ( !$this->skin
) {
111 $this->skin
= $wgUser->getSkin();
117 * Add a subcategory to the internal lists
119 function addSubcategory( $title, $sortkey, $pageLength ) {
121 // Subcategory; strip the 'Category' namespace from the link text.
122 $this->children
[] = $this->getSkin()->makeKnownLinkObj(
123 $title, $wgContLang->convertHtml( $title->getText() ) );
125 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
129 * Get the character to be used for sorting subcategories.
130 * If there's a link from Category:A to Category:B, the sortkey of the resulting
131 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
132 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
133 * else use sortkey...
135 function getSubcategorySortChar( $title, $sortkey ) {
138 if( $title->getPrefixedText() == $sortkey ) {
139 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
141 $firstChar = $wgContLang->firstChar( $sortkey );
144 return $wgContLang->convert( $firstChar );
148 * Add a page in the image namespace
150 function addImage( $title, $sortkey, $pageLength ) {
151 if ( $this->showGallery
) {
152 $image = new Image( $title );
154 $this->gallery
->insert( $image );
156 $this->gallery
->add( $image );
159 $this->addPage( $title, $sortkey, $pageLength );
164 * Add a miscellaneous page
166 function addPage( $title, $sortkey, $pageLength ) {
168 $this->articles
[] = $this->getSkin()->makeSizeLinkObj(
169 $pageLength, $title, $wgContLang->convert( $title->getPrefixedText() )
171 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
174 function finaliseCategoryState() {
176 $this->children
= array_reverse( $this->children
);
177 $this->children_start_char
= array_reverse( $this->children_start_char
);
178 $this->articles
= array_reverse( $this->articles
);
179 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
183 function doCategoryQuery() {
184 $dbr = wfGetDB( DB_SLAVE
);
185 if( $this->from
!= '' ) {
186 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
188 } elseif( $this->until
!= '' ) {
189 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
192 $pageCondition = '1 = 1';
196 array( 'page', 'categorylinks' ),
197 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
198 array( $pageCondition,
200 'cl_to' => $this->title
->getDBKey()),
201 #'page_is_redirect' => 0),
204 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
205 'LIMIT' => $this->limit +
1 ) );
208 $this->nextPage
= null;
209 while( $x = $dbr->fetchObject ( $res ) ) {
210 if( ++
$count > $this->limit
) {
211 // We've reached the one extra which shows that there are
212 // additional pages to be had. Stop here...
213 $this->nextPage
= $x->cl_sortkey
;
217 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
219 if( $title->getNamespace() == NS_CATEGORY
) {
220 $this->addSubcategory( $title, $x->cl_sortkey
, $x->page_len
);
221 } elseif( $title->getNamespace() == NS_IMAGE
) {
222 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
);
224 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
);
227 $dbr->freeResult( $res );
230 function getCategoryTop() {
231 $r = "<br style=\"clear:both;\"/>\n";
232 if( $this->until
!= '' ) {
233 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
234 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
235 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
240 function getSubcategorySection() {
241 # Don't show subcategories section if there are none.
243 $c = count( $this->children
);
245 # Showing subcategories
246 $r .= "<div id=\"mw-subcategories\">\n";
247 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
248 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), $c );
249 $r .= $this->formatList( $this->children
, $this->children_start_char
);
255 function getPagesSection() {
256 $ti = htmlspecialchars( $this->title
->getText() );
257 # Don't show articles section if there are none.
259 $c = count( $this->articles
);
261 $r = "<div id=\"mw-pages\">\n";
262 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
263 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), $c );
264 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
270 function getImageSection() {
271 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
272 return "<div id=\"mw-category-media\">\n" .
273 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title
->getText()) ) . "</h2>\n" .
274 wfMsgExt( 'category-media-count', array( 'parse' ), $this->gallery
->count() ) .
275 $this->gallery
->toHTML() . "\n</div>";
281 function getCategoryBottom() {
282 if( $this->until
!= '' ) {
283 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
284 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
285 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
292 * Format a list of articles chunked by letter, either as a
293 * bullet list or a columnar format, depending on the length.
295 * @param array $articles
296 * @param array $articles_start_char
301 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
302 if ( count ( $articles ) > $cutoff ) {
303 return $this->columnList( $articles, $articles_start_char );
304 } elseif ( count($articles) > 0) {
305 // for short lists of articles in categories.
306 return $this->shortList( $articles, $articles_start_char );
312 * Format a list of articles chunked by letter in a three-column
313 * list, ordered vertically.
315 * @param array $articles
316 * @param array $articles_start_char
320 function columnList( $articles, $articles_start_char ) {
321 // divide list into three equal chunks
322 $chunk = (int) (count ( $articles ) / 3);
324 // get and display header
325 $r = '<table width="100%"><tr valign="top">';
327 $prev_start_char = 'none';
329 // loop through the chunks
330 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
332 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
337 // output all articles in category
338 for ($index = $startChunk ;
339 $index < $endChunk && $index < count($articles);
342 // check for change of starting letter or begining of chunk
343 if ( ($index == $startChunk) ||
344 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
348 $atColumnTop = false;
353 if ( $articles_start_char[$index] == $prev_start_char )
354 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
355 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
356 $prev_start_char = $articles_start_char[$index];
359 $r .= "<li>{$articles[$index]}</li>";
361 if( !$atColumnTop ) {
368 $r .= '</tr></table>';
373 * Format a list of articles chunked by letter in a bullet list.
374 * @param array $articles
375 * @param array $articles_start_char
379 function shortList( $articles, $articles_start_char ) {
380 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
381 $r .= '<ul><li>'.$articles[0].'</li>';
382 for ($index = 1; $index < count($articles); $index++
)
384 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
386 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
389 $r .= "<li>{$articles[$index]}</li>";
396 * @param Title $title
397 * @param string $first
398 * @param string $last
400 * @param array $query - additional query options to pass
404 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
405 global $wgUser, $wgLang;
406 $sk = $this->getSkin();
407 $limitText = $wgLang->formatNum( $limit );
409 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
411 $prevLink = $sk->makeLinkObj( $title, $prevLink,
412 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
414 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
416 $nextLink = $sk->makeLinkObj( $title, $nextLink,
417 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
420 return "($prevLink) ($nextLink)";