Jarry1250 has uploaded a new change for review. https://gerrit.wikimedia.org/r/153044
Change subject: Implement caching for getOnWikiTranslations and tidy up nomenclature ...................................................................... Implement caching for getOnWikiTranslations and tidy up nomenclature Change-Id: I560b83f598b599105b3116cc7ab6b0ad519073a2 --- M SVGFormatReader.php 1 file changed, 45 insertions(+), 25 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/TranslateSvg refs/changes/44/153044/1 diff --git a/SVGFormatReader.php b/SVGFormatReader.php index 5c0e1fb..bbf66c7 100644 --- a/SVGFormatReader.php +++ b/SVGFormatReader.php @@ -31,21 +31,23 @@ protected $started = array(); protected $expanded = array(); - protected $overrides = array(); protected $filteredTextNodes = array(); protected $savedLanguages = array(); - protected $inFileTranslations = null; protected $isTranslationReady = false; + + protected $inProgressTranslations = array(); + protected $inFileTranslations = null; + protected $onWikiTranslations = null; /** * Initialise a new SVGFormatReader from an SVGMessageGroup and an optional array of translation overrides * * @param SVGMessageGroup $group - * @param array $overrides Optional array of translation overrides to be folded in later + * @param array $inProgressTranslations Optional array of translation overrides to be folded in later */ - public function __construct( SVGMessageGroup $group, $overrides = array() ) { + public function __construct( SVGMessageGroup $group, $inProgressTranslations = array() ) { $this->group = $group; - $this->overrides = $overrides; + $this->inProgressTranslations = $inProgressTranslations; $title = Title::makeTitleSafe( NS_FILE, $this->group->getId() ); $file = wfFindFile( $title ); @@ -71,6 +73,7 @@ * Makes $this->svg ready for translation by inserting <switch> tags where they need to be, etc. * Also works as a check on the compatibility of the file since it will return false if it fails. * + * @todo: Find a way of making isTranslationReady a proper check * @return bool False on failure, true on success */ protected function makeTranslationReady() { @@ -246,56 +249,66 @@ $text->parentNode->setAttribute( 'style', $style ); } } + $this->isTranslationReady = true; return true; } /* * Collate and prepare an array of translations from multiple sources: - * in file, on wiki, $this->filteredTextNodes and from $this->overrides. + * in file, on wiki, $this->filteredTextNodes and in-progress. * * return array Array of translations */ - protected function getTranslations() { - $translations = $this->getInFileTranslations(); - $newTranslations = $this->getOnWikiTranslations(); + protected function getPreferredTranslations() { + $inFileTranslations = $this->getInFileTranslations(); + $onWikiTranslations = $this->getOnWikiTranslations(); + $inProgressTranslations = $this->getInProgressTranslations(); // Collapse overrides into new translations - foreach ( $this->overrides as $key => $languages ) { + foreach ( $inProgressTranslations as $key => $languages ) { foreach ( $languages as $language => $translation ) { $language = ( $this->group->getSourceLanguage() === $language ) ? 'fallback' : $language; - $newTranslations[$key][$language] = TranslateSvgUtils::translationToArray( $translation ); + $onWikiTranslations[$key][$language] = TranslateSvgUtils::translationToArray( $translation ); } } // Collapse new translations into old translations - foreach ( $newTranslations as $key => $languages ) { + foreach ( $onWikiTranslations as $key => $languages ) { foreach ( $languages as $language => $translation ) { - $oldItem = isset( $translations[$key][$language] ) ? $translations[$key][$language] : array(); - $translations[$key][$language] = $newTranslations[$key][$language] + $oldItem; + $oldItem = isset( $inFileTranslations[$key][$language] ) ? $inFileTranslations[$key][$language] : array(); + $inFileTranslations[$key][$language] = $onWikiTranslations[$key][$language] + $oldItem; if ( $language !== 'fallback' ) { - $translations[$key][$language]['id'] = $translations[$key]['fallback']['id'] . "-$language"; + $inFileTranslations[$key][$language]['id'] = $inFileTranslations[$key]['fallback']['id'] . "-$language"; } } } // "Unfilter" translations - $translations = array_merge( $translations, $this->filteredTextNodes ); + $inFileTranslations = array_merge( $inFileTranslations, $this->filteredTextNodes ); // Ensure that child tspan translations prompt new <text>s to be created // by duplicating the fallback version. - foreach ( $translations as $languages ) { + foreach ( $inFileTranslations as $languages ) { foreach ( $languages as $language => $translation ) { if ( isset( $languages['fallback']['data-parent'] ) ) { $parent = $languages['fallback']['data-parent']; - $translations[$parent][$language] = $translations[$parent]['fallback']; + $inFileTranslations[$parent][$language] = $inFileTranslations[$parent]['fallback']; if ( $language !== 'fallback' ) { - $translations[$parent][$language]['id'] .= "-$language"; + $inFileTranslations[$parent][$language]['id'] .= "-$language"; } } } } - return $translations; + return $inFileTranslations; + } + + /** + * Get the array of in-progress translations + * @return array + */ + public function getInProgressTranslations() { + return $this->inProgressTranslations; } /* @@ -304,7 +317,7 @@ * @return DOMDocument New SVG file */ public function getSVG() { - $translations = $this->getTranslations(); + $translations = $this->getPreferredTranslations(); $currentLanguages = $this->getSavedLanguages(); $switches = $this->svg->getElementsByTagName( 'switch' ); $number = $switches->length; @@ -399,11 +412,11 @@ /* * Extract translations from the SVG file * - * @param bool $forceUpdate Definitely regenerate the list + * @param bool $forceUpdate Force the regeneration the list (default: false) * @return array Array of translations (indexed by ID, then langcode, then property) */ public function getInFileTranslations( $forceUpdate = false ) { - if ( isset( $this->inFileTranslations ) && !$forceUpdate ) { + if ( $this->inFileTranslations !== null && !$forceUpdate ) { return $this->inFileTranslations; } @@ -483,9 +496,14 @@ /* * Extract translations from on wiki * + * @param bool $forceUpdate Force the regeneration the list (default: false) * @return array Array of translations (indexed by ID, then langcode, then property) */ - public function getOnWikiTranslations() { + public function getOnWikiTranslations( $forceUpdate = false ) { + if( $this->onWikiTranslations !== null && !$forceUpdate ) { + return $this->onWikiTranslations; + } + $onWikiTranslations = array(); $languages = $this->group->getOnWikiLanguages(); @@ -513,7 +531,9 @@ $onWikiTranslations[$key][$language] = $item; } } - return $onWikiTranslations; + + $this->onWikiTranslations = $onWikiTranslations; + return $this->onWikiTranslations; } /* -- To view, visit https://gerrit.wikimedia.org/r/153044 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I560b83f598b599105b3116cc7ab6b0ad519073a2 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/TranslateSvg Gerrit-Branch: master Gerrit-Owner: Jarry1250 <jarry1...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits