Fix for r65818.
[mediawiki.git] / maintenance / install-utils.inc
blob54a76ae2ff9ddde6734185293073f9f5dd27f416
1 <?php
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  */
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 );
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         }
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         }
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         }
46         global $wgCommandLineMode;
47         $wgCommandLineMode = true;
48         umask( 000 );
49         @set_time_limit( 0 );
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>';
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         }
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;
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         }
86         function checkForBrokenRef( &$var ) {
87                 if ( $var ) {
88                         $this->ok = true;
89                 }
90         }
92         function execute() {
93                 $var = true;
94                 call_user_func_array( array( $this, 'foo' ), array( &$var ) );
95         }
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         }
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         }
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         }
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;
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 );
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         }
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);
178 # Read and execute SQL commands from a file
180 function dbsource( $fname, $db = false ) {
181         wfDeprecated( __METHOD__ );
182         if ( !$db ) {
183                 // Try $wgDatabase, which is used in the install and update scripts
184                 global $wgDatabase;
185                 if ( isset( $wgDatabase ) ) {
186                         $db = $wgDatabase;
187                 } else {
188                         // No? Well, we must be outside of those scripts, so use the standard method
189                         $db = wfGetDB( DB_MASTER );
190                 }
191         }
192         $error = $db->sourceFile( $fname );
193         if ( $error !== true ) {
194                 print $error;
195                 exit(1);
196         }
200  * Get the value of session.save_path
202  * Per http://www.php.net/manual/en/ref.session.php#ini.session.save-path,
203  * this might have some additional preceding parts which need to be
204  * ditched
206  * @return string
207  */
208 function mw_get_session_save_path() {
209         $path = ini_get( 'session.save_path' );
210         $path = substr( $path, strrpos( $path, ';' ) );
211         return $path;
215  * Is dl() available to us?
217  * According to http://www.php.net/manual/en/function.dl.php, dl()
218  * is *not* available when `enable_dl` is off, or under `safe_mode`
220  * @return bool
221  */
222 function mw_have_dl() {
223         return function_exists( 'dl' )
224                 && is_callable( 'dl' )
225                 && wfIniGetBool( 'enable_dl' )
226                 && !wfIniGetBool( 'safe_mode' );