* (bug 385) Installer support for PostgreSQL, fixes for PG compatibility
[lhc/web/wiklou.git] / install-utils.inc
1 <?php
2
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 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 }
17
18 $gotdatabase = 0;
19 ## XXX We should quiet the warnings thrown here
20 if (extension_loaded('mysql') or dl('mysql.so')) {
21 $gotdatabase = 'mysql';
22 }
23 else if (extension_loaded('pgsql') or dl('pgsql.so')) {
24 $gotdatabase = 'pg';
25 }
26 if (!$gotdatabase) {
27 print "Could not load the MySQL or the PostgreSQL driver! Please compile ".
28 "php with either --with-mysql or --with-pgsql, or install the mysql.so or pg.so module.\n";
29 exit;
30 }
31
32 global $wgCommandLineMode;
33 $wgCommandLineMode = true;
34 umask( 000 );
35 set_time_limit( 0 );
36 }
37
38 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
39 copyfileto( $sdir, $name, $ddir, $name, $perms );
40 }
41
42 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
43 global $wgInstallOwner, $wgInstallGroup;
44
45 $d = "{$ddir}/{$dname}";
46 if ( copy( "{$sdir}/{$sname}", $d ) ) {
47 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
48 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
49 chmod( $d, $perms );
50 # print "Copied \"{$sname}\" to \"{$d}\".\n";
51 } else {
52 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
53 exit();
54 }
55 }
56
57 function copydirectory( $source, $dest ) {
58 $handle = opendir( $source );
59 while ( false !== ( $f = readdir( $handle ) ) ) {
60 $fullname = "$source/$f";
61 if ( $f{0} != '.' && is_file( $fullname ) ) {
62 copyfile( $source, $f, $dest );
63 }
64 }
65 }
66
67 function readconsole( $prompt = '' ) {
68 static $isatty = null, $fp = null;
69 if ( is_null( $fp ) ) {
70 $fp = fopen( 'php://stdin', 'r' );
71 }
72 if ( is_null( $isatty ) ) {
73 if ( !function_exists( 'posix_isatty' ) || posix_isatty( $fp ) ) {
74 $isatty = true;
75 } else {
76 $isatty = false;
77 }
78 }
79
80 if ( $isatty && function_exists( 'readline' ) ) {
81 return readline( $prompt );
82 } else {
83 if ( $isatty ) {
84 print $prompt;
85 }
86 if ( feof( $fp ) ) {
87 return false;
88 }
89 $st = fgets($fp, 1024);
90 if ($st === false) return false;
91 $resp = trim( $st );
92 return $resp;
93 }
94 }
95
96 #
97 # Read and execute SQL commands from a file
98 #
99 function dbsource( $fname, $db = false ) {
100 if ( !$db ) {
101 // Try $wgDatabase, which is used in the install and update scripts
102 global $wgDatabase;
103 if ( isset( $wgDatabase ) ) {
104 $db =& $wgDatabase;
105 } else {
106 // No? Well, we must be outside of those scripts, so use the standard method
107 $db =& wfGetDB( DB_MASTER );
108 }
109 }
110 $error = $db->sourceFile( $fname );
111 if ( $error !== true ) {
112 print $error;
113 exit(1);
114 }
115 }
116
117 # Obsolete, use Database::fieldExists()
118 function field_exists( $table, $field ) {
119 $fname = 'Update script: field_exists';
120 $db =& wfGetDB( DB_SLAVE );
121 $res = $db->query( "DESCRIBE $table", $fname );
122 $found = false;
123
124 while ( $row = $db->fetchObject( $res ) ) {
125 if ( $row->Field == $field ) {
126 $found = true;
127 break;
128 }
129 }
130 return $found;
131 }
132
133 # Obsolete Database::tableExists()
134 function table_exists( $db ) {
135 global $wgDBname;
136 $res = mysql_list_tables( $wgDBname );
137 if( !$res ) {
138 echo "** " . mysql_error() . "\n";
139 return false;
140 }
141 for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
142 if( mysql_tablename( $res, $i ) == $db ) return true;
143 }
144 return false;
145 }
146
147 # Obsolete, use Database:fieldInfo()
148 function field_info( $table, $field ) {
149 $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
150 $n = mysql_num_fields( $res );
151 for( $i = 0; $i < $n; $i++ ) {
152 $meta = mysql_fetch_field( $res, $i );
153 if( $field == $meta->name ) {
154 return $meta;
155 }
156 }
157 return false;
158 }
159
160 ?>