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