Ok I think this is a sign I'm done for the evening. Fixing other test checkin --...
[lhc/web/wiklou.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 global $wgCommandLineMode;
38 $wgCommandLineMode = true;
39 umask( 000 );
40 @set_time_limit( 0 );
41 }
42
43 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
44 copyfileto( $sdir, $name, $ddir, $name, $perms );
45 }
46
47 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
48 global $wgInstallOwner, $wgInstallGroup;
49
50 $d = "{$ddir}/{$dname}";
51 if ( copy( "{$sdir}/{$sname}", $d ) ) {
52 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
53 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
54 chmod( $d, $perms );
55 # print "Copied \"{$sname}\" to \"{$d}\".\n";
56 } else {
57 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
58 exit();
59 }
60 }
61
62 function copydirectory( $source, $dest ) {
63 $handle = opendir( $source );
64 while ( false !== ( $f = readdir( $handle ) ) ) {
65 $fullname = "$source/$f";
66 if ( $f{0} != '.' && is_file( $fullname ) ) {
67 copyfile( $source, $f, $dest );
68 }
69 }
70 }
71
72 function readconsole( $prompt = '' ) {
73 static $isatty = null;
74 if ( is_null( $isatty ) ) {
75 if ( !function_exists( 'posix_isatty' ) || posix_isatty( 0 /*STDIN*/ ) ) {
76 $isatty = true;
77 } else {
78 $isatty = false;
79 }
80 }
81
82 if ( $isatty && function_exists( 'readline' ) ) {
83 return readline( $prompt );
84 } else {
85 if ( $isatty ) {
86 $st = readlineEmulation( $prompt );
87 } else {
88 if ( feof( STDIN ) ) {
89 $st = false;
90 } else {
91 $st = fgets(STDIN, 1024);
92 }
93 }
94 if ($st === false) return false;
95 $resp = trim( $st );
96 return $resp;
97 }
98 }
99
100 function findExecutable( $name ) {
101 $paths = explode( PATH_SEPARATOR, getenv( "PATH" ) );
102 foreach( $paths as $path ) {
103 $full = $path . DIRECTORY_SEPARATOR . $name;
104 if( file_exists( $full ) ) {
105 if( wfIsWindows() || is_executable( $full ) ) {
106 return $full;
107 }
108 }
109 }
110 return false;
111 }
112
113 function readlineEmulation( $prompt ) {
114 $bash = "bash";
115 if( !wfIsWindows() && findExecutable( $bash ) ) {
116 $retval = false;
117 $encPrompt = wfEscapeShellArg( $prompt );
118 $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
119 $encCommand = wfEscapeShellArg( $command );
120 $line = wfShellExec( "$bash -c $encCommand", $retval );
121
122 if( $retval == 0 ) {
123 return $line;
124 } elseif( $retval == 127 ) {
125 // Couldn't execute bash even though we thought we saw it.
126 // Shell probably spit out an error message, sorry :(
127 // Fall through to fgets()...
128 } else {
129 // EOF/ctrl+D
130 return false;
131 }
132 }
133
134 // Fallback... we'll have no editing controls, EWWW
135 if ( feof( STDIN ) ) {
136 return false;
137 }
138 print $prompt;
139 return fgets(STDIN, 1024);
140 }
141
142
143 #
144 # Read and execute SQL commands from a file
145 #
146 function dbsource( $fname, $db = false ) {
147 wfDeprecated( __METHOD__ );
148 if ( !$db ) {
149 // Try $wgDatabase, which is used in the install and update scripts
150 global $wgDatabase;
151 if ( isset( $wgDatabase ) ) {
152 $db = $wgDatabase;
153 } else {
154 // No? Well, we must be outside of those scripts, so use the standard method
155 $db = wfGetDB( DB_MASTER );
156 }
157 }
158 $error = $db->sourceFile( $fname );
159 if ( $error !== true ) {
160 print $error;
161 exit(1);
162 }
163 }
164
165 /**
166 * Get the value of session.save_path
167 *
168 * Per http://www.php.net/manual/en/ref.session.php#ini.session.save-path,
169 * this might have some additional preceding parts which need to be
170 * ditched
171 *
172 * @return string
173 */
174 function mw_get_session_save_path() {
175 $path = ini_get( 'session.save_path' );
176 $path = substr( $path, strrpos( $path, ';' ) );
177 return $path;
178 }
179
180 /**
181 * Is dl() available to us?
182 *
183 * According to http://www.php.net/manual/en/function.dl.php, dl()
184 * is *not* available when `enable_dl` is off, or under `safe_mode`
185 *
186 * @return bool
187 */
188 function mw_have_dl() {
189 return function_exists( 'dl' )
190 && is_callable( 'dl' )
191 && wfIniGetBool( 'enable_dl' )
192 && !wfIniGetBool( 'safe_mode' );
193 }