Fix #6622: image::newFromTitle deprecated but still used
[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( 1 );
11
12 /**
13 * @package MediaWiki
14 */
15 class CategoryPage extends Article {
16
17 function view() {
18 if(!wfRunHooks('CategoryPageView', array(&$this))) return;
19
20 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
21 $this->openShowCategory();
22 }
23
24 Article::view();
25
26 # If the article we've just shown is in the "Image" namespace,
27 # follow it with the history list and link list for the image
28 # it describes.
29
30 if ( NS_CATEGORY == $this->mTitle->getNamespace() ) {
31 $this->closeShowCategory();
32 }
33 }
34
35 function openShowCategory() {
36 # For overloading
37 }
38
39 function closeShowCategory() {
40 global $wgOut, $wgRequest;
41 $from = $wgRequest->getVal( 'from' );
42 $until = $wgRequest->getVal( 'until' );
43
44 $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
45 }
46
47 /**
48 * Format the category data list.
49 *
50 * @param string $from -- return only sort keys from this item on
51 * @param string $until -- don't return keys after this point.
52 * @return string HTML output
53 * @private
54 */
55 function doCategoryMagic( $from = '', $until = '' ) {
56 global $wgOut;
57 global $wgContLang,$wgUser, $wgCategoryMagicGallery, $wgCategoryPagingLimit;
58 $fname = 'CategoryPage::doCategoryMagic';
59 wfProfileIn( $fname );
60
61 $articles = array();
62 $articles_start_char = array();
63 $children = array();
64 $children_start_char = array();
65
66 $showGallery = $wgCategoryMagicGallery && !$wgOut->mNoGallery;
67 if( $showGallery ) {
68 $ig = new ImageGallery();
69 $ig->setParsing();
70 }
71
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;
82 }
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 ) );
95
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;
106 }
107
108 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
109
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() ) ) );
113
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 );
123 }
124 array_push( $children_start_char, $wgContLang->convert( $sortkey ) ) ;
125 } elseif( $showGallery && $title->getNamespace() == NS_IMAGE ) {
126 // Show thumbnails of categorized images, in a separate chunk
127 $anImage = new Image($title);
128 if( $flip ) {
129 $ig->insert( $anImage );
130 } else {
131 $ig->add( $anImage );
132 }
133 unset($anImage);
134 } else {
135 // Page in this category
136 array_push( $articles, $sk->makeSizeLinkObj( $x->page_len, $title, $wgContLang->convert( $title->getPrefixedText() ) ) ) ;
137 array_push( $articles_start_char, $wgContLang->convert( $wgContLang->firstChar( $x->cl_sortkey ) ) );
138 }
139 }
140 $dbr->freeResult( $res );
141
142 if( $flip ) {
143 $children = array_reverse( $children );
144 $children_start_char = array_reverse( $children_start_char );
145 $articles = array_reverse( $articles );
146 $articles_start_char = array_reverse( $articles_start_char );
147 }
148
149 if( $until != '' ) {
150 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
151 } elseif( $nextPage != '' || $from != '' ) {
152 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
153 }
154
155 # Don't show subcategories section if there are none.
156 if( count( $children ) > 0 ) {
157 # Showing subcategories
158 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
159 $r .= wfMsgExt( 'subcategorycount', array( 'parse' ), count( $children) );
160 $r .= $this->formatList( $children, $children_start_char );
161 }
162
163 # Showing articles in this category
164 $ti = htmlspecialchars( $this->mTitle->getText() );
165 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
166 $r .= wfMsgExt( 'categoryarticlecount', array( 'parse' ), count( $articles) );
167 $r .= $this->formatList( $articles, $articles_start_char );
168
169 if( $showGallery && ! $ig->isEmpty() ) {
170 $r.= $ig->toHTML();
171 }
172
173 if( $until != '' ) {
174 $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
175 } elseif( $nextPage != '' || $from != '' ) {
176 $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
177 }
178
179 wfProfileOut( $fname );
180 return $r;
181 }
182
183 /**
184 * Format a list of articles chunked by letter, either as a
185 * bullet list or a columnar format, depending on the length.
186 *
187 * @param array $articles
188 * @param array $articles_start_char
189 * @param int $cutoff
190 * @return string
191 * @private
192 */
193 function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
194 if ( count ( $articles ) > $cutoff ) {
195 return $this->columnList( $articles, $articles_start_char );
196 } elseif ( count($articles) > 0) {
197 // for short lists of articles in categories.
198 return $this->shortList( $articles, $articles_start_char );
199 }
200 return '';
201 }
202
203 /**
204 * Format a list of articles chunked by letter in a three-column
205 * list, ordered vertically.
206 *
207 * @param array $articles
208 * @param array $articles_start_char
209 * @return string
210 * @private
211 */
212 function columnList( $articles, $articles_start_char ) {
213 // divide list into three equal chunks
214 $chunk = (int) (count ( $articles ) / 3);
215
216 // get and display header
217 $r = '<table width="100%"><tr valign="top">';
218
219 $prev_start_char = 'none';
220
221 // loop through the chunks
222 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
223 $chunkIndex < 3;
224 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
225 {
226 $r .= "<td>\n";
227 $atColumnTop = true;
228
229 // output all articles in category
230 for ($index = $startChunk ;
231 $index < $endChunk && $index < count($articles);
232 $index++ )
233 {
234 // check for change of starting letter or begining of chunk
235 if ( ($index == $startChunk) ||
236 ($articles_start_char[$index] != $articles_start_char[$index - 1]) )
237
238 {
239 if( $atColumnTop ) {
240 $atColumnTop = false;
241 } else {
242 $r .= "</ul>\n";
243 }
244 $cont_msg = "";
245 if ( $articles_start_char[$index] == $prev_start_char )
246 $cont_msg = wfMsgHtml('listingcontinuesabbrev');
247 $r .= "<h3>" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg</h3>\n<ul>";
248 $prev_start_char = $articles_start_char[$index];
249 }
250
251 $r .= "<li>{$articles[$index]}</li>";
252 }
253 if( !$atColumnTop ) {
254 $r .= "</ul>\n";
255 }
256 $r .= "</td>\n";
257
258
259 }
260 $r .= '</tr></table>';
261 return $r;
262 }
263
264 /**
265 * Format a list of articles chunked by letter in a bullet list.
266 * @param array $articles
267 * @param array $articles_start_char
268 * @return string
269 * @private
270 */
271 function shortList( $articles, $articles_start_char ) {
272 $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
273 $r .= '<ul><li>'.$articles[0].'</li>';
274 for ($index = 1; $index < count($articles); $index++ )
275 {
276 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
277 {
278 $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
279 }
280
281 $r .= "<li>{$articles[$index]}</li>";
282 }
283 $r .= '</ul>';
284 return $r;
285 }
286
287 /**
288 * @param Title $title
289 * @param string $first
290 * @param string $last
291 * @param int $limit
292 * @param array $query - additional query options to pass
293 * @return string
294 * @private
295 */
296 function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
297 global $wgUser, $wgLang;
298 $sk =& $wgUser->getSkin();
299 $limitText = $wgLang->formatNum( $limit );
300
301 $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
302 if( $first != '' ) {
303 $prevLink = $sk->makeLinkObj( $title, $prevLink,
304 wfArrayToCGI( $query + array( 'until' => $first ) ) );
305 }
306 $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
307 if( $last != '' ) {
308 $nextLink = $sk->makeLinkObj( $title, $nextLink,
309 wfArrayToCGI( $query + array( 'from' => $last ) ) );
310 }
311
312 return "($prevLink) ($nextLink)";
313 }
314 }
315
316
317 ?>