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