jenkins-bot has submitted this change and it was merged.

Change subject: Fetch user translations support for Translation Stash API
......................................................................


Fetch user translations support for Translation Stash API

Change-Id: I8ce1266a17d8b84c142960b7b1685dc6f454336c
---
M api/ApiTranslationStash.php
M resources/js/ext.translate.special.translationstash.js
M resources/js/ext.translate.translationstashstorage.js
3 files changed, 58 insertions(+), 13 deletions(-)

Approvals:
  Nikerabbit: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/api/ApiTranslationStash.php b/api/ApiTranslationStash.php
index cac03c3..45a8007 100644
--- a/api/ApiTranslationStash.php
+++ b/api/ApiTranslationStash.php
@@ -16,11 +16,14 @@
        public function execute() {
                $params = $this->extractRequestParams();
                $action = $params['subaction'];
-
+               $stash = new TranslationStashStorage( wfGetDB( DB_MASTER ) );
                if ( $action === 'add' ) {
-                       // Ugly, but API modules don't have proper dependency 
injection
-                       $stash = new TranslationStashStorage( wfGetDB( 
DB_MASTER ) );
-
+                       if ( !isset( $params['title'] ) ) {
+                               $this->dieUsageMsg( array( 'missingparam', 
'title' ) );
+                       }
+                       if ( !isset( $params['value'] ) ) {
+                               $this->dieUsageMsg( array( 'missingparam', 
'value' ) );
+                       }
                        $translation = new StashedTranslation(
                                $this->getUser(),
                                Title::newFromText( $params['title'] ),
@@ -30,6 +33,17 @@
                        $stash->addTranslation( $translation );
                }
 
+               if ( $action === 'query' ) {
+                       $translations = $stash->getTranslations( 
$this->getUser() );
+                       foreach( $translations as $translation ) {
+                               $translation = array(
+                                       'title' => 
$translation->getTitle()->getPrefixedDBKey(),
+                                       'value' => $translation->getValue(),
+                                       'metadata' => 
$translation->getMetadata(),
+                               );
+                               $output['translations'][] = $translation;
+                       }
+               }
                // If we got this far, nothing has failed
                $output['result'] = 'ok';
                $this->getResult()->addValue( null, $this->getModuleName(), 
$output );
@@ -63,16 +77,14 @@
        public function getAllowedParams() {
                return array(
                        'subaction' => array(
-                               ApiBase::PARAM_TYPE => array( 'add' ),
+                               ApiBase::PARAM_TYPE => array( 'add', 'query' ),
                                ApiBase::PARAM_REQUIRED => true,
                        ),
                        'title' => array(
                                ApiBase::PARAM_TYPE => 'string',
-                               ApiBase::PARAM_REQUIRED => true,
                        ),
                        'value' => array(
                                ApiBase::PARAM_TYPE => 'string',
-                               ApiBase::PARAM_REQUIRED => true,
                        ),
                        'metadata' => array(
                                ApiBase::PARAM_TYPE => 'string',
@@ -105,6 +117,7 @@
                return array(
                        
"api.php?action=translationstash&subaction=add&title=MediaWiki:Jan/fi&" .
                                "value=tammikuu&metadata={}",
+                       "api.php?action=translationstash&subaction=query",
                );
        }
 
diff --git a/resources/js/ext.translate.special.translationstash.js 
b/resources/js/ext.translate.special.translationstash.js
index a597053..a6e7c38 100644
--- a/resources/js/ext.translate.special.translationstash.js
+++ b/resources/js/ext.translate.special.translationstash.js
@@ -8,10 +8,11 @@
 ( function ( $, mw ) {
        'use strict';
 
-       function getMessages( messageGroup, language, offset, limit ) {
-               var api = new mw.Api();
+       var userTranslations = {},
+               translationStashStorage = new 
mw.translate.TranslationStashStorage();
 
-               return api.get( {
+       function getMessages( messageGroup, language, offset, limit ) {
+               var deferred = new mw.Api().get( {
                        action: 'query',
                        list: 'messagecollection',
                        mcgroup: messageGroup,
@@ -22,7 +23,7 @@
                        mcprop: 'definition|properties'
                } );
 
-               // @todo: We need to get translations from the stash api
+               return deferred.promise();
        }
 
        function addMessage( message ) {
@@ -87,7 +88,7 @@
                // Attach translate editor to the message
                $messageWrapper.translateeditor( {
                        message: message,
-                       storage: new mw.translate.TranslationStashStorage()
+                       storage: translationStashStorage
                } );
        }
 
@@ -100,6 +101,10 @@
                                var messages = result.query.messagecollection;
                                $.each( messages, function ( index, message ) {
                                        message.group = messagegroup;
+                                       if ( userTranslations[message.title] ) {
+                                               message.translation = 
userTranslations[message.title].value;
+                                       }
+
                                        addMessage( message );
                                        if ( index === 0 ) {
                                                // Show the editor for the 
first message.
@@ -138,6 +143,17 @@
                                loadMessages();
                        }
                } );
-               loadMessages();
+               // Get the user translations if any(possibly from an early 
attempt)
+               // and new messages to try.
+               translationStashStorage.getUserTranslations()
+                       .done( function( translations ) {
+                               if ( translations.translationstash.translations 
) {
+                                       $.each( 
translations.translationstash.translations,
+                                               function ( index, translation ) 
{
+                                                       
userTranslations[translation.title] = translation;
+                                       } );
+                               }
+                               loadMessages();
+                       } );
        } );
 }( jQuery, mediaWiki ) );
diff --git a/resources/js/ext.translate.translationstashstorage.js 
b/resources/js/ext.translate.translationstashstorage.js
index 28cb268..3e911d5 100644
--- a/resources/js/ext.translate.translationstashstorage.js
+++ b/resources/js/ext.translate.translationstashstorage.js
@@ -29,7 +29,23 @@
                        } );
 
                        return deferred.promise();
+               },
+
+               /**
+                * Get the current users translations
+                * @return {jQuery.Promise}
+                */
+               getUserTranslations: function () {
+                       var deferred = new mw.Api().get( {
+                               action: 'translationstash',
+                               subaction: 'query',
+                               // TODO: use postWithToken once it is ready in 
core.
+                               token: $( '#token' ).val()
+                       } );
+
+                       return deferred.promise();
                }
+
        };
 
        mw.translate.TranslationStashStorage = TranslationStashStorage;

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I8ce1266a17d8b84c142960b7b1685dc6f454336c
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to