1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
<?php
/**
* Upload Campaign Content Model
*
* @file
* @ingroup Extensions
* @ingroup UploadWizard
*
* @author Ori Livneh <ori@wikimedia.org>
*/
/**
* Represents the configuration of an Upload Campaign
*/
class CampaignContent extends JsonContent {
function __construct( $text ) {
parent::__construct( $text, 'Campaign' );
}
/**
* Checks user input JSON to make sure that it produces a valid campaign object
*
* @throws JsonSchemaException If invalid.
* @return bool True if valid.
*/
function validate() {
$campaign = $this->getJsonData();
if ( !is_array( $campaign ) ) {
throw new JsonSchemaException( wfMessage( 'eventlogging-invalid-json' )->parse() );
}
$schema = include __DIR__ . '/CampaignSchema.php';
// Only validate fields we care about
$campaignFields = array_keys( $schema['properties'] );
$fullConfig = UploadWizardConfig::getConfig();
$defaultCampaignConfig = [];
foreach ( $fullConfig as $key => $value ) {
if ( in_array( $key, $campaignFields ) ) {
$defaultCampaignConfig[ $key ] = $value;
}
}
$mergedConfig = UploadWizardConfig::array_replace_sanely( $defaultCampaignConfig, $campaign );
return EventLogging::schemaValidate( $mergedConfig, $schema );
}
/**
* @return bool Whether content is valid JSON Schema.
*/
function isValid() {
try {
return parent::isValid() && $this->validate();
} catch ( JsonSchemaException $e ) {
return false;
}
}
/**
* Override getParserOutput, since we require $title to generate our output
* @param Title $title
* @param int|null $revId
* @param ParserOptions|null $options
* @param bool $generateHtml
* @return ParserOutput
*/
function getParserOutput( Title $title,
$revId = null,
ParserOptions $options = null, $generateHtml = true
) {
$po = new ParserOutput();
$campaign = new UploadWizardCampaign( $title, $this->getJsonData() );
if ( $generateHtml ) {
$po->setText( $this->generateHtml( $campaign ) );
}
// Register template usage
// FIXME: should we be registering other stuff??
foreach ( $campaign->getTemplates() as $ns => $templates ) {
foreach ( $templates as $dbk => $ids ) {
$title = Title::makeTitle( $ns, $dbk );
$po->addTemplate( $title, $ids[0], $ids[1] );
}
}
// Add some styles
$po->addModuleStyles( 'ext.uploadWizard.uploadCampaign.display' );
return $po;
}
function generateHtml( $campaign ) {
$formatter = new CampaignPageFormatter( $campaign );
return $formatter->generateReadHtml();
}
/**
* Deprecated in JsonContent but still useful here because we need to merge the schema's data
* with a config array
*
* @return array|null
*/
public function getJsonData() {
return FormatJson::decode( $this->getNativeData(), true );
}
}
|