Santhosh has uploaded a new change for review.

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

Change subject: Suggestions: Filter out ongoing translations and existing pages
......................................................................

Suggestions: Filter out ongoing translations and existing pages

If the suggested title already exist in target language wiki, remove
it from the suggestions table too.

Bug: T111143
Change-Id: I94d9c20e5db19c07f60b51f262e5db6df541278f
---
M api/ApiQueryContentTranslationSuggestions.php
M includes/SuggestionListManager.php
M includes/Translation.php
3 files changed, 102 insertions(+), 6 deletions(-)


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

diff --git a/api/ApiQueryContentTranslationSuggestions.php 
b/api/ApiQueryContentTranslationSuggestions.php
index 1d066fb..cf32e19 100644
--- a/api/ApiQueryContentTranslationSuggestions.php
+++ b/api/ApiQueryContentTranslationSuggestions.php
@@ -8,7 +8,9 @@
  */
 
 use ContentTranslation\Translator;
+use ContentTranslation\Translation;
 use ContentTranslation\SuggestionListManager;
+use ContentTranslation\SiteMapper;
 
 /**
  * Api module for querying translation suggestions.
@@ -53,6 +55,11 @@
                $data = $manager->getRelevantSuggestions( $translator, 
$params['from'], $params['to'] );
 
                $lists = array();
+               $suggestions = $data['suggestions'];
+               // Filter out all ongoing translations
+               $suggestions = $this->filterSuggestionsByOngoingTranslations( 
$suggestions );
+               // Filter out all existing target titles
+               $suggestions = $this->filterSuggestionsByExistingTitles( 
$suggestions );
                foreach ( $data['lists'] as $list ) {
                        $lists[$list->getId()] = array(
                                'displayName' => $list->getDisplayNameMessage( 
$this->getContext() )->text(),
@@ -60,7 +67,7 @@
                                'type' => $list->getType(),
                                'suggestions' => array(),
                        );
-                       foreach ( $data['suggestions'] as $suggestion ) {
+                       foreach ( $suggestions as $suggestion ) {
                                
$lists[$suggestion->getListId()]['suggestions'][] = array(
                                        'title' => 
$suggestion->getTitle()->getPrefixedText(),
                                        'sourceLanguage' => 
$suggestion->getSourceLanguage(),
@@ -69,10 +76,78 @@
                                );
                        }
                }
-
                $result->addValue( array( 'query', $this->getModuleName() ), 
'lists', $lists );
        }
 
+       public function filterSuggestionsByOngoingTranslations( array 
$suggestions ) {
+               $params = $this->extractRequestParams();
+               $sourceLanguage = $params['from'];
+               $targetLanguage = $params['to'];
+               $existingTranslationTitles = array();
+               foreach ( $suggestions as $suggestion ) {
+                       $titles[] = $suggestion->getTitle()->getPrefixedText();
+               }
+               $translations = Translation::find( $sourceLanguage, 
$targetLanguage, $titles );
+               foreach ( $translations as $translation ) {
+                       $existingTranslationTitles[] = 
$translation['sourceTitle'];
+               }
+               $suggestions = array_filter( $suggestions,
+                       function( $suggestion ) use( $existingTranslationTitles 
) {
+                               return !in_array(
+                                       
$suggestion->getTitle()->getPrefixedText(),
+                                       $existingTranslationTitles
+                               );
+                       }
+               );
+               return $suggestions;
+       }
+
+       public function filterSuggestionsByExistingTitles( array $suggestions ) 
{
+               $params = $this->extractRequestParams();
+               $titles = array();
+               $missingTitles = array();
+               $sourceLanguage = $params['from'];
+               $targetLanguage = $params['to'];
+               $domain = SiteMapper::getDomainCode( $sourceLanguage );
+               $existingTitles = array();
+               foreach ( $suggestions as $suggestion ) {
+                       $titles[] = $suggestion->getTitle()->getPrefixedText();
+               }
+               $params = array(
+                       'action' => 'query',
+                       'format' => 'json',
+                       'titles' => implode( '|', $titles ),
+                       'prop' => 'langlinks',
+                       'lllimit' => 10,
+                       'lllang' => SiteMapper::getDomainCode( $targetLanguage 
),
+                       'redirects' => true
+               );
+               $apiUrl = "https://$domain.wikipedia.org/w/api.php?";;
+               $url = $apiUrl . http_build_query( $params );
+               $json = Http::get( $url );
+               $response = FormatJson::decode( $json );
+
+               if ( !is_object( $response ) ) {
+                       // Something wrong with response. Should we throw 
exception?
+                       return $suggestions;
+               }
+
+               $pages = $response->query->pages;
+               foreach ( $pages as $page ) {
+                       if ( property_exists( $page, 'langlinks' ) ) {
+                               $existingTitles[] = $page->title;
+                       } else {
+                               $missingTitles[] = $page->title;
+                       }
+               }
+               $suggestions = array_filter( $suggestions, function( 
$suggestion ) use( $missingTitles ) {
+                       return in_array( 
$suggestion->getTitle()->getPrefixedText(), $missingTitles );
+               } );
+               // Remove the already existing links from cx_suggestion table
+               SuggestionListManager::removeTitles( $sourceLanguage, 
$existingTitles );
+               return $suggestions;
+       }
+
        public function getAllowedParams() {
                $allowedParams = array(
                        'from' => array(
diff --git a/includes/SuggestionListManager.php 
b/includes/SuggestionListManager.php
index 8fea00a..e80268e 100644
--- a/includes/SuggestionListManager.php
+++ b/includes/SuggestionListManager.php
@@ -50,6 +50,21 @@
                );
        }
 
+       public static function removeTitles( $sourceLanguage, array $titles ) {
+               if ( count($titles) === 0 ) {
+                       return;
+               }
+               $dbw = Database::getConnection( DB_MASTER );
+               $dbw->delete(
+                       'cx_suggestions',
+                       array(
+                               'cxs_title' => $titles,
+                               'cxs_source_language' => $sourceLanguage,
+                       ),
+                       __METHOD__
+               );
+       }
+
        public function getListByName( $name, $owner = 0 ) {
                $dbr = Database::getConnection( DB_MASTER );
                $row = $dbr->selectRow(
diff --git a/includes/Translation.php b/includes/Translation.php
index 0f54508..fb070ce 100644
--- a/includes/Translation.php
+++ b/includes/Translation.php
@@ -82,13 +82,19 @@
                }
        }
 
-       public static function find( $sourceLanguage, $targetLanguage, $title ) 
{
+       /*
+        * @param string $sourceLanguage
+        * @param string $targetLanguage
+        * @param string|array $titles
+        * @return Translation|array Translation
+        */
+       public static function find( $sourceLanguage, $targetLanguage, $titles 
) {
                $dbr = Database::getConnection( DB_SLAVE );
 
                $values = array(
                        'translation_source_language' => $sourceLanguage,
                        'translation_target_language' => $targetLanguage,
-                       'translation_source_title' => $title
+                       'translation_source_title' => $titles
                );
 
                $rows = $dbr->select(
@@ -104,11 +110,11 @@
                        $result[] = Translation::newFromRow( $row );
                }
 
-               if ( count( $result ) > 0 ) {
+               if ( !is_array( $titles ) ) {
                        return $result[0];
                }
 
-               return null;
+               return $result;
        }
 
        public static function delete( $translationId ) {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I94d9c20e5db19c07f60b51f262e5db6df541278f
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