Nikerabbit has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/117177

Change subject: File readers
......................................................................

File readers

LU rewrite part 1: file readers

* Classes for PHP and JSON formats with tests.
* Factory class for creating suitable readers.
* Moved autoloads to separate file

Since 1.19 is LTS and supports PHP 5.2.3, I cannot use namespaces.
Instead, used LU_ prefix to avoid class name clashes.

Change-Id: Ic4f96df4e01fb2ff5b2904e19ea0d35865dc7406
---
M .gitreview
A Autoload.php
M LocalisationUpdate.php
A reader/JSONReader.php
A reader/PHPReader.php
A reader/Reader.php
A reader/ReaderFactory.php
A tests/phpunit/Makefile
A tests/phpunit/reader/JSONReaderTest.php
A tests/phpunit/reader/ReaderFactoryTest.php
10 files changed, 223 insertions(+), 3 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/LocalisationUpdate 
refs/changes/77/117177/1

diff --git a/.gitreview b/.gitreview
index da37d04..f9975eb 100644
--- a/.gitreview
+++ b/.gitreview
@@ -2,4 +2,4 @@
 host=gerrit.wikimedia.org
 port=29418
 project=mediawiki/extensions/LocalisationUpdate.git
-defaultbranch=master
+defaultbranch=json-rewrite
diff --git a/Autoload.php b/Autoload.php
new file mode 100644
index 0000000..1af8150
--- /dev/null
+++ b/Autoload.php
@@ -0,0 +1,20 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+global $wgAutoloadClasses;
+$dir = __DIR__ . '/';
+
+$wgAutoloadClasses += array(
+       'LocalisationUpdate' => "$dir/LocalisationUpdate.class.php",
+       'QuickArrayReader' => "$dir/QuickArrayReader.php",
+
+       # reader
+       'LU_JSONReader' => "$dir/reader/JSONReader.php",
+       'LU_PHPReader' => "$dir/reader/PHPReader.php",
+       'LU_Reader' => "$dir/reader/Reader.php",
+       'LU_ReaderFactory' => "$dir/reader/ReaderFactory.php",
+);
diff --git a/LocalisationUpdate.php b/LocalisationUpdate.php
index f73304c..eac97e9 100644
--- a/LocalisationUpdate.php
+++ b/LocalisationUpdate.php
@@ -40,5 +40,5 @@
 
 $dir = __DIR__ . '/';
 $wgExtensionMessagesFiles['LocalisationUpdate'] = $dir . 
