summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/includes/CampaignContent.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/UploadWizard/includes/CampaignContent.php')
-rw-r--r--www/wiki/extensions/UploadWizard/includes/CampaignContent.php112
1 files changed, 112 insertions, 0 deletions
diff --git a/www/wiki/extensions/UploadWizard/includes/CampaignContent.php b/www/wiki/extensions/UploadWizard/includes/CampaignContent.php
new file mode 100644
index 00000000..d2688f5c
--- /dev/null
+++ b/www/wiki/extensions/UploadWizard/includes/CampaignContent.php
@@ -0,0 +1,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 );
+ }
+}