Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
yaml-tests.php
Go to the documentation of this file.
1<?php
12// Standard boilerplate to define $IP
13if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
14 $IP = getenv( 'MW_INSTALL_PATH' );
15} else {
16 $dir = __DIR__;
17 $IP = "$dir/../../..";
18}
19require_once "$IP/maintenance/Maintenance.php";
20
21class YamlTests extends Maintenance {
22 public function __construct() {
23 parent::__construct();
24 $this->addDescription( 'Script for comparing supported YAML parser implementations.' );
25 $this->requireExtension( 'Translate' );
26 }
27
28 public function execute() {
29 global $wgTranslateGroupFiles, $wgTranslateYamlLibrary;
30 $documents = [];
31 $times = [];
32 $mems = [];
33 $mempeaks = [];
34
35 foreach ( [ 'syck', 'spyc', 'phpyaml' ] as $driver ) {
36 $mempeaks[$driver] = -memory_get_peak_usage( true );
37 $mems[$driver] = -memory_get_usage( true );
38 $times[$driver] = -microtime( true );
39 $wgTranslateYamlLibrary = $driver;
40 $documents[$driver] = [];
41 foreach ( $wgTranslateGroupFiles as $file ) {
42 foreach ( self::parseGroupFile( $file ) as $id => $docu ) {
43 $documents[$driver]["$file-$id"] = $docu;
44 }
45 }
46
47 $times[$driver] += microtime( true );
48 $mems[$driver] += memory_get_usage( true );
49 $mempeaks[$driver] += memory_get_peak_usage( true );
50
51 self::sortNestedArrayAssoc( $documents[$driver] );
52 file_put_contents( "yaml-test-$driver.txt", var_export( $documents[$driver], true ) );
53 file_put_contents( "yaml-output-$driver.txt", TranslateYaml::dump( $documents[$driver] ) );
54 }
55 var_dump( $times );
56 var_dump( $mems );
57 var_dump( $mempeaks );
58 }
59
60 public static function parseGroupFile( $filename ) {
61 $data = file_get_contents( $filename );
62 $documents = preg_split( "/^---$/m", $data, -1, PREG_SPLIT_NO_EMPTY );
63 $groups = [];
64 $template = false;
65 foreach ( $documents as $document ) {
66 $document = TranslateYaml::loadString( $document );
67 if ( isset( $document['TEMPLATE'] ) ) {
68 $template = $document['TEMPLATE'];
69 } else {
70 if ( !isset( $document['BASIC']['id'] ) ) {
71 trigger_error( 'No path ./BASIC/id (group id not defined) ' .
72 "in yaml document located in $filename" );
73 continue;
74 }
75 $groups[$document['BASIC']['id']] = $document;
76 }
77 }
78
79 return array_map( static function ( array $group ) use ( $template ): array {
80 return MessageGroupConfigurationParser::mergeTemplate( $template, $group );
81 }, $groups );
82 }
83
84 public static function sortNestedArrayAssoc( &$a ) {
85 ksort( $a );
86 foreach ( $a as &$value ) {
87 if ( is_array( $value ) ) {
88 self::sortNestedArrayAssoc( $value );
89 }
90 }
91 }
92}
93
94$maintClass = YamlTests::class;
95require_once RUN_MAINTENANCE_IF_MAIN;
static mergeTemplate(array $base, array $specific)
Merges a document template (base) to actual definition (specific)
static loadString( $text)