These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * List and paging of category members. |
||
4 | * |
||
5 | * This program is free software; you can redistribute it and/or modify |
||
6 | * it under the terms of the GNU General Public License as published by |
||
7 | * the Free Software Foundation; either version 2 of the License, or |
||
8 | * (at your option) any later version. |
||
9 | * |
||
10 | * This program is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | * GNU General Public License for more details. |
||
14 | * |
||
15 | * You should have received a copy of the GNU General Public License along |
||
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
18 | * http://www.gnu.org/copyleft/gpl.html |
||
19 | * |
||
20 | * @file |
||
21 | */ |
||
22 | use MediaWiki\MediaWikiServices; |
||
23 | |||
24 | class CategoryViewer extends ContextSource { |
||
25 | /** @var int */ |
||
26 | public $limit; |
||
27 | |||
28 | /** @var array */ |
||
29 | public $from; |
||
30 | |||
31 | /** @var array */ |
||
32 | public $until; |
||
33 | |||
34 | /** @var string[] */ |
||
35 | public $articles; |
||
36 | |||
37 | /** @var array */ |
||
38 | public $articles_start_char; |
||
39 | |||
40 | /** @var array */ |
||
41 | public $children; |
||
42 | |||
43 | /** @var array */ |
||
44 | public $children_start_char; |
||
45 | |||
46 | /** @var bool */ |
||
47 | public $showGallery; |
||
48 | |||
49 | /** @var array */ |
||
50 | public $imgsNoGallery_start_char; |
||
51 | |||
52 | /** @var array */ |
||
53 | public $imgsNoGallery; |
||
54 | |||
55 | /** @var array */ |
||
56 | public $nextPage; |
||
57 | |||
58 | /** @var array */ |
||
59 | protected $prevPage; |
||
60 | |||
61 | /** @var array */ |
||
62 | public $flip; |
||
63 | |||
64 | /** @var Title */ |
||
65 | public $title; |
||
66 | |||
67 | /** @var Collation */ |
||
68 | public $collation; |
||
69 | |||
70 | /** @var ImageGallery */ |
||
71 | public $gallery; |
||
72 | |||
73 | /** @var Category Category object for this page. */ |
||
74 | private $cat; |
||
75 | |||
76 | /** @var array The original query array, to be used in generating paging links. */ |
||
77 | private $query; |
||
78 | |||
79 | /** |
||
80 | * @since 1.19 $context is a second, required parameter |
||
81 | * @param Title $title |
||
82 | * @param IContextSource $context |
||
83 | * @param array $from An array with keys page, subcat, |
||
84 | * and file for offset of results of each section (since 1.17) |
||
85 | * @param array $until An array with 3 keys for until of each section (since 1.17) |
||
86 | * @param array $query |
||
87 | */ |
||
88 | function __construct( $title, IContextSource $context, $from = [], |
||
89 | $until = [], $query = [] |
||
90 | ) { |
||
91 | $this->title = $title; |
||
92 | $this->setContext( $context ); |
||
93 | $this->getOutput()->addModuleStyles( [ |
||
94 | 'mediawiki.action.view.categoryPage.styles' |
||
95 | ] ); |
||
96 | $this->from = $from; |
||
97 | $this->until = $until; |
||
98 | $this->limit = $context->getConfig()->get( 'CategoryPagingLimit' ); |
||
99 | $this->cat = Category::newFromTitle( $title ); |
||
100 | $this->query = $query; |
||
101 | $this->collation = Collation::singleton(); |
||
102 | unset( $this->query['title'] ); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Format the category data list. |
||
107 | * |
||
108 | * @return string HTML output |
||
109 | */ |
||
110 | public function getHTML() { |
||
111 | |||
112 | $this->showGallery = $this->getConfig()->get( 'CategoryMagicGallery' ) |
||
113 | && !$this->getOutput()->mNoGallery; |
||
114 | |||
115 | $this->clearCategoryState(); |
||
116 | $this->doCategoryQuery(); |
||
117 | $this->finaliseCategoryState(); |
||
118 | |||
119 | $r = $this->getSubcategorySection() . |
||
120 | $this->getPagesSection() . |
||
121 | $this->getImageSection(); |
||
122 | |||
123 | if ( $r == '' ) { |
||
124 | // If there is no category content to display, only |
||
125 | // show the top part of the navigation links. |
||
126 | // @todo FIXME: Cannot be completely suppressed because it |
||
127 | // is unknown if 'until' or 'from' makes this |
||
128 | // give 0 results. |
||
129 | $r = $r . $this->getCategoryTop(); |
||
130 | } else { |
||
131 | $r = $this->getCategoryTop() . |
||
132 | $r . |
||
133 | $this->getCategoryBottom(); |
||
134 | } |
||
135 | |||
136 | // Give a proper message if category is empty |
||
137 | if ( $r == '' ) { |
||
138 | $r = $this->msg( 'category-empty' )->parseAsBlock(); |
||
139 | } |
||
140 | |||
141 | $lang = $this->getLanguage(); |
||
142 | $attribs = [ |
||
143 | 'class' => 'mw-category-generated', |
||
144 | 'lang' => $lang->getHtmlCode(), |
||
145 | 'dir' => $lang->getDir() |
||
146 | ]; |
||
147 | # put a div around the headings which are in the user language |
||
148 | $r = Html::openElement( 'div', $attribs ) . $r . '</div>'; |
||
149 | |||
150 | return $r; |
||
151 | } |
||
152 | |||
153 | function clearCategoryState() { |
||
154 | $this->articles = []; |
||
155 | $this->articles_start_char = []; |
||
156 | $this->children = []; |
||
157 | $this->children_start_char = []; |
||
158 | if ( $this->showGallery ) { |
||
159 | // Note that null for mode is taken to mean use default. |
||
160 | $mode = $this->getRequest()->getVal( 'gallerymode', null ); |
||
161 | try { |
||
162 | $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() ); |
||
163 | } catch ( Exception $e ) { |
||
164 | // User specified something invalid, fallback to default. |
||
165 | $this->gallery = ImageGalleryBase::factory( false, $this->getContext() ); |
||
166 | } |
||
167 | |||
168 | $this->gallery->setHideBadImages(); |
||
169 | } else { |
||
170 | $this->imgsNoGallery = []; |
||
171 | $this->imgsNoGallery_start_char = []; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Add a subcategory to the internal lists, using a Category object |
||
177 | * @param Category $cat |
||
178 | * @param string $sortkey |
||
179 | * @param int $pageLength |
||
180 | */ |
||
181 | function addSubcategoryObject( Category $cat, $sortkey, $pageLength ) { |
||
182 | // Subcategory; strip the 'Category' namespace from the link text. |
||
183 | $title = $cat->getTitle(); |
||
184 | |||
185 | $this->children[] = $this->generateLink( |
||
186 | 'subcat', |
||
187 | $title, |
||
0 ignored issues
–
show
|
|||
188 | $title->isRedirect(), |
||
189 | htmlspecialchars( $title->getText() ) |
||
190 | ); |
||
191 | |||
192 | $this->children_start_char[] = |
||
193 | $this->getSubcategorySortChar( $cat->getTitle(), $sortkey ); |
||
194 | } |
||
195 | |||
196 | function generateLink( $type, Title $title, $isRedirect, $html = null ) { |
||
197 | $link = null; |
||
198 | Hooks::run( 'CategoryViewer::generateLink', [ $type, $title, $html, &$link ] ); |
||
199 | if ( $link === null ) { |
||
200 | $link = Linker::link( $title, $html ); |
||
201 | } |
||
202 | if ( $isRedirect ) { |
||
203 | $link = '<span class="redirect-in-category">' . $link . '</span>'; |
||
204 | } |
||
205 | |||
206 | return $link; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Get the character to be used for sorting subcategories. |
||
211 | * If there's a link from Category:A to Category:B, the sortkey of the resulting |
||
212 | * entry in the categorylinks table is Category:A, not A, which it SHOULD be. |
||
213 | * Workaround: If sortkey == "Category:".$title, than use $title for sorting, |
||
214 | * else use sortkey... |
||
215 | * |
||
216 | * @param Title $title |
||
217 | * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever). |
||
218 | * @return string |
||
219 | */ |
||
220 | function getSubcategorySortChar( $title, $sortkey ) { |
||
221 | global $wgContLang; |
||
222 | |||
223 | if ( $title->getPrefixedText() == $sortkey ) { |
||
224 | $word = $title->getDBkey(); |
||
225 | } else { |
||
226 | $word = $sortkey; |
||
227 | } |
||
228 | |||
229 | $firstChar = $this->collation->getFirstLetter( $word ); |
||
230 | |||
231 | return $wgContLang->convert( $firstChar ); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Add a page in the image namespace |
||
236 | * @param Title $title |
||
237 | * @param string $sortkey |
||
238 | * @param int $pageLength |
||
239 | * @param bool $isRedirect |
||
240 | */ |
||
241 | function addImage( Title $title, $sortkey, $pageLength, $isRedirect = false ) { |
||
242 | global $wgContLang; |
||
243 | if ( $this->showGallery ) { |
||
244 | $flip = $this->flip['file']; |
||
245 | if ( $flip ) { |
||
246 | $this->gallery->insert( $title ); |
||
247 | } else { |
||
248 | $this->gallery->add( $title ); |
||
249 | } |
||
250 | } else { |
||
251 | $this->imgsNoGallery[] = $this->generateLink( 'image', $title, $isRedirect ); |
||
252 | |||
253 | $this->imgsNoGallery_start_char[] = $wgContLang->convert( |
||
254 | $this->collation->getFirstLetter( $sortkey ) ); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Add a miscellaneous page |
||
260 | * @param Title $title |
||
261 | * @param string $sortkey |
||
262 | * @param int $pageLength |
||
263 | * @param bool $isRedirect |
||
264 | */ |
||
265 | function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) { |
||
266 | global $wgContLang; |
||
267 | |||
268 | $this->articles[] = $this->generateLink( 'page', $title, $isRedirect ); |
||
269 | |||
270 | $this->articles_start_char[] = $wgContLang->convert( |
||
271 | $this->collation->getFirstLetter( $sortkey ) ); |
||
272 | } |
||
273 | |||
274 | function finaliseCategoryState() { |
||
275 | if ( $this->flip['subcat'] ) { |
||
276 | $this->children = array_reverse( $this->children ); |
||
277 | $this->children_start_char = array_reverse( $this->children_start_char ); |
||
278 | } |
||
279 | if ( $this->flip['page'] ) { |
||
280 | $this->articles = array_reverse( $this->articles ); |
||
281 | $this->articles_start_char = array_reverse( $this->articles_start_char ); |
||
282 | } |
||
283 | if ( !$this->showGallery && $this->flip['file'] ) { |
||
284 | $this->imgsNoGallery = array_reverse( $this->imgsNoGallery ); |
||
285 | $this->imgsNoGallery_start_char = array_reverse( $this->imgsNoGallery_start_char ); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | function doCategoryQuery() { |
||
290 | $dbr = wfGetDB( DB_REPLICA, 'category' ); |
||
291 | |||
292 | $this->nextPage = [ |
||
293 | 'page' => null, |
||
294 | 'subcat' => null, |
||
295 | 'file' => null, |
||
296 | ]; |
||
297 | $this->prevPage = [ |
||
298 | 'page' => null, |
||
299 | 'subcat' => null, |
||
300 | 'file' => null, |
||
301 | ]; |
||
302 | |||
303 | $this->flip = [ 'page' => false, 'subcat' => false, 'file' => false ]; |
||
304 | |||
305 | foreach ( [ 'page', 'subcat', 'file' ] as $type ) { |
||
306 | # Get the sortkeys for start/end, if applicable. Note that if |
||
307 | # the collation in the database differs from the one |
||
308 | # set in $wgCategoryCollation, pagination might go totally haywire. |
||
309 | $extraConds = [ 'cl_type' => $type ]; |
||
310 | if ( isset( $this->from[$type] ) && $this->from[$type] !== null ) { |
||
311 | $extraConds[] = 'cl_sortkey >= ' |
||
312 | . $dbr->addQuotes( $this->collation->getSortKey( $this->from[$type] ) ); |
||
313 | } elseif ( isset( $this->until[$type] ) && $this->until[$type] !== null ) { |
||
314 | $extraConds[] = 'cl_sortkey < ' |
||
315 | . $dbr->addQuotes( $this->collation->getSortKey( $this->until[$type] ) ); |
||
316 | $this->flip[$type] = true; |
||
317 | } |
||
318 | |||
319 | $res = $dbr->select( |
||
320 | [ 'page', 'categorylinks', 'category' ], |
||
321 | array_merge( |
||
322 | LinkCache::getSelectFields(), |
||
323 | [ |
||
324 | 'page_namespace', |
||
325 | 'page_title', |
||
326 | 'cl_sortkey', |
||
327 | 'cat_id', |
||
328 | 'cat_title', |
||
329 | 'cat_subcats', |
||
330 | 'cat_pages', |
||
331 | 'cat_files', |
||
332 | 'cl_sortkey_prefix', |
||
333 | 'cl_collation' |
||
334 | ] |
||
335 | ), |
||
336 | array_merge( [ 'cl_to' => $this->title->getDBkey() ], $extraConds ), |
||
337 | __METHOD__, |
||
338 | [ |
||
339 | 'USE INDEX' => [ 'categorylinks' => 'cl_sortkey' ], |
||
340 | 'LIMIT' => $this->limit + 1, |
||
341 | 'ORDER BY' => $this->flip[$type] ? 'cl_sortkey DESC' : 'cl_sortkey', |
||
342 | ], |
||
343 | [ |
||
344 | 'categorylinks' => [ 'INNER JOIN', 'cl_from = page_id' ], |
||
345 | 'category' => [ 'LEFT JOIN', [ |
||
346 | 'cat_title = page_title', |
||
347 | 'page_namespace' => NS_CATEGORY |
||
348 | ] ] |
||
349 | ] |
||
350 | ); |
||
351 | |||
352 | Hooks::run( 'CategoryViewer::doCategoryQuery', [ $type, $res ] ); |
||
353 | $linkCache = MediaWikiServices::getInstance()->getLinkCache(); |
||
354 | |||
355 | $count = 0; |
||
356 | foreach ( $res as $row ) { |
||
357 | $title = Title::newFromRow( $row ); |
||
358 | $linkCache->addGoodLinkObjFromRow( $title, $row ); |
||
359 | |||
360 | if ( $row->cl_collation === '' ) { |
||
361 | // Hack to make sure that while updating from 1.16 schema |
||
362 | // and db is inconsistent, that the sky doesn't fall. |
||
363 | // See r83544. Could perhaps be removed in a couple decades... |
||
364 | $humanSortkey = $row->cl_sortkey; |
||
365 | } else { |
||
366 | $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix ); |
||
367 | } |
||
368 | |||
369 | if ( ++$count > $this->limit ) { |
||
370 | # We've reached the one extra which shows that there |
||
371 | # are additional pages to be had. Stop here... |
||
372 | $this->nextPage[$type] = $humanSortkey; |
||
373 | break; |
||
374 | } |
||
375 | if ( $count == $this->limit ) { |
||
376 | $this->prevPage[$type] = $humanSortkey; |
||
377 | } |
||
378 | |||
379 | if ( $title->getNamespace() == NS_CATEGORY ) { |
||
380 | $cat = Category::newFromRow( $row, $title ); |
||
381 | $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len ); |
||
382 | } elseif ( $title->getNamespace() == NS_FILE ) { |
||
383 | $this->addImage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect ); |
||
384 | } else { |
||
385 | $this->addPage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect ); |
||
386 | } |
||
387 | } |
||
388 | } |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @return string |
||
393 | */ |
||
394 | function getCategoryTop() { |
||
395 | $r = $this->getCategoryBottom(); |
||
396 | return $r === '' |
||
397 | ? $r |
||
398 | : "<br style=\"clear:both;\"/>\n" . $r; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @return string |
||
403 | */ |
||
404 | function getSubcategorySection() { |
||
405 | # Don't show subcategories section if there are none. |
||
406 | $r = ''; |
||
407 | $rescnt = count( $this->children ); |
||
408 | $dbcnt = $this->cat->getSubcatCount(); |
||
409 | // This function should be called even if the result isn't used, it has side-effects |
||
410 | $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' ); |
||
411 | |||
412 | View Code Duplication | if ( $rescnt > 0 ) { |
|
413 | # Showing subcategories |
||
414 | $r .= "<div id=\"mw-subcategories\">\n"; |
||
415 | $r .= '<h2>' . $this->msg( 'subcategories' )->parse() . "</h2>\n"; |
||
416 | $r .= $countmsg; |
||
417 | $r .= $this->getSectionPagingLinks( 'subcat' ); |
||
418 | $r .= $this->formatList( $this->children, $this->children_start_char ); |
||
419 | $r .= $this->getSectionPagingLinks( 'subcat' ); |
||
420 | $r .= "\n</div>"; |
||
421 | } |
||
422 | return $r; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Return pretty name which is display name if given and different from prefix text or |
||
427 | * the unprefixed page name. |
||
428 | * |
||
429 | * @return string HTML safe name. |
||
430 | */ |
||
431 | function getPrettyPageNameHtml() { |
||
432 | $displayTitle = $this->getOutput()->getPageTitle(); |
||
433 | if ( $displayTitle === $this->getTitle()->getPrefixedText() ) { |
||
434 | return htmlspecialchars( $this->getTitle()->getText() ); |
||
435 | } else { |
||
436 | return $displayTitle; |
||
437 | } |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * @return string |
||
442 | */ |
||
443 | function getPagesSection() { |
||
444 | $name = $this->getPrettyPageNameHtml(); |
||
445 | # Don't show articles section if there are none. |
||
446 | $r = ''; |
||
447 | |||
448 | # @todo FIXME: Here and in the other two sections: we don't need to bother |
||
449 | # with this rigmarole if the entire category contents fit on one page |
||
450 | # and have already been retrieved. We can just use $rescnt in that |
||
451 | # case and save a query and some logic. |
||
452 | $dbcnt = $this->cat->getPageCount() - $this->cat->getSubcatCount() |
||
453 | - $this->cat->getFileCount(); |
||
454 | $rescnt = count( $this->articles ); |
||
455 | // This function should be called even if the result isn't used, it has side-effects |
||
456 | $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' ); |
||
457 | |||
458 | View Code Duplication | if ( $rescnt > 0 ) { |
|
459 | $r = "<div id=\"mw-pages\">\n"; |
||
460 | $r .= '<h2>' . $this->msg( 'category_header' )->rawParams( $name )->parse() . "</h2>\n"; |
||
461 | $r .= $countmsg; |
||
462 | $r .= $this->getSectionPagingLinks( 'page' ); |
||
463 | $r .= $this->formatList( $this->articles, $this->articles_start_char ); |
||
464 | $r .= $this->getSectionPagingLinks( 'page' ); |
||
465 | $r .= "\n</div>"; |
||
466 | } |
||
467 | return $r; |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * @return string |
||
472 | */ |
||
473 | function getImageSection() { |
||
474 | $name = $this->getPrettyPageNameHtml(); |
||
475 | $r = ''; |
||
476 | $rescnt = $this->showGallery ? $this->gallery->count() : count( $this->imgsNoGallery ); |
||
477 | $dbcnt = $this->cat->getFileCount(); |
||
478 | // This function should be called even if the result isn't used, it has side-effects |
||
479 | $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' ); |
||
480 | |||
481 | if ( $rescnt > 0 ) { |
||
482 | $r .= "<div id=\"mw-category-media\">\n"; |
||
483 | $r .= '<h2>' . |
||
484 | $this->msg( 'category-media-header' )->rawParams( $name )->parse() . |
||
485 | "</h2>\n"; |
||
486 | $r .= $countmsg; |
||
487 | $r .= $this->getSectionPagingLinks( 'file' ); |
||
488 | if ( $this->showGallery ) { |
||
489 | $r .= $this->gallery->toHTML(); |
||
490 | } else { |
||
491 | $r .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char ); |
||
492 | } |
||
493 | $r .= $this->getSectionPagingLinks( 'file' ); |
||
494 | $r .= "\n</div>"; |
||
495 | } |
||
496 | return $r; |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Get the paging links for a section (subcats/pages/files), to go at the top and bottom |
||
501 | * of the output. |
||
502 | * |
||
503 | * @param string $type 'page', 'subcat', or 'file' |
||
504 | * @return string HTML output, possibly empty if there are no other pages |
||
505 | */ |
||
506 | private function getSectionPagingLinks( $type ) { |
||
507 | if ( isset( $this->until[$type] ) && $this->until[$type] !== null ) { |
||
508 | // The new value for the until parameter should be pointing to the first |
||
509 | // result displayed on the page which is the second last result retrieved |
||
510 | // from the database.The next link should have a from parameter pointing |
||
511 | // to the until parameter of the current page. |
||
512 | if ( $this->nextPage[$type] !== null ) { |
||
513 | return $this->pagingLinks( $this->prevPage[$type], $this->until[$type], $type ); |
||
514 | } else { |
||
515 | // If the nextPage variable is null, it means that we have reached the first page |
||
516 | // and therefore the previous link should be disabled. |
||
517 | return $this->pagingLinks( null, $this->until[$type], $type ); |
||
518 | } |
||
519 | } elseif ( $this->nextPage[$type] !== null |
||
520 | || ( isset( $this->from[$type] ) && $this->from[$type] !== null ) |
||
521 | ) { |
||
522 | return $this->pagingLinks( $this->from[$type], $this->nextPage[$type], $type ); |
||
523 | } else { |
||
524 | return ''; |
||
525 | } |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * @return string |
||
530 | */ |
||
531 | function getCategoryBottom() { |
||
532 | return ''; |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Format a list of articles chunked by letter, either as a |
||
537 | * bullet list or a columnar format, depending on the length. |
||
538 | * |
||
539 | * @param array $articles |
||
540 | * @param array $articles_start_char |
||
541 | * @param int $cutoff |
||
542 | * @return string |
||
543 | * @private |
||
544 | */ |
||
545 | function formatList( $articles, $articles_start_char, $cutoff = 6 ) { |
||
546 | $list = ''; |
||
547 | if ( count( $articles ) > $cutoff ) { |
||
548 | $list = self::columnList( $articles, $articles_start_char ); |
||
549 | } elseif ( count( $articles ) > 0 ) { |
||
550 | // for short lists of articles in categories. |
||
551 | $list = self::shortList( $articles, $articles_start_char ); |
||
552 | } |
||
553 | |||
554 | $pageLang = $this->title->getPageLanguage(); |
||
555 | $attribs = [ 'lang' => $pageLang->getHtmlCode(), 'dir' => $pageLang->getDir(), |
||
556 | 'class' => 'mw-content-' . $pageLang->getDir() ]; |
||
557 | $list = Html::rawElement( 'div', $attribs, $list ); |
||
558 | |||
559 | return $list; |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Format a list of articles chunked by letter in a three-column list, ordered |
||
564 | * vertically. This is used for categories with a significant number of pages. |
||
565 | * |
||
566 | * TODO: Take the headers into account when creating columns, so they're |
||
567 | * more visually equal. |
||
568 | * |
||
569 | * TODO: shortList and columnList are similar, need merging |
||
570 | * |
||
571 | * @param string[] $articles HTML links to each article |
||
572 | * @param string[] $articles_start_char The header characters for each article |
||
573 | * @return string HTML to output |
||
574 | * @private |
||
575 | */ |
||
576 | static function columnList( $articles, $articles_start_char ) { |
||
577 | $columns = array_combine( $articles, $articles_start_char ); |
||
578 | |||
579 | $ret = Html::openElement( 'div', [ 'class' => 'mw-category' ] ); |
||
580 | |||
581 | $colContents = []; |
||
582 | |||
583 | # Kind of like array_flip() here, but we keep duplicates in an |
||
584 | # array instead of dropping them. |
||
585 | foreach ( $columns as $article => $char ) { |
||
586 | if ( !isset( $colContents[$char] ) ) { |
||
587 | $colContents[$char] = []; |
||
588 | } |
||
589 | $colContents[$char][] = $article; |
||
590 | } |
||
591 | |||
592 | foreach ( $colContents as $char => $articles ) { |
||
593 | # Change space to non-breaking space to keep headers aligned |
||
594 | $h3char = $char === ' ' ? ' ' : htmlspecialchars( $char ); |
||
595 | |||
596 | $ret .= '<div class="mw-category-group"><h3>' . $h3char; |
||
597 | $ret .= "</h3>\n"; |
||
598 | |||
599 | $ret .= '<ul><li>'; |
||
600 | $ret .= implode( "</li>\n<li>", $articles ); |
||
601 | $ret .= '</li></ul></div>'; |
||
602 | |||
603 | } |
||
604 | |||
605 | $ret .= Html::closeElement( 'div' ); |
||
606 | return $ret; |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * Format a list of articles chunked by letter in a bullet list. This is used |
||
611 | * for categories with a small number of pages (when columns aren't needed). |
||
612 | * @param string[] $articles HTML links to each article |
||
613 | * @param string[] $articles_start_char The header characters for each article |
||
614 | * @return string HTML to output |
||
615 | * @private |
||
616 | */ |
||
617 | static function shortList( $articles, $articles_start_char ) { |
||
618 | $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n"; |
||
619 | $r .= '<ul><li>' . $articles[0] . '</li>'; |
||
620 | $articleCount = count( $articles ); |
||
621 | for ( $index = 1; $index < $articleCount; $index++ ) { |
||
622 | if ( $articles_start_char[$index] != $articles_start_char[$index - 1] ) { |
||
623 | $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>"; |
||
624 | } |
||
625 | |||
626 | $r .= "<li>{$articles[$index]}</li>"; |
||
627 | } |
||
628 | $r .= '</ul>'; |
||
629 | return $r; |
||
630 | } |
||
631 | |||
632 | /** |
||
633 | * Create paging links, as a helper method to getSectionPagingLinks(). |
||
634 | * |
||
635 | * @param string $first The 'until' parameter for the generated URL |
||
636 | * @param string $last The 'from' parameter for the generated URL |
||
637 | * @param string $type A prefix for parameters, 'page' or 'subcat' or |
||
638 | * 'file' |
||
639 | * @return string HTML |
||
640 | */ |
||
641 | private function pagingLinks( $first, $last, $type = '' ) { |
||
642 | $prevLink = $this->msg( 'prev-page' )->text(); |
||
643 | |||
644 | View Code Duplication | if ( $first != '' ) { |
|
645 | $prevQuery = $this->query; |
||
646 | $prevQuery["{$type}until"] = $first; |
||
647 | unset( $prevQuery["{$type}from"] ); |
||
648 | $prevLink = Linker::linkKnown( |
||
649 | $this->addFragmentToTitle( $this->title, $type ), |
||
650 | $prevLink, |
||
651 | [], |
||
652 | $prevQuery |
||
653 | ); |
||
654 | } |
||
655 | |||
656 | $nextLink = $this->msg( 'next-page' )->text(); |
||
657 | |||
658 | View Code Duplication | if ( $last != '' ) { |
|
659 | $lastQuery = $this->query; |
||
660 | $lastQuery["{$type}from"] = $last; |
||
661 | unset( $lastQuery["{$type}until"] ); |
||
662 | $nextLink = Linker::linkKnown( |
||
663 | $this->addFragmentToTitle( $this->title, $type ), |
||
664 | $nextLink, |
||
665 | [], |
||
666 | $lastQuery |
||
667 | ); |
||
668 | } |
||
669 | |||
670 | return $this->msg( 'categoryviewer-pagedlinks' )->rawParams( $prevLink, $nextLink )->escaped(); |
||
671 | } |
||
672 | |||
673 | /** |
||
674 | * Takes a title, and adds the fragment identifier that |
||
675 | * corresponds to the correct segment of the category. |
||
676 | * |
||
677 | * @param Title $title The title (usually $this->title) |
||
678 | * @param string $section Which section |
||
679 | * @throws MWException |
||
680 | * @return Title |
||
681 | */ |
||
682 | private function addFragmentToTitle( $title, $section ) { |
||
683 | switch ( $section ) { |
||
684 | case 'page': |
||
685 | $fragment = 'mw-pages'; |
||
686 | break; |
||
687 | case 'subcat': |
||
688 | $fragment = 'mw-subcategories'; |
||
689 | break; |
||
690 | case 'file': |
||
691 | $fragment = 'mw-category-media'; |
||
692 | break; |
||
693 | default: |
||
694 | throw new MWException( __METHOD__ . |
||
695 | " Invalid section $section." ); |
||
696 | } |
||
697 | |||
698 | return Title::makeTitle( $title->getNamespace(), |
||
699 | $title->getDBkey(), $fragment ); |
||
700 | } |
||
701 | |||
702 | /** |
||
703 | * What to do if the category table conflicts with the number of results |
||
704 | * returned? This function says what. Each type is considered independently |
||
705 | * of the other types. |
||
706 | * |
||
707 | * @param int $rescnt The number of items returned by our database query. |
||
708 | * @param int $dbcnt The number of items according to the category table. |
||
709 | * @param string $type 'subcat', 'article', or 'file' |
||
710 | * @return string A message giving the number of items, to output to HTML. |
||
711 | */ |
||
712 | private function getCountMessage( $rescnt, $dbcnt, $type ) { |
||
713 | // There are three cases: |
||
714 | // 1) The category table figure seems sane. It might be wrong, but |
||
715 | // we can't do anything about it if we don't recalculate it on ev- |
||
716 | // ery category view. |
||
717 | // 2) The category table figure isn't sane, like it's smaller than the |
||
718 | // number of actual results, *but* the number of results is less |
||
719 | // than $this->limit and there's no offset. In this case we still |
||
720 | // know the right figure. |
||
721 | // 3) We have no idea. |
||
722 | |||
723 | // Check if there's a "from" or "until" for anything |
||
724 | |||
725 | // This is a little ugly, but we seem to use different names |
||
726 | // for the paging types then for the messages. |
||
727 | if ( $type === 'article' ) { |
||
728 | $pagingType = 'page'; |
||
729 | } else { |
||
730 | $pagingType = $type; |
||
731 | } |
||
732 | |||
733 | $fromOrUntil = false; |
||
734 | if ( ( isset( $this->from[$pagingType] ) && $this->from[$pagingType] !== null ) || |
||
735 | ( isset( $this->until[$pagingType] ) && $this->until[$pagingType] !== null ) |
||
736 | ) { |
||
737 | $fromOrUntil = true; |
||
738 | } |
||
739 | |||
740 | if ( $dbcnt == $rescnt || |
||
741 | ( ( $rescnt == $this->limit || $fromOrUntil ) && $dbcnt > $rescnt ) |
||
742 | ) { |
||
743 | // Case 1: seems sane. |
||
744 | $totalcnt = $dbcnt; |
||
745 | } elseif ( $rescnt < $this->limit && !$fromOrUntil ) { |
||
746 | // Case 2: not sane, but salvageable. Use the number of results. |
||
747 | // Since there are fewer than 200, we can also take this opportunity |
||
748 | // to refresh the incorrect category table entry -- which should be |
||
749 | // quick due to the small number of entries. |
||
750 | $totalcnt = $rescnt; |
||
751 | $category = $this->cat; |
||
752 | DeferredUpdates::addCallableUpdate( function () use ( $category ) { |
||
753 | $category->refreshCounts(); |
||
754 | } ); |
||
755 | } else { |
||
756 | // Case 3: hopeless. Don't give a total count at all. |
||
757 | // Messages: category-subcat-count-limited, category-article-count-limited, |
||
758 | // category-file-count-limited |
||
759 | return $this->msg( "category-$type-count-limited" )->numParams( $rescnt )->parseAsBlock(); |
||
760 | } |
||
761 | // Messages: category-subcat-count, category-article-count, category-file-count |
||
762 | return $this->msg( "category-$type-count" )->numParams( $rescnt, $totalcnt )->parseAsBlock(); |
||
763 | } |
||
764 | } |
||
765 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.