3 * Special handling for category description pages
4 * Modelled after ImagePage.php
9 if( !defined( 'MEDIAWIKI' ) )
15 class CategoryPage
extends Article
{
17 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
19 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
20 $this->openShowCategory();
25 # If the article we've just shown is in the "Image" namespace,
26 # follow it with the history list and link list for the image
29 if ( NS_CATEGORY
== $this->mTitle
->getNamespace() ) {
30 $this->closeShowCategory();
34 function openShowCategory() {
38 function closeShowCategory() {
39 global $wgOut, $wgRequest;
40 $from = $wgRequest->getVal( 'from' );
41 $until = $wgRequest->getVal( 'until' );
43 $viewer = new CategoryViewer( $this->mTitle
, $from, $until );
44 $wgOut->addHTML( $viewer->getHTML() );
48 class CategoryViewer
{
49 var $title, $limit, $from, $until,
50 $articles, $articles_start_char,
51 $children, $children_start_char,
52 $showGallery, $gallery,
55 function __construct( $title, $from = '', $until = '' ) {
56 global $wgCategoryPagingLimit;
57 $this->title
= $title;
59 $this->until
= $until;
60 $this->limit
= $wgCategoryPagingLimit;
64 * Format the category data list.
66 * @param string $from -- return only sort keys from this item on
67 * @param string $until -- don't return keys after this point.
68 * @return string HTML output
72 global $wgOut, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
73 wfProfileIn( __METHOD__
);
75 $this->showGallery
= $wgCategoryMagicGallery && !$wgOut->mNoGallery
;
77 $this->clearCategoryState();
78 $this->doCategoryQuery();
79 $this->finaliseCategoryState();
81 $r = $this->getCategoryTop() .
82 $this->getSubcategorySection() .
83 $this->getPagesSection() .
84 $this->getImageSection() .
85 $this->getCategoryBottom();
87 wfProfileOut( __METHOD__
);
91 function clearCategoryState() {
92 $this->articles
= array();
93 $this->articles_start_char
= array();
94 $this->children
= array();
95 $this->children_start_char
= array();
96 if( $this->showGallery
) {
97 $this->gallery
= new ImageGallery();
98 $this->gallery
->setParsing();
103 if ( !$this->skin
) {
105 $this->skin
= $wgUser->getSkin();
111 * Add a subcategory to the internal lists
113 function addSubcategory( $title, $sortkey, $pageLength ) {
115 // Subcategory; strip the 'Category' namespace from the link text.
116 $this->children
[] = $this->getSkin()->makeKnownLinkObj(
117 $title, $wgContLang->convertHtml( $title->getText() ) );
119 $this->children_start_char
[] = $this->getSubcategorySortChar( $title, $sortkey );
123 * Get the character to be used for sorting subcategories.
124 * If there's a link from Category:A to Category:B, the sortkey of the resulting
125 * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
126 * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
127 * else use sortkey...
129 function getSubcategorySortChar( $title, $sortkey ) {
132 if( $title->getPrefixedText() == $sortkey ) {
133 $firstChar = $wgContLang->firstChar( $title->getDBkey() );
135 $firstChar = $wgContLang->firstChar( $sortkey );
138 return $wgContLang->convert( $firstChar );
142 * Add a page in the image namespace
144 function addImage( $title, $sortkey, $pageLength ) {
145 if ( $this->showGallery
) {
146 $image = new Image( $title );
148 $this->gallery
->insert( $image );
150 $this->gallery
->add( $image );
153 $this->addPage( $title, $sortkey, $pageLength );
158 * Add a miscellaneous page
160 function addPage( $title, $sortkey, $pageLength ) {
162 $this->articles
[] = $this->getSkin()->makeSizeLinkObj(
163 $pageLength, $title, $wgContLang->convert( $title->getPrefixedText() )
165 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
168 function finaliseCategoryState() {
170 $this->children
= array_reverse( $this->children
);
171 $this->children_start_char
= array_reverse( $this->children_start_char
);
172 $this->articles
= array_reverse( $this->articles
);
173 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
177 function doCategoryQuery() {
178 $dbr =& wfGetDB( DB_SLAVE
);
179 if( $this->from
!= '' ) {
180 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
182 } elseif( $this->until
!= '' ) {
183 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
186 $pageCondition = '1 = 1';
190 array( 'page', 'categorylinks' ),
191 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
192 array( $pageCondition,
194 'cl_to' => $this->title
->getDBKey()),
195 #'page_is_redirect' => 0),
198 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
199 'LIMIT' => $this->limit +
1 ) );
202 $this->nextPage
= null;
203 while( $x = $dbr->fetchObject ( $res ) ) {
204 if( ++
$count > $this->limit
) {
205 // We've reached the one extra which shows that there are
206 // additional pages to be had. Stop here...
207 $this->nextPage
= $x->cl_sortkey
;
211 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
213 if( $title->getNamespace() == NS_CATEGORY
) {
214 $this->addSubcategory( $title, $x->cl_sortkey
, $x->page_len
);
215 } elseif( $title->getNamespace() == NS_IMAGE
) {
216 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
);
218 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
);
221 $dbr->freeResult( $res );
224 function getCategoryTop() {
225 $r = "<br style=\"clear:both;\"/>\n";
226 if( $this->until
!= '' ) {
227 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
228 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
229 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
234 function getSubcategorySection() {
235 # Don't show subcategories section if there are none.
237 if( count( $this->children
) > 0 ) {
238 # Showing subcategories
239 $r .= "<div id=\"mw-subcategories\">\n";
240 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
241 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), count( $this->children
) );
242 $r .= $this->formatList( $this->children
, $this->children_start_char
);
248 function getPagesSection() {
249 $ti = htmlspecialchars( $this->title
->getText() );
250 $r = "<div id=\"mw-pages\">\n";
251 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
252 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), count( $this->articles
) );
253 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
258 function getImageSection() {
259 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
260 return "<div id=\"mw-category-media\">\n" .
261 '<h2>' . wfMsg( 'category-media-header', htmlspecialchars($this->title
->getText()) ) . "</h2>\n" .
262 wfMsgExt( 'category-media-count', array( 'parse' ), $this->gallery
->count() ) .
263 $this->gallery
->toHTML() . "\n</div>";
269 function getCategoryBottom() {
270 if( $this->until
!= '' ) {
271 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
272 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
273 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
280 * Format a list of articles chunked by letter, either as a
281 * bullet list or a columnar format, depending on the length.
283 * @param array $articles
284 * @param array $articles_start_char
289 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
290 if ( count ( $articles ) > $cutoff ) {
291 return $this->columnList( $articles, $articles_start_char );
292 } elseif ( count($articles) > 0) {
293 // for short lists of articles in categories.
294 return $this->shortList( $articles, $articles_start_char );
300 * Format a list of articles chunked by letter in a three-column
301 * list, ordered vertically.
303 * @param array $articles
304 * @param array $articles_start_char
308 function columnList( $articles, $articles_start_char ) {
309 // divide list into three equal chunks
310 $chunk = (int) (count ( $articles ) / 3);
312 // get and display header
313 $r = '<table width="100%"><tr valign="top">';
315 $prev_start_char = 'none';
317 // loop through the chunks
318 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
320 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
325 // output all articles in category
326 for ($index = $startChunk ;
327 $index < $endChunk && $index < count($articles);
330 // check for change of starting letter or begining of chunk
331 if ( ($index == $startChunk) ||
332 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
336 $atColumnTop = false;
341 if ( $articles_start_char[$index] == $prev_start_char )
342 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
343 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
344 $prev_start_char = $articles_start_char[$index];
347 $r .= "<li>{$articles[$index]}</li>";
349 if( !$atColumnTop ) {
356 $r .= '</tr></table>';
361 * Format a list of articles chunked by letter in a bullet list.
362 * @param array $articles
363 * @param array $articles_start_char
367 function shortList( $articles, $articles_start_char ) {
368 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
369 $r .= '<ul><li>'.$articles[0].'</li>';
370 for ($index = 1; $index < count($articles); $index++
)
372 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
374 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
377 $r .= "<li>{$articles[$index]}</li>";
384 * @param Title $title
385 * @param string $first
386 * @param string $last
388 * @param array $query - additional query options to pass
392 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
393 global $wgUser, $wgLang;
394 $sk =& $this->getSkin();
395 $limitText = $wgLang->formatNum( $limit );
397 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
399 $prevLink = $sk->makeLinkObj( $title, $prevLink,
400 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
402 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
404 $nextLink = $sk->makeLinkObj( $title, $nextLink,
405 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
408 return "($prevLink) ($nextLink)";