Note about $wgCacheEpoch format ( `date +%Y%m%d%H%M%S` )
[mediawiki.git] / install-utils.inc
blob2197fe6ab2051cd5699194f3e491cb8171c7168e
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                 die( "Your PHP version is much too old; 4.0.x will _not_ work. 4.3.2 or higher is required. ABORTING.\n" );
11         }
12         if( version_compare( phpversion(), '4.3.2' ) < 0 ) {
13                 die( "PHP 4.3.2 or higher is required. ABORTING.\n" );
14         }
16         if (!extension_loaded('mysql')) {
17                 if (!dl('mysql.so')) {
18                         print 'Could not load MySQL driver! Please compile '.
19                                   "php --with-mysql or install the mysql.so module.\n";
20                 exit;
21                 }
22         }
24         global $wgCommandLineMode;
25         $wgCommandLineMode = true;
26         umask( 000 );
27         set_time_limit( 0 );
30 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
31         copyfileto( $sdir, $name, $ddir, $name, $perms );
34 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
35         global $wgInstallOwner, $wgInstallGroup;
37         $d = "{$ddir}/{$dname}";
38         if ( copy( "{$sdir}/{$sname}", $d ) ) {
39                 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
40                 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
41                 chmod( $d, $perms );
42                 # print "Copied \"{$sname}\" to \"{$d}\".\n";
43         } else {
44                 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
45                 exit();
46         }
49 function copydirectory( $source, $dest ) {
50         $handle = opendir( $source );
51         while ( false !== ( $f = readdir( $handle ) ) ) {
52                 $fullname = "$source/$f";
53                 if ( $f{0} != '.' && is_file( $fullname ) ) {
54                         copyfile( $source, $f, $dest );
55                 }
56         }
59 function readconsole( $prompt = '' ) {
60         static $isatty = null, $fp = null;
61         if ( is_null( $fp ) ) {
62                 $fp = fopen( 'php://stdin', 'r' );
63         }
64         if ( is_null( $isatty ) ) {
65                 if ( !function_exists( 'posix_isatty' ) || posix_isatty( $fp ) ) {
66                         $isatty = true;
67                 } else {
68                         $isatty = false;
69                 }
70         }
72         if ( $isatty && function_exists( 'readline' ) ) {
73                 return readline( $prompt );
74         } else {
75                 if ( $isatty ) {
76                         print $prompt;
77                 }
78                 if ( feof( $fp ) ) {
79                         return false;
80                 }
81                 $st = fgets($fp, 1024);
82                 if ($st === false) return false;
83                 $resp = trim( $st );
84                 return $resp;
85         }
88 function replacevars( $ins ) {
89         $varnames = array(
90                 'wgDBserver', 'wgDBname', 'wgDBintlname', 'wgDBuser',
91                 'wgDBpassword', 'wgDBsqluser', 'wgDBsqlpassword',
92                 'wgDBadminuser', 'wgDBadminpassword', 'wgDBprefix'
93         );
95         foreach ( $varnames as $var ) {
96                 if( isset( $GLOBALS[$var] ) ) {
97                         $val = addslashes( $GLOBALS[$var] );
98                         $ins = str_replace( '{$' . $var . '}', $val, $ins );
99                         $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins );
100                         $ins = str_replace( '/*$' . $var . '*/', $val, $ins );
101                 }
102         }
103         return $ins;
107 # Read and execute SQL commands from a file
109 function dbsource( $fname, $database = false ) {
110         $fp = fopen( $fname, 'r' );
111         if ( false === $fp ) {
112                 print "Could not open \"{$fname}\".\n";
113                 exit();
114         }
116         $cmd = "";
117         $done = false;
119         while ( ! feof( $fp ) ) {
120                 $line = trim( fgets( $fp, 1024 ) );
121                 $sl = strlen( $line ) - 1;
123                 if ( $sl < 0 ) { continue; }
124                 if ( '-' == $line{0} && '-' == $line{1} ) { continue; }
126                 if ( ';' == $line{$sl} && ($sl < 2 || ';' != $line{$sl - 1})) {
127                         $done = true;
128                         $line = substr( $line, 0, $sl );
129                 }
131                 if ( '' != $cmd ) { $cmd .= ' '; }
132                 $cmd .= "$line\n";
134                 if ( $done ) {
135                         $cmd = str_replace(';;', ";", $cmd);
136                         $cmd = replacevars( $cmd );
137                         if( $database )
138                                 $res = $database->query( $cmd, 'dbsource', true );
139                         else
140                                 $res = mysql_query( $cmd );
142                         if ( false === $res ) {
143                                 $err = mysql_error();
144                                 print "Query \"{$cmd}\" failed with error code \"$err\".\n";
145                                 exit();
146                         }
148                         $cmd = '';
149                         $done = false;
150                 }
151         }
152         fclose( $fp );
155 # Obsolete, use Database::fieldExists()
156 function field_exists( $table, $field ) {
157         $fname = 'Update script: field_exists';
158         $db =& wfGetDB( DB_SLAVE );
159         $res = $db->query( "DESCRIBE $table", $fname );
160         $found = false;
162         while ( $row = $db->fetchObject( $res ) ) {
163                 if ( $row->Field == $field ) {
164                         $found = true;
165                         break;
166                 }
167         }
168         return $found;
171 # Obsolete Database::tableExists()
172 function table_exists( $db ) {
173         global $wgDBname;
174         $res = mysql_list_tables( $wgDBname );
175         if( !$res ) {
176                 echo "** " . mysql_error() . "\n";
177                 return false;
178         }
179         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
180                 if( mysql_tablename( $res, $i ) == $db ) return true;
181         }
182         return false;
185 # Obsolete, use Database:fieldInfo()
186 function field_info( $table, $field ) {
187         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
188         $n = mysql_num_fields( $res );
189         for( $i = 0; $i < $n; $i++ ) {
190                 $meta = mysql_fetch_field( $res, $i );
191                 if( $field == $meta->name ) {
192                         return $meta;
193                 }
194         }
195         return false;