Imported the register_globals hack from REL1_2. Also starting work on
[lhc/web/wiklou.git] / install-utils.inc
1 <?php
2
3 function install_version_checks() {
4 if( !function_exists( "version_compare" ) ) {
5 # version_compare was introduced in 4.1.0
6 die( "Your PHP version is much too old; 4.0.x will _not_ work. 4.3.2 or higher is recommended. ABORTING.\n" );
7 }
8 if( version_compare( phpversion(), "4.3.2" ) < 0 ) {
9 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";
10 }
11
12 if (!extension_loaded('mysql')) {
13 if (!dl('mysql.so')) {
14 print "Could not load MySQL driver! Please compile ".
15 "php --with-mysql or install the mysql.so module.\n";
16 exit;
17 }
18 }
19
20 global $wgCommandLineMode;
21 $wgCommandLineMode = true;
22 umask( 000 );
23 set_time_limit( 0 );
24 }
25
26 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
27 copyfileto( $sdir, $name, $ddir, $name, $perms );
28 }
29
30 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
31 global $wgInstallOwner, $wgInstallGroup;
32
33 $d = "{$ddir}/{$dname}";
34 if ( copy( "{$sdir}/{$sname}", $d ) ) {
35 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
36 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
37 chmod( $d, $perms );
38 # print "Copied \"{$sname}\" to \"{$d}\".\n";
39 } else {
40 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
41 exit();
42 }
43 }
44
45 function copydirectory( $source, $dest ) {
46 $handle = opendir( $source );
47 while ( false !== ( $f = readdir( $handle ) ) ) {
48 $fullname = "$source/$f";
49 if ( $f{0} !="." && is_file( $fullname ) ) {
50 copyfile( $source, $f, $dest );
51 }
52 }
53 }
54
55 function readconsole() {
56 $fp = fopen( "php://stdin", "r" );
57 $resp = trim( fgets( $fp, 1024 ) );
58 fclose( $fp );
59 return $resp;
60 }
61
62
63 function replacevars( $ins ) {
64 $varnames = array(
65 "wgDBserver", "wgDBname", "wgDBintlname", "wgDBuser",
66 "wgDBpassword", "wgDBsqluser", "wgDBsqlpassword",
67 "wgDBadminuser", "wgDBadminpassword"
68 );
69
70 foreach ( $varnames as $var ) {
71 global $$var;
72 $ins = str_replace( '{$' . $var . '}', $$var, $ins );
73 }
74 return $ins;
75 }
76
77 #
78 # Read and execute SQL commands from a file
79 #
80 function dbsource( $fname, $database = false ) {
81 $fp = fopen( $fname, "r" );
82 if ( false === $fp ) {
83 print "Could not open \"{$fname}\".\n";
84 exit();
85 }
86
87 $cmd = "";
88 $done = false;
89
90 while ( ! feof( $fp ) ) {
91 $line = trim( fgets( $fp, 1024 ) );
92 $sl = strlen( $line ) - 1;
93
94 if ( $sl < 0 ) { continue; }
95 if ( "-" == $line{0} && "-" == $line{1} ) { continue; }
96
97 if ( ";" == $line{$sl} ) {
98 $done = true;
99 $line = substr( $line, 0, $sl );
100 }
101
102 if ( "" != $cmd ) { $cmd .= " "; }
103 $cmd .= $line;
104
105 if ( $done ) {
106 $cmd = replacevars( $cmd );
107 if( $database )
108 $res = $database->query( $cmd );
109 else
110 $res = mysql_query( $cmd );
111
112 if ( false === $res ) {
113 print "Query \"{$cmd}\" failed.\n";
114 exit();
115 }
116
117 $cmd = "";
118 $done = false;
119 }
120 }
121 fclose( $fp );
122 }
123
124 # Obsolete, use Database::fieldExists()
125 function field_exists( $table, $field ) {
126 $fname = "Update script: field_exists";
127 $res = wfQuery( "DESCRIBE $table", $fname );
128 $found = false;
129
130 while ( $row = wfFetchObject( $res ) ) {
131 if ( $row->Field == $field ) {
132 $found = true;
133 break;
134 }
135 }
136 return $found;
137 }
138
139 # Obsolete Database::tableExists()
140 function table_exists( $db ) {
141 global $wgDBname;
142 $res = mysql_list_tables( $wgDBname );
143 if( !$res ) {
144 echo "** " . mysql_error() . "\n";
145 return false;
146 }
147 for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
148 if( mysql_tablename( $res, $i ) == $db ) return true;
149 }
150 return false;
151 }
152
153 # Obsolete, use Database:fieldInfo()
154 function field_info( $table, $field ) {
155 $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
156 $n = mysql_num_fields( $res );
157 for( $i = 0; $i < $n; $i++ ) {
158 $meta = mysql_fetch_field( $res, $i );
159 if( $field == $meta->name ) {
160 return $meta;
161 }
162 }
163 return false;
164 }
165
166 ?>