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 // If there's a link from Category:A to Category:B, the sortkey of the resulting
120 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
121 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
122 // else use sortkey...
124 if( $title->getPrefixedText() == $sortkey ) {
125 $sortkey = $wgContLang->firstChar( $title->getDBkey() );
127 $sortkey = $wgContLang->firstChar( $sortkey );
129 $this->children_start_char
[] = $wgContLang->convert( $sortkey );
133 * Add a page in the image namespace
135 function addImage( $title, $sortkey, $pageLength ) {
136 if ( $this->showGallery
) {
137 $image = new Image( $title );
139 $this->gallery
->insert( $image );
141 $this->gallery
->add( $image );
144 $this->addPage( $title, $sortkey, $pageLength );
149 * Add a miscellaneous page
151 function addPage( $title, $sortkey, $pageLength ) {
153 $this->articles
[] = $this->getSkin()->makeSizeLinkObj(
154 $pageLength, $title, $wgContLang->convert( $title->getPrefixedText() )
156 $this->articles_start_char
[] = $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
159 function finaliseCategoryState() {
161 $this->children
= array_reverse( $this->children
);
162 $this->children_start_char
= array_reverse( $this->children_start_char
);
163 $this->articles
= array_reverse( $this->articles
);
164 $this->articles_start_char
= array_reverse( $this->articles_start_char
);
168 function doCategoryQuery() {
169 $dbr =& wfGetDB( DB_SLAVE
);
170 if( $this->from
!= '' ) {
171 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from
);
173 } elseif( $this->until
!= '' ) {
174 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until
);
177 $pageCondition = '1 = 1';
181 array( 'page', 'categorylinks' ),
182 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
183 array( $pageCondition,
185 'cl_to' => $this->title
->getDBKey()),
186 #'page_is_redirect' => 0),
189 array( 'ORDER BY' => $this->flip ?
'cl_sortkey DESC' : 'cl_sortkey',
190 'LIMIT' => $this->limit +
1 ) );
193 $this->nextPage
= null;
194 while( $x = $dbr->fetchObject ( $res ) ) {
195 if( ++
$count > $this->limit
) {
196 // We've reached the one extra which shows that there are
197 // additional pages to be had. Stop here...
198 $this->nextPage
= $x->cl_sortkey
;
202 $title = Title
::makeTitle( $x->page_namespace
, $x->page_title
);
204 if( $title->getNamespace() == NS_CATEGORY
) {
205 $this->addSubcategory( $title, $x->cl_sortkey
, $x->page_len
);
206 } elseif( $title->getNamespace() == NS_IMAGE
) {
207 $this->addImage( $title, $x->cl_sortkey
, $x->page_len
);
209 $this->addPage( $title, $x->cl_sortkey
, $x->page_len
);
212 $dbr->freeResult( $res );
215 function getCategoryTop() {
216 $r = "<br style=\"clear:both;\"/>\n";
217 if( $this->until
!= '' ) {
218 $r .= $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
219 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
220 $r .= $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
225 function getSubcategorySection() {
226 # Don't show subcategories section if there are none.
228 if( count( $this->children
) > 0 ) {
229 # Showing subcategories
230 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
231 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), count( $this->children
) );
232 $r .= $this->formatList( $this->children
, $this->children_start_char
);
237 function getPagesSection() {
238 $ti = htmlspecialchars( $this->title
->getText() );
239 $r = '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
240 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), count( $this->articles
) );
241 $r .= $this->formatList( $this->articles
, $this->articles_start_char
);
245 function getImageSection() {
246 if( $this->showGallery
&& ! $this->gallery
->isEmpty() ) {
247 return $this->gallery
->toHTML();
253 function getCategoryBottom() {
254 if( $this->until
!= '' ) {
255 return $this->pagingLinks( $this->title
, $this->nextPage
, $this->until
, $this->limit
);
256 } elseif( $this->nextPage
!= '' ||
$this->from
!= '' ) {
257 return $this->pagingLinks( $this->title
, $this->from
, $this->nextPage
, $this->limit
);
264 * Format a list of articles chunked by letter, either as a
265 * bullet list or a columnar format, depending on the length.
267 * @param array $articles
268 * @param array $articles_start_char
273 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
274 if ( count ( $articles ) > $cutoff ) {
275 return $this->columnList( $articles, $articles_start_char );
276 } elseif ( count($articles) > 0) {
277 // for short lists of articles in categories.
278 return $this->shortList( $articles, $articles_start_char );
284 * Format a list of articles chunked by letter in a three-column
285 * list, ordered vertically.
287 * @param array $articles
288 * @param array $articles_start_char
292 function columnList( $articles, $articles_start_char ) {
293 // divide list into three equal chunks
294 $chunk = (int) (count ( $articles ) / 3);
296 // get and display header
297 $r = '<table width="100%"><tr valign="top">';
299 $prev_start_char = 'none';
301 // loop through the chunks
302 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
304 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
309 // output all articles in category
310 for ($index = $startChunk ;
311 $index < $endChunk && $index < count($articles);
314 // check for change of starting letter or begining of chunk
315 if ( ($index == $startChunk) ||
316 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
320 $atColumnTop = false;
325 if ( $articles_start_char[$index] == $prev_start_char )
326 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
327 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
328 $prev_start_char = $articles_start_char[$index];
331 $r .= "<li>{$articles[$index]}</li>";
333 if( !$atColumnTop ) {
340 $r .= '</tr></table>';
345 * Format a list of articles chunked by letter in a bullet list.
346 * @param array $articles
347 * @param array $articles_start_char
351 function shortList( $articles, $articles_start_char ) {
352 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
353 $r .= '<ul><li>'.$articles[0].'</li>';
354 for ($index = 1; $index < count($articles); $index++
)
356 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
358 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
361 $r .= "<li>{$articles[$index]}</li>";
368 * @param Title $title
369 * @param string $first
370 * @param string $last
372 * @param array $query - additional query options to pass
376 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
377 global $wgUser, $wgLang;
378 $sk =& $this->getSkin();
379 $limitText = $wgLang->formatNum( $limit );
381 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
383 $prevLink = $sk->makeLinkObj( $title, $prevLink,
384 wfArrayToCGI( $query +
array( 'until' => $first ) ) );
386 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
388 $nextLink = $sk->makeLinkObj( $title, $nextLink,
389 wfArrayToCGI( $query +
array( 'from' => $last ) ) );
392 return "($prevLink) ($nextLink)";