Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslateYaml.php
Go to the documentation of this file.
1<?php
21 public static function loadString( $text ) {
22 global $wgTranslateYamlLibrary;
23
24 switch ( $wgTranslateYamlLibrary ) {
25 case 'phpyaml':
26 // Harden: do not support unserializing objects.
27 // @phan-suppress-next-line PhanTypeMismatchArgumentInternal Scalar okay with php8.1
28 $previousValue = ini_set( 'yaml.decode_php', false );
29 $ret = yaml_parse( $text );
30 ini_set( 'yaml.decode_php', $previousValue );
31 if ( $ret === false ) {
32 // Convert failures to exceptions
33 throw new InvalidArgumentException( 'Invalid Yaml string' );
34 }
35
36 return $ret;
37 case 'spyc':
38 $yaml = spyc_load( $text );
39
40 return self::fixSpycSpaces( $yaml );
41 default:
42 throw new RuntimeException( 'Unknown Yaml library' );
43 }
44 }
45
50 public static function fixSpycSpaces( &$yaml ) {
51 foreach ( $yaml as $key => &$value ) {
52 if ( is_array( $value ) ) {
53 self::fixSpycSpaces( $value );
54 } elseif ( is_string( $value ) && $key === 'header' ) {
55 $value = preg_replace( '~^\*~m', ' *', $value ) . "\n";
56 }
57 }
58
59 return $yaml;
60 }
61
62 public static function load( $file ) {
63 $text = file_get_contents( $file );
64
65 return self::loadString( $text );
66 }
67
68 public static function dump( $text ) {
69 global $wgTranslateYamlLibrary;
70
71 switch ( $wgTranslateYamlLibrary ) {
72 case 'phpyaml':
73 return self::phpyamlDump( $text );
74 case 'spyc':
75 return Spyc::YAMLDump( $text );
76 default:
77 throw new RuntimeException( 'Unknown Yaml library' );
78 }
79 }
80
81 protected static function phpyamlDump( $data ) {
82 return yaml_emit( $data, YAML_UTF8_ENCODING );
83 }
84}
This class is a wrapper class to provide interface to parse and generate YAML files with phpyaml or s...
static fixSpycSpaces(&$yaml)
static loadString( $text)