(bug 4940) $action uninitialised
[mediawiki.git] / includes / CategoryPage.php
blobd574ddade2b878e09fd6ffbc94fe66873fbc775d
1 <?php
2 /**
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
6 * @package MediaWiki
7 */
9 if( !defined( 'MEDIAWIKI' ) )
10 die( -1 );
12 global $wgCategoryMagicGallery;
13 if( $wgCategoryMagicGallery )
14 /** */
15 require_once('ImageGallery.php');
17 /**
18 * @package MediaWiki
20 class CategoryPage extends Article {
22 function view() {
23 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
25 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
26 $this->openShowCategory();
29 Article::view();
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
33 # it describes.
35 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
36 $this->closeShowCategory();
40 function openShowCategory() {
41 # For overloading
44 function closeShowCategory() {
45 global $wgOut, $wgRequest;
46 $from = $wgRequest->getVal( 'from' );
47 $until = $wgRequest->getVal( 'until' );
48 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
51 /**
52 * Format the category data list.
54 * @param string $from -- return only sort keys from this item on
55 * @param string $until -- don't return keys after this point.
56 * @return string HTML output
57 * @private
59 function doCategoryMagic( $from = '', $until = '' ) {
60 global $wgContLang,$wgUser, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
61 $fname = 'CategoryPage::doCategoryMagic';
62 wfProfileIn( $fname );
64 $articles = array();
65 $articles_start_char = array();
66 $children = array();
67 $children_start_char = array();
68 if( $wgCategoryMagicGallery ) {
69 $ig = new ImageGallery();
72 $dbr =& wfGetDB( DB_SLAVE );
73 if( $from != '' ) {
74 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
75 $flip = false;
76 } elseif( $until != '' ) {
77 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
78 $flip = true;
79 } else {
80 $pageCondition = '1 = 1';
81 $flip = false;
83 $limit = $wgCategoryPagingLimit;
84 $res = $dbr->select(
85 array( 'page', 'categorylinks' ),
86 array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
87 array( $pageCondition,
88 'cl_from = page_id',
89 'cl_to' => $this->mTitle->getDBKey()),
90 #'page_is_redirect' => 0),
91 #+ $pageCondition,
92 $fname,
93 array( 'ORDER BY' => $flip ? 'cl_sortkey DESC' : 'cl_sortkey',
94 'LIMIT' => $limit + 1 ) );
96 $sk =& $wgUser->getSkin();
97 $r = "<br style=\"clear:both;\"/>\n";
98 $count = 0;
99 $nextPage = null;
100 while( $x = $dbr->fetchObject ( $res ) ) {
101 if( ++$count > $limit ) {
102 // We've reached the one extra which shows that there are
103 // additional pages to be had. Stop here...
104 $nextPage = $x->cl_sortkey;
105 break;
108 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
110 if( $title->getNamespace() == NS_CATEGORY ) {
111 // Subcategory; strip the 'Category' namespace from the link text.
112 array_push( $children, $sk->makeKnownLinkObj( $title, $wgContLang->convertHtml( $title->getText() ) ) );
114 // If there's a link from Category:A to Category:B, the sortkey of the resulting
115 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
116 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
117 // else use sortkey...
118 $sortkey='';
119 if( $title->getPrefixedText() == $x->cl_sortkey ) {
120 $sortkey=$wgContLang->firstChar( $x->page_title );
121 } else {
122 $sortkey=$wgContLang->firstChar( $x->cl_sortkey );
124 array_push( $children_start_char, $wgContLang->convert( $sortkey ) ) ;
125 } elseif( $wgCategoryMagicGallery && $title->getNamespace() == NS_IMAGE ) {
126 // Show thumbnails of categorized images, in a separate chunk
127 if( $flip ) {
128 $ig->insert( Image::newFromTitle( $title ) );
129 } else {
130 $ig->add( Image::newFromTitle( $title ) );
132 } else {
133 // Page in this category
134 array_push( $articles, $sk->makeSizeLinkObj( $x->page_len, $title, $wgContLang->convert( $title->getPrefixedText() ) ) ) ;
135 array_push( $articles_start_char, $wgContLang->convert( $wgContLang->firstChar( $x->cl_sortkey ) ) );
138 $dbr->freeResult( $res );
140 if( $flip ) {
141 $children = array_reverse( $children );
142 $children_start_char = array_reverse( $children_start_char );
143 $articles = array_reverse( $articles );
144 $articles_start_char = array_reverse( $articles_start_char );
147 if( $until != '' ) {
148 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
149 } elseif( $nextPage != '' || $from != '' ) {
150 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
153 # Don't show subcategories section if there are none.
154 if( count( $children ) > 0 ) {
155 # Showing subcategories
156 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
157 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), count( $children) );
158 $r .= $this->formatList( $children, $children_start_char );
161 # Showing articles in this category
162 $ti = htmlspecialchars( $this->mTitle->getText() );
163 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
164 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), count( $articles) );
165 $r .= $this->formatList( $articles, $articles_start_char );
167 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
168 $r.= $ig->toHTML();
171 if( $until != '' ) {
172 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
173 } elseif( $nextPage != '' || $from != '' ) {
174 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
177 wfProfileOut( $fname );
178 return $r;
182 * Format a list of articles chunked by letter, either as a
183 * bullet list or a columnar format, depending on the length.
185 * @param array $articles
186 * @param array $articles_start_char
187 * @param int $cutoff
188 * @return string
189 * @private
191 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
192 if ( count ( $articles ) > $cutoff ) {
193 return $this->columnList( $articles, $articles_start_char );
194 } elseif ( count($articles) > 0) {
195 // for short lists of articles in categories.
196 return $this->shortList( $articles, $articles_start_char );
198 return '';
202 * Format a list of articles chunked by letter in a three-column
203 * list, ordered vertically.
205 * @param array $articles
206 * @param array $articles_start_char
207 * @return string
208 * @private
210 function columnList( $articles, $articles_start_char ) {
211 // divide list into three equal chunks
212 $chunk = (int) (count ( $articles ) / 3);
214 // get and display header
215 $r = '<table width="100%"><tr valign="top">';
217 $prev_start_char = 'none';
219 // loop through the chunks
220 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
221 $chunkIndex < 3;
222 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
224 $r .= "<td>\n";
225 $atColumnTop = true;
227 // output all articles in category
228 for ($index = $startChunk ;
229 $index < $endChunk && $index < count($articles);
230 $index++ )
232 // check for change of starting letter or begining of chunk
233 if ( ($index == $startChunk) ||
234 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
237 if( $atColumnTop ) {
238 $atColumnTop = false;
239 } else {
240 $r .= "</ul>\n";
242 $cont_msg = "";
243 if ( $articles_start_char[$index] == $prev_start_char )
244 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
245 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
246 $prev_start_char = $articles_start_char[$index];
249 $r .= "<li>{$articles[$index]}</li>";
251 if( !$atColumnTop ) {
252 $r .= "</ul>\n";
254 $r .= "</td>\n";
258 $r .= '</tr></table>';
259 return $r;
263 * Format a list of articles chunked by letter in a bullet list.
264 * @param array $articles
265 * @param array $articles_start_char
266 * @return string
267 * @private
269 function shortList( $articles, $articles_start_char ) {
270 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
271 $r .= '<ul><li>'.$articles[0].'</li>';
272 for ($index = 1; $index < count($articles); $index++ )
274 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
276 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
279 $r .= "<li>{$articles[$index]}</li>";
281 $r .= '</ul>';
282 return $r;
286 * @param Title $title
287 * @param string $first
288 * @param string $last
289 * @param int $limit
290 * @param array $query - additional query options to pass
291 * @return string
292 * @private
294 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
295 global $wgUser, $wgLang;
296 $sk =& $wgUser->getSkin();
297 $limitText = $wgLang->formatNum( $limit );
299 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
300 if( $first != '' ) {
301 $prevLink = $sk->makeLinkObj( $title, $prevLink,
302 wfArrayToCGI( $query + array( 'until' => $first ) ) );
304 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
305 if( $last != '' ) {
306 $nextLink = $sk->makeLinkObj( $title, $nextLink,
307 wfArrayToCGI( $query + array( 'from' => $last ) ) );
310 return "($prevLink) ($nextLink)";