diff options
Diffstat (limited to 'MLEB/LocalisationUpdate/includes')
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Fetcher/Fetcher.php (renamed from MLEB/LocalisationUpdate/includes/fetcher/Fetcher.php) | 2 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Fetcher/FetcherFactory.php (renamed from MLEB/LocalisationUpdate/includes/fetcher/FetcherFactory.php) | 2 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Fetcher/FileSystemFetcher.php (renamed from MLEB/LocalisationUpdate/includes/fetcher/FileSystemFetcher.php) | 2 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Fetcher/GitHubFetcher.php (renamed from MLEB/LocalisationUpdate/includes/fetcher/GitHubFetcher.php) | 6 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Fetcher/HttpFetcher.php (renamed from MLEB/LocalisationUpdate/includes/fetcher/HttpFetcher.php) | 10 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Finder.php (renamed from MLEB/LocalisationUpdate/includes/finder/Finder.php) | 40 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/LocalisationUpdate.php | 16 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/QuickArrayReader.php | 214 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Reader/JSONReader.php (renamed from MLEB/LocalisationUpdate/includes/reader/JSONReader.php) | 2 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Reader/Reader.php (renamed from MLEB/LocalisationUpdate/includes/reader/Reader.php) | 2 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Reader/ReaderFactory.php (renamed from MLEB/LocalisationUpdate/includes/reader/ReaderFactory.php) | 14 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/Updater.php | 3 | ||||
-rw-r--r-- | MLEB/LocalisationUpdate/includes/reader/PHPReader.php | 61 |
13 files changed, 28 insertions, 346 deletions
diff --git a/MLEB/LocalisationUpdate/includes/fetcher/Fetcher.php b/MLEB/LocalisationUpdate/includes/Fetcher/Fetcher.php index 62febac8..c9d38155 100644 --- a/MLEB/LocalisationUpdate/includes/fetcher/Fetcher.php +++ b/MLEB/LocalisationUpdate/includes/Fetcher/Fetcher.php @@ -5,7 +5,7 @@ * @license GPL-2.0-or-later */ -namespace LocalisationUpdate; +namespace LocalisationUpdate\Fetcher; /** * Interface for classes which fetch files over different protocols and ways. diff --git a/MLEB/LocalisationUpdate/includes/fetcher/FetcherFactory.php b/MLEB/LocalisationUpdate/includes/Fetcher/FetcherFactory.php index 9273c935..4bb05132 100644 --- a/MLEB/LocalisationUpdate/includes/fetcher/FetcherFactory.php +++ b/MLEB/LocalisationUpdate/includes/Fetcher/FetcherFactory.php @@ -5,7 +5,7 @@ * @license GPL-2.0-or-later */ -namespace LocalisationUpdate; +namespace LocalisationUpdate\Fetcher; /** * Constructs fetchers based on the repository urls. diff --git a/MLEB/LocalisationUpdate/includes/fetcher/FileSystemFetcher.php b/MLEB/LocalisationUpdate/includes/Fetcher/FileSystemFetcher.php index 43daa65d..17e6362d 100644 --- a/MLEB/LocalisationUpdate/includes/fetcher/FileSystemFetcher.php +++ b/MLEB/LocalisationUpdate/includes/Fetcher/FileSystemFetcher.php @@ -5,7 +5,7 @@ * @license GPL-2.0-or-later */ -namespace LocalisationUpdate; +namespace LocalisationUpdate\Fetcher; /** * Accesses file system directly. diff --git a/MLEB/LocalisationUpdate/includes/fetcher/GitHubFetcher.php b/MLEB/LocalisationUpdate/includes/Fetcher/GitHubFetcher.php index f8bea9c1..eba89a9e 100644 --- a/MLEB/LocalisationUpdate/includes/fetcher/GitHubFetcher.php +++ b/MLEB/LocalisationUpdate/includes/Fetcher/GitHubFetcher.php @@ -5,7 +5,7 @@ * @license GPL-2.0-or-later */ -namespace LocalisationUpdate; +namespace LocalisationUpdate\Fetcher; /** * This class uses GitHub api to obtain a list of files present in a directory @@ -23,12 +23,14 @@ class GitHubFetcher extends HttpFetcher { * @throws \Exception */ public function fetchDirectory( $pattern ) { + global $wgLocalisationUpdateHttpRequestOptions; + $domain = preg_quote( 'https://raw.github.com/', '~' ); $p = "~^$domain(?P<org>[^/]+)/(?P<repo>[^/]+)/(?P<branch>[^/]+)/(?P<path>.+)/.+$~"; preg_match( $p, $pattern, $m ); $apiURL = "https://api.github.com/repos/{$m['org']}/{$m['repo']}/contents/{$m['path']}"; - $json = \Http::get( $apiURL ); + $json = \Http::get( $apiURL, $wgLocalisationUpdateHttpRequestOptions, __METHOD__ ); if ( !$json ) { throw new \Exception( "Unable to get directory listing for {$m['org']}/{$m['repo']}" ); } diff --git a/MLEB/LocalisationUpdate/includes/fetcher/HttpFetcher.php b/MLEB/LocalisationUpdate/includes/Fetcher/HttpFetcher.php index 72988e98..80c1394e 100644 --- a/MLEB/LocalisationUpdate/includes/fetcher/HttpFetcher.php +++ b/MLEB/LocalisationUpdate/includes/Fetcher/HttpFetcher.php @@ -5,7 +5,7 @@ * @license GPL-2.0-or-later */ -namespace LocalisationUpdate; +namespace LocalisationUpdate\Fetcher; /** * Fetches files over HTTP(s). @@ -17,7 +17,8 @@ class HttpFetcher implements Fetcher { * @return bool|string */ public function fetchFile( $url ) { - return \Http::get( $url ); + global $wgLocalisationUpdateHttpRequestOptions; + return \Http::get( $url, $wgLocalisationUpdateHttpRequestOptions, __METHOD__ ); } /** @@ -32,11 +33,6 @@ class HttpFetcher implements Fetcher { $languages = \Language::fetchLanguageNames( null, 'mwfile' ); foreach ( array_keys( $languages ) as $code ) { - // Hack for core - if ( strpos( $pattern, 'Messages*.php' ) !== false ) { - $code = ucfirst( strtr( $code, '-', '_' ) ); - } - $url = str_replace( '*', $code, $pattern ); $file = $this->fetchFile( $url ); if ( $file ) { diff --git a/MLEB/LocalisationUpdate/includes/finder/Finder.php b/MLEB/LocalisationUpdate/includes/Finder.php index acebc42a..74be5787 100644 --- a/MLEB/LocalisationUpdate/includes/finder/Finder.php +++ b/MLEB/LocalisationUpdate/includes/Finder.php @@ -12,12 +12,6 @@ namespace LocalisationUpdate; * included for l10n updates. */ class Finder { - - /** - * @var array - */ - private $php; - /** * @var array */ @@ -27,36 +21,23 @@ class Finder { * @var string */ private $core; + /** - * @param array $php See $wgExtensionMessagesFiles * @param array $json See $wgMessagesDirs * @param string $core Absolute path to MediaWiki core */ - public function __construct( $php, $json, $core ) { - $this->php = $php; + public function __construct( $json, $core ) { $this->json = $json; $this->core = $core; } /** - * @return array + * @return array[] */ public function getComponents() { $components = []; - // For older versions of Mediawiki, pull json updates even though its still using php - if ( !isset( $this->json['core'] ) ) { - $components['core'] = [ - 'repo' => 'mediawiki', - 'orig' => "file://{$this->core}/languages/messages/Messages*.php", - 'path' => 'languages/messages/i18n/*.json', - ]; - } - foreach ( $this->json as $key => $value ) { - // Json should take priority if both exist - unset( $this->php[$key] ); - foreach ( (array)$value as $subkey => $subvalue ) { // Mediawiki core files $matches = []; @@ -85,21 +66,6 @@ class Finder { } } - foreach ( $this->php as $key => $value ) { - $matches = []; - $ok = preg_match( '~/extensions/(?P<name>[^/]+)/(?P<path>.*\.i18n\.php)$~', $value, $matches ); - if ( !$ok ) { - continue; - } - - $components[$key] = [ - 'repo' => 'extension', - 'name' => $matches['name'], - 'orig' => "file://$value", - 'path' => $matches['path'], - ]; - } - return $components; } diff --git a/MLEB/LocalisationUpdate/includes/LocalisationUpdate.php b/MLEB/LocalisationUpdate/includes/LocalisationUpdate.php index a0b5f044..87e60f62 100644 --- a/MLEB/LocalisationUpdate/includes/LocalisationUpdate.php +++ b/MLEB/LocalisationUpdate/includes/LocalisationUpdate.php @@ -1,5 +1,11 @@ <?php +namespace LocalisationUpdate; + +use FileDependency; +use FormatJson; +use LocalisationCache; + /** * Class for localization update hooks and static methods. */ @@ -9,12 +15,11 @@ class LocalisationUpdate { * @param LocalisationCache $lc * @param string $code * @param array &$cache - * @return true */ public static function onRecacheFallback( LocalisationCache $lc, $code, array &$cache ) { $dir = self::getDirectory(); if ( !$dir ) { - return true; + return; } $fileName = "$dir/" . self::getFilename( $code ); @@ -22,8 +27,6 @@ class LocalisationUpdate { $data = FormatJson::decode( file_get_contents( $fileName ), true ); $cache['messages'] = array_merge( $cache['messages'], $data ); } - - return true; } /** @@ -31,12 +34,11 @@ class LocalisationUpdate { * @param LocalisationCache $lc * @param string $code * @param array &$cache - * @return true */ public static function onRecache( LocalisationCache $lc, $code, array &$cache ) { $dir = self::getDirectory(); if ( !$dir ) { - return true; + return; } $codeSequence = array_merge( [ $code ], $cache['fallbackSequence'] ); @@ -44,8 +46,6 @@ class LocalisationUpdate { $fileName = "$dir/" . self::getFilename( $csCode ); $cache['deps'][] = new FileDependency( $fileName ); } - - return true; } /** diff --git a/MLEB/LocalisationUpdate/includes/QuickArrayReader.php b/MLEB/LocalisationUpdate/includes/QuickArrayReader.php deleted file mode 100644 index 51b424d8..00000000 --- a/MLEB/LocalisationUpdate/includes/QuickArrayReader.php +++ /dev/null @@ -1,214 +0,0 @@ -<?php - -/** - * Quickie parser class that can happily read the subset of PHP we need - * for our localization arrays safely. - * - * Still an order of magnitude slower than eval(). - */ -class QuickArrayReader { - private $vars = []; - - /** - * @param string $string - */ - public function __construct( $string ) { - $scalarTypes = [ - T_LNUMBER => true, - T_DNUMBER => true, - T_STRING => true, - T_CONSTANT_ENCAPSED_STRING => true, - ]; - $skipTypes = [ - T_WHITESPACE => true, - T_COMMENT => true, - T_DOC_COMMENT => true, - ]; - $tokens = token_get_all( $string ); - $count = count( $tokens ); - for ( $i = 0; $i < $count; ) { - while ( isset( $skipTypes[$tokens[$i][0]] ) ) { - $i++; - } - switch ( $tokens[$i][0] ) { - case T_OPEN_TAG: - $i++; - break; - case T_VARIABLE: - // '$messages' -> 'messages' - $varname = trim( substr( $tokens[$i][1], 1 ) ); - $varindex = null; - - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - - if ( $tokens[$i] === '[' ) { - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - - if ( isset( $scalarTypes[$tokens[$i][0]] ) ) { - $varindex = $this->parseScalar( $tokens[$i] ); - } else { - throw $this->except( $tokens[$i], 'scalar index' ); - } - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - - if ( $tokens[$i] !== ']' ) { - throw $this->except( $tokens[$i], ']' ); - } - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - } - - if ( $tokens[$i] !== '=' ) { - throw $this->except( $tokens[$i], '=' ); - } - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - - if ( isset( $scalarTypes[$tokens[$i][0]] ) ) { - $buildval = $this->parseScalar( $tokens[$i] ); - } elseif ( $tokens[$i][0] === T_ARRAY ) { - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - if ( $tokens[$i] !== '(' ) { - throw $this->except( $tokens[$i], '(' ); - } - $buildval = []; - do { - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - - if ( $tokens[$i] === ')' ) { - break; - } - if ( isset( $scalarTypes[$tokens[$i][0]] ) ) { - $key = $this->parseScalar( $tokens[$i] ); - } - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - - if ( $tokens[$i][0] !== T_DOUBLE_ARROW ) { - throw $this->except( $tokens[$i], '=>' ); - } - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - - if ( isset( $scalarTypes[$tokens[$i][0]] ) ) { - $val = $this->parseScalar( $tokens[$i] ); - } - wfSuppressWarnings(); - $buildval[$key] = $val; - wfRestoreWarnings(); - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - - if ( $tokens[$i] === ',' ) { - continue; - } elseif ( $tokens[$i] === ')' ) { - break; - } else { - throw $this->except( $tokens[$i], ', or )' ); - } - } while ( true ); - } else { - throw $this->except( $tokens[$i], 'scalar or array' ); - } - if ( is_null( $varindex ) ) { - $this->vars[$varname] = $buildval; - } else { - wfSuppressWarnings(); - $this->vars[$varname][$varindex] = $buildval; - wfRestoreWarnings(); - } - while ( isset( $skipTypes[$tokens[++$i][0]] ) ) { - } - if ( $tokens[$i] !== ';' ) { - throw $this->except( $tokens[$i], ';' ); - } - $i++; - break; - default: - throw $this->except( $tokens[$i], 'open tag, whitespace, or variable.' ); - } - } - } - - /** - * @param string $got - * @param string $expected - * @return Exception - */ - private function except( $got, $expected ) { - if ( is_array( $got ) ) { - $got = token_name( $got[0] ) . " ('" . $got[1] . "')"; - } else { - $got = "'" . $got . "'"; - } - - return new Exception( "Expected $expected, got $got" ); - } - - /** - * Parse a scalar value in PHP - * - * @param string $token - * - * @return mixed Parsed value - */ - private function parseScalar( $token ) { - if ( is_array( $token ) ) { - $str = $token[1]; - } else { - $str = $token; - } - if ( $str !== '' && $str[0] == '\'' ) { - // Single-quoted string - // @fixme trim() call is due to mystery bug where whitespace gets - // appended to the token; without it we ended up reading in the - // extra quote on the end! - return strtr( substr( trim( $str ), 1, -1 ), - [ '\\\'' => '\'', '\\\\' => '\\' ] ); - } - - wfSuppressWarnings(); - if ( $str !== '' && $str[0] == '"' ) { - // Double-quoted string - // @fixme trim() call is due to mystery bug where whitespace gets - // appended to the token; without it we ended up reading in the - // extra quote on the end! - wfRestoreWarnings(); - return stripcslashes( substr( trim( $str ), 1, -1 ) ); - } - wfRestoreWarnings(); - - if ( substr( $str, 0, 4 ) === 'true' ) { - return true; - } - - if ( substr( $str, 0, 5 ) === 'false' ) { - return false; - } - - if ( substr( $str, 0, 4 ) === 'null' ) { - return null; - } - - // Must be some kind of numeric value, so let PHP's weak typing - // be useful for a change - return $str; - } - - /** - * @param string $varname - * @return null|string|array - */ - public function getVar( $varname ) { - if ( isset( $this->vars[$varname] ) ) { - return $this->vars[$varname]; - } else { - return null; - } - } -} diff --git a/MLEB/LocalisationUpdate/includes/reader/JSONReader.php b/MLEB/LocalisationUpdate/includes/Reader/JSONReader.php index e8613660..6d27eed1 100644 --- a/MLEB/LocalisationUpdate/includes/reader/JSONReader.php +++ b/MLEB/LocalisationUpdate/includes/Reader/JSONReader.php @@ -5,7 +5,7 @@ * @license GPL-2.0-or-later */ -namespace LocalisationUpdate; +namespace LocalisationUpdate\Reader; /** * Reads MediaWiki JSON i18n files. diff --git a/MLEB/LocalisationUpdate/includes/reader/Reader.php b/MLEB/LocalisationUpdate/includes/Reader/Reader.php index fd37322e..1898b1bf 100644 --- a/MLEB/LocalisationUpdate/includes/reader/Reader.php +++ b/MLEB/LocalisationUpdate/includes/Reader/Reader.php @@ -5,7 +5,7 @@ * @license GPL-2.0-or-later */ -namespace LocalisationUpdate; +namespace LocalisationUpdate\Reader; /** * Interface for file readers. diff --git a/MLEB/LocalisationUpdate/includes/reader/ReaderFactory.php b/MLEB/LocalisationUpdate/includes/Reader/ReaderFactory.php index 44bc5e40..6d09e757 100644 --- a/MLEB/LocalisationUpdate/includes/reader/ReaderFactory.php +++ b/MLEB/LocalisationUpdate/includes/Reader/ReaderFactory.php @@ -5,7 +5,7 @@ * @license GPL-2.0-or-later */ -namespace LocalisationUpdate; +namespace LocalisationUpdate\Reader; /** * Constructs readers for files based on the names. @@ -15,19 +15,9 @@ class ReaderFactory { * Constructs a suitable reader for a given path. * @param string $filename Usually a relative path to the file name. * @return Reader - * @throws Exception + * @throws \Exception */ public function getReader( $filename ) { - if ( preg_match( '/i18n\.php$/', $filename ) ) { - return new PHPReader(); - } - - // Ugly hack for core i18n files - if ( preg_match( '/Messages(.*)\.php$/', $filename ) ) { - $code = \Language::getCodeFromFileName( basename( $filename ), 'Messages' ); - return new PHPReader( $code ); - } - if ( preg_match( '/\.json/', $filename ) ) { $code = basename( $filename, '.json' ); return new JSONReader( $code ); diff --git a/MLEB/LocalisationUpdate/includes/Updater.php b/MLEB/LocalisationUpdate/includes/Updater.php index 863dc04a..9c1046f6 100644 --- a/MLEB/LocalisationUpdate/includes/Updater.php +++ b/MLEB/LocalisationUpdate/includes/Updater.php @@ -7,6 +7,9 @@ namespace LocalisationUpdate; +use LocalisationUpdate\Fetcher\FetcherFactory; +use LocalisationUpdate\Reader\ReaderFactory; + /** * Executes the localisation update. */ diff --git a/MLEB/LocalisationUpdate/includes/reader/PHPReader.php b/MLEB/LocalisationUpdate/includes/reader/PHPReader.php deleted file mode 100644 index 43e4db25..00000000 --- a/MLEB/LocalisationUpdate/includes/reader/PHPReader.php +++ /dev/null @@ -1,61 +0,0 @@ -<?php -/** - * @file - * @author Niklas Laxström - * @license GPL-2.0-or-later - */ - -namespace LocalisationUpdate; - -/** - * Reads MediaWiki PHP i18n files. - */ -class PHPReader implements Reader { - /// @var string Language tag - protected $code; - - public function __construct( $code = null ) { - $this->code = $code; - } - - /** - * @param string $contents - * - * @return array - */ - public function parse( $contents ) { - if ( strpos( $contents, '$messages' ) === false ) { - // This happens for some core languages that only have a fallback. - return []; - } - - $php = $this->cleanupFile( $contents ); - $reader = new \QuickArrayReader( "<?php $php" ); - $messages = $reader->getVar( 'messages' ); - - if ( $this->code ) { - return [ $this->code => $messages ]; - } - - // Assuming that the array is keyed by language codes - return $messages; - } - - /** - * Removes all unneeded content from a file and returns it. - * - * @param string $contents String - * @return string PHP code without PHP tags - */ - protected function cleanupFile( $contents ) { - // We hate the windows vs linux linebreaks. - $contents = preg_replace( '/\r\n?/', "\n", $contents ); - - // We only want message arrays. - $results = []; - preg_match_all( '/\$messages(?:.*\s)*?\);/', $contents, $results ); - - // But we want them all in one string. - return implode( "\n\n", $results[0] ); - } -} |