Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 101 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
CampaignPageFormatter | |
0.00% |
0 / 101 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
isCampaignExtensionEnabled | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
generateReadHtml | |
0.00% |
0 / 95 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | /** |
3 | * Upload Campaign Formatter |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @ingroup UploadWizard |
8 | * |
9 | * @author Yuvi Panda <yuvipanda@gmail.com> |
10 | */ |
11 | |
12 | namespace MediaWiki\Extension\UploadWizard; |
13 | |
14 | use IContextSource; |
15 | use ImageGalleryBase; |
16 | use MediaWiki\Html\Html; |
17 | use MediaWiki\MediaWikiServices; |
18 | use OOUI\ButtonWidget; |
19 | use RequestContext; |
20 | use Skin; |
21 | |
22 | /** |
23 | * Helper class to produce formatted HTML output for Campaigns |
24 | */ |
25 | class CampaignPageFormatter { |
26 | /** @var Campaign|null */ |
27 | protected $campaign = null; |
28 | /** @var IContextSource|null */ |
29 | protected $context = null; |
30 | |
31 | public function __construct( $campaign, $context = null ) { |
32 | $this->campaign = $campaign; |
33 | if ( $context === null ) { |
34 | $this->context = RequestContext::getMain(); |
35 | } else { |
36 | $this->context = $context; |
37 | } |
38 | } |
39 | |
40 | /** |
41 | * @return bool |
42 | */ |
43 | private function isCampaignExtensionEnabled() { |
44 | $rl = MediaWikiServices::getInstance()->getResourceLoader(); |
45 | // FIXME: This string exists nowhere in Wikimedia Gerrit outside this file. |
46 | return $rl->isModuleRegistered( 'ext.campaigns' ); |
47 | } |
48 | |
49 | public function generateReadHtml() { |
50 | $config = $this->campaign->getParsedConfig(); |
51 | |
52 | $campaignTitle = $config['title'] ?? $this->campaign->getName(); |
53 | $campaignDescription = $config['description'] ?? ''; |
54 | $campaignViewMoreLink = $this->campaign->getTrackingCategory()->getFullURL(); |
55 | |
56 | $gallery = ImageGalleryBase::factory( 'packed-hover' ); |
57 | $gallery->setContext( $this->context ); |
58 | $gallery->setWidths( '180' ); |
59 | $gallery->setHeights( '180' ); |
60 | $gallery->setShowBytes( false ); |
61 | |
62 | $this->context->getOutput()->setCdnMaxage( |
63 | Config::getSetting( 'campaignSquidMaxAge' ) |
64 | ); |
65 | $this->context->getOutput()->setHTMLTitle( $this->context->msg( 'pagetitle', $campaignTitle ) ); |
66 | $this->context->getOutput()->enableOOUI(); |
67 | $this->context->getOutput()->addBodyClasses( 'mwe-upwiz-campaign-page' ); |
68 | |
69 | $images = $this->campaign->getUploadedMedia(); |
70 | |
71 | if ( !$this->context->getUser()->isNamed() ) { |
72 | $urlParams = [ 'returnto' => $this->campaign->getTitle()->getPrefixedText() ]; |
73 | |
74 | if ( $this->isCampaignExtensionEnabled() ) { |
75 | $campaignTemplate = Config::getSetting( 'campaignCTACampaignTemplate' ); |
76 | $urlParams['campaign'] = str_replace( '$1', $this->campaign->getName(), $campaignTemplate ); |
77 | } |
78 | $createAccountUrl = Skin::makeSpecialUrlSubpage( 'Userlogin', 'signup', $urlParams ); |
79 | $uploadLink = new ButtonWidget( [ |
80 | 'label' => wfMessage( 'mwe-upwiz-campaign-create-account-button' )->text(), |
81 | 'flags' => [ 'progressive', 'primary' ], |
82 | 'href' => $createAccountUrl |
83 | ] ); |
84 | } else { |
85 | $uploadUrl = Skin::makeSpecialUrl( |
86 | 'UploadWizard', [ 'campaign' => $this->campaign->getName() ] |
87 | ); |
88 | $uploadLink = new ButtonWidget( [ |
89 | 'label' => wfMessage( 'mwe-upwiz-campaign-upload-button' )->text(), |
90 | 'flags' => [ 'progressive', 'primary' ], |
91 | 'href' => $uploadUrl |
92 | ] ); |
93 | } |
94 | |
95 | if ( $images === [] ) { |
96 | $body = Html::element( |
97 | 'div', |
98 | [ 'id' => 'mw-campaign-no-uploads-yet' ], |
99 | wfMessage( 'mwe-upwiz-campaign-no-uploads-yet' )->plain() |
100 | ); |
101 | } else { |
102 | foreach ( $images as $image ) { |
103 | $gallery->add( $image ); |
104 | } |
105 | |
106 | $body = |
107 | Html::rawElement( 'div', [ 'id' => 'mw-campaign-images' ], $gallery->toHTML() ) . |
108 | Html::rawElement( 'a', |
109 | [ 'id' => 'mw-campaign-view-all', 'href' => $campaignViewMoreLink ], |
110 | Html::rawElement( |
111 | 'span', |
112 | [ 'class' => 'mw-campaign-chevron mw-campaign-float-left' ], ' ' |
113 | ) . |
114 | wfMessage( 'mwe-upwiz-campaign-view-all-media' )->escaped() . |
115 | Html::rawElement( |
116 | 'span', |
117 | [ 'class' => 'mw-campaign-chevron mw-campaign-float-right' ], ' ' |
118 | ) |
119 | ); |
120 | } |
121 | |
122 | if ( Config::getSetting( 'campaignExpensiveStatsEnabled' ) === true ) { |
123 | $uploaderCount = $this->campaign->getTotalContributorsCount(); |
124 | $campaignExpensiveStats = |
125 | Html::rawElement( 'div', [ 'class' => 'mw-campaign-number-container' ], |
126 | Html::element( 'div', [ 'class' => 'mw-campaign-number' ], |
127 | $this->context->getLanguage()->formatNum( $uploaderCount ) ) . |
128 | Html::element( 'span', |
129 | [ 'class' => 'mw-campaign-number-desc' ], |
130 | wfMessage( 'mwe-upwiz-campaign-contributors-count-desc' ) |
131 | ->numParams( $uploaderCount ) |
132 | ->text() |
133 | ) |
134 | ); |
135 | } else { |
136 | $campaignExpensiveStats = ''; |
137 | } |
138 | |
139 | $uploadCount = $this->campaign->getUploadedMediaCount(); |
140 | return Html::rawElement( 'div', [ 'id' => 'mw-campaign-container' ], |
141 | Html::rawElement( 'div', [ 'id' => 'mw-campaign-header' ], |
142 | Html::rawElement( 'div', [ 'id' => 'mw-campaign-primary-info' ], |
143 | // No need to escape these, since they are just parsed wikitext |
144 | // Any stripping that needed to be done should've been done by the parser |
145 | Html::rawElement( 'p', [ 'id' => 'mw-campaign-title' ], $campaignTitle ) . |
146 | Html::rawElement( 'p', [ 'id' => 'mw-campaign-description' ], $campaignDescription ) . |
147 | $uploadLink |
148 | ) . |
149 | Html::rawElement( 'div', [ 'id' => 'mw-campaign-numbers' ], |
150 | $campaignExpensiveStats . |
151 | Html::rawElement( 'div', [ 'class' => 'mw-campaign-number-container' ], |
152 | Html::element( 'div', [ 'class' => 'mw-campaign-number' ], |
153 | $this->context->getLanguage()->formatNum( $uploadCount ) |
154 | ) . |
155 | Html::element( 'span', |
156 | [ 'class' => 'mw-campaign-number-desc' ], |
157 | wfMessage( 'mwe-upwiz-campaign-media-count-desc' ) |
158 | ->numParams( $uploadCount ) |
159 | ->text() |
160 | ) |
161 | ) |
162 | ) |
163 | ) . $body |
164 | ); |
165 | } |
166 | } |