Fixed bug causing infinite recursion on failure to contact a slave. Some other genera...
[mediawiki.git] / install-utils.inc
blob34dd701762ae998a85095c98fa3a3f01287cee88
1 <?php
3 function install_version_checks() {
4         # Turn off output buffering if it's on
5         @ob_end_flush();
6         
7         if( !function_exists( "version_compare" ) ) {
8                 # version_compare was introduced in 4.1.0
9                 die( "Your PHP version is much too old; 4.0.x will _not_ work. 4.3.2 or higher is recommended. ABORTING.\n" );
10         }
11         if( version_compare( phpversion(), "4.3.2" ) < 0 ) {
12                 echo "WARNING: PHP 4.3.2 or higher is recommended. Older versions from 4.1.x up may work but are not actively supported.\n\n";
13         }
14         
15         if (!extension_loaded('mysql')) {
16                 if (!dl('mysql.so')) {
17                         print "Could not load MySQL driver! Please compile ".
18                                   "php --with-mysql or install the mysql.so module.\n";
19                 exit;
20                 }
21         }
22         
23         global $wgCommandLineMode;
24         $wgCommandLineMode = true;
25         umask( 000 );
26         set_time_limit( 0 );
29 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
30         copyfileto( $sdir, $name, $ddir, $name, $perms );
33 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
34         global $wgInstallOwner, $wgInstallGroup;
36         $d = "{$ddir}/{$dname}";
37         if ( copy( "{$sdir}/{$sname}", $d ) ) {
38                 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
39                 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
40                 chmod( $d, $perms );
41                 # print "Copied \"{$sname}\" to \"{$d}\".\n";
42         } else {
43                 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
44                 exit();
45         }
48 function copydirectory( $source, $dest ) {
49         $handle = opendir( $source );
50         while ( false !== ( $f = readdir( $handle ) ) ) {
51                 $fullname = "$source/$f";
52                 if ( $f{0} !="." && is_file( $fullname ) ) {
53                         copyfile( $source, $f, $dest );
54                 }
55         }
58 function readconsole( $prompt = "" ) {
59         if ( function_exists( "readline" ) ) {
60                 return readline( $prompt );
61         } else {
62                 print $prompt;
63                 $fp = fopen( "php://stdin", "r" );
64                 $resp = trim( fgets( $fp, 1024 ) );
65                 fclose( $fp );
66                 return $resp;
67         }
70 function replacevars( $ins ) {
71         $varnames = array(
72                 "wgDBserver", "wgDBname", "wgDBintlname", "wgDBuser",
73                 "wgDBpassword", "wgDBsqluser", "wgDBsqlpassword",
74                 "wgDBadminuser", "wgDBadminpassword"
75         );
77         foreach ( $varnames as $var ) {
78                 global $$var;
79                 $ins = str_replace( '{$' . $var . '}', $$var, $ins );
80         }
81         return $ins;
85 # Read and execute SQL commands from a file
87 function dbsource( $fname, $database = false ) {
88         $fp = fopen( $fname, "r" );
89         if ( false === $fp ) {
90                 print "Could not open \"{$fname}\".\n";
91                 exit();
92         }
94         $cmd = "";
95         $done = false;
97         while ( ! feof( $fp ) ) {
98                 $line = trim( fgets( $fp, 1024 ) );
99                 $sl = strlen( $line ) - 1;
101                 if ( $sl < 0 ) { continue; }
102                 if ( "-" == $line{0} && "-" == $line{1} ) { continue; }
104                 if ( ";" == $line{$sl} ) {
105                         $done = true;
106                         $line = substr( $line, 0, $sl );
107                 }
109                 if ( "" != $cmd ) { $cmd .= " "; }
110                 $cmd .= $line;
112                 if ( $done ) {
113                         $cmd = replacevars( $cmd );
114                         if( $database )
115                                 $res = $database->query( $cmd );
116                         else
117                                 $res = mysql_query( $cmd );
119                         if ( false === $res ) {
120                                 $err = mysql_error();
121                                 print "Query \"{$cmd}\" failed with error code \"$err\".\n";
122                                 exit();
123                         }
125                         $cmd = "";
126                         $done = false;
127                 }
128         }
129         fclose( $fp );
132 # Obsolete, use Database::fieldExists()
133 function field_exists( $table, $field ) {
134         $fname = "Update script: field_exists";
135         $db =& wfGetDB( DB_SLAVE );
136         $res = $db->query( "DESCRIBE $table", $fname );
137         $found = false;
138         
139         while ( $row = $db->fetchObject( $res ) ) {
140                 if ( $row->Field == $field ) {
141                         $found = true;
142                         break;
143                 }
144         }
145         return $found;
148 # Obsolete Database::tableExists()
149 function table_exists( $db ) {
150         global $wgDBname;
151         $res = mysql_list_tables( $wgDBname );
152         if( !$res ) {
153                 echo "** " . mysql_error() . "\n";
154                 return false;
155         }
156         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
157                 if( mysql_tablename( $res, $i ) == $db ) return true;
158         }
159         return false;
162 # Obsolete, use Database:fieldInfo()
163 function field_info( $table, $field ) {
164         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
165         $n = mysql_num_fields( $res );
166         for( $i = 0; $i < $n; $i++ ) {
167                 $meta = mysql_fetch_field( $res, $i );
168                 if( $field == $meta->name ) {
169                         return $meta;
170                 }
171         }
172         return false;