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