'LocalisationUpdate.i18n.php';
-$wgAutoloadClasses['LocalisationUpdate'] = $dir . 
'LocalisationUpdate.class.php';
-$wgAutoloadClasses['QuickArrayReader'] = $dir . 'QuickArrayReader.php';
+
+require "$dir/Autoload.php";
diff --git a/reader/JSONReader.php b/reader/JSONReader.php
new file mode 100644
index 0000000..1a8cd80
--- /dev/null
+++ b/reader/JSONReader.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * Reads MediaWiki JSON i18n files.
+ */
+class LU_JSONReader implements LU_Reader {
+       /// @var string Language tag
+       protected $code;
+
+       public function __construct( $code ) {
+               $this->code = $code;
+       }
+
+       public function parse( $contents ) {
+               $messages = FormatJson::decode( $contents, true );
+               unset( $messages['@metadata'] );
+               return array( $this->code => $messages );
+       }
+}
diff --git a/reader/PHPReader.php b/reader/PHPReader.php
new file mode 100644
index 0000000..d65f2cf
--- /dev/null
+++ b/reader/PHPReader.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * Reads MediaWiki PHP i18n files.
+ */
+class LU_PHPReader implements LU_Reader {
+       public function parse( $contents ) {
+               if ( strpos( $file1contents, '$messages' ) === false ) {
+                       // This happens for some core languages that only have 
a fallback.
+                       return array();
+               }
+
+               $php = $this->cleanup( $contents );
+               $reader = new QuickArrayReader( "<?php $php" );
+               return $reader->getVar( $varname );
+       }
+
+       /**
+        * 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 = array();
+               preg_match_all( '/\$messages(.*\s)*?\);/', $contents, $results 
);
+
+               // But we want them all in one string.
+               return implode( "\n\n", $results[0] );
+       }
+}
diff --git a/reader/Reader.php b/reader/Reader.php
new file mode 100644
index 0000000..f55a937
--- /dev/null
+++ b/reader/Reader.php
@@ -0,0 +1,19 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * Interface for file readers.
+ */
+interface LU_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/reader/ReaderFactory.php b/reader/ReaderFactory.php
new file mode 100644
index 0000000..b8cb175
--- /dev/null
+++ b/reader/ReaderFactory.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+/**
+ * Constructs readers for files based on the names.
+ */
+class LU_ReaderFactory {
+       /**
+        * Constructs a suitable reader for a given path.
+        * @param string $filename Usually a relative path to the file name.
+        * @return LU_Reader
+        * @throw MWException
+        */
+       public function getReader( $filename ) {
+               if ( preg_match( '/\.php$/', $filename ) ) {
+                       return new LU_PHPReader();
+               }
+
+               if ( preg_match( '/\.json/', $filename ) ) {
+                       $code = basename( $filename, '.json' );
+                       return new LU_JSONReader( $code );
+               }
+
+               throw new MWException( "Unknown file format" );
+       }
+}
diff --git a/tests/phpunit/Makefile b/tests/phpunit/Makefile
new file mode 100644
index 0000000..e98c12c
--- /dev/null
+++ b/tests/phpunit/Makefile
@@ -0,0 +1,12 @@
+ifndef MW_INSTALL_PATH
+       MW_INSTALL_PATH=../../../..
+endif
+
+DIRS=reader
+
+default:
+       php ${MW_INSTALL_PATH}/tests/phpunit/phpunit.php .
+
+.PHONY: *Test.php $(DIRS)
+*Test.php $(DIRS):
+       php ${MW_INSTALL_PATH}/tests/phpunit/phpunit.php $@
diff --git a/tests/phpunit/reader/JSONReaderTest.php 
b/tests/phpunit/reader/JSONReaderTest.php
new file mode 100644
index 0000000..a6baed5
--- /dev/null
+++ b/tests/phpunit/reader/JSONReaderTest.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+class JSONReaderTest extends MediaWikiTestCase {
+       /**
+        * @dataProvider parseProvider
+        */
+       public function testParse( $input, $expected, $comment ) {
+               $reader = new LU_JSONReader( 'xx' );
+               $observed = $reader->parse( $input );
+               $this->assertEquals( $expected, $observed['xx'], $comment );
+       }
+
+       public function parseProvider() {
+               return array(
+                       array(
+                               '{}',
+                               array(),
+                               'empty file',
+                       ),
+                       array(
+                               '{"key":"value"}',
+                               array( 'key' => 'value' ),
+                               'file with one string',
+                       ),
+                       array(
+                               
'{"@metadata":{"authors":["Nike"]},"key":"value2"}',
+                               array( 'key' => 'value2' ),
+                               '@metadata is ignored',
+                       )
+               );
+       }
+}
diff --git a/tests/phpunit/reader/ReaderFactoryTest.php 
b/tests/phpunit/reader/ReaderFactoryTest.php
new file mode 100644
index 0000000..ee155b3
--- /dev/null
+++ b/tests/phpunit/reader/ReaderFactoryTest.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0+
+ */
+
+class LU_ReaderFactoryTest extends MediaWikiTestCase {
+       /**
+        * @dataProvider getReaderProvider
+        */
+       public function testGetReader( $input, $expected, $comment ) {
+               $factory = new LU_ReaderFactory();
+               $reader = $factory->getReader( $input );
+               $observed = get_class( $reader );
+               $this->assertEquals( $expected, $observed, $comment );
+       }
+
+       public function getReaderProvider() {
+               return array(
+                       array(
+                               'languages/messages/MessagesFi.php',
+                               'LU_PHPReader',
+                               'core php file',
+                       ),
+                       array(
+                               'extensions/Translate/Translate.i18n.php',
+                               'LU_PHPReader',
+                               'extension php file',
+                       ),
+                       array(
+                               'extension/Translate/i18n/core/de.json',
+                               'LU_JSONReader',
+                               'extension json file',
+                       ),
+               );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/117177
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic4f96df4e01fb2ff5b2904e19ea0d35865dc7406
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/LocalisationUpdate
Gerrit-Branch: json-rewrite
Gerrit-Owner: Nikerabbit <niklas.laxst...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to