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