bb2cac399977f25bee87d56297b55a47474a5ad4
[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 $test->execute();
48 if ( !$test->ok ) {
49 echo "PHP 5.3.1 is not compatible with MediaWiki due to a bug involving\n" .
50 "reference parameters to __call. Upgrade to PHP 5.3.2 or higher, or \n" .
51 "downgrade to PHP 5.3.0 to fix this.\n" .
52 "ABORTING (see http://bugs.php.net/bug.php?id=50394 for details)\n";
53 die( 1 );
54 }
55
56 global $wgCommandLineMode;
57 $wgCommandLineMode = true;
58 umask( 000 );
59 @set_time_limit( 0 );
60 }
61
62 /**
63 * Test for PHP+libxml2 bug which breaks XML input subtly with certain versions.
64 * http://bugs.php.net/bug.php?id=45996
65 * Known fixed with PHP 5.2.9 + libxml2-2.7.3
66 */
67 class PhpXmlBugTester {
68 private $parsedData = '';
69 public $ok = false;
70 public function __construct() {
71 $charData = '<b>c</b>';
72 $xml = '<a>' . htmlspecialchars( $charData ) . '</a>';
73
74 $parser = xml_parser_create();
75 xml_set_character_data_handler( $parser, array( $this, 'chardata' ) );
76 $parsedOk = xml_parse($parser, $xml, true);
77 $this->ok = $parsedOk && ($this->parsedData == $charData);
78 }
79 public function chardata($parser, $data) {
80 $this->parsedData .= $data;
81 }
82 }
83
84 /**
85 * Test for PHP bug #50394 (PHP 5.3.x conversion to null only, not 5.2.x)
86 */
87 class PhpRefCallBugTester {
88 public $ok = false;
89
90 function __call( $name, $args ) {
91 $old = error_reporting( E_ALL & ~E_WARNING );
92 call_user_func_array( array( $this, 'checkForBrokenRef' ), $args );
93 error_reporting( $old );
94 }
95
96 function checkForBrokenRef( &$var ) {
97 if ( $var ) {
98 $this->ok = true;
99 }
100 }
101
102 function execute() {
103 $var = true;
104 call_user_func_array( array( $this, 'foo' ), array( &$var ) );
105 }
106 }
107
108 function readconsole( $prompt = '' ) {
109 static $isatty = null;
110 if ( is_null( $isatty ) ) {
111 if ( !function_exists( 'posix_isatty' ) || posix_isatty( 0 /*STDIN*/ ) ) {
112 $isatty = true;
113 } else {
114 $isatty = false;
115 }
116 }
117
118 if ( $isatty && function_exists( 'readline' ) ) {
119 return readline( $prompt );
120 } else {
121 if ( $isatty ) {
122 $st = readlineEmulation( $prompt );
123 } else {
124 if ( feof( STDIN ) ) {
125 $st = false;
126 } else {
127 $st = fgets(STDIN, 1024);
128 }
129 }
130 if ($st === false) return false;
131 $resp = trim( $st );
132 return $resp;
133 }
134 }
135
136 function findExecutable( $name ) {
137 $paths = explode( PATH_SEPARATOR, getenv( "PATH" ) );
138 foreach( $paths as $path ) {
139 $full = $path . DIRECTORY_SEPARATOR . $name;
140 if( file_exists( $full ) ) {
141 if( wfIsWindows() || is_executable( $full ) ) {
142 return $full;
143 }
144 }
145 }
146 return false;
147 }
148
149 function readlineEmulation( $prompt ) {
150 $bash = "bash";
151 if( !wfIsWindows() && findExecutable( $bash ) ) {
152 $retval = false;
153 $encPrompt = wfEscapeShellArg( $prompt );
154 $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
155 $encCommand = wfEscapeShellArg( $command );
156 $line = wfShellExec( "$bash -c $encCommand", $retval );
157
158 if( $retval == 0 ) {
159 return $line;
160 } elseif( $retval == 127 ) {
161 // Couldn't execute bash even though we thought we saw it.
162 // Shell probably spit out an error message, sorry :(
163 // Fall through to fgets()...
164 } else {
165 // EOF/ctrl+D
166 return false;
167 }
168 }
169
170 // Fallback... we'll have no editing controls, EWWW
171 if ( feof( STDIN ) ) {
172 return false;
173 }
174 print $prompt;
175 return fgets(STDIN, 1024);
176 }
177
178
179 #
180 # Read and execute SQL commands from a file
181 #
182 function dbsource( $fname, $db = false ) {
183 wfDeprecated( __METHOD__ );
184 if ( !$db ) {
185 // Try $wgDatabase, which is used in the install and update scripts
186 global $wgDatabase;
187 if ( isset( $wgDatabase ) ) {
188 $db = $wgDatabase;
189 } else {
190 // No? Well, we must be outside of those scripts, so use the standard method
191 $db = wfGetDB( DB_MASTER );
192 }
193 }
194 $error = $db->sourceFile( $fname );
195 if ( $error !== true ) {
196 print $error;
197 exit(1);
198 }
199 }
200
201 /**
202 * Get the value of session.save_path
203 *
204 * Per http://www.php.net/manual/en/ref.session.php#ini.session.save-path,
205 * this might have some additional preceding parts which need to be
206 * ditched
207 *
208 * @return string
209 */
210 function mw_get_session_save_path() {
211 $path = ini_get( 'session.save_path' );
212 $path = substr( $path, strrpos( $path, ';' ) );
213 return $path;
214 }
215
216 /**
217 * Is dl() available to us?
218 *
219 * According to http://www.php.net/manual/en/function.dl.php, dl()
220 * is *not* available when `enable_dl` is off, or under `safe_mode`
221 *
222 * @return bool
223 */
224 function mw_have_dl() {
225 return function_exists( 'dl' )
226 && is_callable( 'dl' )
227 && wfIniGetBool( 'enable_dl' )
228 && !wfIniGetBool( 'safe_mode' );
229 }