Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 106
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CampaignPageFormatter
0.00% covered (danger)
0.00%
0 / 106
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 fillParserOutput
0.00% covered (danger)
0.00%
0 / 102
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace MediaWiki\Extension\MediaUploader\Campaign;
4
5use Html;
6use ImageGalleryBase;
7use MediaWiki\Extension\MediaUploader\Config\CampaignParsedConfig;
8use MediaWiki\Extension\MediaUploader\MediaUploaderServices;
9use MWException;
10use ParserOutput;
11use RequestContext;
12use Skin;
13use Title;
14
15/**
16 * Helper class to produce formatted HTML output for campaigns
17 */
18class CampaignPageFormatter {
19
20    /** @var CampaignRecord */
21    private $record;
22
23    /** @var CampaignParsedConfig */
24    private $config;
25
26    /** @var CampaignStats */
27    private $campaignStats;
28
29    /** @var RequestContext */
30    private $context;
31
32    public function __construct(
33        CampaignRecord $campaignRecord,
34        CampaignParsedConfig $config
35    ) {
36        $this->record = $campaignRecord;
37        $this->config = $config;
38
39        // TODO: use DI
40        $this->campaignStats = MediaUploaderServices::getCampaignStats();
41        // No way to get rid of this for now.
42        // Blockers: ImageGalleryBase and message localization.
43        $this->context = RequestContext::getMain();
44    }
45
46    /**
47     * @param ParserOutput $output
48     *
49     * @return void
50     * @throws MWException
51     */
52    public function fillParserOutput( ParserOutput $output ): void {
53        $campaignTitle = Title::makeTitle(
54            NS_CAMPAIGN,
55            $this->record->getPage()->getDBkey()
56        );
57        $campaignName = $this->config->getSetting(
58            'title',
59            $campaignTitle->getText()
60        );
61
62        $campaignDescription = $this->config->getSetting( 'description', '' );
63        $trackingCat = Title::newFromText(
64            $this->record->getTrackingCategoryName( $this->config ),
65            NS_CATEGORY
66        );
67        $campaignViewMoreLink = $trackingCat ? $trackingCat->getFullURL() : '';
68
69        $gallery = ImageGalleryBase::factory( 'packed-hover', $this->context );
70        $gallery->setWidths( '180' );
71        $gallery->setHeights( '180' );
72        $gallery->setShowBytes( false );
73
74        $outputPage = $this->context->getOutput();
75        $outputPage->setCdnMaxage(
76            $this->config->getSetting( 'campaignCdnMaxAge' )
77        );
78        $outputPage->enableOOUI();
79
80        $stats = $this->campaignStats->getStatsForRecord( $this->record ) ?? [];
81        $images = $stats['uploadedMedia'] ?? [];
82
83        if ( $this->context->getUser()->isAnon() ) {
84            $urlParams = [ 'returnto' => $campaignTitle->getPrefixedText() ];
85            $createAccountUrl = Skin::makeSpecialUrlSubpage( 'Userlogin', 'signup', $urlParams );
86            $uploadLink = new \OOUI\ButtonWidget( [
87                'label' => $this->context->msg( 'mediauploader-campaign-create-account-button' )->text(),
88                'flags' => [ 'progressive', 'primary' ],
89                'href' => $createAccountUrl
90            ] );
91        } else {
92            $uploadUrl = Skin::makeSpecialUrl(
93                'MediaUploader', [ 'campaign' => $this->record->getPage()->getDBkey() ]
94            );
95            $uploadLink = new \OOUI\ButtonWidget( [
96                'label' => $this->context->msg( 'mediauploader-campaign-upload-button' )->text(),
97                'flags' => [ 'progressive', 'primary' ],
98                'href' => $uploadUrl
99            ] );
100        }
101
102        if ( $images === [] ) {
103            $body = Html::element(
104                'div',
105                [ 'id' => 'mw-campaign-no-uploads-yet' ],
106                $this->context->msg( 'mediauploader-campaign-no-uploads-yet' )->plain()
107            );
108        } else {
109            foreach ( $images as $image ) {
110                $gallery->add( Title::newFromText( $image, NS_FILE ) );
111            }
112
113            $body =
114                Html::rawElement( 'div', [ 'id' => 'mw-campaign-images' ], $gallery->toHTML() ) .
115                Html::rawElement( 'a',
116                    [ 'id' => 'mw-campaign-view-all', 'href' => $campaignViewMoreLink ],
117                    Html::rawElement(
118                        'span',
119                        [ 'class' => 'mw-campaign-chevron mw-campaign-float-left' ], '&nbsp;'
120                    ) .
121                    $this->context->msg( 'mediauploader-campaign-view-all-media' )->escaped() .
122                    Html::rawElement(
123                        'span',
124                        [ 'class' => 'mw-campaign-chevron mw-campaign-float-right' ], '&nbsp;'
125                    )
126                );
127        }
128
129        $contributorsCount = $stats['contributorsCount'] ?? 0;
130        $campaignExpensiveStats =
131            Html::rawElement( 'div', [ 'class' => 'mw-campaign-number-container' ],
132                Html::element( 'div', [ 'class' => 'mw-campaign-number' ],
133                    $this->context->getLanguage()->formatNum( $contributorsCount ) ) .
134                Html::element( 'span',
135                    [ 'class' => 'mw-campaign-number-desc' ],
136                    $this->context->msg( 'mediauploader-campaign-contributors-count-desc' )
137                    ->numParams( $contributorsCount )
138                    ->text()
139                )
140            );
141
142        $uploadCount = $stats['uploadedMediaCount'] ?? 0;
143        $result =
144            Html::rawElement( 'div', [ 'id' => 'mw-campaign-container' ],
145                Html::rawElement( 'div', [ 'id' => 'mw-campaign-header' ],
146                    Html::rawElement( 'div', [ 'id' => 'mw-campaign-primary-info' ],
147                        // No need to escape these, since they are just parsed wikitext
148                        // Any stripping that needed to be done should've been done by the parser
149                        Html::rawElement( 'p', [ 'id' => 'mw-campaign-title' ], $campaignName ) .
150                        Html::rawElement( 'p', [ 'id' => 'mw-campaign-description' ], $campaignDescription ) .
151                    $uploadLink
152                    ) .
153                    Html::rawElement( 'div', [ 'id' => 'mw-campaign-numbers' ],
154                        $campaignExpensiveStats .
155                        Html::rawElement( 'div', [ 'class' => 'mw-campaign-number-container' ],
156                            Html::element( 'div', [ 'class' => 'mw-campaign-number' ],
157                                $this->context->getLanguage()->formatNum( $uploadCount )
158                            ) .
159                            Html::element( 'span',
160                                [ 'class' => 'mw-campaign-number-desc' ],
161                                $this->context->msg( 'mediauploader-campaign-media-count-desc' )
162                                ->numParams( $uploadCount )
163                                ->text()
164                            )
165                        )
166                    )
167                ) .
168                $body
169            );
170        $output->setText( $result );
171    }
172}