1d018a606a7b085d4983b412085adb9ac76f1930
[lhc/web/wiklou.git] / maintenance / install-utils.inc
1 <?php
2
3 /**
4 * This file contains functions used by the install script (config/index.php)
5 * and maintenance scripts. It is not loaded in normal web requests.
6 *
7 * @file
8 */
9
10 function install_version_checks() {
11 # We dare not turn output buffer _off_ since this will break completely
12 # if PHP is globally configured to run through a gzip filter.
13 @ob_implicit_flush( true );
14
15 if( !function_exists( 'version_compare' ) ) {
16 # version_compare was introduced in 4.1.0
17 echo "Your PHP version is much too old; 4.0.x will _not_ work. 5.0.0 or higher is required. ABORTING.\n";
18 die( 1 );
19 }
20 if( version_compare( phpversion(), '5.0.0' ) < 0 ) {
21 echo "PHP 5.0.0 or higher is required. If PHP 5 is available only when \n".
22 "PHP files have a .php5 extension, please navigate to <a href=\"index.php5\">index.php5</a> \n".
23 "to continue installation. ABORTING.\n";
24 die( 1 );
25 }
26
27 // Test for PHP bug which breaks PHP 5.0.x on 64-bit...
28 // As of 1.8 this breaks lots of common operations instead
29 // of just some rare ones like export.
30 $borked = str_replace( 'a', 'b', array( -1 => -1 ) );
31 if( !isset( $borked[-1] ) ) {
32 echo "PHP 5.0.x is buggy on your 64-bit system; you must upgrade to PHP 5.1.x\n" .
33 "or higher. ABORTING. (http://bugs.php.net/bug.php?id=34879 for details)\n";
34 die( 1 );
35 }
36
37 $test = new PhpXmlBugTester();
38 if( !$test->ok ) {
39 echo "Your system has a combination of PHP and libxml2 versions which is buggy\n" .
40 "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
41 "Upgrade to PHP 5.2.9 or later and libxml2 2.7.3 or later!\n" .
42 "ABORTING (http://bugs.php.net/bug.php?id=45996 for details).\n";
43 die( 1 );
44 }
45
46 $test = new PhpRefCallBugTester;
47 $ok = false;
48 call_user_func_array( array( $test, 'test' ), array( &$ok ) );
49 if ( !$ok ) {
50 echo "PHP " . phpversion() . " is not compatible with MediaWiki due to a bug involving\n" .
51 "reference parameters to __call. Upgrade to PHP 5.3.2, or downgrade\n" .
52 "to PHP 5.3.0. to fix this.\n" .
53 "ABORTING (see http://bugs.php.net/bug.php?id=50394 for details)\n";
54 die( 1 );
55 }
56
57
58 global $wgCommandLineMode;
59 $wgCommandLineMode = true;
60 umask( 000 );
61 @set_time_limit( 0 );
62 }
63
64 /**
65 * Test for PHP+libxml2 bug which breaks XML input subtly with certain versions.
66 * http://bugs.php.net/bug.php?id=45996
67 * Known fixed with PHP 5.2.9 + libxml2-2.7.3
68 */
69 class PhpXmlBugTester {
70 private $parsedData = '';
71 public $ok = false;
72 public function __construct() {
73 $charData = '<b>c</b>';
74 $xml = '<a>' . htmlspecialchars( $charData ) . '</a>';
75
76 $parser = xml_parser_create();
77 xml_set_character_data_handler( $parser, array( $this, 'chardata' ) );
78 $parsedOk = xml_parse($parser, $xml, true);
79 $this->ok = $parsedOk && ($this->parsedData == $charData);
80 }
81 public function chardata($parser, $data) {
82 $this->parsedData .= $data;
83 }
84 }
85
86 /**
87 * Test for PHP bug #50394
88 */
89 class PhpRefCallBugTester {
90 function __call( $name, $args ) {
91 $args[0] = true;
92 }
93 }
94
95 function readconsole( $prompt = '' ) {
96 static $isatty = null;
97 if ( is_null( $isatty ) ) {
98 if ( !function_exists( 'posix_isatty' ) || posix_isatty( 0 /*STDIN*/ ) ) {
99 $isatty = true;
100 } else {
101 $isatty = false;
102 }
103 }
104
105 if ( $isatty && function_exists( 'readline' ) ) {
106 return readline( $prompt );
107 } else {
108 if ( $isatty ) {
109 $st = readlineEmulation( $prompt );
110 } else {
111 if ( feof( STDIN ) ) {
112 $st = false;
113 } else {
114 $st = fgets(STDIN, 1024);
115 }
116 }
117 if ($st === false) return false;
118 $resp = trim( $st );
119 return $resp;
120 }
121 }
122
123 function findExecutable( $name ) {
124 $paths = explode( PATH_SEPARATOR, getenv( "PATH" ) );
125 foreach( $paths as $path ) {
126 $full = $path . DIRECTORY_SEPARATOR . $name;
127 if( file_exists( $full ) ) {
128 if( wfIsWindows() || is_executable( $full ) ) {
129 return $full;
130 }
131 }
132 }
133 return false;
134 }
135
136 function readlineEmulation( $prompt ) {
137 $bash = "bash";
138 if( !wfIsWindows() && findExecutable( $bash ) ) {
139 $retval = false;
140 $encPrompt = wfEscapeShellArg( $prompt );
141 $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
142 $encCommand = wfEscapeShellArg( $command );
143 $line = wfShellExec( "$bash -c $encCommand", $retval );
144
145 if( $retval == 0 ) {
146 return $line;
147 } elseif( $retval == 127 ) {
148 // Couldn't execute bash even though we thought we saw it.
149 // Shell probably spit out an error message, sorry :(
150 // Fall through to fgets()...
151 } else {
152 // EOF/ctrl+D
153 return false;
154 }
155 }
156
157 // Fallback... we'll have no editing controls, EWWW
158 if ( feof( STDIN ) ) {
159 return false;
160 }
161 print $prompt;
162 return fgets(STDIN, 1024);
163 }
164
165
166 #
167 # Read and execute SQL commands from a file
168 #
169 function dbsource( $fname, $db = false ) {
170 wfDeprecated( __METHOD__ );
171 if ( !$db ) {
172 // Try $wgDatabase, which is used in the install and update scripts
173 global $wgDatabase;
174 if ( isset( $wgDatabase ) ) {
175 $db = $wgDatabase;
176 } else {
177 // No? Well, we must be outside of those scripts, so use the standard method
178 $db = wfGetDB( DB_MASTER );
179 }
180 }
181 $error = $db->sourceFile( $fname );
182 if ( $error !== true ) {
183 print $error;
184 exit(1);
185 }
186 }
187
188 /**
189 * Get the value of session.save_path
190 *
191 * Per http://www.php.net/manual/en/ref.session.php#ini.session.save-path,
192 * this might have some additional preceding parts which need to be
193 * ditched
194 *
195 * @return string
196 */
197 function mw_get_session_save_path() {
198 $path = ini_get( 'session.save_path' );
199 $path = substr( $path, strrpos( $path, ';' ) );
200 return $path;
201 }
202
203 /**
204 * Is dl() available to us?
205 *
206 * According to http://www.php.net/manual/en/function.dl.php, dl()
207 * is *not* available when `enable_dl` is off, or under `safe_mode`
208 *
209 * @return bool
210 */
211 function mw_have_dl() {
212 return function_exists( 'dl' )
213 && is_callable( 'dl' )
214 && wfIniGetBool( 'enable_dl' )
215 && !wfIniGetBool( 'safe_mode' );
216 }