quotes
[mediawiki.git] / install-utils.inc
blob913dae531ee1981538197f50358d5d25cce4aaa0
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 );
7         
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         }
15         
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         }
23         
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                 $resp = trim( fgets( $fp, 1024 ) );
66                 fclose( $fp );
67                 return $resp;
68         }
71 function replacevars( $ins ) {
72         $varnames = array(
73                 'wgDBserver', 'wgDBname', 'wgDBintlname', 'wgDBuser',
74                 'wgDBpassword', 'wgDBsqluser', 'wgDBsqlpassword',
75                 'wgDBadminuser', 'wgDBadminpassword', 'wgDBprefix'
76         );
78         foreach ( $varnames as $var ) {
79                 if( isset( $GLOBALS[$var] ) ) {
80                         $val = addslashes( $GLOBALS[$var] );
81                         $ins = str_replace( '{$' . $var . '}', $val, $ins );
82                         $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins );
83                         $ins = str_replace( '/*$' . $var . '*/', $val, $ins );
84                 }
85         }
86         return $ins;
90 # Read and execute SQL commands from a file
92 function dbsource( $fname, $database = false ) {
93         $fp = fopen( $fname, 'r' );
94         if ( false === $fp ) {
95                 print "Could not open \"{$fname}\".\n";
96                 exit();
97         }
99         $cmd = "";
100         $done = false;
102         while ( ! feof( $fp ) ) {
103                 $line = trim( fgets( $fp, 1024 ) );
104                 $sl = strlen( $line ) - 1;
106                 if ( $sl < 0 ) { continue; }
107                 if ( '-' == $line{0} && '-' == $line{1} ) { continue; }
109                 if ( ';' == $line{$sl} ) {
110                         $done = true;
111                         $line = substr( $line, 0, $sl );
112                 }
114                 if ( '' != $cmd ) { $cmd .= ' '; }
115                 $cmd .= $line;
117                 if ( $done ) {
118                         $cmd = replacevars( $cmd );
119                         if( $database )
120                                 $res = $database->query( $cmd );
121                         else
122                                 $res = mysql_query( $cmd );
124                         if ( false === $res ) {
125                                 $err = mysql_error();
126                                 print "Query \"{$cmd}\" failed with error code \"$err\".\n";
127                                 exit();
128                         }
130                         $cmd = '';
131                         $done = false;
132                 }
133         }
134         fclose( $fp );
137 # Obsolete, use Database::fieldExists()
138 function field_exists( $table, $field ) {
139         $fname = 'Update script: field_exists';
140         $db =& wfGetDB( DB_SLAVE );
141         $res = $db->query( "DESCRIBE $table", $fname );
142         $found = false;
143         
144         while ( $row = $db->fetchObject( $res ) ) {
145                 if ( $row->Field == $field ) {
146                         $found = true;
147                         break;
148                 }
149         }
150         return $found;
153 # Obsolete Database::tableExists()
154 function table_exists( $db ) {
155         global $wgDBname;
156         $res = mysql_list_tables( $wgDBname );
157         if( !$res ) {
158                 echo "** " . mysql_error() . "\n";
159                 return false;
160         }
161         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
162                 if( mysql_tablename( $res, $i ) == $db ) return true;
163         }
164         return false;
167 # Obsolete, use Database:fieldInfo()
168 function field_info( $table, $field ) {
169         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
170         $n = mysql_num_fields( $res );
171         for( $i = 0; $i < $n; $i++ ) {
172                 $meta = mysql_fetch_field( $res, $i );
173                 if( $field == $meta->name ) {
174                         return $meta;
175                 }
176         }
177         return false;