]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - install-utils.inc
MediaWiki 1.15.3
[autoinstallsdev/mediawiki.git] / 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
47         $test = new PhpRefCallBugTester;
48         $test->execute();
49         if ( !$test->ok ) {
50                 echo "PHP 5.3.1 is not compatible with MediaWiki due to a bug involving\n" .
51                         "reference parameters to __call. Upgrade to PHP 5.3.2 or higher, or \n" .
52                         "downgrade to PHP 5.3.0 to fix this.\n" .
53                         "ABORTING (see http://bugs.php.net/bug.php?id=50394 for details)\n";
54                 die( -1 );
55         }
56
57         global $wgCommandLineMode;
58         $wgCommandLineMode = true;
59         umask( 000 );
60         @set_time_limit( 0 );
61 }
62
63 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
64         copyfileto( $sdir, $name, $ddir, $name, $perms );
65 }
66
67 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
68         global $wgInstallOwner, $wgInstallGroup;
69
70         $d = "{$ddir}/{$dname}";
71         if ( copy( "{$sdir}/{$sname}", $d ) ) {
72                 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
73                 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
74                 chmod( $d, $perms );
75                 # print "Copied \"{$sname}\" to \"{$d}\".\n";
76         } else {
77                 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
78                 exit();
79         }
80 }
81
82 function copydirectory( $source, $dest ) {
83         $handle = opendir( $source );
84         while ( false !== ( $f = readdir( $handle ) ) ) {
85                 $fullname = "$source/$f";
86                 if ( $f{0} != '.' && is_file( $fullname ) ) {
87                         copyfile( $source, $f, $dest );
88                 }
89         }
90 }
91
92 /**
93  * Test for PHP+libxml2 bug which breaks XML input subtly with certain versions.
94  * http://bugs.php.net/bug.php?id=45996
95  * Known fixed with PHP 5.2.9 + libxml2-2.7.3
96  */
97 class PhpXmlBugTester {
98         var $parsedData = '';
99         var $ok = false;
100         function __construct() {
101                 $charData = '<b>c</b>';
102                 $xml = '<a>' . htmlspecialchars( $charData ) . '</a>';
103                 
104                 $parser = xml_parser_create();
105                 xml_set_character_data_handler( $parser, array( $this, 'chardata' ) );
106                 $parsedOk = xml_parse($parser, $xml, true);
107                 $this->ok = $parsedOk && ($this->parsedData == $charData);
108         }
109         function chardata($parser, $data) {
110                 $this->parsedData .= $data;
111         }
112 }
113
114 /**
115  * Test for PHP bug #50394 (PHP 5.3.x conversion to null only, not 5.2.x)
116  */
117 class PhpRefCallBugTester {
118         public $ok = false;
119
120         function __call( $name, $args ) {
121                 $old = error_reporting( E_ALL & ~E_WARNING );
122                 call_user_func_array( array( $this, 'checkForBrokenRef' ), $args );
123                 error_reporting( $old );
124         }
125
126         function checkForBrokenRef( &$var ) {
127                 if ( $var ) {
128                         $this->ok = true;
129                 }
130         }
131
132         function execute() {
133                 $var = true;
134                 call_user_func_array( array( $this, 'foo' ), array( &$var ) );
135         }
136 }
137
138 function readconsole( $prompt = '' ) {
139         static $isatty = null;
140         if ( is_null( $isatty ) ) {
141                 if ( !function_exists( 'posix_isatty' ) || posix_isatty( 0 /*STDIN*/ ) ) {
142                         $isatty = true;
143                 } else {
144                         $isatty = false;
145                 }
146         }
147
148         if ( $isatty && function_exists( 'readline' ) ) {
149                 return readline( $prompt );
150         } else {
151                 if ( $isatty ) {
152                         print $prompt;
153                 }
154                 if ( feof( STDIN ) ) {
155                         return false;
156                 }
157                 $st = fgets(STDIN, 1024);
158                 if ($st === false) return false;
159                 $resp = trim( $st );
160                 return $resp;
161         }
162 }
163
164 #
165 # Read and execute SQL commands from a file
166 #
167 function dbsource( $fname, $db = false ) {
168         if ( !$db ) {
169                 // Try $wgDatabase, which is used in the install and update scripts
170                 global $wgDatabase;
171                 if ( isset( $wgDatabase ) ) {
172                         $db = $wgDatabase;
173                 } else {
174                         // No? Well, we must be outside of those scripts, so use the standard method
175                         $db = wfGetDB( DB_MASTER );
176                 }
177         }
178         $error = $db->sourceFile( $fname );
179         if ( $error !== true ) {
180                 print $error;
181                 exit(1);
182         }
183 }
184
185 /**
186  * Get the value of session.save_path
187  *
188  * Per http://www.php.net/manual/en/ref.session.php#ini.session.save-path,
189  * this might have some additional preceding parts which need to be
190  * ditched
191  *
192  * @return string
193  */
194 function mw_get_session_save_path() {
195         $path = ini_get( 'session.save_path' );
196         $path = substr( $path, strrpos( $path, ';' ) );
197         return $path;
198 }
199
200 /**
201  * Is dl() available to us?
202  *
203  * According to http://www.php.net/manual/en/function.dl.php, dl()
204  * is *not* available when `enable_dl` is off, or under `safe_mode`
205  *
206  * @return bool
207  */
208 function mw_have_dl() {
209         return function_exists( 'dl' )
210                 && is_callable( 'dl' )
211                 && wfIniGetBool( 'enable_dl' )
212                 && !wfIniGetBool( 'safe_mode' );
213 }