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