Move wfMakeStaticArrayFile() into a class
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
This commit is contained in:
parent
0087dcf2ac
commit
3b1e8a5cef
7 changed files with 103 additions and 16 deletions
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
49
includes/libs/StaticArrayWriter.php
Normal file
49
includes/libs/StaticArrayWriter.php
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
) );
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
) );
|
||||
|
|
|
|||
44
tests/phpunit/includes/libs/StaticArrayWriterTest.php
Normal file
44
tests/phpunit/includes/libs/StaticArrayWriterTest.php
Normal file
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue