notice hopefully gone now
[mediawiki.git] / install-utils.inc
blob608c6abcd5132452c05a7ab51440a082e1331cbb
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() {
59         $fp = fopen( "php://stdin", "r" );
60         $resp = trim( fgets( $fp, 1024 ) );
61         fclose( $fp );
62         return $resp;
66 function replacevars( $ins ) {
67         $varnames = array(
68                 "wgDBserver", "wgDBname", "wgDBintlname", "wgDBuser",
69                 "wgDBpassword", "wgDBsqluser", "wgDBsqlpassword",
70                 "wgDBadminuser", "wgDBadminpassword"
71         );
73         foreach ( $varnames as $var ) {
74                 global $$var;
75                 $ins = str_replace( '{$' . $var . '}', $$var, $ins );
76         }
77         return $ins;
81 # Read and execute SQL commands from a file
83 function dbsource( $fname, $database = false ) {
84         $fp = fopen( $fname, "r" );
85         if ( false === $fp ) {
86                 print "Could not open \"{$fname}\".\n";
87                 exit();
88         }
90         $cmd = "";
91         $done = false;
93         while ( ! feof( $fp ) ) {
94                 $line = trim( fgets( $fp, 1024 ) );
95                 $sl = strlen( $line ) - 1;
97                 if ( $sl < 0 ) { continue; }
98                 if ( "-" == $line{0} && "-" == $line{1} ) { continue; }
100                 if ( ";" == $line{$sl} ) {
101                         $done = true;
102                         $line = substr( $line, 0, $sl );
103                 }
105                 if ( "" != $cmd ) { $cmd .= " "; }
106                 $cmd .= $line;
108                 if ( $done ) {
109                         $cmd = replacevars( $cmd );
110                         if( $database )
111                                 $res = $database->query( $cmd );
112                         else
113                                 $res = mysql_query( $cmd );
115                         if ( false === $res ) {
116                                 print "Query \"{$cmd}\" failed.\n";
117                                 exit();
118                         }
120                         $cmd = "";
121                         $done = false;
122                 }
123         }
124         fclose( $fp );
127 # Obsolete, use Database::fieldExists()
128 function field_exists( $table, $field ) {
129         $fname = "Update script: field_exists";
130         $res = wfQuery( "DESCRIBE $table", $fname );
131         $found = false;
132         
133         while ( $row = wfFetchObject( $res ) ) {
134                 if ( $row->Field == $field ) {
135                         $found = true;
136                         break;
137                 }
138         }
139         return $found;
142 # Obsolete Database::tableExists()
143 function table_exists( $db ) {
144         global $wgDBname;
145         $res = mysql_list_tables( $wgDBname );
146         if( !$res ) {
147                 echo "** " . mysql_error() . "\n";
148                 return false;
149         }
150         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
151                 if( mysql_tablename( $res, $i ) == $db ) return true;
152         }
153         return false;
156 # Obsolete, use Database:fieldInfo()
157 function field_info( $table, $field ) {
158         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
159         $n = mysql_num_fields( $res );
160         for( $i = 0; $i < $n; $i++ ) {
161                 $meta = mysql_fetch_field( $res, $i );
162                 if( $field == $meta->name ) {
163                         return $meta;
164                 }
165         }
166         return false;