Fix misplaced comma
[mediawiki.git] / install-utils.inc
blobcacb8e590c7ade26a6344caf8b0c0f438fef817f
1 <?php
3 function install_version_checks() {
4         # We dare not turn output buffer _off_ since this will break completely
5         # if PHP is globally configured to run through a gzip filter.
6         @ob_implicit_flush( true );
8         if( !function_exists( 'version_compare' ) ) {
9                 # version_compare was introduced in 4.1.0
10                 echo "Your PHP version is much too old; 4.0.x will _not_ work. 4.3.2 or higher is required. ABORTING.\n";
11                 die( -1 );
12         }
13         if( version_compare( phpversion(), '4.3.2' ) < 0 ) {
14                 echo "PHP 4.3.2 or higher is required. ABORTING.\n";
15                 die( -1 );
16         }
18         if (!extension_loaded('mysql')) {
19                 if (!dl('mysql.so')) {
20                         print 'Could not load MySQL driver! Please compile '.
21                                   "php --with-mysql or install the mysql.so module.\n";
22                 exit;
23                 }
24         }
26         global $wgCommandLineMode;
27         $wgCommandLineMode = true;
28         umask( 000 );
29         set_time_limit( 0 );
32 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
33         copyfileto( $sdir, $name, $ddir, $name, $perms );
36 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
37         global $wgInstallOwner, $wgInstallGroup;
39         $d = "{$ddir}/{$dname}";
40         if ( copy( "{$sdir}/{$sname}", $d ) ) {
41                 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
42                 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
43                 chmod( $d, $perms );
44                 # print "Copied \"{$sname}\" to \"{$d}\".\n";
45         } else {
46                 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
47                 exit();
48         }
51 function copydirectory( $source, $dest ) {
52         $handle = opendir( $source );
53         while ( false !== ( $f = readdir( $handle ) ) ) {
54                 $fullname = "$source/$f";
55                 if ( $f{0} != '.' && is_file( $fullname ) ) {
56                         copyfile( $source, $f, $dest );
57                 }
58         }
61 function readconsole( $prompt = '' ) {
62         static $isatty = null, $fp = null;
63         if ( is_null( $fp ) ) {
64                 $fp = fopen( 'php://stdin', 'r' );
65         }
66         if ( is_null( $isatty ) ) {
67                 if ( !function_exists( 'posix_isatty' ) || posix_isatty( $fp ) ) {
68                         $isatty = true;
69                 } else {
70                         $isatty = false;
71                 }
72         }
74         if ( $isatty && function_exists( 'readline' ) ) {
75                 return readline( $prompt );
76         } else {
77                 if ( $isatty ) {
78                         print $prompt;
79                 }
80                 if ( feof( $fp ) ) {
81                         return false;
82                 }
83                 $st = fgets($fp, 1024);
84                 if ($st === false) return false;
85                 $resp = trim( $st );
86                 return $resp;
87         }
90 function replacevars( $ins ) {
91         $varnames = array(
92                 'wgDBserver', 'wgDBname', 'wgDBintlname', 'wgDBuser',
93                 'wgDBpassword', 'wgDBsqluser', 'wgDBsqlpassword',
94                 'wgDBadminuser', 'wgDBadminpassword', 'wgDBprefix'
95         );
97         foreach ( $varnames as $var ) {
98                 if( isset( $GLOBALS[$var] ) ) {
99                         $val = addslashes( $GLOBALS[$var] );
100                         $ins = str_replace( '{$' . $var . '}', $val, $ins );
101                         $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins );
102                         $ins = str_replace( '/*$' . $var . '*/', $val, $ins );
103                 }
104         }
105         return $ins;
109 # Read and execute SQL commands from a file
111 function dbsource( $fname, $database = false ) {
112         $fp = fopen( $fname, 'r' );
113         if ( false === $fp ) {
114                 print "Could not open \"{$fname}\".\n";
115                 exit();
116         }
118         $cmd = "";
119         $done = false;
121         while ( ! feof( $fp ) ) {
122                 $line = trim( fgets( $fp, 1024 ) );
123                 $sl = strlen( $line ) - 1;
125                 if ( $sl < 0 ) { continue; }
126                 if ( '-' == $line{0} && '-' == $line{1} ) { continue; }
128                 if ( ';' == $line{$sl} && ($sl < 2 || ';' != $line{$sl - 1})) {
129                         $done = true;
130                         $line = substr( $line, 0, $sl );
131                 }
133                 if ( '' != $cmd ) { $cmd .= ' '; }
134                 $cmd .= "$line\n";
136                 if ( $done ) {
137                         $cmd = str_replace(';;', ";", $cmd);
138                         $cmd = replacevars( $cmd );
139                         if( $database )
140                                 $res = $database->query( $cmd, 'dbsource', true );
141                         else
142                                 $res = mysql_query( $cmd );
144                         if ( false === $res ) {
145                                 $err = mysql_error();
146                                 print "Query \"{$cmd}\" failed with error code \"$err\".\n";
147                                 exit();
148                         }
150                         $cmd = '';
151                         $done = false;
152                 }
153         }
154         fclose( $fp );
157 # Obsolete, use Database::fieldExists()
158 function field_exists( $table, $field ) {
159         $fname = 'Update script: field_exists';
160         $db =& wfGetDB( DB_SLAVE );
161         $res = $db->query( "DESCRIBE $table", $fname );
162         $found = false;
164         while ( $row = $db->fetchObject( $res ) ) {
165                 if ( $row->Field == $field ) {
166                         $found = true;
167                         break;
168                 }
169         }
170         return $found;
173 # Obsolete Database::tableExists()
174 function table_exists( $db ) {
175         global $wgDBname;
176         $res = mysql_list_tables( $wgDBname );
177         if( !$res ) {
178                 echo "** " . mysql_error() . "\n";
179                 return false;
180         }
181         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
182                 if( mysql_tablename( $res, $i ) == $db ) return true;
183         }
184         return false;
187 # Obsolete, use Database:fieldInfo()
188 function field_info( $table, $field ) {
189         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
190         $n = mysql_num_fields( $res );
191         for( $i = 0; $i < $n; $i++ ) {
192                 $meta = mysql_fetch_field( $res, $i );
193                 if( $field == $meta->name ) {
194                         return $meta;
195                 }
196         }
197         return false;