Bring reportQueryError back in sync and support the IGNORE option
[mediawiki.git] / install-utils.inc
blobfa7b80e580eb8e18bb64f1809690c3462d9718cd
1 <?php
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 );
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         // Test for PHP bug which breaks PHP 5.0.x on 64-bit...
19         // As of 1.8 this breaks lots of common operations instead
20         // of just some rare ones like export.
21         $borked = str_replace( 'a', 'b', array( -1 => -1 ) );
22         if( !isset( $borked[-1] ) ) {
23                 echo "PHP 5.0.x is buggy on your 64-bit system; you must upgrade to PHP 5.1.x\n" .
24                         "or higher. ABORTING. (http://bugs.php.net/bug.php?id=34879 for details)\n";
25                 die( -1 );
26         }
28         global $wgCommandLineMode;
29         $wgCommandLineMode = true;
30         umask( 000 );
31         set_time_limit( 0 );
34 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
35         copyfileto( $sdir, $name, $ddir, $name, $perms );
38 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
39         global $wgInstallOwner, $wgInstallGroup;
41         $d = "{$ddir}/{$dname}";
42         if ( copy( "{$sdir}/{$sname}", $d ) ) {
43                 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
44                 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
45                 chmod( $d, $perms );
46                 # print "Copied \"{$sname}\" to \"{$d}\".\n";
47         } else {
48                 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
49                 exit();
50         }
53 function copydirectory( $source, $dest ) {
54         $handle = opendir( $source );
55         while ( false !== ( $f = readdir( $handle ) ) ) {
56                 $fullname = "$source/$f";
57                 if ( $f{0} != '.' && is_file( $fullname ) ) {
58                         copyfile( $source, $f, $dest );
59                 }
60         }
63 function readconsole( $prompt = '' ) {
64         static $isatty = null;
65         if ( is_null( $isatty ) ) {
66                 if ( !function_exists( 'posix_isatty' ) || posix_isatty( STDIN ) ) {
67                         $isatty = true;
68                 } else {
69                         $isatty = false;
70                 }
71         }
73         if ( $isatty && function_exists( 'readline' ) ) {
74                 return readline( $prompt );
75         } else {
76                 if ( $isatty ) {
77                         print $prompt;
78                 }
79                 if ( feof( STDIN ) ) {
80                         return false;
81                 }
82                 $st = fgets(STDIN, 1024);
83                 if ($st === false) return false;
84                 $resp = trim( $st );
85                 return $resp;
86         }
90 # Read and execute SQL commands from a file
92 function dbsource( $fname, $db = false ) {
93         if ( !$db ) {
94                 // Try $wgDatabase, which is used in the install and update scripts
95                 global $wgDatabase;
96                 if ( isset( $wgDatabase ) ) {
97                         $db =& $wgDatabase;
98                 } else {
99                         // No? Well, we must be outside of those scripts, so use the standard method
100                         $db =& wfGetDB( DB_MASTER );
101                 }
102         }
103         $error = $db->sourceFile( $fname );
104         if ( $error !== true ) {
105                 print $error;
106                 exit(1);
107         }
110 # Obsolete, use Database::fieldExists()
111 function field_exists( $table, $field ) {
112         $fname = 'Update script: field_exists';
113         $db =& wfGetDB( DB_SLAVE );
114         $res = $db->query( "DESCRIBE $table", $fname );
115         $found = false;
117         while ( $row = $db->fetchObject( $res ) ) {
118                 if ( $row->Field == $field ) {
119                         $found = true;
120                         break;
121                 }
122         }
123         return $found;
126 # Obsolete Database::tableExists()
127 function table_exists( $db ) {
128         global $wgDBname;
129         $res = mysql_list_tables( $wgDBname );
130         if( !$res ) {
131                 echo "** " . mysql_error() . "\n";
132                 return false;
133         }
134         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
135                 if( mysql_tablename( $res, $i ) == $db ) return true;
136         }
137         return false;
140 # Obsolete, use Database:fieldInfo()
141 function field_info( $table, $field ) {
142         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
143         $n = mysql_num_fields( $res );
144         for( $i = 0; $i < $n; $i++ ) {
145                 $meta = mysql_fetch_field( $res, $i );
146                 if( $field == $meta->name ) {
147                         return $meta;
148                 }
149         }
150         return false;