Santhosh has uploaded a new change for review.

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

Change subject: WIP: Move save functionality from cxpublish to cxsave API
......................................................................

WIP: Move save functionality from cxpublish to cxsave API

Bug: T125645
Change-Id: Icf5b931235b69b493525710b3906fa6346b24176
---
M api/ApiContentTranslationPublish.php
M api/ApiContentTranslationSave.php
M modules/translation/ext.cx.translation.draft.js
3 files changed, 179 insertions(+), 112 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation 
refs/changes/92/268092/1

diff --git a/api/ApiContentTranslationPublish.php 
b/api/ApiContentTranslationPublish.php
index 5c8b32f..6408d1b 100755
--- a/api/ApiContentTranslationPublish.php
+++ b/api/ApiContentTranslationPublish.php
@@ -22,6 +22,10 @@
         * @var VirtualRESTServiceClient
         */
        protected $restbaseClient;
+       /**
+        * @var Translation
+        */
+       protected $translation;
 
        public function __construct( ApiMain $main, $name ) {
                parent::__construct( $main, $name );
@@ -30,27 +34,7 @@
        }
 
        protected function saveWikitext( $title, $wikitext, $params ) {
-               global $wgContentTranslationHighMTCategory;
-               $categories = array();
-
-               $sourceLink = '[[:' . $params['from']
-                       . ':Special:Redirect/revision/'
-                       . $params['sourcerevision']
-                       . '|' . $params['sourcetitle'] . ']]';
-
-               if ( $params['categories'] ) {
-                       $categories = explode( '|', $params['categories'] );
-               }
-
-               $progress = json_decode( $params['progress'], true );
-               if (
-                       $progress &&
-                       $wgContentTranslationHighMTCategory &&
-                       $this->hasHighMT( $progress )
-               ) {
-                       $categories[] = $wgContentTranslationHighMTCategory;
-               }
-
+               $categories = $this->getCategories( $params );
                if ( count( $categories ) ) {
                        $categoryText = "\n[[" . implode( "]]\n[[", $categories 
) . ']]';
                        // If publishing to User namespace, wrap categories in 
<nowiki>
@@ -89,6 +73,30 @@
                return $api->getResult()->getResultData();
        }
 
+       protected function getCategories( $params ) {
+               global $wgContentTranslationHighMTCategory;
+               $categories = array();
+
+               $sourceLink = '[[:' . $params['from']
+                       . ':Special:Redirect/revision/'
+                       . $params['sourcerevision']
+                       . '|' . $params['sourcetitle'] . ']]';
+
+               if ( $params['categories'] ) {
+                       $categories = explode( '|', $params['categories'] );
+               }
+
+               $progress = json_decode( 
$this->translation->translation['progress'], true );
+               if (
+                       $progress &&
+                       $wgContentTranslationHighMTCategory &&
+                       $this->hasHighMT( $progress )
+               ) {
+                       $categories[] = $wgContentTranslationHighMTCategory;
+               }
+               return $categories;
+       }
+
        public function execute() {
                $params = $this->extractRequestParams();
 
@@ -108,22 +116,43 @@
                        $this->dieUsage( 'html cannot be empty', 'invalidhtml' 
);
                }
 
-               if ( $params['status'] === 'draft' ) {
-                       $this->saveAsDraft();
-               } else {
-                       $this->publish();
-               }
+               $this->publish();
 
        }
 
        public function publish() {
+               global $wgContentTranslationTranslateInTarget;
+
                $params = $this->extractRequestParams();
                $user = $this->getUser();
 
-               $targetTitle = ContentTranslation\SiteMapper::getTargetTitle(
-                       $params['title'],
-                       $user->getName()
+               $this->translation = ContentTranslation\Translation::find(
+                       $params['from'],
+                       $params['to'],
+                       $params['sourcetitle']
                );
+
+               if ( $this->translation === null ) {
+                  // Translation does not exist or belong to another translator
+                  $this->dieUsage( 'Translation not found' );
+               }
+
+               $translator = new ContentTranslation\Translator( $user );
+
+               if ( $wgContentTranslationTranslateInTarget ) {
+                       $targetTitle = 
ContentTranslation\SiteMapper::getTargetTitle(
+                               $params['title'],
+                               $user->getName()
+                       );
+                       $targetURL = ContentTranslation\SiteMapper::getPageURL( 
$params['to'], $targetTitle );
+               } else {
+                       $targetTitle = Title::newFromText( $params['title'] );
+                       if ( !$targetTitle ) {
+                               throw new InvalidArgumentException( "Invalid 
target title given" );
+                       }
+                       $targetURL = $targetTitle->getCanonicalUrl();
+               }
+
                $title = Title::newFromText( $targetTitle );
 
                if ( !$title ) {
@@ -161,11 +190,18 @@
                                'result' => 'success',
                        );
 
+                       $this->translation->translation['status'] = 'published';
+                       
$this->translation->translation['lastUpdatedTranslator'] = 
$translator->getGlobalUserId();
+                       $this->translation->translation['targetURL'] = 
$targetURL;
+
                        if ( isset( $saveresult['edit']['newrevid'] ) ) {
                                $result['newrevid'] = intval( 
$saveresult['edit']['newrevid'] );
-                               $params['targetRevisionId'] = 
$result['newrevid'];
+                               
$this->translation->translation['targetRevisionId'] = $result['newrevid'];
                        }
-                       $this->saveTranslationHistory( $params );
+
+                       // Save the translation history.
+                       $this->translation->save();
+
                        // Notify user about milestones
                        $this->notifyTranslator();
                } else {
@@ -190,7 +226,7 @@
                }
 
                $user = $this->getUser();
-               $translator = new ContentTranslation\Translator( $user );
+               $this->translator = new ContentTranslation\Translator( $user );
                $translationCount = $translator->getTranslationsCount();
 
                switch ( $translationCount ) {
@@ -207,15 +243,6 @@
                                
ContentTranslation\Notification::hundredthTranslation( $user );
                                break;
                }
-       }
-
-       public function saveAsDraft() {
-               $params = $this->extractRequestParams();
-               $this->saveTranslationHistory( $params );
-               $result = array(
-                       'result' => 'success',
-               );
-               $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
        }
 
        /**
@@ -237,65 +264,6 @@
                return $data;
        }
 
-       public function saveTranslationHistory( $params ) {
-               global $wgContentTranslationDatabase, 
$wgContentTranslationTranslateInTarget;
-
-               if ( $wgContentTranslationDatabase === null ) {
-                       // Central CX database not configured.
-                       return;
-               }
-               $user = $this->getUser();
-               $translator = new ContentTranslation\Translator( $user );
-
-               if ( !$wgContentTranslationTranslateInTarget ) {
-                       $targetTitle = Title::newFromText( $params['title'] );
-                       if ( !$targetTitle ) {
-                               throw new InvalidArgumentException( "Invalid 
target title given" );
-                       }
-                       $targetURL = $targetTitle->getCanonicalUrl();
-               } else {
-                       $targetTitle = 
ContentTranslation\SiteMapper::getTargetTitle(
-                               $params['title'],
-                               $this->getUser()->getName()
-                       );
-                       $targetURL = ContentTranslation\SiteMapper::getPageURL( 
$params['to'], $targetTitle );
-               }
-               $translation = array(
-                       'sourceTitle' => $params['sourcetitle'],
-                       'targetTitle' => $params['title'],
-                       'sourceLanguage' => $params['from'],
-                       'sourceRevisionId' => $params['sourcerevision'],
-                       'targetLanguage' => $params['to'],
-                       'sourceURL' => 
ContentTranslation\SiteMapper::getPageURL(
-                               $params['from'], $params['sourcetitle']
-                       ),
-                       'status' => $params['status'],
-                       'progress' => $params['progress'],
-                       // XXX Do not overwrite startedTranslator when we have 
collaborative editing!
-                       'startedTranslator' => $translator->getGlobalUserId(),
-                       'lastUpdatedTranslator' => 
$translator->getGlobalUserId(),
-               );
-               // Save targetURL only when the status is published.
-               if ( $params['status'] === 'published' ) {
-                       $translation['targetURL'] = $targetURL;
-                       $translation['targetRevisionId'] = 
$params['targetRevisionId'];
-               };
-               $cxtranslation = new ContentTranslation\Translation( 
$translation );
-               $cxtranslation->save();
-               $translationId = $cxtranslation->getTranslationId();
-               $translator->addTranslation( $translationId );
-               if ( $params['status'] === 'draft' ) {
-                       // Save the draft
-                       ContentTranslation\Draft::save( $translationId, 
$this->getHtml() );
-               }
-
-               $result = array(
-                       'translationid' => $translationId
-               );
-
-               $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
-       }
-
        public function getAllowedParams() {
                return array(
                        'title' => array(
@@ -307,21 +275,11 @@
                        'from' => array(
                                ApiBase::PARAM_REQUIRED => true,
                        ),
-                       'progress' => array(
-                               ApiBase::PARAM_REQUIRED => true,
-                       ),
                        'to' => array(
                                ApiBase::PARAM_REQUIRED => true,
                        ),
                        'sourcetitle' => array(
                                ApiBase::PARAM_REQUIRED => true,
-                       ),
-                       'sourcerevision' => array(
-                               ApiBase::PARAM_REQUIRED => true,
-                       ),
-                       'status' => array(
-                               ApiBase::PARAM_REQUIRED => true,
-                               ApiBase::PARAM_TYPE => array( 'draft', 
'published' ),
                        ),
                        'categories' => null,
                        /** @todo These should be renamed to something 
all-lowercase and lacking a "wp" prefix */
diff --git a/api/ApiContentTranslationSave.php 
b/api/ApiContentTranslationSave.php
index 2df58a7..d49c986 100644
--- a/api/ApiContentTranslationSave.php
+++ b/api/ApiContentTranslationSave.php
@@ -91,15 +91,124 @@
                return $checker->checkSection( $this->getUser(), $title, $text 
);
        }
 
