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