25 global $wgTranslateYamlLibrary;
27 switch ( $wgTranslateYamlLibrary ) {
30 $previousValue = ini_set(
'yaml.decode_php',
false );
31 $ret = yaml_parse( $text );
32 ini_set(
'yaml.decode_php', $previousValue );
33 if ( $ret ===
false ) {
35 throw new InvalidArgumentException(
'Invalid Yaml string' );
40 $yaml = spyc_load( $text );
44 $yaml = self::syckLoad( $text );
48 throw new MWException(
'Unknown Yaml library' );
57 foreach ( $yaml as &$value ) {
58 if ( is_array( $value ) ) {
60 } elseif ( $value ===
'yes' ) {
73 foreach ( $yaml as $key => &$value ) {
74 if ( is_array( $value ) ) {
76 } elseif ( is_string( $value ) && $key ===
'header' ) {
77 $value = preg_replace(
'~^\*~m',
' *', $value ) .
"\n";
84 public static function load( $file ) {
85 $text = file_get_contents( $file );
90 public static function dump( $text ) {
91 global $wgTranslateYamlLibrary;
93 switch ( $wgTranslateYamlLibrary ) {
95 return self::phpyamlDump( $text );
97 return Spyc::YAMLDump( $text );
99 return self::syckDump( $text );
101 throw new MWException(
'Unknown Yaml library' );
105 protected static function phpyamlDump( $data ) {
106 return yaml_emit( $data, YAML_UTF8_ENCODING );
109 protected static function syckLoad( $data ) {
110 # Make temporary file
112 $tf = tempnam( $td,
'yaml-load-' );
115 file_put_contents( $tf, $data );
117 $cmd =
"perl -MYAML::Syck=LoadFile -MPHP::Serialization=serialize -wle '" .
118 'my $tf = q[' . $tf .
'];' .
119 'my $yaml = LoadFile($tf);' .
120 'open my $fh, ">", "$tf.serialized" or die qq[Can not open "$tf.serialized"];' .
121 'print $fh serialize($yaml);' .
125 self::runCommand( $cmd );
127 $serialized = file_get_contents(
"$tf.serialized" );
128 $php_data = unserialize( $serialized );
131 unlink(
"$tf.serialized" );
136 protected static function syckDump( $data ) {
137 # Make temporary file
139 $tf = tempnam( $td,
'yaml-load-' );
142 $sdata = serialize( $data );
143 file_put_contents( $tf, $sdata );
145 $cmd =
"perl -MYAML::Syck=DumpFile -MPHP::Serialization=unserialize -MFile::Slurp=slurp -we '" .
146 '$YAML::Syck::Headless = 1;' .
147 '$YAML::Syck::SortKeys = 1;' .
148 'my $tf = q[' . $tf .
'];' .
149 'my $serialized = slurp($tf);' .
150 'my $unserialized = unserialize($serialized);' .
151 'my $unserialized_utf8 = deutf8($unserialized);' .
152 'DumpFile(qq[$tf.yaml], $unserialized_utf8);' .
154 'if(ref($_[0]) eq "HASH") {' .
155 'return { map { deutf8($_) } %{$_[0]} };' .
156 '} elsif(ref($_[0]) eq "ARRAY") {' .
157 'return [ map { deutf8($_) } @{$_[0]} ];' .
160 'utf8::decode($s);' .
166 self::runCommand( $cmd );
167 $yaml = file_get_contents(
"$tf.yaml" );
170 unlink(
"$tf.yaml" );
175 private static function runCommand(
string $cmd ): void {
176 $result = Shell::command( $cmd )->execute();
177 $exitCode = $result->getExitCode();
178 $stdOutput = $result->getStdout();
179 $stdError = $result->getStderr();
180 if ( $exitCode !== 0 ) {
181 throw new MWException(
182 "The command '$cmd' died in execution with exit code '$exitCode': Output: $stdOutput \nError: $stdError"