Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
CampaignRecord
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
8 / 8
17
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getPageId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValidity
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 assertValid
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
7
 getTrackingCategoryName
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace MediaWiki\Extension\MediaUploader\Campaign;
4
5use MediaWiki\Extension\MediaUploader\Campaign\Exception\BaseCampaignException;
6use MediaWiki\Extension\MediaUploader\Campaign\Exception\IncompleteRecordException;
7use MediaWiki\Extension\MediaUploader\Campaign\Exception\InvalidFormatException;
8use MediaWiki\Extension\MediaUploader\Campaign\Exception\InvalidSchemaException;
9use MediaWiki\Extension\MediaUploader\Config\ConfigBase;
10use MediaWiki\Page\PageReference;
11use MWException;
12
13/**
14 * Represents a row in the mu_campaign table.
15 * This is not the actual campaign page or its effective config. The purpose
16 * of this DB table is to "cache" the status and raw content of the campaign.
17 *
18 * @newable
19 */
20class CampaignRecord {
21
22    public const CONTENT_VALID = 1;
23    public const CONTENT_INVALID_FORMAT = 2;
24    public const CONTENT_INVALID_SCHEMA = 3;
25
26    /** @var null|int */
27    private $pageId;
28
29    /** @var bool */
30    private $enabled;
31
32    /** @var int */
33    private $validity;
34
35    /** @var null|array */
36    private $content;
37
38    /** @var null|PageReference */
39    private $pageReference;
40
41    /** @var false|null|string */
42
43    /**
44     * @param null|int $pageId can be null for unsaved pages
45     * @param bool $enabled
46     * @param int $validity
47     * @param null|array $content optional
48     * @param null|PageReference $pageReference the corresponding page, optional
49     */
50    public function __construct(
51        ?int $pageId,
52        bool $enabled,
53        int $validity,
54        array $content = null,
55        PageReference $pageReference = null
56    ) {
57        $this->pageId = $pageId;
58        $this->enabled = $enabled;
59        $this->validity = $validity;
60        $this->content = $content;
61        $this->pageReference = $pageReference;
62    }
63
64    /**
65     * The ID of the page this campaign is stored on.
66     * Can be null for unsaved pages.
67     *
68     * @return int|null
69     */
70    public function getPageId(): ?int {
71        return $this->pageId;
72    }
73
74    /**
75     * Whether this campaign is enabled (not necessarily active).
76     * @return bool
77     */
78    public function isEnabled(): bool {
79        return $this->enabled;
80    }
81
82    /**
83     * The validity of this campaign definition.
84     * One of self::CONTENT_* constants
85     * @return int
86     */
87    public function getValidity(): int {
88        return $this->validity;
89    }
90
91    /**
92     * The raw, unparsed content of the campaign, as array.
93     * @return array|null
94     */
95    public function getContent(): ?array {
96        return $this->content;
97    }
98
99    /**
100     * Reference to the page this campaign is on.
101     * @return PageReference|null
102     */
103    public function getPage(): ?PageReference {
104        return $this->pageReference;
105    }
106
107    /**
108     * Asserts that the campaign is valid. Throws an exception otherwise.
109     *
110     * @param string $name The DB key or other identifier of the campaign.
111     * @param int $requiredFields A bitfield of CampaignStore::SELECT_* constants
112     *
113     * @throws BaseCampaignException
114     */
115    public function assertValid( string $name, int $requiredFields = 0 ): void {
116        switch ( $this->validity ) {
117            case self::CONTENT_INVALID_FORMAT:
118                throw new InvalidFormatException( $name );
119            case self::CONTENT_INVALID_SCHEMA:
120                throw new InvalidSchemaException( $name );
121        }
122
123        if ( $requiredFields & CampaignStore::SELECT_CONTENT && $this->content === null ) {
124            throw new IncompleteRecordException( $name, 'content' );
125        }
126        if ( $requiredFields & CampaignStore::SELECT_TITLE && $this->pageReference === null ) {
127            throw new IncompleteRecordException( $name, 'title' );
128        }
129    }
130
131    /**
132     * Convenience function for retrieving the tracking category name of this campaign.
133     *
134     * @param ConfigBase $config Any MU config, can be RawConfig
135     *
136     * @return string|null name of the category (without NS prefix)
137     * @throws MWException
138     */
139    public function getTrackingCategoryName( ConfigBase $config ): ?string {
140        if ( $this->pageReference === null ) {
141            throw new MWException( "The title of the campaign was not fetched." );
142        }
143
144        $config = $config->getConfigArray();
145        $catTemplate = $config['trackingCategory']['campaign'] ?? null;
146        if ( $catTemplate !== null && strpos( $catTemplate, '$1' ) !== false ) {
147            return str_replace(
148                '$1',
149                $this->pageReference->getDBkey(),
150                $catTemplate
151            );
152        }
153
154        return null;
155    }
156}