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