Fix #3350: missing label for move talk page
[mediawiki.git] / install-utils.inc
blob6de9377428a541b06dd731a588bec0b510d032e6
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 recommended. ABORTING.\n" );
11         }
12         if( version_compare( phpversion(), '4.3.2' ) < 0 ) {
13                 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";
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         if ( function_exists( 'posix_isatty' ) && posix_isatty( STDIN ) ) {
61                 $isatty = true;
62         } else {
63                 $isatty = false;
64         }
66         if ( $isatty && function_exists( 'readline' ) ) {
67                 return readline( $prompt );
68         } else {
69                 if ( $isatty ) {
70                         print $prompt;
71                 }
72                 $fp = fopen( 'php://stdin', 'r' );
73                 $st = fgets($fp, 1024);
74                 if ($st === false) return false;
75                 $resp = trim( $st );
76                 fclose( $fp );
77                 return $resp;
78         }
81 function replacevars( $ins ) {
82         $varnames = array(
83                 'wgDBserver', 'wgDBname', 'wgDBintlname', 'wgDBuser',
84                 'wgDBpassword', 'wgDBsqluser', 'wgDBsqlpassword',
85                 'wgDBadminuser', 'wgDBadminpassword', 'wgDBprefix'
86         );
88         foreach ( $varnames as $var ) {
89                 if( isset( $GLOBALS[$var] ) ) {
90                         $val = addslashes( $GLOBALS[$var] );
91                         $ins = str_replace( '{$' . $var . '}', $val, $ins );
92                         $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins );
93                         $ins = str_replace( '/*$' . $var . '*/', $val, $ins );
94                 }
95         }
96         return $ins;
100 # Read and execute SQL commands from a file
102 function dbsource( $fname, $database = false ) {
103         $fp = fopen( $fname, 'r' );
104         if ( false === $fp ) {
105                 print "Could not open \"{$fname}\".\n";
106                 exit();
107         }
109         $cmd = "";
110         $done = false;
112         while ( ! feof( $fp ) ) {
113                 $line = trim( fgets( $fp, 1024 ) );
114                 $sl = strlen( $line ) - 1;
116                 if ( $sl < 0 ) { continue; }
117                 if ( '-' == $line{0} && '-' == $line{1} ) { continue; }
119                 if ( ';' == $line{$sl} && ($sl < 2 || ';' != $line{$sl - 1})) {
120                         $done = true;
121                         $line = substr( $line, 0, $sl );
122                 }
124                 if ( '' != $cmd ) { $cmd .= ' '; }
125                 $cmd .= "$line\n";
127                 if ( $done ) {
128                         $cmd = str_replace(';;', ";", $cmd);
129                         $cmd = replacevars( $cmd );
130                         if( $database )
131                                 $res = $database->query( $cmd, 'dbsource', true );
132                         else
133                                 $res = mysql_query( $cmd );
135                         if ( false === $res ) {
136                                 $err = mysql_error();
137                                 print "Query \"{$cmd}\" failed with error code \"$err\".\n";
138                                 exit();
139                         }
141                         $cmd = '';
142                         $done = false;
143                 }
144         }
145         fclose( $fp );
148 # Obsolete, use Database::fieldExists()
149 function field_exists( $table, $field ) {
150         $fname = 'Update script: field_exists';
151         $db =& wfGetDB( DB_SLAVE );
152         $res = $db->query( "DESCRIBE $table", $fname );
153         $found = false;
155         while ( $row = $db->fetchObject( $res ) ) {
156                 if ( $row->Field == $field ) {
157                         $found = true;
158                         break;
159                 }
160         }
161         return $found;
164 # Obsolete Database::tableExists()
165 function table_exists( $db ) {
166         global $wgDBname;
167         $res = mysql_list_tables( $wgDBname );
168         if( !$res ) {
169                 echo "** " . mysql_error() . "\n";
170                 return false;
171         }
172         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
173                 if( mysql_tablename( $res, $i ) == $db ) return true;
174         }
175         return false;
178 # Obsolete, use Database:fieldInfo()
179 function field_info( $table, $field ) {
180         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
181         $n = mysql_num_fields( $res );
182         for( $i = 0; $i < $n; $i++ ) {
183                 $meta = mysql_fetch_field( $res, $i );
184                 if( $field == $meta->name ) {
185                         return $meta;
186                 }
187         }
188         return false;