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