Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
EventFormatter | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 1 |
formatWikis | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | declare( strict_types=1 ); |
4 | namespace MediaWiki\Extension\CampaignEvents\Formatters; |
5 | |
6 | use MediaWiki\Extension\CampaignEvents\Event\EventRegistration; |
7 | use MediaWiki\Extension\CampaignEvents\MWEntity\WikiLookup; |
8 | use MediaWiki\Extension\CampaignEvents\Special\SpecialEventDetails; |
9 | use MediaWiki\Language\Language; |
10 | use MediaWiki\Linker\LinkRenderer; |
11 | use MediaWiki\SpecialPage\SpecialPage; |
12 | use MediaWiki\WikiMap\WikiMap; |
13 | use OOUI\HtmlSnippet; |
14 | use Wikimedia\Message\ITextFormatter; |
15 | use Wikimedia\Message\MessageValue; |
16 | |
17 | /** |
18 | * To make UI code more DRY, all display formatting logic will be moved to this class. |
19 | * For this patch, only wiki display code has been implemented here, but this will change in future patches. |
20 | * TODO: Add formatting logic for Organizers, Timezones, Participants etc |
21 | * TODO: Make this a service |
22 | */ |
23 | class EventFormatter { |
24 | public const DISPLAYED_WIKI_COUNT = 3; |
25 | |
26 | /** |
27 | * @param EventRegistration $event |
28 | * @param ITextFormatter $messageFormatter |
29 | * @param WikiLookup $wikiLookup |
30 | * @param Language $language |
31 | * @param LinkRenderer $linkRenderer |
32 | * @param string $allWikisMessage |
33 | * @param string $moreWikisMessage |
34 | * @return HtmlSnippet|string |
35 | */ |
36 | public static function formatWikis( |
37 | EventRegistration $event, |
38 | ITextFormatter $messageFormatter, |
39 | WikiLookup $wikiLookup, |
40 | Language $language, |
41 | LinkRenderer $linkRenderer, |
42 | string $allWikisMessage, |
43 | string $moreWikisMessage |
44 | ) { |
45 | $wikis = $event->getWikis(); |
46 | if ( $wikis === EventRegistration::ALL_WIKIS ) { |
47 | return $messageFormatter->format( MessageValue::new( $allWikisMessage ) ); |
48 | } |
49 | $currentWikiId = WikiMap::getCurrentWikiId(); |
50 | $curWikiKey = array_search( $currentWikiId, $wikis, true ); |
51 | if ( $curWikiKey !== false ) { |
52 | unset( $wikis[$curWikiKey] ); |
53 | array_unshift( $wikis, $currentWikiId ); |
54 | } |
55 | $displayedWikiNames = $wikiLookup->getLocalizedNames( |
56 | array_slice( $wikis, 0, self::DISPLAYED_WIKI_COUNT ) |
57 | ); |
58 | $wikiCount = count( $wikis ); |
59 | $escapedWikiNames = []; |
60 | foreach ( $displayedWikiNames as $name ) { |
61 | $escapedWikiNames[] = htmlspecialchars( $name ); |
62 | } |
63 | if ( $wikiCount > self::DISPLAYED_WIKI_COUNT ) { |
64 | $escapedWikiNames[] = $linkRenderer->makeKnownLink( |
65 | SpecialPage::getTitleFor( SpecialEventDetails::PAGE_NAME, (string)$event->getID() ), |
66 | $messageFormatter->format( MessageValue::new( $moreWikisMessage ) |
67 | ->numParams( $wikiCount - self::DISPLAYED_WIKI_COUNT ) |
68 | ) |
69 | ); |
70 | } |
71 | return new HtmlSnippet( $language->listToText( $escapedWikiNames ) ); |
72 | } |
73 | } |