Two blocking features: IP range blocks, and expiry times configurable block-by-block.
[mediawiki.git] / install-utils.inc
blobd78b7b02b5494fd7c8eb0b1b7fa51a0b24a31e89
1 <?
3 function install_version_checks() {
4         if( !function_exists( "version_compare" ) ) {
5                 # version_compare was introduced in 4.1.0
6                 die( "Your PHP version is much too old; 4.0.x will _not_ work. 4.3.2 or higher is recommended. ABORTING.\n" );
7         }
8         if( version_compare( phpversion(), "4.3.2" ) < 0 ) {
9                 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";
10         }
11         if( !ini_get( "register_globals" ) ) {
12                 echo "WARNING: register_globals is not on; MediaWiki currently relies on this option.\n\n";
13         }
14         
15         if (!extension_loaded('mysql')) {
16                 if (!dl('mysql.so')) {
17                         print "Could not load MySQL driver! Please compile ".
18                                   "php --with-mysql or install the mysql.so module.\n";
19                 exit;
20                 }
21         }
22         
23         global $wgCommandLineMode;
24         $wgCommandLineMode = true;
25         umask( 000 );
26         set_time_limit( 0 );
29 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
30         global $wgInstallOwner, $wgInstallGroup;
32         $d = "{$ddir}/{$name}";
33         if ( copy( "{$sdir}/{$name}", $d ) ) {
34                 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
35                 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
36                 chmod( $d, $perms );
37                 # print "Copied \"{$name}\" to \"{$ddir}\".\n";
38         } else {
39                 print "Failed to copy file \"{$name}\" to \"{$ddir}\".\n";
40                 exit();
41         }
44 function copydirectory( $source, $dest ) {
45         $handle = opendir( $source );
46         while ( false !== ( $f = readdir( $handle ) ) ) {
47                 $fullname = "$source/$f";
48                 if ( $f{0} !="." && is_file( $fullname ) ) {
49                         copyfile( $source, $f, $dest );
50                 }
51         }
54 function readconsole() {
55         $fp = fopen( "php://stdin", "r" );
56         $resp = trim( fgets( $fp, 1024 ) );
57         fclose( $fp );
58         return $resp;
62 function replacevars( $ins ) {
63         $varnames = array(
64                 "wgDBserver", "wgDBname", "wgDBintlname", "wgDBuser",
65                 "wgDBpassword", "wgDBsqluser", "wgDBsqlpassword",
66                 "wgDBadminuser", "wgDBadminpassword"
67         );
69         foreach ( $varnames as $var ) {
70                 global $$var;
71                 $ins = str_replace( '{$' . $var . '}', $$var, $ins );
72         }
73         return $ins;
77 # Read and execute SQL commands from a file
79 function dbsource( $fname, $database = false ) {
80         $fp = fopen( $fname, "r" );
81         if ( false === $fp ) {
82                 print "Could not open \"{$fname}\".\n";
83                 exit();
84         }
86         $cmd = "";
87         $done = false;
89         while ( ! feof( $fp ) ) {
90                 $line = trim( fgets( $fp, 1024 ) );
91                 $sl = strlen( $line ) - 1;
93                 if ( $sl < 0 ) { continue; }
94                 if ( "-" == $line{0} && "-" == $line{1} ) { continue; }
96                 if ( ";" == $line{$sl} ) {
97                         $done = true;
98                         $line = substr( $line, 0, $sl );
99                 }
101                 if ( "" != $cmd ) { $cmd .= " "; }
102                 $cmd .= $line;
104                 if ( $done ) {
105                         $cmd = replacevars( $cmd );
106                         if( $database )
107                                 $res = $database->query( $cmd );
108                         else
109                                 $res = mysql_query( $cmd );
111                         if ( false === $res ) {
112                                 print "Query \"{$cmd}\" failed.\n";
113                                 exit();
114                         }
116                         $cmd = "";
117                         $done = false;
118                 }
119         }
120         fclose( $fp );
123 # Obsolete, use Database::fieldExists()
124 function field_exists( $table, $field ) {
125         $fname = "Update script: field_exists";
126         $res = wfQuery( "DESCRIBE $table", $fname );
127         $found = false;
128         
129         while ( $row = wfFetchObject( $res ) ) {
130                 if ( $row->Field == $field ) {
131                         $found = true;
132                         break;
133                 }
134         }
135         return $found;
138 # Obsolete Database::tableExists()
139 function table_exists( $db ) {
140         global $wgDBname;
141         $res = mysql_list_tables( $wgDBname );
142         if( !$res ) {
143                 echo "** " . mysql_error() . "\n";
144                 return false;
145         }
146         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
147                 if( mysql_tablename( $res, $i ) == $db ) return true;
148         }
149         return false;
152 # Obsolete, use Database:fieldInfo()
153 function field_info( $table, $field ) {
154         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
155         $n = mysql_num_fields( $res );
156         for( $i = 0; $i < $n; $i++ ) {
157                 $meta = mysql_fetch_field( $res, $i );
158                 if( $field == $meta->name ) {
159                         return $meta;
160                 }
161         }
162         return false;