+       public function saveAsDraft() {
+               $params = $this->extractRequestParams();
+               $this->saveTranslationHistory( $params );
+               $result = array(
+                       'result' => 'success',
+               );
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
+       }
+
+
+       /**
+        * Get the HTML content from request and abstract the compression it 
may have.
+        * @return string The HTML content in the request. Decompressed, if it 
was compressed.
+        */
+       protected function getHtml() {
+               $params = $this->extractRequestParams();
+               $data = $params['html'];
+
+               if ( substr( $params['html'], 0, 11 ) === 'rawdeflate,' ) {
+                       $data = gzinflate( base64_decode( substr( $params[ 
'html' ], 11 ) ) );
+                       // gzinflate returns false on error.
+                       if ( $data === false ) {
+                               throw new InvalidArgumentException( 'Invalid 
HTML content.' );
+                       }
+               }
+
+               return $data;
+       }
+
+       public function saveTranslationHistory( $params ) {
+               global $wgContentTranslationDatabase, 
$wgContentTranslationTranslateInTarget;
+
+               if ( $wgContentTranslationDatabase === null ) {
+                       // Central CX database not configured.
+                       return;
+               }
+               $user = $this->getUser();
+               $translator = new ContentTranslation\Translator( $user );
+
+               if ( !$wgContentTranslationTranslateInTarget ) {
+                       $targetTitle = Title::newFromText( $params['title'] );
+                       if ( !$targetTitle ) {
+                               throw new InvalidArgumentException( "Invalid 
target title given" );
+                       }
+                       $targetURL = $targetTitle->getCanonicalUrl();
+               } else {
+                       $targetTitle = 
ContentTranslation\SiteMapper::getTargetTitle(
+                               $params['title'],
+                               $this->getUser()->getName()
+                       );
+                       $targetURL = ContentTranslation\SiteMapper::getPageURL( 
$params['to'], $targetTitle );
+               }
+               $translation = array(
+                       'sourceTitle' => $params['sourcetitle'],
+                       'targetTitle' => $params['title'],
+                       'sourceLanguage' => $params['from'],
+                       'sourceRevisionId' => $params['sourcerevision'],
+                       'targetLanguage' => $params['to'],
+                       'sourceURL' => 
ContentTranslation\SiteMapper::getPageURL(
+                               $params['from'], $params['sourcetitle']
+                       ),
+                       'status' => 'draft',
+                       'progress' => $params['progress'],
+                       // XXX Do not overwrite startedTranslator when we have 
collaborative editing!
+                       'startedTranslator' => $translator->getGlobalUserId(),
+                       'lastUpdatedTranslator' => 
$translator->getGlobalUserId(),
+               );
+               // Save targetURL only when the status is published.
+               if ( $params['status'] === 'published' ) {
+                       $translation['targetURL'] = $targetURL;
+                       $translation['targetRevisionId'] = 
$params['targetRevisionId'];
+               };
+               $cxtranslation = new ContentTranslation\Translation( 
$translation );
+               $cxtranslation->save();
+               $translationId = $cxtranslation->getTranslationId();
+               $translator->addTranslation( $translationId );
+               if ( $params['status'] === 'draft' ) {
+                       // Save the draft
+                       ContentTranslation\Draft::save( $translationId, 
$this->getHtml() );
+               }
+
+               $result = array(
+                       'translationid' => $translationId
+               );
+
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
+       }
+
        public function getAllowedParams() {
                return array(
                        'translationid' => array(
                                ApiBase::PARAM_TYPE => 'integer',
-                               ApiBase::PARAM_REQUIRED => true,
+                               ApiBase::PARAM_REQUIRED => false,
                        ),
                        'content' => array(
                                ApiBase::PARAM_REQUIRED => true,
                        ),
+                       'title' => array(
+                               ApiBase::PARAM_REQUIRED => true,
+                       ),
+                       'html' => array(
+                               ApiBase::PARAM_REQUIRED => true,
+                       ),
+                       'from' => array(
+                               ApiBase::PARAM_REQUIRED => true,
+                       ),
+                       'progress' => array(
+                               ApiBase::PARAM_REQUIRED => true,
+                       ),
+                       'to' => array(
+                               ApiBase::PARAM_REQUIRED => true,
+                       ),
+                       'sourcetitle' => array(
+                               ApiBase::PARAM_REQUIRED => true,
+                       ),
+                       'sourcerevision' => array(
+                               ApiBase::PARAM_REQUIRED => true,
+                       )
                );
        }
 
diff --git a/modules/translation/ext.cx.translation.draft.js 
b/modules/translation/ext.cx.translation.draft.js
index 35a0b44..3f3e123 100644
--- a/modules/translation/ext.cx.translation.draft.js
+++ b/modules/translation/ext.cx.translation.draft.js
@@ -385,7 +385,7 @@
                now = Date.now();
                apiParams = $.extend( {}, params, {
                        assert: 'user',
-                       action: 'cxpublish'
+                       action: 'cxsave'
                } );
                api.postWithToken( 'edit', apiParams, {
                        timeout: 100 * 1000 // in milliseconds

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icf5b931235b69b493525710b3906fa6346b24176
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to