'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',
use MediaWiki\MediaWikiServices;
use MediaWiki\Shell\Shell;
use Wikimedia\ScopedCallback;
+use Wikimedia\StaticArrayWriter;
use Wikimedia\Rdbms\DBReplicationWaitError;
use Wikimedia\WrappedString;
}
/**
+ * @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 );
}
/**
--- /dev/null
+<?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;
+ }
+}
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";
}
}
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'
) );
}
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'
) );
--- /dev/null
+<?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 );
+ }
+}