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