19 public static function loadString(
string $text ): array {
20 global $wgTranslateYamlLibrary;
22 switch ( $wgTranslateYamlLibrary ) {
25 $previousValue = ini_set(
'yaml.decode_php',
'0' );
26 $ret = yaml_parse( $text );
27 if ( $previousValue !==
false ) {
28 ini_set(
'yaml.decode_php', $previousValue );
31 if ( $ret ===
false ) {
33 throw new InvalidArgumentException(
'Invalid Yaml string' );
38 $yaml = spyc_load( $text );
40 return self::fixSpycSpaces( $yaml );
42 throw new RuntimeException(
'Unknown Yaml library' );
46 private static function fixSpycSpaces( array &$yaml ): array {
47 foreach ( $yaml as $key => &$value ) {
48 if ( is_array( $value ) ) {
49 self::fixSpycSpaces( $value );
50 } elseif ( is_string( $value ) && $key ===
'header' ) {
51 $value = preg_replace(
'~^\*~m',
' *', $value ) .
"\n";
58 public static function load(
string $file ): array {
59 $text = file_get_contents( $file );
61 return self::loadString( $text );
64 public static function dump( array $text ):
string {
65 global $wgTranslateYamlLibrary;
67 switch ( $wgTranslateYamlLibrary ) {
69 return yaml_emit( $text, YAML_UTF8_ENCODING );
71 return Spyc::YAMLDump( $text );
73 throw new RuntimeException(
'Unknown Yaml library' );