* Formatting
[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 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 );
28 }
29
30 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
31 copyfileto( $sdir, $name, $ddir, $name, $perms );
32 }
33
34 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
35 global $wgInstallOwner, $wgInstallGroup;
36
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 }
47 }
48
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 }
57 }
58
59 function readconsole( $prompt = '' ) {
60 if ( function_exists( 'posix_isatty' ) && posix_isatty( STDIN ) ) {
61 $isatty = true;
62 } else {
63 $isatty = false;
64 }
65
66 if ( $isatty && function_exists( 'readline' ) ) {
67 return readline( $prompt );
68 } else {
69 if ( $isatty ) {
70 print $prompt;
71 }
72 $fp = fopen( 'php://stdin', 'r' );
73 $st = fgets($fp, 1024);
74 if ($st === false) return false;
75 $resp = trim( $st );
76 fclose( $fp );
77 return $resp;
78 }
79 }
80
81 function replacevars( $ins ) {
82 $varnames = array(
83 'wgDBserver', 'wgDBname', 'wgDBintlname', 'wgDBuser',
84 'wgDBpassword', 'wgDBsqluser', 'wgDBsqlpassword',
85 'wgDBadminuser', 'wgDBadminpassword', 'wgDBprefix'
86 );
87
88 foreach ( $varnames as $var ) {
89 if( isset( $GLOBALS[$var] ) ) {
90 $val = addslashes( $GLOBALS[$var] );
91 $ins = str_replace( '{$' . $var . '}', $val, $ins );
92 $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins );
93 $ins = str_replace( '/*$' . $var . '*/', $val, $ins );
94 }
95 }
96 return $ins;
97 }
98
99 #
100 # Read and execute SQL commands from a file
101 #
102 function dbsource( $fname, $database = false ) {
103 $fp = fopen( $fname, 'r' );
104 if ( false === $fp ) {
105 print "Could not open \"{$fname}\".\n";
106 exit();
107 }
108
109 $cmd = "";
110 $done = false;
111
112 while ( ! feof( $fp ) ) {
113 $line = trim( fgets( $fp, 1024 ) );
114 $sl = strlen( $line ) - 1;
115
116 if ( $sl < 0 ) { continue; }
117 if ( '-' == $line{0} && '-' == $line{1} ) { continue; }
118
119 if ( ';' == $line{$sl} && ($sl < 2 || ';' != $line{$sl - 1})) {
120 $done = true;
121 $line = substr( $line, 0, $sl );
122 }
123
124 if ( '' != $cmd ) { $cmd .= ' '; }
125 $cmd .= "$line\n";
126
127 if ( $done ) {
128 $cmd = str_replace(';;', ";", $cmd);
129 $cmd = replacevars( $cmd );
130 if( $database )
131 $res = $database->query( $cmd, 'dbsource', true );
132 else
133 $res = mysql_query( $cmd );
134
135 if ( false === $res ) {
136 $err = mysql_error();
137 print "Query \"{$cmd}\" failed with error code \"$err\".\n";
138 exit();
139 }
140
141 $cmd = '';
142 $done = false;
143 }
144 }
145 fclose( $fp );
146 }
147
148 # Obsolete, use Database::fieldExists()
149 function field_exists( $table, $field ) {
150 $fname = 'Update script: field_exists';
151 $db =& wfGetDB( DB_SLAVE );
152 $res = $db->query( "DESCRIBE $table", $fname );
153 $found = false;
154
155 while ( $row = $db->fetchObject( $res ) ) {
156 if ( $row->Field == $field ) {
157 $found = true;
158 break;
159 }
160 }
161 return $found;
162 }
163
164 # Obsolete Database::tableExists()
165 function table_exists( $db ) {
166 global $wgDBname;
167 $res = mysql_list_tables( $wgDBname );
168 if( !$res ) {
169 echo "** " . mysql_error() . "\n";
170 return false;
171 }
172 for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
173 if( mysql_tablename( $res, $i ) == $db ) return true;
174 }
175 return false;
176 }
177
178 # Obsolete, use Database:fieldInfo()
179 function field_info( $table, $field ) {
180 $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
181 $n = mysql_num_fields( $res );
182 for( $i = 0; $i < $n; $i++ ) {
183 $meta = mysql_fetch_field( $res, $i );
184 if( $field == $meta->name ) {
185 return $meta;
186 }
187 }
188 return false;
189 }
190
191 ?>