diff options
author | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
commit | fc7369835258467bf97eb64f184b93691f9a9fd5 (patch) | |
tree | daabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/LocalisationUpdate/reader |
first commit
Diffstat (limited to 'www/wiki/extensions/LocalisationUpdate/reader')
4 files changed, 147 insertions, 0 deletions
diff --git a/www/wiki/extensions/LocalisationUpdate/reader/JSONReader.php b/www/wiki/extensions/LocalisationUpdate/reader/JSONReader.php new file mode 100644 index 00000000..fdc4e1d6 --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/reader/JSONReader.php @@ -0,0 +1,32 @@ +<?php +/** + * @file + * @author Niklas Laxström + * @license GPL-2.0+ + */ + +namespace LocalisationUpdate; + +/** + * Reads MediaWiki JSON i18n files. + */ +class JSONReader implements Reader { + /// @var string Language tag + protected $code; + + public function __construct( $code = null ) { + $this->code = $code; + } + + public function parse( $contents ) { + $messages = \FormatJson::decode( $contents, true ); + unset( $messages['@metadata'] ); + + if ( $this->code ) { + return [ $this->code => $messages ]; + } + + // Assuming that the array is keyed by language codes + return $messages; + } +} diff --git a/www/wiki/extensions/LocalisationUpdate/reader/PHPReader.php b/www/wiki/extensions/LocalisationUpdate/reader/PHPReader.php new file mode 100644 index 00000000..f314744b --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/reader/PHPReader.php @@ -0,0 +1,56 @@ +<?php +/** + * @file + * @author Niklas Laxström + * @license GPL-2.0+ + */ + +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; + } + + 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] ); + } +} diff --git a/www/wiki/extensions/LocalisationUpdate/reader/Reader.php b/www/wiki/extensions/LocalisationUpdate/reader/Reader.php new file mode 100644 index 00000000..8c263ff0 --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/reader/Reader.php @@ -0,0 +1,21 @@ +<?php +/** + * @file + * @author Niklas Laxström + * @license GPL-2.0+ + */ + +namespace LocalisationUpdate; + +/** + * Interface for file readers. + */ +interface Reader { + /** + * Returns a list of messages indexed by language code. Example + * array( 'en' => array( 'key' => 'value' ) ); + * @param string $contents File contents as a string. + * @return array + */ + public function parse( $contents ); +} diff --git a/www/wiki/extensions/LocalisationUpdate/reader/ReaderFactory.php b/www/wiki/extensions/LocalisationUpdate/reader/ReaderFactory.php new file mode 100644 index 00000000..ab5cdf1b --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/reader/ReaderFactory.php @@ -0,0 +1,38 @@ +<?php +/** + * @file + * @author Niklas Laxström + * @license GPL-2.0+ + */ + +namespace LocalisationUpdate; + +/** + * Constructs readers for files based on the names. + */ +class ReaderFactory { + /** + * Constructs a suitable reader for a given path. + * @param string $filename Usually a relative path to the file name. + * @return Reader + * @throw 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 ); + } + + throw new \Exception( "Unknown file format: " . $filename ); + } +} |