Tpt has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/295709

Change subject: [WIP] Adds action=reformat API action
......................................................................

[WIP] Adds action=reformat API action

Its goal is to allow to do easy conversion between different versions of the 
same content format

Unit tests are still to add

Change-Id: Id34b2c10bbf0cb23643b3b300e3182a4a12c9933
---
M autoload.php
M includes/api/ApiMain.php
A includes/api/ApiReformat.php
3 files changed, 111 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/09/295709/1

diff --git a/autoload.php b/autoload.php
index e729b6f..d69ec4d 100644
--- a/autoload.php
+++ b/autoload.php
@@ -129,6 +129,7 @@
        'ApiQueryWatchlist' => __DIR__ . '/includes/api/ApiQueryWatchlist.php',
        'ApiQueryWatchlistRaw' => __DIR__ . 
'/includes/api/ApiQueryWatchlistRaw.php',
        'ApiRawMessage' => __DIR__ . '/includes/api/ApiMessage.php',
+       'ApiReformat' => __DIR__ . '/includes/api/ApiReformat.php',
        'ApiRemoveAuthenticationData' => __DIR__ . 
'/includes/api/ApiRemoveAuthenticationData.php',
        'ApiResetPassword' => __DIR__ . '/includes/api/ApiResetPassword.php',
        'ApiResult' => __DIR__ . '/includes/api/ApiResult.php',
diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php
index ce9587f..606fadb 100644
--- a/includes/api/ApiMain.php
+++ b/includes/api/ApiMain.php
@@ -71,6 +71,7 @@
                'compare' => 'ApiComparePages',
                'tokens' => 'ApiTokens',
                'checktoken' => 'ApiCheckToken',
+               'reformat' => 'ApiReformat',
 
                // Write modules
                'purge' => 'ApiPurge',
diff --git a/includes/api/ApiReformat.php b/includes/api/ApiReformat.php
new file mode 100644
index 0000000..1204ab2
--- /dev/null
+++ b/includes/api/ApiReformat.php
@@ -0,0 +1,109 @@
+<?php
+
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 1.28
+ */
+
+/**
+ * API interface for conversion between two different formats of the same 
content model
+ *
+ * @ingroup API
+ */
+class ApiReformat extends ApiBase {
+
+       /**
+        * Purges the cache of a page
+        */
+       public function execute() {
+               $params = $this->extractRequestParams();
+               $contentHandler = $this->getContentHandler( 
$params['contentmodel'] );
+               $inputContentFormat = $this->cleanInputContentFormat( 
$params['inputcontentformat'], $contentHandler );
+               $outputContentFormat = $this->cleanOutputContentFormat( 
$params['outputcontentformat'], $contentHandler );
+
+               try {
+                       $content = $contentHandler->unserializeContent( 
$params['serialization'], $inputContentFormat );
+               } catch ( MWContentSerializationException $ex ) {
+                       $this->dieUsage( $ex->getMessage(), 'parseerror' );
+                       return;
+               }
+               $serialization = $contentHandler->serializeContent( $content, 
$outputContentFormat );
+               $apiResult = $this->getResult();
+               $apiResult->addValue( null, 'serialization', $serialization );
+               $apiResult->addValue( null, 'contentmodel', 
$contentHandler->getModelID() );
+               $apiResult->addValue( null, 'contentformat', 
$outputContentFormat );
+       }
+
+       private function getContentHandler( $contentModelId ) {
+               try {
+                       return ContentHandler::getForModelID( $contentModelId );
+               } catch ( MWUnknownContentModelException $e ) {
+                       $this->dieUsage( $e->getMessage(), 'badparams', 400 );
+               }
+       }
+
+       private function cleanInputContentFormat( $format, ContentHandler 
$contentHandler ) {
+               if( $format !== null && !$contentHandler->isSupportedFormat( 
$format ) ) {
+                       $this->dieUsage(
+                               "Input format $format is not supported for 
content model " . $contentHandler->getModelID(), 'badparams', 400
+                       );
+               }
+               return $format;
+       }
+
+       private function cleanOutputContentFormat( $format, ContentHandler 
$contentHandler ) {
+               if ( $format === null ) {
+                       $format = $contentHandler->getDefaultFormat();
+               }
+               if( !$contentHandler->isSupportedFormat( $format ) ) {
+                       $this->dieUsage(
+                               "Output format $format is not supported for 
content model " . $contentHandler->getModelID(), 'badparams', 400
+                       );
+               }
+               return $format;
+       }
+
+       public function getAllowedParams() {
+               return [
+                       'serialization' => [
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ],
+                       'contentmodel' => [
+                               ApiBase::PARAM_TYPE => 
ContentHandler::getContentModels(),
+                               ApiBase::PARAM_REQUIRED => true
+                       ],
+                       'inputcontentformat' => [
+                               ApiBase::PARAM_TYPE => 
ContentHandler::getAllContentFormats()
+                       ],
+                       'outputcontentformat' => [
+                               ApiBase::PARAM_TYPE => 
ContentHandler::getAllContentFormats()
+                       ]
+               ];
+       }
+
+       protected function getExamplesMessages() {
+               return [
+                       
'action=reformat&serialization=foobar&contentmodel=wikitext&inputformat=text/x-wiki&outputformat=text/x-wiki',
+               ];
+       }
+
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/API:Reformat';
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/295709
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id34b2c10bbf0cb23643b3b300e3182a4a12c9933
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Tpt <thoma...@hotmail.fr>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to