Removed $wgDatabase from dbsource()
[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 if ( !function_exists( 'posix_isatty' ) ) {
99 # We default as considering stdin a tty (for nice readline methods)
100 # but treating stout as not a tty to avoid color codes
101 function posix_isatty( $fd ) {
102 return !$fd;
103 }
104 }
105
106 function readconsole( $prompt = '' ) {
107 static $isatty = null;
108 if ( is_null( $isatty ) ) {
109 if ( posix_isatty( 0 /*STDIN*/ ) ) {
110 $isatty = true;
111 } else {
112 $isatty = false;
113 }
114 }
115
116 if ( $isatty && function_exists( 'readline' ) ) {
117 return readline( $prompt );
118 } else {
119 if ( $isatty ) {
120 $st = readlineEmulation( $prompt );
121 } else {
122 if ( feof( STDIN ) ) {
123 $st = false;
124 } else {
125 $st = fgets( STDIN, 1024 );
126 }
127 }
128 if ( $st === false ) return false;
129 $resp = trim( $st );
130 return $resp;
131 }
132 }
133
134 function findExecutable( $name ) {
135 $paths = explode( PATH_SEPARATOR, getenv( "PATH" ) );
136 foreach ( $paths as $path ) {
137 $full = $path . DIRECTORY_SEPARATOR . $name;
138 if ( file_exists( $full ) ) {
139 if ( wfIsWindows() || is_executable( $full ) ) {
140 return $full;
141 }
142 }
143 }
144 return false;
145 }
146
147 function readlineEmulation( $prompt ) {
148 $bash = "bash";
149 if ( !wfIsWindows() && findExecutable( $bash ) ) {
150 $retval = false;
151 $encPrompt = wfEscapeShellArg( $prompt );
152 $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
153 $encCommand = wfEscapeShellArg( $command );
154 $line = wfShellExec( "$bash -c $encCommand", $retval );
155
156 if ( $retval == 0 ) {
157 return $line;
158 } elseif ( $retval == 127 ) {
159 // Couldn't execute bash even though we thought we saw it.
160 // Shell probably spit out an error message, sorry :(
161 // Fall through to fgets()...
162 } else {
163 // EOF/ctrl+D
164 return false;
165 }
166 }
167
168 // Fallback... we'll have no editing controls, EWWW
169 if ( feof( STDIN ) ) {
170 return false;
171 }
172 print $prompt;
173 return fgets( STDIN, 1024 );
174 }
175
176
177 #
178 # Read and execute SQL commands from a file
179 #
180 function dbsource( $fname, $db = false ) {
181 wfDeprecated( __METHOD__ );
182 if ( !$db ) {
183 $db = wfGetDB( DB_MASTER );
184 }
185 $error = $db->sourceFile( $fname );
186 if ( $error !== true ) {
187 print $error;
188 exit( 1 );
189 }
190 }
191
192 /**
193 * Helper function: check if the given key is present in the updatelog table.
194 * Obviously, only use this for updates that occur after the updatelog table was
195 * created!
196 */
197 function update_row_exists( $key ) {
198 $row = wfGetDB( DB_MASTER )->selectRow(
199 'updatelog',
200 '1',
201 array( 'ul_key' => $key ),
202 __FUNCTION__
203 );
204 return (bool)$row;
205 }
206
207 /**
208 * Get the value of session.save_path
209 *
210 * Per http://www.php.net/manual/en/ref.session.php#ini.session.save-path,
211 * this might have some additional preceding parts which need to be
212 * ditched
213 *
214 * @return string
215 */
216 function mw_get_session_save_path() {
217 $path = ini_get( 'session.save_path' );
218 $path = substr( $path, strrpos( $path, ';' ) );
219 return $path;
220 }
221
222 /**
223 * Is dl() available to us?
224 *
225 * According to http://www.php.net/manual/en/function.dl.php, dl()
226 * is *not* available when `enable_dl` is off, or under `safe_mode`
227 *
228 * @return bool
229 */
230 function mw_have_dl() {
231 return function_exists( 'dl' )
232 && is_callable( 'dl' )
233 && wfIniGetBool( 'enable_dl' )
234 && !wfIniGetBool( 'safe_mode' );
235 }