Santhosh has uploaded a new change for review.

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

Change subject: WIP: ApiQueryContentTranslation: Serve the saved translation 
from cxc_corpora table
......................................................................

WIP: ApiQueryContentTranslation: Serve the saved translation from cxc_corpora 
table

TODO: Requires some changes in section restore logic to accommodate the data 
returned
by the API. Now the draft content has sectionId keys.

Bug: T125621
Change-Id: I3d101f84240b2d15fb055237f117e6d062466c03
---
M api/ApiQueryContentTranslation.php
M includes/Draft.php
M includes/Translator.php
3 files changed, 53 insertions(+), 5 deletions(-)


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

diff --git a/api/ApiQueryContentTranslation.php 
b/api/ApiQueryContentTranslation.php
index 7c81557..1caa0a7 100644
--- a/api/ApiQueryContentTranslation.php
+++ b/api/ApiQueryContentTranslation.php
@@ -7,6 +7,12 @@
  * @license GPL-2.0+
  */
 
+use ContentTranslation\Translator;
+use ContentTranslation\Translation;
+use ContentTranslation\Draft;
+use ContentTranslation\CorporaLookup;
+use ContentTranslation\Database;
+
 /**
  * Api module for querying ContentTranslation.
  *
@@ -47,9 +53,10 @@
                }
 
                if ( $params['translationid'] ) {
-                       $translator = new ContentTranslation\Translator( $user 
);
+                       $translator = new Translator( $user );
                        $translation = $translator->getTranslation( 
$params['translationid'] );
                        if ( $translation !== null ) {
+                               $translation->translation['draftContent'] = 
$this->getTranslationContent( $translation );
                                $result->addValue(
                                        array( 'query', 'contenttranslation' ),
                                        'translation',
@@ -63,7 +70,7 @@
                }
 
                // The main case, no filters
-               $translator = new ContentTranslation\Translator( $user );
+               $translator = new Translator( $user );
                $translations = $translator->getAllTranslations(
                        $params['limit'],
                        $params['offset'],
@@ -100,7 +107,7 @@
         */
        public function find( $sourceTitle, $sourceLanguage, $targetLanguage ) {
                $result = $this->getResult();
-               $translation = ContentTranslation\Translation::find(
+               $translation = Translation::find(
                        $sourceLanguage,
                        $targetLanguage,
                        $sourceTitle
@@ -118,6 +125,21 @@
                $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
        }
 
+       protected function getTranslationContent( $translation ) {
+               $content = null;
+               $db = Database::getConnection( DB_SLAVE );
+
+               $lookup = new CorporaLookup( $db );
+               $content = $lookup->getByTranslationId( 
$translation->getTranslationId() );
+               // If no content present in Corpora table, try drafts table
+               if ( $content === null ) {
+                       $draft = Draft::fetch( $translation->getTranslationId() 
);
+                       $content = $draft['draftContent'];
+               }
+
+               return $content;
+       }
+
        public function getAllowedParams() {
                $allowedParams = array(
                        'translationid' => array(
diff --git a/includes/Draft.php b/includes/Draft.php
index c20d772..456c4a5 100644
--- a/includes/Draft.php
+++ b/includes/Draft.php
@@ -36,4 +36,31 @@
                        __METHOD__
                );
        }
+
+       /**
+        * Fetch the draft for the given draftId
+        * @param {int} $draftId
+        * @return array
+        */
+       public static function fetch( $draftId ) {
+               $dbw = Database::getConnection( DB_MASTER );
+               $row = $dbw->selectRow(
+                       'cx_drafts',
+                       '*',
+                       array(
+                               'draft_id' => $draftId,
+                       ),
+                       __METHOD__
+               );
+
+               if ( $row ) {
+                       return array(
+                               'draftIdd' => $row->draft_id,
+                               'draftContent' => $row->draft_content,
+                               'draftTimestamp' => $row->draft_timestamp,
+                       );
+               }
+
+               return null;
+       }
 }
diff --git a/includes/Translator.php b/includes/Translator.php
index c1b85ef..1d8b375 100644
--- a/includes/Translator.php
+++ b/includes/Translator.php
@@ -53,12 +53,11 @@
        public function getTranslation( $translationId ) {
                $dbr = Database::getConnection( DB_SLAVE );
                $row = $dbr->selectRow(
-                       array( 'cx_drafts', 'cx_translators', 'cx_translations' 
),
+                       array( 'cx_translators', 'cx_translations' ),
                        '*',
                        array(
                                 'translator_user_id' => 
$this->getGlobalUserId(),
                                 'translator_translation_id' => $translationId,
-                                'translator_translation_id = draft_id',
                                 'translator_translation_id = translation_id',
                        ),
                        __METHOD__

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3d101f84240b2d15fb055237f117e6d062466c03
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com>

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

Reply via email to