CategoryPageView hook as in 1.4
[lhc/web/wiklou.git] / includes / CategoryPage.php
1 <?php
2 /**
3 * Special handling for category description pages
4 * Modelled after ImagePage.php
5 *
6 * @package MediaWiki
7 */
8
9 if( !defined( 'MEDIAWIKI' ) )
10 die();
11
12 if( $wgCategoryMagicGallery )
13 /** */
14 require_once('ImageGallery.php');
15
16 /**
17 * @package MediaWiki
18 */
19 class CategoryPage extends Article {
20
21 function view() {
22 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
23
24 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
25 $this->openShowCategory();
26 }
27
28 Article::view();
29
30 # If the article we've just shown is in the "Image" namespace,
31 # follow it with the history list and link list for the image
32 # it describes.
33
34 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
35 $this->closeShowCategory();
36 }
37 }
38
39 function openShowCategory() {
40 # For overloading
41 }
42
43 # generate a list of subcategories and pages for a category
44 # depending on wfMsg("usenewcategorypage") it either calls the new
45 # or the old code. The new code will not work properly for some
46 # languages due to sorting issues, so they might want to turn it
47 # off.
48
49 function closeShowCategory() {
50 global $wgOut, $wgRequest;
51 $pageConditions = array();
52 $from = $wgRequest->getVal( 'from' );
53 $until = $wgRequest->getVal( 'until' );
54 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
55 }
56
57 /**
58 * Format the category data list.
59 *
60 * @param string $from -- return only sort keys from this item on
61 * @param string $until -- don't return keys after this point.
62 * @return string HTML output
63 * @access private
64 */
65 function doCategoryMagic( $from = '', $until = '' ) {
66 global $wgContLang,$wgUser, $wgCategoryMagicGallery;
67 $fname = 'CategoryPage::doCategoryMagic';
68 wfProfileIn( $fname );
69
70 $articles = array();
71 $articles_start_char = array();
72 $children = array();
73 $children_start_char = array();
74 $data = array();
75 if( $wgCategoryMagicGallery ) {
76 $ig = new ImageGallery();
77 }
78
79 $dbr =& wfGetDB( DB_SLAVE );
80 if( $from != '' ) {
81 $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
82 $flip = false;
83 } elseif( $until != '' ) {
84 $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
85 $flip = true;
86 } else {
87 $pageCondition = '1';
88 $flip = false;
89 }
90 $limit = 200;
91 $res = $dbr->select(
92 array( 'page', 'categorylinks' ),
93 array( 'page_title', 'page_namespace', 'cl_sortkey' ),
94 array( $pageCondition,
95 'cl_from = page_id',
96 'cl_to' => $this->mTitle->getDBKey()),
97 #'page_is_redirect' => 0),
98 #+ $pageCondition,
99 $fname,
100 array( 'ORDER BY' => $flip ? 'cl_sortkey DESC' : 'cl_sortkey',
101 'LIMIT' => $limit + 1 ) );
102
103 $sk =& $wgUser->getSkin();
104 $r = "<br style=\"clear:both;\"/>\n";
105 $count = 0;
106 $nextPage = null;
107 while( $x = $dbr->fetchObject ( $res ) ) {
108 if( ++$count > $limit ) {
109 // We've reached the one extra which shows that there are
110 // additional pages to be had. Stop here...
111 $nextPage = $x->cl_sortkey;
112 break;
113 }
114
115 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
116
117 if( $title->getNamespace() == NS_CATEGORY ) {
118 // Subcategory; strip the 'Category' namespace from the link text.
119 array_push( $children, $sk->makeKnownLinkObj( $title, $title->getText() ) );
120
121 // If there's a link from Category:A to Category:B, the sortkey of the resulting
122 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
123 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
124 // else use sortkey...
125 if( $title->getPrefixedText() == $x->cl_sortkey ) {
126 array_push( $children_start_char, $wgContLang->firstChar( $x->page_title ) );
127 } else {
128 array_push( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
129 }
130 } elseif( $wgCategoryMagicGallery && $title->getNamespace() == NS_IMAGE ) {
131 // Show thumbnails of categorized images, in a separate chunk
132 if( $flip ) {
133 $ig->insert( Image::newFromTitle( $title ) );
134 } else {
135 $ig->add( Image::newFromTitle( $title ) );
136 }
137 } else {
138 // Page in this category
139 array_push( $articles, $sk->makeKnownLinkObj( $title ) ) ;
140 array_push( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
141 }
142 }
143 $dbr->freeResult( $res );
144
145 if( $flip ) {
146 $children = array_reverse( $children );
147 $children_start_char = array_reverse( $children_start_char );
148 $articles = array_reverse( $articles );
149 $articles_start_char = array_reverse( $articles_start_char );
150 }
151
152 if( $until != '' ) {
153 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
154 } elseif( $nextPage != '' || $from != '' ) {
155 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
156 }
157
158 # Don't show subcategories section if there are none.
159 if( count( $children ) > 0 ) {
160 # Showing subcategories
161 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
162 $r .= $this->formatCount( $children, 'subcategorycount' );
163 $r .= $this->formatList( $children, $children_start_char );
164 }
165
166 # Showing articles in this category
167 $ti = htmlspecialchars( $this->mTitle->getText() );
168 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
169 $r .= $this->formatCount( $articles, 'categoryarticlecount' );
170 $r .= $this->formatList( $articles, $articles_start_char );
171
172 if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
173 $r.= $ig->toHTML();
174 }
175
176 wfProfileOut( $fname );
177 return $r;
178 }
179
180 /**
181 * @param array $articles
182 * @param string $message
183 * @return string
184 * @access private
185 */
186 function formatCount( $articles, $message ) {
187 global $wgContLang;
188 $numart = count( $articles );
189 if( $numart == 1 ) {
190 # Slightly different message to avoid silly plural
191 $message .= '1';
192 }
193 return wfMsg( $message, $wgContLang->formatNum( $numart ) );
194 }
195 /**
196 * Format a list of articles chunked by letter, either as a
197 * bullet list or a columnar format, depending on the length.
198 *
199 * @param array $articles
200 * @param array $articles_start_char
201 * @param int $cutoff
202 * @return string
203 * @access private
204 */
205 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
206 if ( count ( $articles ) > $cutoff ) {
207 return $this->columnList( $articles, $articles_start_char );
208 } elseif ( count($articles) > 0) {
209 // for short lists of articles in categories.
210 return $this->shortList( $articles, $articles_start_char );
211 }
212 return '';
213 }
214
215 /**
216 * Format a list of articles chunked by letter in a three-column
217 * list, ordered vertically.
218 *
219 * @param array $articles
220 * @param array $articles_start_char
221 * @return string
222 * @access private
223 */
224 function columnList( $articles, $articles_start_char ) {
225 // divide list into three equal chunks
226 $chunk = (int) (count ( $articles ) / 3);
227
228 // get and display header
229 $r = '<table width="100%"><tr valign="top">';
230
231 $prev_start_char = 'none';
232
233 // loop through the chunks
234 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
235 $chunkIndex < 3;
236 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
237 {
238 $r .= "<td>\n";
239 $atColumnTop = true;
240
241 // output all articles in category
242 for ($index = $startChunk ;
243 $index < $endChunk && $index < count($articles);
244 $index++ )
245 {
246 // check for change of starting letter or begining of chunk
247 if ( ($index == $startChunk) ||
248 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
249
250 {
251 if( $atColumnTop ) {
252 $atColumnTop = false;
253 } else {
254 $r .= "</ul>\n";
255 }
256 $cont_msg = "";
257 if ( $articles_start_char[$index] == $prev_start_char )
258 $cont_msg = wfMsg('listingcontinuesabbrev');
259 $r .= "<h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
260 $prev_start_char = $articles_start_char[$index];
261 }
262
263 $r .= "<li>{$articles[$index]}</li>";
264 }
265 if( !$atColumnTop ) {
266 $r .= "</ul>\n";
267 }
268 $r .= "</td>\n";
269
270
271 }
272 $r .= '</tr></table>';
273 return $r;
274 }
275
276 /**
277 * Format a list of articles chunked by letter in a bullet list.
278 * @param array $articles
279 * @param array $articles_start_char
280 * @return string
281 * @access private
282 */
283 function shortList( $articles, $articles_start_char ) {
284 $r = '<h3>'.$articles_start_char[0]."</h3>\n";
285 $r .= '<ul><li>'.$articles[0].'</li>';
286 for ($index = 1; $index < count($articles); $index++ )
287 {
288 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
289 {
290 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
291 }
292
293 $r .= "<li>{$articles[$index]}</li>";
294 }
295 $r .= '</ul>';
296 return $r;
297 }
298
299 /**
300 * @param Title $title
301 * @param string $first
302 * @param string $last
303 * @param int $limit
304 * @param array $query - additional query options to pass
305 * @return string
306 * @access private
307 */
308 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
309 global $wgUser, $wgLang;
310 $sk =& $wgUser->getSkin();
311 $limitText = $wgLang->formatNum( $limit );
312
313 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
314 if( $first != '' ) {
315 $prevLink = $sk->makeLinkObj( $title, $prevLink,
316 wfArrayToCGI( $query + array( 'until' => $first ) ) );
317 }
318 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
319 if( $last != '' ) {
320 $nextLink = $sk->makeLinkObj( $title, $nextLink,
321 wfArrayToCGI( $query + array( 'from' => $last ) ) );
322 }
323
324 return "($prevLink) ($nextLink)";
325 }
326 }
327
328
329 ?>