[MediaWiki-commits] [Gerrit] Suggestions: Filter out ongoing translations and existing pages - change (mediawiki...ContentTranslation)
jenkins-bot has submitted this change and it was merged. 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: T43 Change-Id: I94d9c20e5db19c07f60b51f262e5db6df541278f --- M api/ApiQueryContentTranslationSuggestions.php M includes/SiteMapper.php M includes/SuggestionListManager.php M includes/Translation.php 4 files changed, 144 insertions(+), 10 deletions(-) Approvals: Nikerabbit: Looks good to me, approved jenkins-bot: Verified diff --git a/api/ApiQueryContentTranslationSuggestions.php b/api/ApiQueryContentTranslationSuggestions.php index 1d066fb..a803a3c 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. @@ -50,9 +52,27 @@ $translator = new Translator( $user ); $manager = new SuggestionListManager(); - $data = $manager->getRelevantSuggestions( $translator, $params['from'], $params['to'] ); + $data = $manager->getRelevantSuggestions( + $translator, + $params['from'], + $params['to'], + $params['limit'] + ); $lists = array(); + $suggestions = $data['suggestions']; + + // Find the titles to filter out from suggestions. + $ongoingTranslations = $this->getOngoingTranslations( $suggestions ); + $existingTitles = $this->getExistingTitles( $suggestions ); + $suggestions = $this->filterSuggestions( + $suggestions, + array_merge( $existingTitles, $ongoingTranslations ) + ); + + // Remove the Suggestions that are no longer valid. + $this->removeInvalidSuggestions( $params['from'], $existingTitles ); + foreach ( $data['lists'] as $list ) { $lists[$list->getId()] = array( 'displayName' => $list->getDisplayNameMessage( $this->getContext() )->text(), @@ -60,7 +80,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,8 +89,80 @@ ); } } - $result->addValue( array( 'query', $this->getModuleName() ), 'lists', $lists ); + } + + private function getOngoingTranslations( array $suggestions ) { + $params = $this->extractRequestParams(); + $sourceLanguage = $params['from']; + $targetLanguage = $params['to']; + $ongoingTranslationTitles = array(); + foreach ( $suggestions as $suggestion ) { + $titles[] = $suggestion->getTitle()->getPrefixedText(); + } + $translations = Translation::find( $sourceLanguage, $targetLanguage, $titles ); + foreach ( $translations as $translation ) { + // $translation['sourceTitle'] is prefixed title with spaces + $ongoingTranslationTitles[] = $translation->translation['sourceTitle']; + } + return $ongoingTranslationTitles; + } + + private function getExistingTitles( array $suggestions ) { + $params = $this->extractRequestParams(); + $titles = 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', +
[MediaWiki-commits] [Gerrit] Suggestions: Filter out ongoing translations and existing pages - change (mediawiki...ContentTranslation)
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: T43 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 =