phpcs: More require/include is not a function
[lhc/web/wiklou.git] / serialized / serialize.php
1 <?php
2 /**
3 * Serialize variables found in input file and store the result in the
4 * specified file.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 if ( !defined( 'MEDIAWIKI' ) ) {
25 $wgNoDBParam = true;
26 $optionsWithArgs = array( 'o' );
27 require_once __DIR__ .'/../maintenance/commandLine.inc';
28
29 $stderr = fopen( 'php://stderr', 'w' );
30 if ( !isset( $args[0] ) ) {
31 fwrite( $stderr, "No input file specified\n" );
32 exit( 1 );
33 }
34 if ( wfIsWindows() ) {
35 $files = array();
36 foreach ( $args as $arg ) {
37 $files = array_merge( $files, glob( $arg ) );
38 }
39 if ( !$files ) {
40 fwrite( $stderr, "No files found\n" );
41 }
42 } else {
43 $files = $args;
44 }
45
46 if ( isset( $options['o'] ) ) {
47 $out = fopen( $options['o'], 'wb' );
48 if ( !$out ) {
49 fwrite( $stderr, "Unable to open file \"{$options['o']}\" for output\n" );
50 exit( 1 );
51 }
52 } else {
53 $out = fopen( 'php://stdout', 'wb' );
54 }
55
56 $vars = array();
57 foreach ( $files as $inputFile ) {
58 $vars = array_merge( $vars, getVars( $inputFile ) );
59 }
60 fwrite( $out, serialize( $vars ) );
61 fclose( $out );
62 exit( 0 );
63 }
64
65 //----------------------------------------------------------------------------
66
67 function getVars( $_gv_filename ) {
68 require $_gv_filename;
69 $vars = get_defined_vars();
70 unset( $vars['_gv_filename'] );
71
72 # Clean up line endings
73 if ( wfIsWindows() ) {
74 $vars = unixLineEndings( $vars );
75 }
76 return $vars;
77 }
78
79 function unixLineEndings( $var ) {
80 static $recursionLevel = 0;
81 if ( $recursionLevel > 50 ) {
82 global $stderr;
83 fwrite( $stderr, "Error: Recursion limit exceeded. Possible circular reference in array variable.\n" );
84 exit( 2 );
85 }
86
87 if ( is_array( $var ) ) {
88 ++$recursionLevel;
89 $var = array_map( 'unixLineEndings', $var );
90 --$recursionLevel;
91 } elseif ( is_string( $var ) ) {
92 $var = str_replace( "\r\n", "\n", $var );
93 }
94 return $var;
95 }