typo
[mediawiki.git] / install-utils.inc
blobfaabab61ba349e0120390ca311eff1e1f1764c38
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                 die( "Your PHP version is much too old; 4.0.x will _not_ work. 4.3.2 or higher is recommended. ABORTING.\n" );
11         }
12         if( version_compare( phpversion(), '4.3.2' ) < 0 ) {
13                 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";
14         }
16         if (!extension_loaded('mysql')) {
17                 if (!dl('mysql.so')) {
18                         print 'Could not load MySQL driver! Please compile '.
19                                   "php --with-mysql or install the mysql.so module.\n";
20                 exit;
21                 }
22         }
24         global $wgCommandLineMode;
25         $wgCommandLineMode = true;
26         umask( 000 );
27         set_time_limit( 0 );
30 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
31         copyfileto( $sdir, $name, $ddir, $name, $perms );
34 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
35         global $wgInstallOwner, $wgInstallGroup;
37         $d = "{$ddir}/{$dname}";
38         if ( copy( "{$sdir}/{$sname}", $d ) ) {
39                 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
40                 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
41                 chmod( $d, $perms );
42                 # print "Copied \"{$sname}\" to \"{$d}\".\n";
43         } else {
44                 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
45                 exit();
46         }
49 function copydirectory( $source, $dest ) {
50         $handle = opendir( $source );
51         while ( false !== ( $f = readdir( $handle ) ) ) {
52                 $fullname = "$source/$f";
53                 if ( $f{0} != '.' && is_file( $fullname ) ) {
54                         copyfile( $source, $f, $dest );
55                 }
56         }
59 function readconsole( $prompt = '' ) {
60         if ( function_exists( 'readline' ) ) {
61                 return readline( $prompt );
62         } else {
63                 print $prompt;
64                 $fp = fopen( 'php://stdin', 'r' );
65                 $st = fgets($fp, 1024);
66                 if ($st === false) return false;
67                 $resp = trim( $st );
68                 fclose( $fp );
69                 return $resp;
70         }
73 function replacevars( $ins ) {
74         $varnames = array(
75                 'wgDBserver', 'wgDBname', 'wgDBintlname', 'wgDBuser',
76                 'wgDBpassword', 'wgDBsqluser', 'wgDBsqlpassword',
77                 'wgDBadminuser', 'wgDBadminpassword', 'wgDBprefix'
78         );
80         foreach ( $varnames as $var ) {
81                 if( isset( $GLOBALS[$var] ) ) {
82                         $val = addslashes( $GLOBALS[$var] );
83                         $ins = str_replace( '{$' . $var . '}', $val, $ins );
84                         $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins );
85                         $ins = str_replace( '/*$' . $var . '*/', $val, $ins );
86                 }
87         }
88         return $ins;
92 # Read and execute SQL commands from a file
94 function dbsource( $fname, $database = false ) {
95         $fp = fopen( $fname, 'r' );
96         if ( false === $fp ) {
97                 print "Could not open \"{$fname}\".\n";
98                 exit();
99         }
101         $cmd = "";
102         $done = false;
104         while ( ! feof( $fp ) ) {
105                 $line = trim( fgets( $fp, 1024 ) );
106                 $sl = strlen( $line ) - 1;
108                 if ( $sl < 0 ) { continue; }
109                 if ( '-' == $line{0} && '-' == $line{1} ) { continue; }
111                 if ( ';' == $line{$sl} && ($sl < 2 || ';' != $line{$sl - 1})) {
112                         $done = true;
113                         $line = substr( $line, 0, $sl );
114                 }
116                 if ( '' != $cmd ) { $cmd .= ' '; }
117                 $cmd .= "$line\n";
119                 if ( $done ) {
120                         $cmd = str_replace(';;', ";", $cmd);
121                         $cmd = replacevars( $cmd );
122                         if( $database )
123                                 $res = $database->query( $cmd, 'dbsource', true );
124                         else
125                                 $res = mysql_query( $cmd );
127                         if ( false === $res ) {
128                                 $err = mysql_error();
129                                 print "Query \"{$cmd}\" failed with error code \"$err\".\n";
130                                 exit();
131                         }
133                         $cmd = '';
134                         $done = false;
135                 }
136         }
137         fclose( $fp );
140 # Obsolete, use Database::fieldExists()
141 function field_exists( $table, $field ) {
142         $fname = 'Update script: field_exists';
143         $db =& wfGetDB( DB_SLAVE );
144         $res = $db->query( "DESCRIBE $table", $fname );
145         $found = false;
147         while ( $row = $db->fetchObject( $res ) ) {
148                 if ( $row->Field == $field ) {
149                         $found = true;
150                         break;
151                 }
152         }
153         return $found;
156 # Obsolete Database::tableExists()
157 function table_exists( $db ) {
158         global $wgDBname;
159         $res = mysql_list_tables( $wgDBname );
160         if( !$res ) {
161                 echo "** " . mysql_error() . "\n";
162                 return false;
163         }
164         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
165                 if( mysql_tablename( $res, $i ) == $db ) return true;
166         }
167         return false;
170 # Obsolete, use Database:fieldInfo()
171 function field_info( $table, $field ) {
172         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
173         $n = mysql_num_fields( $res );
174         for( $i = 0; $i < $n; $i++ ) {
175                 $meta = mysql_fetch_field( $res, $i );
176                 if( $field == $meta->name ) {
177                         return $meta;
178                 }
179         }
180         return false;