Fix docs for OutputPage::addLanguageLinks and OutputPage::setLanguageLinks
[lhc/web/wiklou.git] / languages / Language.php
index cb3b4b8..2e4ef89 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 /**
  * Internationalisation code.
+ * See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more information.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -136,6 +137,12 @@ class Language {
         */
        static private $fallbackLanguageCache = [];
 
+       /**
+        * Cache for grammar rules data
+        * @var MapCacheLRU|null
+        */
+       static private $grammarTransformations;
+
        /**
         * Cache for language names
         * @var HashBagOStuff|null
@@ -290,7 +297,7 @@ class Language {
                # Since these are limited, this is safe even later changes to the registry --
                # the only oddity is that it might change the type of the tag, and thus
                # the results from the capturing groups.
-               # http://www.iana.org/assignments/language-subtag-registry
+               # https://www.iana.org/assignments/language-subtag-registry
 
                $grandfathered = "en{$s}GB{$s}oed"
                        . "|i{$s}(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)"
@@ -331,7 +338,7 @@ class Language {
                        // People think language codes are html safe, so enforce it.
                        // Ideally we should only allow a-zA-Z0-9-
                        // but, .+ and other chars are often used for {{int:}} hacks
-                       // see bugs 37564, 37587, 36938
+                       // see bugs T39564, T39587, T38938
                        $cache[$code] =
                                // Protect against path traversal
                                strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code )
@@ -1037,6 +1044,7 @@ class Language {
         *    xiy  y (two digit year) in Iranian calendar
         *    xiY  Y (full year) in Iranian calendar
         *    xit  t (days in month) in Iranian calendar
+        *    xiz  z (day of the year) in Iranian calendar
         *
         *    xjj  j (day number) in Hebrew calendar
         *    xjF  F (month name) in Hebrew calendar
@@ -1339,6 +1347,13 @@ class Language {
                                        }
                                        $num = self::$IRANIAN_DAYS[$iranian[1] - 1];
                                        break;
+                               case 'xiz':
+                                       $usedIranianYear = true;
+                                       if ( !$iranian ) {
+                                               $iranian = self::tsToIranian( $ts );
+                                       }
+                                       $num = $iranian[3];
+                                       break;
                                case 'a':
                                        $usedAMPM = true;
                                        $s .= intval( substr( $ts, 8, 2 ) ) < 12 ? 'am' : 'pm';
@@ -1597,6 +1612,8 @@ class Language {
                        $jDayNo = floor( ( $jDayNo - 1 ) % 365 );
                }
 
+               $jz = $jDayNo;
+
                for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
                        $jDayNo -= self::$IRANIAN_DAYS[$i];
                }
@@ -1604,7 +1621,7 @@ class Language {
                $jm = $i + 1;
                $jd = $jDayNo + 1;
 
-               return [ $jy, $jm, $jd ];
+               return [ $jy, $jm, $jd, $jz ];
        }
 
        /**
@@ -1612,7 +1629,7 @@ class Language {
         *
         * Based on a PHP-Nuke block by Sharjeel which is released under GNU/GPL license
         *
-        * @see http://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
+        * @see https://phpnuke.org/modules.php?name=News&file=article&sid=8234&mode=thread&order=0&thold=0
         *
         * @param string $ts
         *
@@ -1841,9 +1858,9 @@ class Language {
         * Algorithm to convert Gregorian dates to Thai solar dates,
         * Minguo dates or Minguo dates.
         *
-        * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
-        *       http://en.wikipedia.org/wiki/Minguo_calendar
-        *       http://en.wikipedia.org/wiki/Japanese_era_name
+        * Link: https://en.wikipedia.org/wiki/Thai_solar_calendar
+        *       https://en.wikipedia.org/wiki/Minguo_calendar
+        *       https://en.wikipedia.org/wiki/Japanese_era_name
         *
         * @param string $ts 14-character timestamp
         * @param string $cName Calender name
@@ -2292,7 +2309,7 @@ class Language {
 
        /**
         * Takes a number of seconds and returns an array with a set of corresponding intervals.
-        * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
+        * For example 65 will be turned into [ minutes => 1, seconds => 5 ].
         *
         * @since 1.20
         *
@@ -2582,7 +2599,7 @@ class Language {
        public function iconv( $in, $out, $string ) {
                # Even with //IGNORE iconv can whine about illegal characters in
                # *input* string. We just ignore those too.
-               # REF: http://bugs.php.net/bug.php?id=37166
+               # REF: https://bugs.php.net/bug.php?id=37166
                # REF: https://phabricator.wikimedia.org/T18885
                MediaWiki\suppressWarnings();
                $text = iconv( $in, $out . '//IGNORE', $string );
@@ -3719,6 +3736,7 @@ class Language {
 
                return $word;
        }
+
        /**
         * Get the grammar forms for the content language
         * @return array Array of grammar forms
@@ -3734,6 +3752,46 @@ class Language {
 
                return [];
        }
+
+       /**
+        * Get the grammar transformations data for the language.
+        * Used like grammar forms, with {{GRAMMAR}} and cases,
+        * but uses pairs of regexes and replacements instead of code.
+        *
+        * @return array[] Array of grammar transformations.
+        * @throws MWException
+        * @since 1.28
+        */
+       public function getGrammarTransformations() {
+               $languageCode = $this->getCode();
+
+               if ( self::$grammarTransformations === null ) {
+                       self::$grammarTransformations = new MapCacheLRU( 10 );
+               }
+
+               if ( self::$grammarTransformations->has( $languageCode ) ) {
+                       return self::$grammarTransformations->get( $languageCode );
+               }
+
+               $data = [];
+
+               $grammarDataFile = __DIR__ . "/data/grammarTransformations/$languageCode.json";
+               if ( is_readable( $grammarDataFile ) ) {
+                       $data = FormatJson::decode(
+                               file_get_contents( $grammarDataFile ),
+                               true
+                       );
+
+                       if ( $data === null ) {
+                               throw new MWException( "Invalid grammar data for \"$languageCode\"." );
+                       }
+
+                       self::$grammarTransformations->set( $languageCode, $data );
+               }
+
+               return $data;
+       }
+
        /**
         * Provides an alternative text depending on specified gender.
         * Usage {{gender:username|masculine|feminine|unknown}}.
@@ -4450,14 +4508,15 @@ class Language {
        }
 
        /**
-        * @todo Document
+        * Formats a time given in seconds into a string representation of that time.
+        *
         * @param int|float $seconds
-        * @param array $format Optional
-        *   If $format['avoid'] === 'avoidseconds': don't mention seconds if $seconds >= 1 hour.
-        *   If $format['avoid'] === 'avoidminutes': don't mention seconds/minutes if $seconds > 48 hours.
+        * @param array $format An optional argument that formats the returned string in different ways:
+        *   If $format['avoid'] === 'avoidseconds': don't show seconds if $seconds >= 1 hour,
+        *   If $format['avoid'] === 'avoidminutes': don't show seconds/minutes if $seconds > 48 hours,
         *   If $format['noabbrevs'] is true: use 'seconds' and friends instead of 'seconds-abbrev'
         *     and friends.
-        *   For backwards compatibility, $format may also be one of the strings 'avoidseconds'
+        * @note For backwards compatibility, $format may also be one of the strings 'avoidseconds'
         *     or 'avoidminutes'.
         * @return string
         */