Move wfMakeStaticArrayFile() into a class
authorKunal Mehta <legoktm@member.fsf.org>
Sat, 18 Aug 2018 05:31:36 +0000 (22:31 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Sat, 18 Aug 2018 05:48:19 +0000 (22:48 -0700)
And include tests :)

This code is independent of MediaWiki, but not really large enough to be
worth librarizing right now.

Bug: T200626
Change-Id: I022c074e8a708fb5219bc4ff4b53e7e31f60dc4b

autoload.php
includes/GlobalFunctions.php
includes/libs/StaticArrayWriter.php [new file with mode: 0644]
maintenance/language/generateCollationData.php
maintenance/language/generateNormalizerDataAr.php
maintenance/language/generateNormalizerDataMl.php
tests/phpunit/includes/libs/StaticArrayWriterTest.php [new file with mode: 0644]

index 999d82d..3b4f025 100644 (file)
@@ -1682,6 +1682,7 @@ $wgAutoloadLocalClasses = [
        'Wikimedia\\Rdbms\\SessionConsistentConnectionManager' => __DIR__ . '/includes/libs/rdbms/connectionmanager/SessionConsistentConnectionManager.php',
        'Wikimedia\\Rdbms\\Subquery' => __DIR__ . '/includes/libs/rdbms/encasing/Subquery.php',
        'Wikimedia\\Rdbms\\TransactionProfiler' => __DIR__ . '/includes/libs/rdbms/TransactionProfiler.php',
+       'Wikimedia\\StaticArrayWriter' => __DIR__ . '/includes/libs/StaticArrayWriter.php',
        'WikitextContent' => __DIR__ . '/includes/content/WikitextContent.php',
        'WikitextContentHandler' => __DIR__ . '/includes/content/WikitextContentHandler.php',
        'WikitextLogFormatter' => __DIR__ . '/includes/logging/WikitextLogFormatter.php',
index d24b74d..433cd2b 100644 (file)
@@ -30,6 +30,7 @@ use MediaWiki\Session\SessionManager;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Shell\Shell;
 use Wikimedia\ScopedCallback;
+use Wikimedia\StaticArrayWriter;
 use Wikimedia\Rdbms\DBReplicationWaitError;
 use Wikimedia\WrappedString;
 
@@ -2675,25 +2676,14 @@ function wfGetPrecompiledData( $name ) {
 }
 
 /**
+ * @deprecated since 1.32 use StaticArrayWriter instead
  * @since 1.32
  * @param string[] $data Array with string keys/values to export
  * @param string $header
  * @return string PHP code
  */
 function wfMakeStaticArrayFile( array $data, $header = 'Automatically generated' ) {
-       $format = "\t%s => %s,\n";
-       $code = "<?php\n"
-               . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
-               . "return [\n";
-       foreach ( $data as $key => $value ) {
-               $code .= sprintf(
-                       $format,
-                       var_export( $key, true ),
-                       var_export( $value, true )
-               );
-       }
-       $code .= "];\n";
-       return $code;
+       return ( new StaticArrayWriter() )->create( $data, $header );
 }
 
 /**
diff --git a/includes/libs/StaticArrayWriter.php b/includes/libs/StaticArrayWriter.php
new file mode 100644 (file)
index 0000000..cd1860f
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/**
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+namespace Wikimedia;
+
+/**
+ * Format a static PHP array to be written to a file
+ *
+ * @since 1.32
+ */
+class StaticArrayWriter {
+
+       /**
+        * @param string[] $data Array with string keys/values to export
+        * @param string $header
+        *
+        * @return string PHP code
+        */
+       public function create( array $data, $header = 'Automatically generated' ) {
+               $format = "\t%s => %s,\n";
+               $code = "<?php\n"
+                       . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
+                       . "return [\n";
+               foreach ( $data as $key => $value ) {
+                       $code .= sprintf(
+                               $format,
+                               var_export( $key, true ),
+                               var_export( $value, true )
+                       );
+               }
+               $code .= "];\n";
+               return $code;
+       }
+}
index 32adafd..97b46f7 100644 (file)
@@ -321,9 +321,10 @@ class GenerateCollationData extends Maintenance {
                print "Out of order: $numOutOfOrder / " . count( $headerChars ) . "\n";
 
                global $IP;
+               $writer = new StaticArrayWriter();
                file_put_contents(
                        "$IP/includes/collation/data/first-letters-root.php",
-                       wfMakeStaticArrayFile( $headerChars, 'File created by generateCollationData.php' )
+                       $writer->create( $headerChars, 'File created by generateCollationData.php' )
                );
                echo "first-letters-root: file written.\n";
        }
index fad35cb..d3e0655 100644 (file)
@@ -127,7 +127,8 @@ class GenerateNormalizerDataAr extends Maintenance {
                }
 
                global $IP;
-               file_put_contents( "$IP/languages/data/normalize-ar.php", wfMakeStaticArrayFile(
+               $writer = new StaticArrayWriter();
+               file_put_contents( "$IP/languages/data/normalize-ar.php", $writer->create(
                        $pairs,
                        'File created by generateNormalizerDataAr.php'
                ) );
index 5b75d8b..1b8ea09 100644 (file)
@@ -61,7 +61,8 @@ class GenerateNormalizerDataMl extends Maintenance {
                }
 
                global $IP;
-               file_put_contents( "$IP/languages/data/normalize-ml.php", wfMakeStaticArrayFile(
+               $writer = new StaticArrayWriter();
+               file_put_contents( "$IP/languages/data/normalize-ml.php", $writer->create(
                        $pairs,
                        'File created by generateNormalizerDataMl.php'
                ) );
diff --git a/tests/phpunit/includes/libs/StaticArrayWriterTest.php b/tests/phpunit/includes/libs/StaticArrayWriterTest.php
new file mode 100644 (file)
index 0000000..bfb3b58
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+/**
+ * Copyright (C) 2018 Kunal Mehta <legoktm@member.fsf.org>
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+use Wikimedia\StaticArrayWriter;
+
+class StaticArrayWriterTest extends PHPUnit\Framework\TestCase {
+       public function testCreate() {
+               $data = [
+                       'foo' => 'bar',
+                       'baz' => 'rawr',
+               ];
+               $writer = new StaticArrayWriter();
+               $actual = $writer->create( $data, "Header\nWith\nNewlines" );
+               $expected = <<<PHP
+<?php
+// Header
+// With
+// Newlines
+return [
+       'foo' => 'bar',
+       'baz' => 'rawr',
+];
+
+PHP;
+               $this->assertSame( $expected, $actual );
+       }
+}