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

Change subject: API: Support limit and offset params for suggestion and 
translation queries
......................................................................


API: Support limit and offset params for suggestion and translation queries

Bug: T109556
Change-Id: I1984d4187132c4d7eb58bf19b9a8245271a1b907
---
M api/ApiQueryContentTranslation.php
M api/ApiQueryContentTranslationSuggestions.php
M includes/SuggestionListManager.php
M includes/Translator.php
4 files changed, 92 insertions(+), 6 deletions(-)

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



diff --git a/api/ApiQueryContentTranslation.php 
b/api/ApiQueryContentTranslation.php
index 5801e6a..7e437ab 100644
--- a/api/ApiQueryContentTranslation.php
+++ b/api/ApiQueryContentTranslation.php
@@ -64,7 +64,11 @@
 
                // The main case, no filters
                $translator = new ContentTranslation\Translator( $user );
-               $translations = $translator->getAllTranslations( 
$params['limit'], $params['offset'] );
+               $translations = $translator->getAllTranslations(
+                       $params['limit'],
+                       $params['offset'],
+                       $params['type']
+               );
 
                // We will have extra continue in case the last batch is 
exactly the size of the limit
                $count = count( $translations );
@@ -78,6 +82,15 @@
                        'translations',
                        $translations
                );
+
+               // Simple optimization
+               if ( $params['offset'] === null ) {
+                       $result->addValue(
+                               array( 'query', 'contenttranslation' ),
+                               'languages',
+                               $translator->getLanguages( $params['type'] )
+                       );
+               }
        }
 
        /**
@@ -127,6 +140,10 @@
                                ApiBase::PARAM_DFLT => null,
                                ApiBase::PARAM_TYPE => 'string',
                        ),
+                       'type' => array(
+                               ApiBase::PARAM_DFLT => null,
+                               ApiBase::PARAM_TYPE => array( 'draft', 
'published' ),
+                       ),
                );
                return $allowedParams;
        }
diff --git a/api/ApiQueryContentTranslationSuggestions.php 
b/api/ApiQueryContentTranslationSuggestions.php
index fcfad32..4c56c6f 100644
--- a/api/ApiQueryContentTranslationSuggestions.php
+++ b/api/ApiQueryContentTranslationSuggestions.php
@@ -56,11 +56,18 @@
                        $translator,
                        $params['from'],
                        $params['to'],
-                       $params['limit']
+                       $params['limit'],
+                       $params['offset'],
+                       $params['seed']
                );
 
                $lists = array();
                $suggestions = $data['suggestions'];
+
+               if ( !count( $suggestions ) ) {
+                       $result->addValue( array( 'query', 
$this->getModuleName() ), 'lists', array() );
+                       return;
+               }
 
                // Find the titles to filter out from suggestions.
                $ongoingTranslations = $this->getOngoingTranslations( 
$suggestions );
@@ -89,6 +96,10 @@
                                );
                        }
                }
+
+               if ( count( $suggestions ) ) {
+                       $this->setContinueEnumParameter( 'offset', 
$params['limit'] + $params['offset'] );
+               }
                $result->addValue( array( 'query', $this->getModuleName() ), 
'lists', $lists );
        }
 
@@ -97,7 +108,6 @@
                $sourceLanguage = $params['from'];
                $targetLanguage = $params['to'];
                $ongoingTranslationTitles = array();
-               $titles = array();
                foreach ( $suggestions as $suggestion ) {
                        $titles[] = $suggestion->getTitle()->getPrefixedText();
                }
@@ -183,6 +193,14 @@
                                ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
                        ),
+                       'offset' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_DFLT => null,
+                       ),
+                       'seed' => array(
+                               ApiBase::PARAM_TYPE => 'integer',
+                               ApiBase::PARAM_DFLT => null,
+                       ),
                );
                return $allowedParams;
        }
diff --git a/includes/SuggestionListManager.php 
b/includes/SuggestionListManager.php
index 3101d4f..0cb2fb6 100644
--- a/includes/SuggestionListManager.php
+++ b/includes/SuggestionListManager.php
@@ -118,7 +118,14 @@
                }
        }
 
-       public function getRelevantSuggestions( Translator $translators, $from, 
$to, $limit ) {
+       public function getRelevantSuggestions(
+               Translator $translators,
+               $from,
+               $to,
+               $limit,
+               $offset,
+               $seed
+       ) {
                $dbw = Database::getConnection( DB_MASTER );
 
                $lists = array();
@@ -146,11 +153,17 @@
                                'cxs_target_language' => array( $to, '*' ),
                        );
 
+                       $seed = (int)$seed;
+
                        $options = array(
                                'LIMIT' => $limit,
-                               'ORDER BY' => 'RAND()'
+                               'ORDER BY' => "RAND( $seed )"
                        );
 
+                       if ( $offset ) {
+                               $options['OFFSET'] = $offset;
+                       }
+
                        $res = $dbw->select( 'cx_suggestions', '*', $conds, 
__METHOD__, $options );
 
                        foreach ( $res as $row ) {
diff --git a/includes/Translator.php b/includes/Translator.php
index 6c16d74..f085221 100644
--- a/includes/Translator.php
+++ b/includes/Translator.php
@@ -67,20 +67,29 @@
         * @param string [$offset] Offset condition (timestamp)
         * @return Translation[]
         */
-       public function getAllTranslations( $limit, $offset = null ) {
+       public function getAllTranslations(
+               $limit,
+               $offset = null,
+               $type = null
+       ) {
                // Note: there is no index on translation_last_updated_timestamp
                $dbr = Database::getConnection( DB_SLAVE );
 
                $tables = array( 'cx_translations', 'cx_translators' );
                $fields = '*';
+
                $conds = array(
                        'translator_translation_id = translation_id',
                        'translator_user_id' => $this->getGlobalUserId()
                );
+               if ( $type !== null ) {
+                       $conds['translation_status'] = $type;
+               }
                if ( $offset !== null ) {
                        $ts = $dbr->addQuotes( $dbr->timestamp( $offset ) );
                        $conds[] = "translation_last_updated_timestamp < $ts";
                }
+
                $options = array(
                        'ORDER BY' => 'translation_last_updated_timestamp DESC',
                        'LIMIT' => $limit,
@@ -96,6 +105,35 @@
                return $result;
        }
 
+       public function getLanguages( $type ) {
+               // Note: there is no index on translation_last_updated_timestamp
+               $dbr = Database::getConnection( DB_SLAVE );
+
+               $queries = array();
+               foreach ( array( 'source', 'target' ) as $field ) {
+                       $tables = array( 'cx_translations', 'cx_translators' );
+                       $field = "translation_{$field}_language as code";
+                       $conds = array(
+                               'translator_translation_id = translation_id',
+                               'translator_user_id' => $this->getGlobalUserId()
+                       );
+                       if ( $type !== null ) {
+                               $conds['translation_status'] = $type;
+                       }
+
+                       $queries[] = $dbr->selectSqlText( $tables, $field, 
$conds, __METHOD__ );
+               }
+
+               $res = $dbr->query( $dbr->unionQueries( $queries, false ) );
+
+               $result = array();
+               foreach ( $res as $row ) {
+                       $result[] = $row->code;
+               }
+
+               return $result;
+       }
+
        /**
         * Get the number of published translation by current translator.
         * @return Integer

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

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

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

Reply via email to