Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CampaignContent | |
0.00% |
0 / 17 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
validate | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
isValid | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
getJsonData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Upload Campaign Content Model |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @ingroup UploadWizard |
8 | * |
9 | * @author Ori Livneh <ori@wikimedia.org> |
10 | */ |
11 | |
12 | namespace MediaWiki\Extension\UploadWizard; |
13 | |
14 | use FormatJson; |
15 | use JsonContent; |
16 | use JsonSchemaException; |
17 | use MediaWiki\Extension\EventLogging\EventLogging; |
18 | |
19 | /** |
20 | * Represents the configuration of an Upload Campaign |
21 | */ |
22 | class CampaignContent extends JsonContent { |
23 | |
24 | public function __construct( $text ) { |
25 | parent::__construct( $text, 'Campaign' ); |
26 | } |
27 | |
28 | /** |
29 | * Checks user input JSON to make sure that it produces a valid campaign object |
30 | * |
31 | * @throws JsonSchemaException If invalid. |
32 | * @return bool True if valid. |
33 | */ |
34 | public function validate() { |
35 | $campaign = $this->getJsonData(); |
36 | if ( !is_array( $campaign ) ) { |
37 | throw new JsonSchemaException( 'eventlogging-invalid-json' ); |
38 | } |
39 | |
40 | $schema = include __DIR__ . '/CampaignSchema.php'; |
41 | |
42 | // Only validate fields we care about |
43 | $campaignFields = array_keys( $schema['properties'] ); |
44 | |
45 | $fullConfig = Config::getConfig(); |
46 | |
47 | $defaultCampaignConfig = []; |
48 | |
49 | foreach ( $fullConfig as $key => $value ) { |
50 | if ( in_array( $key, $campaignFields ) ) { |
51 | $defaultCampaignConfig[ $key ] = $value; |
52 | } |
53 | } |
54 | |
55 | $mergedConfig = Config::arrayReplaceSanely( $defaultCampaignConfig, $campaign ); |
56 | return EventLogging::schemaValidate( $mergedConfig, $schema ); |
57 | } |
58 | |
59 | /** |
60 | * @return bool Whether content is valid JSON Schema. |
61 | */ |
62 | public function isValid() { |
63 | try { |
64 | return parent::isValid() && $this->validate(); |
65 | } catch ( JsonSchemaException $e ) { |
66 | return false; |
67 | } |
68 | } |
69 | |
70 | /** |
71 | * Deprecated in JsonContent but still useful here because we need to merge the schema's data |
72 | * with a config array |
73 | * |
74 | * @return array|null |
75 | */ |
76 | public function getJsonData() { |
77 | return FormatJson::decode( $this->getText(), true ); |
78 | } |
79 | } |