Dan-nl has uploaded a new change for review. https://gerrit.wikimedia.org/r/134167
Change subject: Add pretty JSON output to the GWToolset namespace ...................................................................... Add pretty JSON output to the GWToolset namespace Bug: 58507 Change-Id: I209b1f76877e35e9e3b47328787b1f58c779d1e9 --- M GWToolset.php M i18n/en.json A includes/Content.php A includes/ContentHandler.php M includes/Models/Mapping.php M includes/Utils.php 6 files changed, 193 insertions(+), 24 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GWToolset refs/changes/67/134167/1 diff --git a/GWToolset.php b/GWToolset.php index 4a3b457..892aba2 100644 --- a/GWToolset.php +++ b/GWToolset.php @@ -45,15 +45,19 @@ $wgNamespacesWithSubpages[NS_GWTOOLSET] = true; $wgNamespacesWithSubpages[NS_GWTOOLSET_TALK] = true; +// Content Handler +$wgNamespaceContentModels[NS_GWTOOLSET] = 'GWToolset'; +$wgContentHandlers['GWToolset'] = 'GWToolset\ContentHandler'; + // add user permissions $wgGroupPermissions["gwtoolset"]["gwtoolset"] = true; $wgGroupPermissions["gwtoolset"]["upload_by_url"] = true; // add autoloader classes -$wgAutoloadClasses = $wgAutoloadClasses + array( +$wgAutoloadClasses += array( 'GWToolset\Config' => $wgGWToolsetDir . '/includes/Config.php', - 'GWToolset\GWTException' => $wgGWToolsetDir . '/includes/GWTException.php', - 'GWToolset\Utils' => $wgGWToolsetDir . '/includes/Utils.php', + 'GWToolset\Content' => $wgGWToolsetDir . '/includes/Content.php', + 'GWToolset\ContentHandler' => $wgGWToolsetDir . '/includes/ContentHandler.php', 'GWToolset\Adapters\DataAdapterInterface' => $wgGWToolsetDir . '/includes/Adapters/DataAdapterInterface.php', 'GWToolset\Adapters\Php\MappingPhpAdapter' => $wgGWToolsetDir . '/includes/Adapters/Php/MappingPhpAdapter.php', 'GWToolset\Adapters\Php\MediawikiTemplatePhpAdapter' => $wgGWToolsetDir . '/includes/Adapters/Php/MediawikiTemplatePhpAdapter.php', @@ -61,6 +65,7 @@ 'GWToolset\Forms\MetadataDetectForm' => $wgGWToolsetDir . '/includes/Forms/MetadataDetectForm.php', 'GWToolset\Forms\MetadataMappingForm' => $wgGWToolsetDir . '/includes/Forms/MetadataMappingForm.php', 'GWToolset\Forms\PreviewForm' => $wgGWToolsetDir . '/includes/Forms/PreviewForm.php', + 'GWToolset\GWTException' => $wgGWToolsetDir . '/includes/GWTException.php', 'GWToolset\Handlers\Forms\FormHandler' => $wgGWToolsetDir . '/includes/Handlers/Forms/FormHandler.php', 'GWToolset\Handlers\Forms\MetadataDetectHandler' => $wgGWToolsetDir . '/includes/Handlers/Forms/MetadataDetectHandler.php', 'GWToolset\Handlers\Forms\MetadataMappingHandler' => $wgGWToolsetDir . '/includes/Handlers/Forms/MetadataMappingHandler.php', @@ -81,6 +86,7 @@ 'GWToolset\Models\Metadata' => $wgGWToolsetDir . '/includes/Models/Metadata.php', 'GWToolset\Models\ModelInterface' => $wgGWToolsetDir . '/includes/Models/ModelInterface.php', 'GWToolset\SpecialGWToolset' => $wgGWToolsetDir . '/includes/Specials/SpecialGWToolset.php', + 'GWToolset\Utils' => $wgGWToolsetDir . '/includes/Utils.php', 'Php\File' => $wgGWToolsetDir . '/includes/Php/File.php', 'Php\FileException' => $wgGWToolsetDir . '/includes/Php/FileException.php' ); diff --git a/i18n/en.json b/i18n/en.json index 3a6258b..56e1a77 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1,7 +1,7 @@ { "@metadata": { "authors": [ - "dan-nl", + "dan entous", "Brian Wolff", "Siebrand Mazeland", "Shirayuki", @@ -16,7 +16,7 @@ "group-gwtoolset": "GWToolset users", "group-gwtoolset-member": "{{GENDER:$1|GWToolset user}}", "grouppage-gwtoolset": "{{ns:project}}:GWToolset users", - "gwtoolset-back-to-top": "⇪ top of the page", + "gwtoolset-back-to-top": "top of the page", "gwtoolset-batchjob-creation-failure": "Could not create a batch job of type \"$1\".", "gwtoolset-could-not-close-xml": "Could not close the XML reader.", "gwtoolset-could-not-open-xml": "Could not open the XML file for reading.", diff --git a/includes/Content.php b/includes/Content.php new file mode 100644 index 0000000..c0331c2 --- /dev/null +++ b/includes/Content.php @@ -0,0 +1,112 @@ +<?php +/** + * GWToolset + * + * @file + * @ingroup Extensions + * @license GNU General Public License 3.0 http://www.gnu.org/licenses/gpl.html + */ + +namespace GWToolset; +use FormatJson; +use ParserOutput; +use ParserOptions; +use TextContent; +use Title; +use User; + +class Content extends TextContent { + + /** + * @param {string} $text + */ + public function __construct( $text ) { + parent::__construct( $text, 'GWToolset' ); + } + + /** + * Parse the Content object and generate a ParserOutput from the result. + * + * @param {Title} $title + * The page title to use as a context for rendering. + * + * @param {int} $revId + * Optional revision ID being rendered. + * + * @param {ParserOptions} $options + * Any parser options. + * + * @param {bool} $generateHtml + * Whether to generate HTML (default: true). If false, the result of + * calling getText() on the ParserOutput object returned by + * this method is undefined. + * + * + * @return {ParserOutput} + */ + public function getParserOutput( + Title $title, + $revId = null, + ParserOptions $options = null, + $generateHtml = true + ) { + global $wgParser; + + $json = $this->getNativeData(); + $json = FormatJson::decode( $json, true ); + $status = Utils::jsonCheckForError(); + + if ( !$status->isGood() ) { + $json = FormatJson::encode( + array( 'error' => $status->getWikiText() ), + true + ); + } else { + $json = FormatJson::encode( $json, true ); + } + + $wikitext = + '<syntaxhighlight lang="javascript">' . + $json . + '</syntaxhighlight>'; + + $output = $wgParser->parse( + $wikitext, + $title, + $options, + true, + true, + $revId + ); + + return $output; + } + + /** + * Returns a Content object with pre-save transformations applied + * + * @param {Title} $title + * @param {User} $user + * @param {ParserOptions} $parserOptions + * + * @return {GWToolset\Content} + */ + public function preSaveTransform( + Title $title, + User $user, + ParserOptions $parserOptions + ) { + $text = $this->getNativeData(); + $json = FormatJson::decode( $text, true ); + $status = Utils::jsonCheckForError(); + + if ( !$status->isGood() ) { + $json = $text; + } else { + $json = FormatJson::encode( $json, true ); + } + + return new Content( $json ); + } + +} diff --git a/includes/ContentHandler.php b/includes/ContentHandler.php new file mode 100644 index 0000000..2389255 --- /dev/null +++ b/includes/ContentHandler.php @@ -0,0 +1,47 @@ +<?php +/** + * GWToolset + * + * @file + * @ingroup Extensions + * @license GNU General Public License 3.0 http://www.gnu.org/licenses/gpl.html + */ + +namespace GWToolset; +use TextContentHandler; + +class ContentHandler extends TextContentHandler { + + /** + * @param {string} $modelId + * @param {array} $formats + */ + public function __construct( + $modelId = 'GWToolset', + $formats = array( CONTENT_FORMAT_TEXT ) + ) { + parent::__construct( $modelId, $formats ); + } + + /** + * Unserializes a GWToolset\Content object. + * + * @param {string} $text + * @param {null|string} $format + * @return {GWToolset\Content} + */ + public function unserializeContent( $text, $format = null ) { + $this->checkFormat( $format ); + return new Content( $text ); + } + + /** + * Creates an empty GWToolset\Content object. + * + * @return {GWToolset\Content} + */ + public function makeEmptyContent() { + return new Content( '' ); + } + +} diff --git a/includes/Models/Mapping.php b/includes/Models/Mapping.php index 8a4831e..47b765c 100644 --- a/includes/Models/Mapping.php +++ b/includes/Models/Mapping.php @@ -83,11 +83,16 @@ public function getJsonAsArray( array &$options = array() ) { try { $result = json_decode( $this->mapping_json, true ); - Utils::jsonCheckForError(); + $status = Utils::jsonCheckForError(); + + if ( !$status->isGood() ) { + throw new GWTException( $status->getWikiText() ); + } } catch ( GWTException $e ) { - $error_msg = $e->getMessage(); + $error = $e->getMessage(); + if ( isset( $options['Metadata-Mapping-Title'] ) ) { - $error_msg .= ' ' . + $error .= ' ' . Linker::link( $options['Metadata-Mapping-Title'], null, @@ -96,7 +101,7 @@ } throw new GWTException( - array( 'gwtoolset-metadata-mapping-bad' => array( $error_msg ) ) + array( 'gwtoolset-metadata-mapping-bad' => array( $error ) ) ); } diff --git a/includes/Utils.php b/includes/Utils.php index 318e205..eb91c38 100644 --- a/includes/Utils.php +++ b/includes/Utils.php @@ -8,10 +8,11 @@ */ namespace GWToolset; -use Language, - MWException, - Sanitizer, - Title; +use Language; +use MWException; +use Sanitizer; +use Status; +use Title; class Utils { @@ -223,43 +224,41 @@ } /** - * @throws {GWTException} + * @return {Status} */ public static function jsonCheckForError() { - $error_msg = null; + $status = Status::newGood(); switch ( json_last_error() ) { case JSON_ERROR_NONE: break; case JSON_ERROR_DEPTH: - $error_msg = 'gwtoolset-json-error-depth'; + $status = Status::newFatal( 'gwtoolset-json-error-depth' ); break; case JSON_ERROR_STATE_MISMATCH: - $error_msg = 'gwtoolset-json-error-state-mismatch'; + $status = Status::newFatal( 'gwtoolset-json-error-state-mismatch' ); break; case JSON_ERROR_CTRL_CHAR: - $error_msg = 'gwtoolset-json-error-ctrl-char'; + $status = Status::newFatal( 'gwtoolset-json-error-ctrl-char' ); break; case JSON_ERROR_SYNTAX: - $error_msg = 'gwtoolset-json-error-syntax'; + $status = Status::newFatal( 'gwtoolset-json-error-syntax' ); break; case JSON_ERROR_UTF8: - $error_msg = 'gwtoolset-json-error-utf8'; + $status = Status::newFatal( 'gwtoolset-json-error-utf8' ); break; default: - $error_msg = 'gwtoolset-json-error-unknown'; + $status = Status::newFatal( 'gwtoolset-json-error-unknown' ); break; } - if ( !empty( $error_msg ) ) { - throw new GWTException( $error_msg ); - } + return $status; } /** -- To view, visit https://gerrit.wikimedia.org/r/134167 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I209b1f76877e35e9e3b47328787b1f58c779d1e9 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/GWToolset Gerrit-Branch: master Gerrit-Owner: Dan-nl <d_ent...@yahoo.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits