Merged localisation-work branch:
[lhc/web/wiklou.git] / serialized / serialize.php
1 <?php
2
3 if ( !defined( 'MEDIAWIKI' ) ) {
4 $optionsWithArgs = array( 'o' );
5 require_once( dirname(__FILE__).'/../maintenance/commandLine.inc' );
6
7 $stderr = fopen( 'php://stderr', 'w' );
8 if ( !isset( $args[0] ) ) {
9 fwrite( $stderr, "No input file specified\n" );
10 exit( 1 );
11 }
12 if ( wfIsWindows() ) {
13 $files = array();
14 foreach ( $args as $arg ) {
15 $files = array_merge( $files, glob( $arg ) );
16 }
17 if ( !$files ) {
18 fwrite( $stderr, "No files found\n" );
19 }
20 } else {
21 $files = $args;
22 }
23
24 if ( isset( $options['o'] ) ) {
25 $out = fopen( $options['o'], 'wb' );
26 if ( !$out ) {
27 fwrite( $stderr, "Unable to open file \"{$options['o']}\" for output\n" );
28 exit( 1 );
29 }
30 } else {
31 $out = fopen( 'php://stdout', 'wb' );
32 }
33
34 $vars = array();
35 foreach ( $files as $inputFile ) {
36 $vars = array_merge( $vars, getVars( $inputFile ) );
37 }
38 fwrite( $out, serialize( $vars ) );
39 fclose( $out );
40 exit( 0 );
41 }
42
43 //----------------------------------------------------------------------------
44
45 function getVars( $_gv_filename ) {
46 require( $_gv_filename );
47 $vars = get_defined_vars();
48 unset( $vars['_gv_filename'] );
49
50 # Clean up line endings
51 if ( wfIsWindows() ) {
52 $vars = unixLineEndings( $vars );
53 }
54 return $vars;
55 }
56
57 function unixLineEndings( $var ) {
58 static $recursionLevel = 0;
59 if ( $recursionLevel > 50 ) {
60 global $stderr;
61 fwrite( $stderr, "Error: Recursion limit exceeded. Possible circular reference in array variable.\n" );
62 exit( 2 );
63 }
64
65 if ( is_array( $var ) ) {
66 ++$recursionLevel;
67 $var = array_map( 'unixLineEndings', $var );
68 --$recursionLevel;
69 } elseif ( is_string( $var ) ) {
70 $var = str_replace( "\r\n", "\n", $var );
71 }
72 return $var;
73 }
74 ?>