X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=languages%2FLanguageConverter.php;h=18d1dbc0be66ef18a5cf875a49f26abd39f19250;hb=1f5925f395a332642fa96a6acfe2a3bdab4d9116;hp=7921d551bb531d316f483eb30d915d5fabff99db;hpb=0de8d5299d8adb4968f13cdf2869f25a8d0c2a7b;p=lhc%2Fweb%2Fwiklou.git diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php index 7921d551bb..18d1dbc0be 100644 --- a/languages/LanguageConverter.php +++ b/languages/LanguageConverter.php @@ -72,7 +72,7 @@ class LanguageConverter { $this->mMainLanguageCode = $maincode; $this->mVariants = array_diff( $variants, $wgDisabledVariants ); $this->mVariantFallbacks = $variantfallbacks; - $this->mVariantNames = Language::getLanguageNames(); + $this->mVariantNames = Language::fetchLanguageNames(); $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode ); $defaultflags = array( // 'S' show converted text @@ -117,7 +117,7 @@ class LanguageConverter { * in this case. Right now this is only used by zh. * * @param $variant String: the language code of the variant - * @return String: The code of the fallback language or the + * @return String|array: The code of the fallback language or the * main code if there is no fallback */ public function getVariantFallbacks( $variant ) { @@ -158,7 +158,7 @@ class LanguageConverter { // not memoized (i.e. there return value is not cached) since // new information might appear during processing after this // is first called. - if ( $req ) { + if ( $this->validateVariant( $req ) ) { return $req; } return $this->mMainLanguageCode; @@ -189,7 +189,7 @@ class LanguageConverter { * @param $variant String: the variant to validate * @return Mixed: returns the variant if it is valid, null otherwise */ - protected function validateVariant( $variant = null ) { + public function validateVariant( $variant = null ) { if ( $variant !== null && in_array( $variant, $this->mVariants ) ) { return $variant; } @@ -373,11 +373,11 @@ class LanguageConverter { $sourceBlob .= substr( $text, $startPos, $elementPos - $startPos ) . "\000"; // Advance to the next position - $startPos = $elementPos + strlen( $element ); + $startPos = $elementPos + strlen( $element ); // Translate any alt or title attributes inside the matched element - if ( $element !== '' && preg_match( '/^(<[^>\s]*)\s([^>]*)(.*)$/', $element, - $elementMatches ) ) + if ( $element !== '' && preg_match( '/^(<[^>\s]*)\s([^>]*)(.*)$/', $element, + $elementMatches ) ) { $attrs = Sanitizer::decodeTagAttributes( $elementMatches[2] ); $changed = false; @@ -390,7 +390,7 @@ class LanguageConverter { if ( !strpos( $attr, '://' ) ) { $attr = $this->translate( $attr, $toVariant ); } - + // Remove HTML tags to avoid disrupting the layout $attr = preg_replace( '/<[^>]+>/', '', $attr ); if ( $attr !== $attrs[$attrName] ) { @@ -399,7 +399,7 @@ class LanguageConverter { } } if ( $changed ) { - $element = $elementMatches[1] . Html::expandAttributes( $attrs ) . + $element = $elementMatches[1] . Html::expandAttributes( $attrs ) . $elementMatches[3]; } } @@ -576,7 +576,7 @@ class LanguageConverter { */ public function convertTo( $text, $variant ) { global $wgDisableLangConversion; - if ( $wgDisableLangConversion || $this->guessVariant( $text, $variant ) ) { + if ( $wgDisableLangConversion ) { return $text; } return $this->recursiveConvertTopLevel( $text, $variant ); @@ -595,18 +595,22 @@ class LanguageConverter { $startPos = 0; $out = ''; $length = strlen( $text ); + $shouldConvert = !$this->guessVariant( $text, $variant ); + while ( $startPos < $length ) { $pos = strpos( $text, '-{', $startPos ); if ( $pos === false ) { // No more markup, append final segment - $out .= $this->autoConvert( substr( $text, $startPos ), $variant ); + $fragment = substr( $text, $startPos ); + $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment; return $out; } // Markup found // Append initial segment - $out .= $this->autoConvert( substr( $text, $startPos, $pos - $startPos ), $variant ); + $fragment = substr( $text, $startPos, $pos - $startPos ); + $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment; // Advance position $startPos = $pos; @@ -626,6 +630,7 @@ class LanguageConverter { * @param $startPos int * @param $depth Integer: depth of recursion * + * @throws MWException * @return String: converted text */ protected function recursiveConvertRule( $text, $variant, &$startPos, $depth = 0 ) { @@ -785,7 +790,7 @@ class LanguageConverter { * @return bool true if $text appears to be written in $variant, false if not * * @author Nikola Smolenski - * @since 1.18 + * @since 1.19 */ public function guessVariant($text, $variant) { return false; @@ -796,6 +801,7 @@ class LanguageConverter { * This method must be implemented in derived class. * * @private + * @throws MWException */ function loadDefaultTables() { $name = get_class( $this ); @@ -808,16 +814,18 @@ class LanguageConverter { * @param $fromCache Boolean: load from memcached? Defaults to true. */ function loadTables( $fromCache = true ) { + global $wgLangConvMemc; + if ( $this->mTablesLoaded ) { return; } - global $wgMemc; + wfProfileIn( __METHOD__ ); $this->mTablesLoaded = true; $this->mTables = false; if ( $fromCache ) { wfProfileIn( __METHOD__ . '-cache' ); - $this->mTables = $wgMemc->get( $this->mCacheKey ); + $this->mTables = $wgLangConvMemc->get( $this->mCacheKey ); wfProfileOut( __METHOD__ . '-cache' ); } if ( !$this->mTables @@ -835,7 +843,7 @@ class LanguageConverter { $this->postLoadTables(); $this->mTables[self::CACHE_VERSION_KEY] = true; - $wgMemc->set( $this->mCacheKey, $this->mTables, 43200 ); + $wgLangConvMemc->set( $this->mCacheKey, $this->mTables, 43200 ); wfProfileOut( __METHOD__ . '-recache' ); } wfProfileOut( __METHOD__ ); @@ -889,26 +897,26 @@ class LanguageConverter { return array(); } - if ( strpos( $code, '/' ) === false ) { - $txt = MessageCache::singleton()->get( 'Conversiontable', true, $code ); - if ( $txt === false ) { - # @todo FIXME: This method doesn't seem to be expecting - # this possible outcome... - $txt = '<Conversiontable>'; - } + $parsed[$key] = true; + + if ( $subpage === '' ) { + $txt = MessageCache::singleton()->get( 'conversiontable', true, $code ); } else { - $title = Title::makeTitleSafe( - NS_MEDIAWIKI, - "Conversiontable/$code" - ); + $txt = false; + $title = Title::makeTitleSafe( NS_MEDIAWIKI, $key ); if ( $title && $title->exists() ) { - $article = new Article( $title ); - $txt = $article->getContents(); - } else { - $txt = ''; + $revision = Revision::newFromTitle( $title ); + if ( $revision ) { + $txt = $revision->getRawText(); + } } } + # Nothing to parse if there's no text + if ( $txt === false || $txt === null || $txt === '' ) { + return array(); + } + // get all subpage links of the form // [[MediaWiki:Conversiontable/zh-xx/...|...]] $linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) . @@ -957,7 +965,6 @@ class LanguageConverter { $ret[trim( $m[0] )] = trim( $tt[0] ); } } - $parsed[$key] = true; // recursively parse the subpages if ( $recursive ) { @@ -1016,8 +1023,8 @@ class LanguageConverter { * @param $summary String: edit summary of the edit * @param $isMinor Boolean: was the edit marked as minor? * @param $isWatch Boolean: did the user watch this page or not? - * @param $section Unused - * @param $flags Bitfield + * @param $section + * @param $flags int Bitfield * @param $revision Object: new Revision object or null * @return Boolean: true */ @@ -1383,19 +1390,21 @@ class ConverterRule { if ( isset( $this->mVariantFlags[$variant] ) ) { // then convert to current language $this->mRules = $this->mConverter->autoConvert( $this->mRules, - $variant ); + $variant ); } else { // if current variant no in flags, // then we check its fallback variants. $variantFallbacks = $this->mConverter->getVariantFallbacks( $variant ); - foreach ( $variantFallbacks as $variantFallback ) { - // if current variant's fallback exist in flags - if ( isset( $this->mVariantFlags[$variantFallback] ) ) { - // then convert to fallback language - $this->mRules = - $this->mConverter->autoConvert( $this->mRules, - $variantFallback ); - break; + if( is_array( $variantFallbacks ) ) { + foreach ( $variantFallbacks as $variantFallback ) { + // if current variant's fallback exist in flags + if ( isset( $this->mVariantFlags[$variantFallback] ) ) { + // then convert to fallback language + $this->mRules = + $this->mConverter->autoConvert( $this->mRules, + $variantFallback ); + break; + } } } }