Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
14.77% covered (danger)
14.77%
35 / 237
26.09% covered (danger)
26.09%
6 / 23
CRAP
0.00% covered (danger)
0.00%
0 / 1
Utils
14.77% covered (danger)
14.77%
35 / 237
26.09% covered (danger)
26.09%
6 / 23
2931.04
0.00% covered (danger)
0.00%
0 / 1
 stripPHPNamespace
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isVoidElement
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 cloneArray
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 lastUniChar
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 isUniWord
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 decodeURI
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 decodeURIComponent
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 extractExtBody
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 isValidOffset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 isValidDSR
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
 normalizeNamespaceName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 decodeWtEntities
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 escapeWtEntities
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 escapeWt
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
42
 escapeHtml
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 entityEncodeAll
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 isProtocolValid
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getExtArgInfo
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 parseMediaDimensions
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
90
 validateMediaParam
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 bcp47ToMwCode
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
6
 mwCodeToBcp47
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
182
 isBcp47CodeEqual
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Utils;
5
6use Psr\Log\LoggerInterface;
7use Wikimedia\Bcp47Code\Bcp47Code;
8use Wikimedia\Bcp47Code\Bcp47CodeValue;
9use Wikimedia\Parsoid\Config\Env;
10use Wikimedia\Parsoid\Config\SiteConfig;
11use Wikimedia\Parsoid\Core\DomSourceRange;
12use Wikimedia\Parsoid\Core\Sanitizer;
13use Wikimedia\Parsoid\NodeData\DataMw;
14use Wikimedia\Parsoid\NodeData\DataMwBody;
15use Wikimedia\Parsoid\NodeData\DataMwExtAttribs;
16use Wikimedia\Parsoid\Tokens\Token;
17use Wikimedia\Parsoid\Wikitext\Consts;
18
19/**
20 * This file contains general utilities for token transforms.
21 */
22class Utils {
23    /**
24     * Regular expression fragment for matching wikitext comments.
25     * Meant for inclusion in other regular expressions.
26     */
27    // Maintenance note: this is used in /x regexes so all whitespace and # should be escaped
28    public const COMMENT_REGEXP_FRAGMENT = '<!--(?>[\s\S]*?-->)';
29    /** Regular fragment for matching a wikitext comment */
30    public const COMMENT_REGEXP = '/' . self::COMMENT_REGEXP_FRAGMENT . '/';
31
32    public const COMMENT_OR_WS_REGEXP = '/^(\s|' . self::COMMENT_REGEXP_FRAGMENT . ')*$/D';
33
34    /**
35     * Strip PHP namespace from the fully qualified class name
36     * @param string $className
37     * @return string
38     */
39    public static function stripPHPNamespace( string $className ): string {
40        return preg_replace( '/.*\\\\/', '', $className );
41    }
42
43    /**
44     * Determine if the named tag is void (can not have content).
45     *
46     * @param string $name tag name
47     * @return bool
48     */
49    public static function isVoidElement( string $name ): bool {
50        return isset( Consts::$HTML['VoidTags'][$name] );
51    }
52
53    public static function cloneArray( array $arr ): array {
54        return array_map(
55            static function ( $val ) {
56                if ( is_array( $val ) ) {
57                    return self::cloneArray( $val );
58                } elseif ( is_object( $val ) ) {
59                    return clone $val;
60                } else {
61                    return $val;
62                }
63            },
64            $arr
65        );
66    }
67
68    /**
69     * Extract the last *unicode* character of the string.
70     * This might be more than one byte, if the last character
71     * is non-ASCII.
72     * @param string $str
73     * @param ?int $idx The index *after* the character to extract; defaults
74     *   to the length of $str, which will extract the last character in
75     *   $str.
76     * @return string
77     */
78    public static function lastUniChar( string $str, ?int $idx = null ): string {
79        if ( $idx === null ) {
80            $idx = strlen( $str );
81        } elseif ( $idx <= 0 || $idx > strlen( $str ) ) {
82            return '';
83        }
84        $c = $str[--$idx];
85        while ( ( ord( $c[0] ) & 0xC0 ) === 0x80 ) {
86            $c = $str[--$idx] . $c;
87        }
88        return $c;
89    }
90
91    /**
92     * Return true if the first character in $s is a unicode word character.
93     * @param string $s
94     * @return bool
95     */
96    public static function isUniWord( string $s ): bool {
97        return preg_match( '#^\w#u', $s ) === 1;
98    }
99
100    /**
101     * Percent-decode only valid UTF-8 characters, leaving other encoded bytes alone.
102     *
103     * Distinct from `decodeURIComponent` in that certain escapes are not decoded,
104     * matching the behavior of JavaScript's decodeURI().
105     *
106     * @see https://www.ecma-international.org/ecma-262/6.0/#sec-decodeuri-encodeduri
107     * @param string $s URI to be decoded
108     * @return string
109     */
110    public static function decodeURI( string $s ): string {
111        // Escape the '%' in sequences for the reserved characters, then use decodeURIComponent.
112        $s = preg_replace( '/%(?=2[346bcfBCF]|3[abdfABDF]|40)/', '%25', $s );
113        return self::decodeURIComponent( $s );
114    }
115
116    /**
117     * Percent-decode only valid UTF-8 characters, leaving other encoded bytes alone.
118     *
119     * @param string $s URI to be decoded
120     * @return string
121     */
122    public static function decodeURIComponent( string $s ): string {
123        // Most of the time we should have valid input
124        $ret = rawurldecode( $s );
125        if ( mb_check_encoding( $ret, 'UTF-8' ) ) {
126            return $ret;
127        }
128
129        // Extract each encoded character and decode it individually
130        return preg_replace_callback(
131            // phpcs:ignore Generic.Files.LineLength.TooLong
132            '/%[0-7][0-9A-F]|%[CD][0-9A-F]%[89AB][0-9A-F]|%E[0-9A-F](?:%[89AB][0-9A-F]){2}|%F[0-4](?:%[89AB][0-9A-F]){3}/i',
133            static function ( $match ) {
134                $ret = rawurldecode( $match[0] );
135                return mb_check_encoding( $ret, 'UTF-8' ) ? $ret : $match[0];
136            }, $s
137        );
138    }
139
140    /**
141     * Extract extension source from the token
142     *
143     * @param Token $token token
144     * @return string
145     */
146    public static function extractExtBody( Token $token ): string {
147        $src = $token->getAttributeV( 'source' );
148        $extTagOffsets = $token->dataParsoid->extTagOffsets;
149        '@phan-var \Wikimedia\Parsoid\Core\DomSourceRange $extTagOffsets';
150        return $extTagOffsets->stripTags( $src );
151    }
152
153    /**
154     * Helper function checks numeric values
155     *
156     * @param ?int $n checks parameters for numeric type and value zero or positive
157     * @return bool
158     */
159    private static function isValidOffset( ?int $n ): bool {
160        return $n !== null && $n >= 0;
161    }
162
163    /**
164     * Basic check if a DOM Source Range (DSR) is valid.
165     *
166     * Clarifications about the "basic validity checks":
167     * - Only checks for underflow, not for overflow.
168     * - Does not verify that start <= end
169     * - Does not verify that openWidth + endWidth <= end - start
170     *   (even so, the values might be invalid because of content)
171     * These would be overkill for our purposes. Given how DSR computation
172     * works in thie codebase, the real scenarios we care about are
173     * non-null / non-negative values since that can happen.
174     *
175     * @param ?DomSourceRange $dsr DSR source range values
176     * @param bool $all Also check the widths of the container tag
177     * @return bool
178     */
179    public static function isValidDSR(
180        ?DomSourceRange $dsr, bool $all = false
181    ): bool {
182        return $dsr !== null &&
183            self::isValidOffset( $dsr->start ) &&
184            self::isValidOffset( $dsr->end ) &&
185            ( !$all || (
186                self::isValidOffset( $dsr->openWidth ) &&
187                self::isValidOffset( $dsr->closeWidth )
188              )
189            );
190    }
191
192    /**
193     * Cannonicalizes a namespace name.
194     *
195     * @param string $name Non-normalized namespace name.
196     * @return string
197     */
198    public static function normalizeNamespaceName( string $name ): string {
199        return strtr( mb_strtolower( $name ), ' ', '_' );
200    }
201
202    /**
203     * Decode HTML5 entities in wikitext.
204     *
205     * NOTE that wikitext only allows semicolon-terminated entities, while
206     * HTML allows a number of "legacy" entities to be decoded without
207     * a terminating semicolon.  This function deliberately does not
208     * decode these HTML-only entity forms.
209     *
210     * @param string $text
211     * @return string
212     */
213    public static function decodeWtEntities( string $text ): string {
214        // Note that HTML5 allows semicolon-less entities which
215        // wikitext does not: in wikitext all entities must end in a
216        // semicolon.
217        // By normalizing before decoding, this routine deliberately
218        // does not decode entity references which are invalid in wikitext
219        // (mostly because they decode to invalid codepoints).
220        return Sanitizer::decodeCharReferences(
221            Sanitizer::normalizeCharReferences( $text )
222        );
223    }
224
225    /**
226     * Entity-escape anything that would decode to a valid wikitext entity.
227     *
228     * Note that HTML5 allows certain "semicolon-less" entities, like
229     * `&para`; these aren't allowed in wikitext and won't be escaped
230     * by this function.
231     *
232     * @param string $text
233     * @return string
234     */
235    public static function escapeWtEntities( string $text ): string {
236        // We just want to encode ampersands that precede valid entities.
237        // (And note that semicolon-less entities aren't valid wikitext.)
238        return preg_replace_callback( '/&[#0-9a-zA-Z\x80-\xff]+;/', function ( $match ) {
239            $m = $match[0];
240            $decodedChar = self::decodeWtEntities( $m );
241            if ( $decodedChar !== $m ) {
242                // Escape the ampersand
243                return '&amp;' . substr( $m, 1 );
244            } else {
245                // Not an entity, just return the string
246                return $m;
247            }
248        }, $text );
249    }
250
251    /**
252     * Ensure that the given literal string is safe to parse as wikitext.
253     * See wfEscapeWikiText() in core.
254     */
255    public static function escapeWt( string $input ): string {
256        static $repl = null, $repl2 = null, $repl3 = null, $repl4 = null;
257        if ( $repl === null ) {
258            $repl = [
259                '"' => '&#34;', '&' => '&#38;', "'" => '&#39;', '<' => '&#60;',
260                '=' => '&#61;', '>' => '&#62;', '[' => '&#91;', ']' => '&#93;',
261                '{' => '&#123;', '|' => '&#124;', '}' => '&#125;',
262                ';' => '&#59;', // a token inside language converter brackets
263                '!!' => '&#33;!', // a token inside table context
264                "\n!" => "\n&#33;", "\r!" => "\r&#33;", // a token inside table context
265                "\n#" => "\n&#35;", "\r#" => "\r&#35;",
266                "\n*" => "\n&#42;", "\r*" => "\r&#42;",
267                "\n:" => "\n&#58;", "\r:" => "\r&#58;",
268                "\n " => "\n&#32;", "\r " => "\r&#32;",
269                "\n\n" => "\n&#10;", "\r\n" => "&#13;\n",
270                "\n\r" => "\n&#13;", "\r\r" => "\r&#13;",
271                "\n\t" => "\n&#9;", "\r\t" => "\r&#9;", // "\n\t\n" is treated like "\n\n"
272                "\n----" => "\n&#45;---", "\r----" => "\r&#45;---",
273                '__' => '_&#95;', '://' => '&#58;//',
274                '~~~' => '~~&#126;', // protect from PST, just to be safe(r)
275            ];
276
277            $magicLinks = [ 'ISBN', 'PMID', 'RFC' ];
278            // We have to catch everything "\s" matches in PCRE
279            foreach ( $magicLinks as $magic ) {
280                $repl["$magic "] = "$magic&#32;";
281                $repl["$magic\t"] = "$magic&#9;";
282                $repl["$magic\r"] = "$magic&#13;";
283                $repl["$magic\n"] = "$magic&#10;";
284                $repl["$magic\f"] = "$magic&#12;";
285            }
286            // Additionally escape the following characters at the beginning of the
287            // string, in case they merge to form tokens when spliced into a
288            // string.  Tokens like -{ {{ [[ {| etc are already escaped because
289            // the second character is escaped above, but the following tokens
290            // are handled here: |+ |- __FOO__ ~~~
291            $repl3 = [
292                '+' => '&#43;', '-' => '&#45;', '_' => '&#95;', '~' => '&#126;',
293            ];
294            // Similarly, protect the following characters at the end of the
295            // string, which could turn form the start of `__FOO__` or `~~~~`
296            // A trailing newline could also form the unintended start of a
297            // paragraph break if it is glued to a newline in the following
298            // context.
299            $repl4 = [
300                '_' => '&#95;', '~' => '&#126;',
301                "\n" => "&#10;", "\r" => "&#13;",
302                "\t" => "&#9;", // "\n\t\n" is treated like "\n\n"
303            ];
304
305            // And handle protocols that don't use "://"
306            $urlProtocols = [
307                'bitcoin:', 'geo:', 'magnet:', 'mailto:', 'matrix:', 'news:',
308                'sip:', 'sips:', 'sms:', 'tel:', 'urn:', 'xmpp:',
309            ];
310            $repl2 = [];
311            foreach ( $urlProtocols as $prot ) {
312                $repl2[] = preg_quote( substr( $prot, 0, -1 ), '/' );
313            }
314            $repl2 = '/\b(' . implode( '|', $repl2 ) . '):/i';
315        }
316        // Tell phan that $repl2, $repl3 and $repl4 will also be non-null here
317        '@phan-var string $repl2';
318        '@phan-var string $repl3';
319        '@phan-var string $repl4';
320        // This will also stringify input in case it's not a string
321        $text = substr( strtr( "\n$input", $repl ), 1 );
322        if ( $text === '' ) {
323            return $text;
324        }
325        $first = strtr( $text[0], $repl3 ); // protect first character
326        if ( strlen( $text ) > 1 ) {
327            $text = $first . substr( $text, 1, -1 ) .
328                  strtr( substr( $text, -1 ), $repl4 ); // protect last character
329        } else {
330            // special case for single-character strings
331            $text = strtr( $first, $repl4 ); // protect last character
332        }
333        $text = preg_replace( $repl2, '$1&#58;', $text );
334        return $text;
335    }
336
337    /**
338     * Convert special characters to HTML entities
339     *
340     * @param string $s
341     * @return string
342     */
343    public static function escapeHtml( string $s ): string {
344        // Only encodes five characters: " ' & < >
345        $s = htmlspecialchars( $s, ENT_QUOTES | ENT_HTML5 );
346        $s = str_replace( "\u{0338}", '&#x338;', $s );
347        return $s;
348    }
349
350    /**
351     * Encode all characters as entity references.  This is done to make
352     * characters safe for wikitext (regardless of whether they are
353     * HTML-safe). Typically only called with single-codepoint strings.
354     * @param string $s
355     * @return string
356     */
357    public static function entityEncodeAll( string $s ): string {
358        // This is Unicode aware.
359        static $conventions = [
360            // We always use at least two characters for the hex code
361            '&#x0;' => '&#x00;', '&#x1;' => '&#x01;', '&#x2;' => '&#x02;', '&#x3;' => '&#x03;',
362            '&#x4;' => '&#x04;', '&#x5;' => '&#x05;', '&#x6;' => '&#x06;', '&#x7;' => '&#x07;',
363            '&#x8;' => '&#x08;', '&#x9;' => '&#x09;', '&#xA;' => '&#x0A;', '&#xB;' => '&#x0B;',
364            '&#xC;' => '&#x0C;', '&#xD;' => '&#x0D;', '&#xE;' => '&#x0E;', '&#xF;' => '&#x0F;',
365            // By convention we use &nbsp; where possible
366            '&#xA0;' => '&nbsp;',
367        ];
368
369        return strtr( mb_encode_numericentity(
370            $s, [ 0, 0x10ffff, 0, ~0 ], 'utf-8', true
371        ), $conventions );
372    }
373
374    /**
375     * Determine whether the protocol of a link is potentially valid. Use the
376     * environment's per-wiki config to do so.
377     *
378     * @param mixed $linkTarget
379     * @param Env $env
380     * @return bool
381     */
382    public static function isProtocolValid( $linkTarget, Env $env ): bool {
383        $siteConf = $env->getSiteConfig();
384        if ( is_string( $linkTarget ) ) {
385            return $siteConf->hasValidProtocol( $linkTarget );
386        } else {
387            return true;
388        }
389    }
390
391    /**
392     * Get argument information for an extension tag token.
393     *
394     * @param Token $extToken
395     * @return DataMw
396     */
397    public static function getExtArgInfo( Token $extToken ): DataMw {
398        $name = $extToken->getAttributeV( 'name' );
399        $options = $extToken->getAttributeV( 'options' );
400        $defaultDataMw = new DataMw( [
401            'name' => $name,
402            // Back-compat w/ existing DOM spec output: ensure 'extAttribs'
403            // exists even if there are no attributes.
404            'extAttribs' => new DataMwExtAttribs,
405        ] );
406        foreach ( TokenUtils::kvToHash( $options ) as $name => $value ) {
407            // Explicit cast to string is needed here, since a numeric
408            // attribute name will get converted to 'int' when it is used
409            // as an array key.
410            $defaultDataMw->setExtAttrib( (string)$name, $value );
411        }
412        $extTagOffsets = $extToken->dataParsoid->extTagOffsets;
413        if ( $extTagOffsets->closeWidth !== 0 ) {
414            // If not self-closing...
415            $defaultDataMw->body = new DataMwBody(
416                self::extractExtBody( $extToken ),
417            );
418        }
419        return $defaultDataMw;
420    }
421
422    /**
423     * Parse media dimensions
424     *
425     * @param SiteConfig $siteConfig
426     * @param string $str media dimension string to parse
427     * @param bool $onlyOne If set, returns null if multiple dimenstions are present
428     * @param bool $localized Defaults to false; set to true if the $str
429     *   has already been matched against `img_width` to localize the `px`
430     *   suffix.
431     * @return ?array{x:int,y?:int,bogusPx:bool}
432     */
433    public static function parseMediaDimensions(
434        SiteConfig $siteConfig, string $str, bool $onlyOne = false,
435        bool $localized = false
436    ): ?array {
437        if ( !$localized ) {
438            $getOption = $siteConfig->getMediaPrefixParameterizedAliasMatcher();
439            $bits = $getOption( $str );
440            $normalizedBit0 = $bits ? mb_strtolower( trim( $bits['k'] ) ) : null;
441            if ( $normalizedBit0 === 'img_width' ) {
442                $str = $bits['v'];
443            }
444        }
445        $dimensions = null;
446        // We support a trailing 'px' here for historical reasons
447        // (T15500, T53628, T207032)
448        if ( preg_match( '/^(\d*)(?:x(\d+))?\s*(px\s*)?$/D', $str, $match ) ) {
449            $dimensions = [ 'x' => null, 'y' => null, 'bogusPx' => false ];
450            if ( !empty( $match[1] ) ) {
451                $dimensions['x'] = intval( $match[1], 10 );
452            }
453            if ( !empty( $match[2] ) ) {
454                if ( $onlyOne ) {
455                    return null;
456                }
457                $dimensions['y'] = intval( $match[2], 10 );
458            }
459            if ( !empty( $match[3] ) ) {
460                $dimensions['bogusPx'] = true;
461            }
462        }
463        return $dimensions;
464    }
465
466    /**
467     * Validate media parameters
468     * More generally, this is defined by the media handler in core
469     *
470     * @param ?int $num
471     * @return bool
472     */
473    public static function validateMediaParam( ?int $num ): bool {
474        return $num !== null && $num > 0;
475    }
476
477    /**
478     * Convert BCP-47-compliant language code to MediaWiki-internal code.
479     *
480     * This is a temporary back-compatibility hack; Parsoid should be
481     * using BCP 47 strings or Bcp47Code objects in all its external APIs.
482     * Try to avoid using it, though: there's no guarantee
483     * that this mapping will remain in sync with upstream.
484     *
485     * @param string|Bcp47Code $code BCP-47 language code
486     * @return string MediaWiki-internal language code
487     */
488    public static function bcp47ToMwCode( $code ): string {
489        // This map is dumped from
490        // LanguageCode::NON_STANDARD_LANGUAGE_CODE_MAPPING in core, but
491        // with keys and values swapped and BCP-47 codes lowercased:
492        //
493        //   array_flip(array_map(strtolower,
494        //       LanguageCode::NON_STANDARD_LANGUAGE_CODE_MAPPING))
495        //
496        // Hopefully we will be able to deprecate and remove this from
497        // Parsoid quickly enough that keeping it in sync with upstream
498        // is not an issue.
499        static $MAP = [
500            "cbk" => "cbk-zam",
501            "de-x-formal" => "de-formal",
502            "egl" => "eml",
503            "en-x-rtl" => "en-rtl",
504            "es-x-formal" => "es-formal",
505            "hu-x-formal" => "hu-formal",
506            "jv-x-bms" => "map-bms",
507            "ro-cyrl-md" => "mo",
508            "nrf" => "nrm",
509            "nl-x-informal" => "nl-informal",
510            "nap-x-tara" => "roa-tara",
511            "en-simple" => "simple",
512            "sr-cyrl" => "sr-ec",
513            "sr-latn" => "sr-el",
514            "zh-hans-cn" => "zh-cn",
515            "zh-hans-sg" => "zh-sg",
516            "zh-hans-my" => "zh-my",
517            "zh-hant-tw" => "zh-tw",
518            "zh-hant-hk" => "zh-hk",
519            "zh-hant-mo" => "zh-mo",
520        ];
521        if ( $code instanceof Bcp47Code ) {
522            $code = $code->toBcp47Code();
523        }
524        $code = strtolower( $code ); // All MW-internal codes are lowercase
525        return $MAP[$code] ?? $code;
526    }
527
528    /**
529     * Convert MediaWiki-internal language code to a BCP-47-compliant
530     * language code suitable for including in HTML.
531     *
532     * This is a temporary back-compatibility hack, needed for compatibility
533     * when running in standalone mode with MediaWiki Action APIs which expose
534     * internal language codes.  These APIs should eventually be improved
535     * so that they also expose BCP-47 compliant codes, which can then be
536     * used directly by Parsoid without conversion.  But until that day
537     * comes, this function will paper over the differences.
538     *
539     * Note that MediaWiki-internal Language objects implement Bcp47Code,
540     * so we can transition interfaces which currently take a string code
541     * to pass a Language object instead; that will make this method
542     * effectively a no-op and avoid the issue of upstream sync of the
543     * mapping table.
544     *
545     * @param string|Bcp47Code $code MediaWiki-internal language code or object
546     * @param bool $strict If true, this code will log a deprecation message
547     *  or fail if a MediaWiki-internal language code is passed.
548     * @param ?LoggerInterface $warnLogger A deprecation warning will be
549     *   emitted on $warnLogger if $strict is true and a string-valued
550     *   MediaWiki-internal language code is passed; otherwise an exception
551     *   will be thrown.
552     * @return Bcp47Code BCP-47 language code.
553     * @see LanguageCode::bcp47()
554     */
555    public static function mwCodeToBcp47(
556        $code, bool $strict = false, ?LoggerInterface $warnLogger = null
557    ): Bcp47Code {
558        if ( $code instanceof Bcp47Code ) {
559            return $code;
560        }
561        if ( $strict ) {
562            $msg = "Use of string-valued BCP-47 codes is deprecated.";
563            if ( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_PARSER_TEST' ) ) {
564                // Always throw an error if running tests
565                throw new \Error( $msg );
566            }
567            if ( $warnLogger ) {
568                $warnLogger->warning( $msg );
569            } else {
570                // Strict mode requested but no deprecation logger provided
571                throw new \Error( $msg );
572            }
573        }
574        // This map is dumped from
575        // LanguageCode::getNonstandardLanguageCodeMapping() in core.
576        // Hopefully we will be able to deprecate and remove this method
577        // from Parsoid quickly enough that keeping it in sync with upstream
578        // will not be an issue.
579        static $MAP = [
580            "als" => "gsw",
581            "bat-smg" => "sgs",
582            "be-x-old" => "be-tarask",
583            "fiu-vro" => "vro",
584            "roa-rup" => "rup",
585            "zh-classical" => "lzh",
586            "zh-min-nan" => "nan",
587            "zh-yue" => "yue",
588            "cbk-zam" => "cbk",
589            "de-formal" => "de-x-formal",
590            "eml" => "egl",
591            "en-rtl" => "en-x-rtl",
592            "es-formal" => "es-x-formal",
593            "hu-formal" => "hu-x-formal",
594            "map-bms" => "jv-x-bms",
595            "mo" => "ro-Cyrl-MD",
596            "nrm" => "nrf",
597            "nl-informal" => "nl-x-informal",
598            "roa-tara" => "nap-x-tara",
599            "simple" => "en-simple",
600            "sr-ec" => "sr-Cyrl",
601            "sr-el" => "sr-Latn",
602            "zh-cn" => "zh-Hans-CN",
603            "zh-sg" => "zh-Hans-SG",
604            "zh-my" => "zh-Hans-MY",
605            "zh-tw" => "zh-Hant-TW",
606            "zh-hk" => "zh-Hant-HK",
607            "zh-mo" => "zh-Hant-MO",
608        ];
609        $code = $MAP[$code] ?? $code;
610        // The rest of this code is copied verbatim from LanguageCode::bcp47()
611        // in core.
612        $codeSegment = explode( '-', $code );
613        $codeBCP = [];
614        foreach ( $codeSegment as $segNo => $seg ) {
615            // when previous segment is x, it is a private segment and should be lc
616            if ( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
617                $codeBCP[$segNo] = strtolower( $seg );
618            // ISO 3166 country code
619            } elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
620                $codeBCP[$segNo] = strtoupper( $seg );
621            // ISO 15924 script code
622            } elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
623                $codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
624            // Use lowercase for other cases
625            } else {
626                $codeBCP[$segNo] = strtolower( $seg );
627            }
628        }
629        return new Bcp47CodeValue( implode( '-', $codeBCP ) );
630    }
631
632    /**
633     * BCP 47 codes are case-insensitive, so this helper does a "proper"
634     * comparison of Bcp47Code objects.
635     * @param Bcp47Code $a
636     * @param Bcp47Code $b
637     * @return bool true iff $a and $b represent the same language
638     */
639    public static function isBcp47CodeEqual( Bcp47Code $a, Bcp47Code $b ): bool {
640        return strcasecmp( $a->toBcp47Code(), $b->toBcp47Code() ) === 0;
641    }
642}