22 public static function loadString(
string $text ): array {
23 global $wgTranslateYamlLibrary;
25 switch ( $wgTranslateYamlLibrary ) {
28 $previousValue = ini_set(
'yaml.decode_php',
'0' );
29 $ret = yaml_parse( $text );
30 if ( $previousValue !==
false ) {
31 ini_set(
'yaml.decode_php', $previousValue );
34 if ( $ret ===
false ) {
36 throw new InvalidArgumentException(
'Invalid Yaml string' );
41 $yaml = spyc_load( $text );
43 return self::fixSpycSpaces( $yaml );
45 throw new RuntimeException(
'Unknown Yaml library' );
49 private static function fixSpycSpaces( array &$yaml ): array {
50 foreach ( $yaml as $key => &$value ) {
51 if ( is_array( $value ) ) {
52 self::fixSpycSpaces( $value );
53 } elseif ( is_string( $value ) && $key ===
'header' ) {
54 $value = preg_replace(
'~^\*~m',
' *', $value ) .
"\n";
61 public static function load(
string $file ): array {
62 $text = file_get_contents( $file );
64 return self::loadString( $text );
67 public static function dump( array $text ):
string {
68 global $wgTranslateYamlLibrary;
70 switch ( $wgTranslateYamlLibrary ) {
72 return yaml_emit( $text, YAML_UTF8_ENCODING );
74 return Spyc::YAMLDump( $text );
76 throw new RuntimeException(
'Unknown Yaml library' );