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: T111143 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', + 'lllimit' => $params['limit'], + 'lllang' => SiteMapper::getDomainCode( $targetLanguage ), + 'redirects' => true + ); + $apiUrl = SiteMapper::getApiURL( $sourceLanguage, $params ); + $json = Http::get( $apiUrl ); + $response = FormatJson::decode( $json, true ); + if ( !isset( $response['query'] ) || !isset( $response['query']['pages'] ) ) { + // Something wrong with response. Should we throw exception? + return $existingTitles; + } + + $pages = $response['query']['pages']; + foreach ( $pages as $page ) { + if ( isset( $page['langlinks'] ) ) { + // API returns titles in PrefixedText format + $existingTitles[] = $page['title']; + } + } + + return $existingTitles; + } + + private function filterSuggestions( array $suggestions, array $titlesToFilter ) { + return array_filter( $suggestions, + function( $suggestion ) use( $titlesToFilter ) { + return !in_array( + $suggestion->getTitle()->getPrefixedText(), + $titlesToFilter + ); + } + ); + } + + private function removeInvalidSuggestions( $sourceLanguage, array $existingTitles ) { + DeferredUpdates::addCallableUpdate( function() use ( $sourceLanguage, $existingTitles ) { + // Remove the already existing links from cx_suggestion table + $manager = new SuggestionListManager(); + $manager->removeTitles( $sourceLanguage, $existingTitles ); + } ); } public function getAllowedParams() { @@ -83,6 +175,13 @@ ApiBase::PARAM_TYPE => 'string', ApiBase::PARAM_REQUIRED => true, ), + 'limit' => array( + ApiBase::PARAM_DFLT => 10, + ApiBase::PARAM_TYPE => 'limit', + ApiBase::PARAM_MIN => 1, + ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, + ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 + ), ); return $allowedParams; } diff --git a/includes/SiteMapper.php b/includes/SiteMapper.php index 0ed3801..455d54b 100644 --- a/includes/SiteMapper.php +++ b/includes/SiteMapper.php @@ -18,6 +18,19 @@ return $language; } + /** + * Get the API URL constructed from the domain template of sites + */ + public static function getApiURL( $language, $params = null ) { + global $wgContentTranslationSiteTemplates; + + $domain = self::getDomainCode( $language ); + // $wgContentTranslationSiteTemplates['api'] is protocol relative path + $url = 'https:' . str_replace( '$1', $domain, $wgContentTranslationSiteTemplates['api'] ); + $url = wfAppendQuery( $url, $params ); + + return $url; + } /** * Get the page URL constructed from the domain template of sites diff --git a/includes/SuggestionListManager.php b/includes/SuggestionListManager.php index 8fea00a..3101d4f 100644 --- a/includes/SuggestionListManager.php +++ b/includes/SuggestionListManager.php @@ -50,6 +50,22 @@ ); } + public 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( @@ -102,7 +118,7 @@ } } - public function getRelevantSuggestions( Translator $translators, $from, $to ) { + public function getRelevantSuggestions( Translator $translators, $from, $to, $limit ) { $dbw = Database::getConnection( DB_MASTER ); $lists = array(); @@ -131,7 +147,7 @@ ); $options = array( - 'LIMIT' => '10', + 'LIMIT' => $limit, 'ORDER BY' => 'RAND()' ); diff --git a/includes/Translation.php b/includes/Translation.php index 84100b1..43ef7d6 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|string[] $titles + * @return Translation|Translation[]|null 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 ) { - return $result[0]; + if ( !is_array( $titles ) ) { + return isset( $result[0] ) ? $result[0]: null; } - 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: merged Gerrit-Change-Id: I94d9c20e5db19c07f60b51f262e5db6df541278f Gerrit-PatchSet: 9 Gerrit-Project: mediawiki/extensions/ContentTranslation Gerrit-Branch: master Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com> Gerrit-Reviewer: Nikerabbit <niklas.laxst...@gmail.com> Gerrit-Reviewer: Santhosh <santhosh.thottin...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits