Merge "npm: bump grunt-karma to 3.0.2"
[lhc/web/wiklou.git] / languages / LanguageConverter.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Language
20 */
21 use MediaWiki\MediaWikiServices;
22
23 use MediaWiki\Logger\LoggerFactory;
24
25 /**
26 * Base class for language conversion.
27 * @ingroup Language
28 *
29 * @author Zhengzhu Feng <zhengzhu@gmail.com>
30 * @author fdcn <fdcn64@gmail.com>
31 * @author shinjiman <shinjiman@gmail.com>
32 * @author PhiLiP <philip.npc@gmail.com>
33 */
34 class LanguageConverter {
35 /**
36 * languages supporting variants
37 * @since 1.20
38 * @var array
39 */
40 public static $languagesWithVariants = [
41 'en',
42 'crh',
43 'gan',
44 'iu',
45 'kk',
46 'ku',
47 'shi',
48 'sr',
49 'tg',
50 'uz',
51 'zh',
52 ];
53
54 public $mMainLanguageCode;
55
56 /**
57 * @var string[]
58 */
59 public $mVariants;
60 public $mVariantFallbacks;
61 public $mVariantNames;
62 public $mTablesLoaded = false;
63
64 /**
65 * @var ReplacementArray[]
66 * @phan-var array<string,ReplacementArray>
67 */
68 public $mTables;
69
70 // 'bidirectional' 'unidirectional' 'disable' for each variant
71 public $mManualLevel;
72
73 public $mLangObj;
74 public $mFlags;
75 public $mDescCodeSep = ':', $mDescVarSep = ';';
76 public $mUcfirst = false;
77 public $mConvRuleTitle = false;
78 public $mURLVariant;
79 public $mUserVariant;
80 public $mHeaderVariant;
81 public $mMaxDepth = 10;
82 public $mVarSeparatorPattern;
83
84 const CACHE_VERSION_KEY = 'VERSION 7';
85
86 /**
87 * @param Language $langobj
88 * @param string $maincode The main language code of this language
89 * @param string[] $variants The supported variants of this language
90 * @param array $variantfallbacks The fallback language of each variant
91 * @param array $flags Defining the custom strings that maps to the flags
92 * @param array $manualLevel Limit for supported variants
93 */
94 public function __construct( Language $langobj, $maincode, $variants = [],
95 $variantfallbacks = [], $flags = [],
96 $manualLevel = [] ) {
97 global $wgDisabledVariants;
98 $this->mLangObj = $langobj;
99 $this->mMainLanguageCode = $maincode;
100 $this->mVariants = array_diff( $variants, $wgDisabledVariants );
101 $this->mVariantFallbacks = $variantfallbacks;
102 $this->mVariantNames = Language::fetchLanguageNames();
103 $defaultflags = [
104 // 'S' show converted text
105 // '+' add rules for alltext
106 // 'E' the gave flags is error
107 // these flags above are reserved for program
108 'A' => 'A', // add rule for convert code (all text convert)
109 'T' => 'T', // title convert
110 'R' => 'R', // raw content
111 'D' => 'D', // convert description (subclass implement)
112 '-' => '-', // remove convert (not implement)
113 'H' => 'H', // add rule for convert code (but no display in placed code)
114 'N' => 'N', // current variant name
115 ];
116 $this->mFlags = array_merge( $defaultflags, $flags );
117 foreach ( $this->mVariants as $v ) {
118 if ( array_key_exists( $v, $manualLevel ) ) {
119 $this->mManualLevel[$v] = $manualLevel[$v];
120 } else {
121 $this->mManualLevel[$v] = 'bidirectional';
122 }
123 $this->mFlags[$v] = $v;
124 }
125 }
126
127 /**
128 * Get all valid variants.
129 * Call this instead of using $this->mVariants directly.
130 *
131 * @return string[] Contains all valid variants
132 */
133 public function getVariants() {
134 return $this->mVariants;
135 }
136
137 /**
138 * In case some variant is not defined in the markup, we need
139 * to have some fallback. For example, in zh, normally people
140 * will define zh-hans and zh-hant, but less so for zh-sg or zh-hk.
141 * when zh-sg is preferred but not defined, we will pick zh-hans
142 * in this case. Right now this is only used by zh.
143 *
144 * @param string $variant The language code of the variant
145 * @return string|array The code of the fallback language or the
146 * main code if there is no fallback
147 */
148 public function getVariantFallbacks( $variant ) {
149 return $this->mVariantFallbacks[$variant] ?? $this->mMainLanguageCode;
150 }
151
152 /**
153 * Get the title produced by the conversion rule.
154 * @return string The converted title text
155 */
156 public function getConvRuleTitle() {
157 return $this->mConvRuleTitle;
158 }
159
160 /**
161 * Get preferred language variant.
162 * @return string The preferred language code
163 */
164 public function getPreferredVariant() {
165 global $wgDefaultLanguageVariant, $wgUser;
166
167 $req = $this->getURLVariant();
168
169 Hooks::run( 'GetLangPreferredVariant', [ &$req ] );
170
171 if ( $wgUser->isSafeToLoad() && $wgUser->isLoggedIn() && !$req ) {
172 $req = $this->getUserVariant();
173 } elseif ( !$req ) {
174 $req = $this->getHeaderVariant();
175 }
176
177 if ( $wgDefaultLanguageVariant && !$req ) {
178 $req = $this->validateVariant( $wgDefaultLanguageVariant );
179 }
180
181 $req = $this->validateVariant( $req );
182
183 // This function, unlike the other get*Variant functions, is
184 // not memoized (i.e. there return value is not cached) since
185 // new information might appear during processing after this
186 // is first called.
187 if ( $req ) {
188 return $req;
189 }
190 return $this->mMainLanguageCode;
191 }
192
193 /**
194 * Get default variant.
195 * This function would not be affected by user's settings
196 * @return string The default variant code
197 */
198 public function getDefaultVariant() {
199 global $wgDefaultLanguageVariant;
200
201 $req = $this->getURLVariant();
202
203 if ( !$req ) {
204 $req = $this->getHeaderVariant();
205 }
206
207 if ( $wgDefaultLanguageVariant && !$req ) {
208 $req = $this->validateVariant( $wgDefaultLanguageVariant );
209 }
210
211 if ( $req ) {
212 return $req;
213 }
214 return $this->mMainLanguageCode;
215 }
216
217 /**
218 * Validate the variant and return an appropriate strict internal
219 * variant code if one exists. Compare to Language::hasVariant()
220 * which does a strict test.
221 *
222 * @param string|null $variant The variant to validate
223 * @return mixed Returns an equivalent valid variant code if possible,
224 * null otherwise
225 */
226 public function validateVariant( $variant = null ) {
227 if ( $variant === null ) {
228 return null;
229 }
230 // Our internal variants are always lower-case; the variant we
231 // are validating may have mixed case.
232 $variant = LanguageCode::replaceDeprecatedCodes( strtolower( $variant ) );
233 if ( in_array( $variant, $this->mVariants ) ) {
234 return $variant;
235 }
236 // Browsers are supposed to use BCP 47 standard in the
237 // Accept-Language header, but not all of our internal
238 // mediawiki variant codes are BCP 47. Map BCP 47 code
239 // to our internal code.
240 foreach ( $this->mVariants as $v ) {
241 // Case-insensitive match (BCP 47 is mixed case)
242 if ( strtolower( LanguageCode::bcp47( $v ) ) === $variant ) {
243 return $v;
244 }
245 }
246 return null;
247 }
248
249 /**
250 * Get the variant specified in the URL
251 *
252 * @return mixed Variant if one found, null otherwise
253 */
254 public function getURLVariant() {
255 global $wgRequest;
256
257 if ( $this->mURLVariant ) {
258 return $this->mURLVariant;
259 }
260
261 // see if the preference is set in the request
262 $ret = $wgRequest->getText( 'variant' );
263
264 if ( !$ret ) {
265 $ret = $wgRequest->getVal( 'uselang' );
266 }
267
268 $this->mURLVariant = $this->validateVariant( $ret );
269 return $this->mURLVariant;
270 }
271
272 /**
273 * Determine if the user has a variant set.
274 *
275 * @return mixed Variant if one found, null otherwise
276 */
277 protected function getUserVariant() {
278 global $wgUser;
279
280 // memoizing this function wreaks havoc on parserTest.php
281 /*
282 if ( $this->mUserVariant ) {
283 return $this->mUserVariant;
284 }
285 */
286
287 // Get language variant preference from logged in users
288 // Don't call this on stub objects because that causes infinite
289 // recursion during initialisation
290 if ( !$wgUser->isSafeToLoad() ) {
291 return false;
292 }
293 if ( $wgUser->isLoggedIn() ) {
294 if (
295 $this->mMainLanguageCode ==
296 MediaWikiServices::getInstance()->getContentLanguage()->getCode()
297 ) {
298 $ret = $wgUser->getOption( 'variant' );
299 } else {
300 $ret = $wgUser->getOption( 'variant-' . $this->mMainLanguageCode );
301 }
302 } else {
303 // figure out user lang without constructing wgLang to avoid
304 // infinite recursion
305 $ret = $wgUser->getOption( 'language' );
306 }
307
308 $this->mUserVariant = $this->validateVariant( $ret );
309 return $this->mUserVariant;
310 }
311
312 /**
313 * Determine the language variant from the Accept-Language header.
314 *
315 * @return mixed Variant if one found, null otherwise
316 */
317 protected function getHeaderVariant() {
318 global $wgRequest;
319
320 if ( $this->mHeaderVariant ) {
321 return $this->mHeaderVariant;
322 }
323
324 // See if some supported language variant is set in the
325 // HTTP header.
326 $languages = array_keys( $wgRequest->getAcceptLang() );
327 if ( empty( $languages ) ) {
328 return null;
329 }
330
331 $fallbackLanguages = [];
332 foreach ( $languages as $language ) {
333 $this->mHeaderVariant = $this->validateVariant( $language );
334 if ( $this->mHeaderVariant ) {
335 break;
336 }
337
338 // To see if there are fallbacks of current language.
339 // We record these fallback variants, and process
340 // them later.
341 $fallbacks = $this->getVariantFallbacks( $language );
342 if ( is_string( $fallbacks ) && $fallbacks !== $this->mMainLanguageCode ) {
343 $fallbackLanguages[] = $fallbacks;
344 } elseif ( is_array( $fallbacks ) ) {
345 $fallbackLanguages =
346 array_merge( $fallbackLanguages, $fallbacks );
347 }
348 }
349
350 if ( !$this->mHeaderVariant ) {
351 // process fallback languages now
352 $fallback_languages = array_unique( $fallbackLanguages );
353 foreach ( $fallback_languages as $language ) {
354 $this->mHeaderVariant = $this->validateVariant( $language );
355 if ( $this->mHeaderVariant ) {
356 break;
357 }
358 }
359 }
360
361 return $this->mHeaderVariant;
362 }
363
364 /**
365 * Dictionary-based conversion.
366 * This function would not parse the conversion rules.
367 * If you want to parse rules, try to use convert() or
368 * convertTo().
369 *
370 * @param string $text The text to be converted
371 * @param bool|string $toVariant The target language code
372 * @return string The converted text
373 */
374 public function autoConvert( $text, $toVariant = false ) {
375 $this->loadTables();
376
377 if ( !$toVariant ) {
378 $toVariant = $this->getPreferredVariant();
379 if ( !$toVariant ) {
380 return $text;
381 }
382 }
383
384 if ( $this->guessVariant( $text, $toVariant ) ) {
385 return $text;
386 }
387 /* we convert everything except:
388 1. HTML markups (anything between < and >)
389 2. HTML entities
390 3. placeholders created by the parser
391 IMPORTANT: Beware of failure from pcre.backtrack_limit (T124404).
392 Minimize use of backtracking where possible.
393 */
394 $marker = '|' . Parser::MARKER_PREFIX . '[^\x7f]++\x7f';
395
396 // this one is needed when the text is inside an HTML markup
397 $htmlfix = '|<[^>\004]++(?=\004$)|^[^<>]*+>';
398
399 // Optimize for the common case where these tags have
400 // few or no children. Thus try and possesively get as much as
401 // possible, and only engage in backtracking when we hit a '<'.
402
403 // disable convert to variants between <code> tags
404 $codefix = '<code>[^<]*+(?:(?:(?!<\/code>).)[^<]*+)*+<\/code>|';
405 // disable conversion of <script> tags
406 $scriptfix = '<script[^>]*+>[^<]*+(?:(?:(?!<\/script>).)[^<]*+)*+<\/script>|';
407 // disable conversion of <pre> tags
408 $prefix = '<pre[^>]*+>[^<]*+(?:(?:(?!<\/pre>).)[^<]*+)*+<\/pre>|';
409 // The "|.*+)" at the end, is in case we missed some part of html syntax,
410 // we will fail securely (hopefully) by matching the rest of the string.
411 $htmlFullTag = '<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)|';
412
413 $reg = '/' . $codefix . $scriptfix . $prefix . $htmlFullTag .
414 '&[a-zA-Z#][a-z0-9]++;' . $marker . $htmlfix . '|\004$/s';
415 $startPos = 0;
416 $sourceBlob = '';
417 $literalBlob = '';
418
419 // Guard against delimiter nulls in the input
420 // (should never happen: see T159174)
421 $text = str_replace( "\000", '', $text );
422 $text = str_replace( "\004", '', $text );
423
424 $markupMatches = null;
425 $elementMatches = null;
426
427 // We add a marker (\004) at the end of text, to ensure we always match the
428 // entire text (Otherwise, pcre.backtrack_limit might cause silent failure)
429 while ( $startPos < strlen( $text ) ) {
430 if ( preg_match( $reg, $text . "\004", $markupMatches, PREG_OFFSET_CAPTURE, $startPos ) ) {
431 $elementPos = $markupMatches[0][1];
432 $element = $markupMatches[0][0];
433 if ( $element === "\004" ) {
434 // We hit the end.
435 $elementPos = strlen( $text );
436 $element = '';
437 } elseif ( substr( $element, -1 ) === "\004" ) {
438 // This can sometimes happen if we have
439 // unclosed html tags (For example
440 // when converting a title attribute
441 // during a recursive call that contains
442 // a &lt; e.g. <div title="&lt;">.
443 $element = substr( $element, 0, -1 );
444 }
445 } else {
446 // If we hit here, then Language Converter could be tricked
447 // into doing an XSS, so we refuse to translate.
448 // If non-crazy input manages to reach this code path,
449 // we should consider it a bug.
450 $log = LoggerFactory::getInstance( 'languageconverter' );
451 $log->error( "Hit pcre.backtrack_limit in " . __METHOD__
452 . ". Disabling language conversion for this page.",
453 [
454 "method" => __METHOD__,
455 "variant" => $toVariant,
456 "startOfText" => substr( $text, 0, 500 )
457 ]
458 );
459 return $text;
460 }
461 // Queue the part before the markup for translation in a batch
462 $sourceBlob .= substr( $text, $startPos, $elementPos - $startPos ) . "\000";
463
464 // Advance to the next position
465 $startPos = $elementPos + strlen( $element );
466
467 // Translate any alt or title attributes inside the matched element
468 if ( $element !== ''
469 && preg_match( '/^(<[^>\s]*+)\s([^>]*+)(.*+)$/', $element, $elementMatches )
470 ) {
471 // FIXME, this decodes entities, so if you have something
472 // like <div title="foo&lt;bar"> the bar won't get
473 // translated since after entity decoding it looks like
474 // unclosed html and we call this method recursively
475 // on attributes.
476 $attrs = Sanitizer::decodeTagAttributes( $elementMatches[2] );
477 // Ensure self-closing tags stay self-closing.
478 $close = substr( $elementMatches[2], -1 ) === '/' ? ' /' : '';
479 $changed = false;
480 foreach ( [ 'title', 'alt' ] as $attrName ) {
481 if ( !isset( $attrs[$attrName] ) ) {
482 continue;
483 }
484 $attr = $attrs[$attrName];
485 // Don't convert URLs
486 if ( !strpos( $attr, '://' ) ) {
487 $attr = $this->recursiveConvertTopLevel( $attr, $toVariant );
488 }
489
490 if ( $attr !== $attrs[$attrName] ) {
491 $attrs[$attrName] = $attr;
492 $changed = true;
493 }
494 }
495 if ( $changed ) {
496 $element = $elementMatches[1] . Html::expandAttributes( $attrs ) .
497 $close . $elementMatches[3];
498 }
499 }
500 $literalBlob .= $element . "\000";
501 }
502
503 // Do the main translation batch
504 $translatedBlob = $this->translate( $sourceBlob, $toVariant );
505
506 // Put the output back together
507 $translatedIter = StringUtils::explode( "\000", $translatedBlob );
508 $literalIter = StringUtils::explode( "\000", $literalBlob );
509 $output = '';
510 while ( $translatedIter->valid() && $literalIter->valid() ) {
511 $output .= $translatedIter->current();
512 $output .= $literalIter->current();
513 $translatedIter->next();
514 $literalIter->next();
515 }
516
517 return $output;
518 }
519
520 /**
521 * Translate a string to a variant.
522 * Doesn't parse rules or do any of that other stuff, for that use
523 * convert() or convertTo().
524 *
525 * @param string $text Text to convert
526 * @param string $variant Variant language code
527 * @return string Translated text
528 */
529 public function translate( $text, $variant ) {
530 // If $text is empty or only includes spaces, do nothing
531 // Otherwise translate it
532 if ( trim( $text ) ) {
533 $this->loadTables();
534 $text = $this->mTables[$variant]->replace( $text );
535 }
536 return $text;
537 }
538
539 /**
540 * Call translate() to convert text to all valid variants.
541 *
542 * @param string $text The text to be converted
543 * @return array Variant => converted text
544 */
545 public function autoConvertToAllVariants( $text ) {
546 $this->loadTables();
547
548 $ret = [];
549 foreach ( $this->mVariants as $variant ) {
550 $ret[$variant] = $this->translate( $text, $variant );
551 }
552
553 return $ret;
554 }
555
556 /**
557 * Apply manual conversion rules.
558 *
559 * @param ConverterRule $convRule
560 */
561 protected function applyManualConv( $convRule ) {
562 // Use syntax -{T|zh-cn:TitleCN; zh-tw:TitleTw}- to custom
563 // title conversion.
564 // T26072: $mConvRuleTitle was overwritten by other manual
565 // rule(s) not for title, this breaks the title conversion.
566 $newConvRuleTitle = $convRule->getTitle();
567 if ( $newConvRuleTitle ) {
568 // So I add an empty check for getTitle()
569 $this->mConvRuleTitle = $newConvRuleTitle;
570 }
571
572 // merge/remove manual conversion rules to/from global table
573 $convTable = $convRule->getConvTable();
574 $action = $convRule->getRulesAction();
575 foreach ( $convTable as $variant => $pair ) {
576 $v = $this->validateVariant( $variant );
577 if ( !$v ) {
578 continue;
579 }
580
581 if ( $action == 'add' ) {
582 // More efficient than array_merge(), about 2.5 times.
583 foreach ( $pair as $from => $to ) {
584 $this->mTables[$v]->setPair( $from, $to );
585 }
586 } elseif ( $action == 'remove' ) {
587 $this->mTables[$v]->removeArray( $pair );
588 }
589 }
590 }
591
592 /**
593 * Auto convert a Title object to a readable string in the
594 * preferred variant.
595 *
596 * @param Title $title A object of Title
597 * @return string Converted title text
598 */
599 public function convertTitle( $title ) {
600 $variant = $this->getPreferredVariant();
601 $index = $title->getNamespace();
602 if ( $index !== NS_MAIN ) {
603 $text = $this->convertNamespace( $index, $variant ) . ':';
604 } else {
605 $text = '';
606 }
607 $text .= $this->translate( $title->getText(), $variant );
608 return $text;
609 }
610
611 /**
612 * Get the namespace display name in the preferred variant.
613 *
614 * @param int $index Namespace id
615 * @param string|null $variant Variant code or null for preferred variant
616 * @return string Namespace name for display
617 */
618 public function convertNamespace( $index, $variant = null ) {
619 if ( $index === NS_MAIN ) {
620 return '';
621 }
622
623 if ( $variant === null ) {
624 $variant = $this->getPreferredVariant();
625 }
626
627 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
628 $key = $cache->makeKey( 'languageconverter', 'namespace-text', $index, $variant );
629 $nsVariantText = $cache->get( $key );
630 if ( $nsVariantText !== false ) {
631 return $nsVariantText;
632 }
633
634 // First check if a message gives a converted name in the target variant.
635 $nsConvMsg = wfMessage( 'conversion-ns' . $index )->inLanguage( $variant );
636 if ( $nsConvMsg->exists() ) {
637 $nsVariantText = $nsConvMsg->plain();
638 }
639
640 // Then check if a message gives a converted name in content language
641 // which needs extra translation to the target variant.
642 if ( $nsVariantText === false ) {
643 $nsConvMsg = wfMessage( 'conversion-ns' . $index )->inContentLanguage();
644 if ( $nsConvMsg->exists() ) {
645 $nsVariantText = $this->translate( $nsConvMsg->plain(), $variant );
646 }
647 }
648
649 if ( $nsVariantText === false ) {
650 // No message exists, retrieve it from the target variant's namespace names.
651 $langObj = $this->mLangObj->factory( $variant );
652 $nsVariantText = $langObj->getFormattedNsText( $index );
653 }
654
655 $cache->set( $key, $nsVariantText, 60 );
656
657 return $nsVariantText;
658 }
659
660 /**
661 * Convert text to different variants of a language. The automatic
662 * conversion is done in autoConvert(). Here we parse the text
663 * marked with -{}-, which specifies special conversions of the
664 * text that can not be accomplished in autoConvert().
665 *
666 * Syntax of the markup:
667 * -{code1:text1;code2:text2;...}- or
668 * -{flags|code1:text1;code2:text2;...}- or
669 * -{text}- in which case no conversion should take place for text
670 *
671 * @warning Glossary state is maintained between calls. Never feed this
672 * method input that hasn't properly been escaped as it may result in
673 * an XSS in subsequent calls, even if those subsequent calls properly
674 * escape things.
675 * @param string $text Text to be converted, already html escaped.
676 * @return string Converted text (html)
677 */
678 public function convert( $text ) {
679 $variant = $this->getPreferredVariant();
680 return $this->convertTo( $text, $variant );
681 }
682
683 /**
684 * Same as convert() except a extra parameter to custom variant.
685 *
686 * @param string $text Text to be converted, already html escaped
687 * @param-taint $text exec_html
688 * @param string $variant The target variant code
689 * @return string Converted text
690 * @return-taint escaped
691 */
692 public function convertTo( $text, $variant ) {
693 global $wgDisableLangConversion;
694 if ( $wgDisableLangConversion ) {
695 return $text;
696 }
697 // Reset converter state for a new converter run.
698 $this->mConvRuleTitle = false;
699 return $this->recursiveConvertTopLevel( $text, $variant );
700 }
701
702 /**
703 * Recursively convert text on the outside. Allow to use nested
704 * markups to custom rules.
705 *
706 * @param string $text Text to be converted
707 * @param string $variant The target variant code
708 * @param int $depth Depth of recursion
709 * @return string Converted text
710 */
711 protected function recursiveConvertTopLevel( $text, $variant, $depth = 0 ) {
712 $startPos = 0;
713 $out = '';
714 $length = strlen( $text );
715 $shouldConvert = !$this->guessVariant( $text, $variant );
716 $continue = 1;
717
718 $noScript = '<script.*?>.*?<\/script>(*SKIP)(*FAIL)';
719 $noStyle = '<style.*?>.*?<\/style>(*SKIP)(*FAIL)';
720 // phpcs:ignore Generic.Files.LineLength
721 $noHtml = '<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)(*SKIP)(*FAIL)';
722 while ( $startPos < $length && $continue ) {
723 $continue = preg_match(
724 // Only match -{ outside of html.
725 "/$noScript|$noStyle|$noHtml|-\{/",
726 $text,
727 $m,
728 PREG_OFFSET_CAPTURE,
729 $startPos
730 );
731
732 if ( !$continue ) {
733 // No more markup, append final segment
734 $fragment = substr( $text, $startPos );
735 $out .= $shouldConvert ? $this->autoConvert( $fragment, $variant ) : $fragment;
736 return $out;
737 }
738
739 // Offset of the match of the regex pattern.
740 $pos = $m[0][1];
741
742 // Append initial segment
743 $fragment = substr( $text, $startPos, $pos - $startPos );
744 $out .= $shouldConvert ? $this->autoConvert( $fragment, $variant ) : $fragment;
745 // -{ marker found, not in attribute
746 // Advance position up to -{ marker.
747 $startPos = $pos;
748 // Do recursive conversion
749 // Note: This passes $startPos by reference, and advances it.
750 $out .= $this->recursiveConvertRule( $text, $variant, $startPos, $depth + 1 );
751 }
752 return $out;
753 }
754
755 /**
756 * Recursively convert text on the inside.
757 *
758 * @param string $text Text to be converted
759 * @param string $variant The target variant code
760 * @param int &$startPos
761 * @param int $depth Depth of recursion
762 *
763 * @throws MWException
764 * @return string Converted text
765 */
766 protected function recursiveConvertRule( $text, $variant, &$startPos, $depth = 0 ) {
767 // Quick sanity check (no function calls)
768 if ( $text[$startPos] !== '-' || $text[$startPos + 1] !== '{' ) {
769 throw new MWException( __METHOD__ . ': invalid input string' );
770 }
771
772 $startPos += 2;
773 $inner = '';
774 $warningDone = false;
775 $length = strlen( $text );
776
777 while ( $startPos < $length ) {
778 $m = false;
779 preg_match( '/-\{|\}-/', $text, $m, PREG_OFFSET_CAPTURE, $startPos );
780 if ( !$m ) {
781 // Unclosed rule
782 break;
783 }
784
785 $token = $m[0][0];
786 $pos = $m[0][1];
787
788 // Markup found
789 // Append initial segment
790 $inner .= substr( $text, $startPos, $pos - $startPos );
791
792 // Advance position
793 $startPos = $pos;
794
795 switch ( $token ) {
796 case '-{':
797 // Check max depth
798 if ( $depth >= $this->mMaxDepth ) {
799 $inner .= '-{';
800 if ( !$warningDone ) {
801 $inner .= '<span class="error">' .
802 wfMessage( 'language-converter-depth-warning' )
803 ->numParams( $this->mMaxDepth )->inContentLanguage()->text() .
804 '</span>';
805 $warningDone = true;
806 }
807 $startPos += 2;
808 break;
809 }
810 // Recursively parse another rule
811 $inner .= $this->recursiveConvertRule( $text, $variant, $startPos, $depth + 1 );
812 break;
813 case '}-':
814 // Apply the rule
815 $startPos += 2;
816 $rule = new ConverterRule( $inner, $this );
817 $rule->parse( $variant );
818 $this->applyManualConv( $rule );
819 return $rule->getDisplay();
820 default:
821 throw new MWException( __METHOD__ . ': invalid regex match' );
822 }
823 }
824
825 // Unclosed rule
826 if ( $startPos < $length ) {
827 $inner .= substr( $text, $startPos );
828 }
829 $startPos = $length;
830 return '-{' . $this->autoConvert( $inner, $variant );
831 }
832
833 /**
834 * If a language supports multiple variants, it is possible that
835 * non-existing link in one variant actually exists in another variant.
836 * This function tries to find it. See e.g. LanguageZh.php
837 * The input parameters may be modified upon return
838 *
839 * @param string &$link The name of the link
840 * @param Title &$nt The title object of the link
841 * @param bool $ignoreOtherCond To disable other conditions when
842 * we need to transclude a template or update a category's link
843 */
844 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
845 # If the article has already existed, there is no need to
846 # check it again, otherwise it may cause a fault.
847 if ( is_object( $nt ) && $nt->exists() ) {
848 return;
849 }
850
851 global $wgDisableLangConversion, $wgDisableTitleConversion, $wgRequest;
852 $isredir = $wgRequest->getText( 'redirect', 'yes' );
853 $action = $wgRequest->getText( 'action' );
854 if ( $action == 'edit' && $wgRequest->getBool( 'redlink' ) ) {
855 $action = 'view';
856 }
857 $linkconvert = $wgRequest->getText( 'linkconvert', 'yes' );
858 $disableLinkConversion = $wgDisableLangConversion
859 || $wgDisableTitleConversion;
860 $linkBatch = new LinkBatch();
861
862 $ns = NS_MAIN;
863
864 if ( $disableLinkConversion ||
865 ( !$ignoreOtherCond &&
866 ( $isredir == 'no'
867 || $action == 'edit'
868 || $action == 'submit'
869 || $linkconvert == 'no' ) ) ) {
870 return;
871 }
872
873 if ( is_object( $nt ) ) {
874 $ns = $nt->getNamespace();
875 }
876
877 $variants = $this->autoConvertToAllVariants( $link );
878 if ( !$variants ) { // give up
879 return;
880 }
881
882 $titles = [];
883
884 foreach ( $variants as $v ) {
885 if ( $v != $link ) {
886 $varnt = Title::newFromText( $v, $ns );
887 if ( !is_null( $varnt ) ) {
888 $linkBatch->addObj( $varnt );
889 $titles[] = $varnt;
890 }
891 }
892 }
893
894 // fetch all variants in single query
895 $linkBatch->execute();
896
897 foreach ( $titles as $varnt ) {
898 if ( $varnt->getArticleID() > 0 ) {
899 $nt = $varnt;
900 $link = $varnt->getText();
901 break;
902 }
903 }
904 }
905
906 /**
907 * Returns language specific hash options.
908 *
909 * @return string
910 */
911 public function getExtraHashOptions() {
912 $variant = $this->getPreferredVariant();
913
914 return '!' . $variant;
915 }
916
917 /**
918 * Guess if a text is written in a variant. This should be implemented in subclasses.
919 *
920 * @param string $text The text to be checked
921 * @param string $variant Language code of the variant to be checked for
922 * @return bool True if $text appears to be written in $variant, false if not
923 *
924 * @author Nikola Smolenski <smolensk@eunet.rs>
925 * @since 1.19
926 */
927 public function guessVariant( $text, $variant ) {
928 return false;
929 }
930
931 /**
932 * Load default conversion tables.
933 * This method must be implemented in derived class.
934 *
935 * @private
936 * @throws MWException
937 */
938 function loadDefaultTables() {
939 $class = static::class;
940 throw new MWException( "Must implement loadDefaultTables() method in class $class" );
941 }
942
943 /**
944 * Load conversion tables either from the cache or the disk.
945 * @private
946 * @param bool $fromCache Load from memcached? Defaults to true.
947 */
948 function loadTables( $fromCache = true ) {
949 global $wgLanguageConverterCacheType;
950
951 if ( $this->mTablesLoaded ) {
952 return;
953 }
954
955 $this->mTablesLoaded = true;
956 $this->mTables = false;
957 $cache = ObjectCache::getInstance( $wgLanguageConverterCacheType );
958 $cacheKey = $cache->makeKey( 'conversiontables', $this->mMainLanguageCode );
959 if ( $fromCache ) {
960 $this->mTables = $cache->get( $cacheKey );
961 }
962 if ( !$this->mTables || !array_key_exists( self::CACHE_VERSION_KEY, $this->mTables ) ) {
963 // not in cache, or we need a fresh reload.
964 // We will first load the default tables
965 // then update them using things in MediaWiki:Conversiontable/*
966 $this->loadDefaultTables();
967 foreach ( $this->mVariants as $var ) {
968 $cached = $this->parseCachedTable( $var );
969 $this->mTables[$var]->mergeArray( $cached );
970 }
971
972 $this->postLoadTables();
973 $this->mTables[self::CACHE_VERSION_KEY] = true;
974
975 $cache->set( $cacheKey, $this->mTables, 43200 );
976 }
977 }
978
979 /**
980 * Hook for post processing after conversion tables are loaded.
981 */
982 function postLoadTables() {
983 }
984
985 /**
986 * Reload the conversion tables.
987 *
988 * Also used by test suites which need to reset the converter state.
989 *
990 * @private
991 */
992 private function reloadTables() {
993 if ( $this->mTables ) {
994 unset( $this->mTables );
995 }
996
997 $this->mTablesLoaded = false;
998 $this->loadTables( false );
999 }
1000
1001 /**
1002 * Parse the conversion table stored in the cache.
1003 *
1004 * The tables should be in blocks of the following form:
1005 * -{
1006 * word => word ;
1007 * word => word ;
1008 * ...
1009 * }-
1010 *
1011 * To make the tables more manageable, subpages are allowed
1012 * and will be parsed recursively if $recursive == true.
1013 *
1014 * @param string $code Language code
1015 * @param string $subpage Subpage name
1016 * @param bool $recursive Parse subpages recursively? Defaults to true.
1017 *
1018 * @return array
1019 */
1020 function parseCachedTable( $code, $subpage = '', $recursive = true ) {
1021 static $parsed = [];
1022
1023 $key = 'Conversiontable/' . $code;
1024 if ( $subpage ) {
1025 $key .= '/' . $subpage;
1026 }
1027 if ( array_key_exists( $key, $parsed ) ) {
1028 return [];
1029 }
1030
1031 $parsed[$key] = true;
1032
1033 if ( $subpage === '' ) {
1034 $txt = MessageCache::singleton()->getMsgFromNamespace( $key, $code );
1035 } else {
1036 $txt = false;
1037 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $key );
1038 if ( $title && $title->exists() ) {
1039 $revision = Revision::newFromTitle( $title );
1040 if ( $revision ) {
1041 if ( $revision->getContentModel() == CONTENT_MODEL_WIKITEXT ) {
1042 $txt = $revision->getContent( Revision::RAW )->getText();
1043 }
1044
1045 // @todo in the future, use a specialized content model, perhaps based on json!
1046 }
1047 }
1048 }
1049
1050 # Nothing to parse if there's no text
1051 if ( $txt === false || $txt === null || $txt === '' ) {
1052 return [];
1053 }
1054
1055 // get all subpage links of the form
1056 // [[MediaWiki:Conversiontable/zh-xx/...|...]]
1057 $linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) .
1058 ':Conversiontable';
1059 $subs = StringUtils::explode( '[[', $txt );
1060 $sublinks = [];
1061 foreach ( $subs as $sub ) {
1062 $link = explode( ']]', $sub, 2 );
1063 if ( count( $link ) != 2 ) {
1064 continue;
1065 }
1066 $b = explode( '|', $link[0], 2 );
1067 $b = explode( '/', trim( $b[0] ), 3 );
1068 if ( count( $b ) == 3 ) {
1069 $sublink = $b[2];
1070 } else {
1071 $sublink = '';
1072 }
1073
1074 if ( $b[0] == $linkhead && $b[1] == $code ) {
1075 $sublinks[] = $sublink;
1076 }
1077 }
1078
1079 // parse the mappings in this page
1080 $blocks = StringUtils::explode( '-{', $txt );
1081 $ret = [];
1082 $first = true;
1083 foreach ( $blocks as $block ) {
1084 if ( $first ) {
1085 // Skip the part before the first -{
1086 $first = false;
1087 continue;
1088 }
1089 $mappings = explode( '}-', $block, 2 )[0];
1090 $stripped = str_replace( [ "'", '"', '*', '#' ], '', $mappings );
1091 $table = StringUtils::explode( ';', $stripped );
1092 foreach ( $table as $t ) {
1093 $m = explode( '=>', $t, 3 );
1094 if ( count( $m ) != 2 ) {
1095 continue;
1096 }
1097 // trim any trailling comments starting with '//'
1098 $tt = explode( '//', $m[1], 2 );
1099 $ret[trim( $m[0] )] = trim( $tt[0] );
1100 }
1101 }
1102
1103 // recursively parse the subpages
1104 if ( $recursive ) {
1105 foreach ( $sublinks as $link ) {
1106 $s = $this->parseCachedTable( $code, $link, $recursive );
1107 $ret = $s + $ret;
1108 }
1109 }
1110
1111 if ( $this->mUcfirst ) {
1112 foreach ( $ret as $k => $v ) {
1113 $ret[$this->mLangObj->ucfirst( $k )] = $this->mLangObj->ucfirst( $v );
1114 }
1115 }
1116 return $ret;
1117 }
1118
1119 /**
1120 * Enclose a string with the "no conversion" tag. This is used by
1121 * various functions in the Parser.
1122 *
1123 * @param string $text Text to be tagged for no conversion
1124 * @param bool $noParse Unused
1125 * @return string The tagged text
1126 */
1127 public function markNoConversion( $text, $noParse = false ) {
1128 # don't mark if already marked
1129 if ( strpos( $text, '-{' ) || strpos( $text, '}-' ) ) {
1130 return $text;
1131 }
1132
1133 $ret = "-{R|$text}-";
1134 return $ret;
1135 }
1136
1137 /**
1138 * Convert the sorting key for category links. This should make different
1139 * keys that are variants of each other map to the same key.
1140 *
1141 * @param string $key
1142 *
1143 * @return string
1144 */
1145 function convertCategoryKey( $key ) {
1146 return $key;
1147 }
1148
1149 /**
1150 * Refresh the cache of conversion tables when
1151 * MediaWiki:Conversiontable* is updated.
1152 *
1153 * @param Title $titleobj The Title of the page being updated
1154 */
1155 public function updateConversionTable( Title $titleobj ) {
1156 if ( $titleobj->getNamespace() == NS_MEDIAWIKI ) {
1157 $title = $titleobj->getDBkey();
1158 $t = explode( '/', $title, 3 );
1159 $c = count( $t );
1160 if ( $c > 1 && $t[0] == 'Conversiontable' ) {
1161 if ( $this->validateVariant( $t[1] ) ) {
1162 $this->reloadTables();
1163 }
1164 }
1165 }
1166 }
1167
1168 /**
1169 * Get the cached separator pattern for ConverterRule::parseRules()
1170 * @return string
1171 */
1172 function getVarSeparatorPattern() {
1173 if ( is_null( $this->mVarSeparatorPattern ) ) {
1174 // varsep_pattern for preg_split:
1175 // text should be splited by ";" only if a valid variant
1176 // name exist after the markup, for example:
1177 // -{zh-hans:<span style="font-size:120%;">xxx</span>;zh-hant:\
1178 // <span style="font-size:120%;">yyy</span>;}-
1179 // we should split it as:
1180 // [
1181 // [0] => 'zh-hans:<span style="font-size:120%;">xxx</span>'
1182 // [1] => 'zh-hant:<span style="font-size:120%;">yyy</span>'
1183 // [2] => ''
1184 // ]
1185 $expandedVariants = [];
1186 foreach ( $this->mVariants as $variant ) {
1187 $expandedVariants[ $variant ] = 1;
1188 // Accept standard BCP 47 names for variants as well.
1189 $expandedVariants[ LanguageCode::bcp47( $variant ) ] = 1;
1190 }
1191 // Accept old deprecated names for variants
1192 foreach ( LanguageCode::getDeprecatedCodeMapping() as $old => $new ) {
1193 if ( isset( $expandedVariants[ $new ] ) ) {
1194 $expandedVariants[ $old ] = 1;
1195 }
1196 }
1197
1198 $pat = '/;\s*(?=';
1199 foreach ( $expandedVariants as $variant => $ignore ) {
1200 // zh-hans:xxx;zh-hant:yyy
1201 $pat .= $variant . '\s*:|';
1202 // xxx=>zh-hans:yyy; xxx=>zh-hant:zzz
1203 $pat .= '[^;]*?=>\s*' . $variant . '\s*:|';
1204 }
1205 $pat .= '\s*$)/';
1206 $this->mVarSeparatorPattern = $pat;
1207 }
1208 return $this->mVarSeparatorPattern;
1209 }
1210 }