API * Implemented backlinks / imagelinks / embeddedin modules
[mediawiki.git] / install-utils.inc
blob2bc67c287bdf5b25d51ca1f97c0f30daa0b58c5c
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         }
18         global $wgCommandLineMode;
19         $wgCommandLineMode = true;
20         umask( 000 );
21         set_time_limit( 0 );
24 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
25         copyfileto( $sdir, $name, $ddir, $name, $perms );
28 function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
29         global $wgInstallOwner, $wgInstallGroup;
31         $d = "{$ddir}/{$dname}";
32         if ( copy( "{$sdir}/{$sname}", $d ) ) {
33                 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
34                 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
35                 chmod( $d, $perms );
36                 # print "Copied \"{$sname}\" to \"{$d}\".\n";
37         } else {
38                 print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
39                 exit();
40         }
43 function copydirectory( $source, $dest ) {
44         $handle = opendir( $source );
45         while ( false !== ( $f = readdir( $handle ) ) ) {
46                 $fullname = "$source/$f";
47                 if ( $f{0} != '.' && is_file( $fullname ) ) {
48                         copyfile( $source, $f, $dest );
49                 }
50         }
53 function readconsole( $prompt = '' ) {
54         static $isatty = null;
55         if ( is_null( $isatty ) ) {
56                 if ( !function_exists( 'posix_isatty' ) || posix_isatty( STDIN ) ) {
57                         $isatty = true;
58                 } else {
59                         $isatty = false;
60                 }
61         }
63         if ( $isatty && function_exists( 'readline' ) ) {
64                 return readline( $prompt );
65         } else {
66                 if ( $isatty ) {
67                         print $prompt;
68                 }
69                 if ( feof( STDIN ) ) {
70                         return false;
71                 }
72                 $st = fgets(STDIN, 1024);
73                 if ($st === false) return false;
74                 $resp = trim( $st );
75                 return $resp;
76         }
80 # Read and execute SQL commands from a file
82 function dbsource( $fname, $db = false ) {
83         if ( !$db ) {
84                 // Try $wgDatabase, which is used in the install and update scripts
85                 global $wgDatabase;
86                 if ( isset( $wgDatabase ) ) {
87                         $db =& $wgDatabase;
88                 } else {
89                         // No? Well, we must be outside of those scripts, so use the standard method
90                         $db =& wfGetDB( DB_MASTER );
91                 }
92         }
93         $error = $db->sourceFile( $fname );
94         if ( $error !== true ) {
95                 print $error;
96                 exit(1);
97         }
100 # Obsolete, use Database::fieldExists()
101 function field_exists( $table, $field ) {
102         $fname = 'Update script: field_exists';
103         $db =& wfGetDB( DB_SLAVE );
104         $res = $db->query( "DESCRIBE $table", $fname );
105         $found = false;
107         while ( $row = $db->fetchObject( $res ) ) {
108                 if ( $row->Field == $field ) {
109                         $found = true;
110                         break;
111                 }
112         }
113         return $found;
116 # Obsolete Database::tableExists()
117 function table_exists( $db ) {
118         global $wgDBname;
119         $res = mysql_list_tables( $wgDBname );
120         if( !$res ) {
121                 echo "** " . mysql_error() . "\n";
122                 return false;
123         }
124         for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
125                 if( mysql_tablename( $res, $i ) == $db ) return true;
126         }
127         return false;
130 # Obsolete, use Database:fieldInfo()
131 function field_info( $table, $field ) {
132         $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
133         $n = mysql_num_fields( $res );
134         for( $i = 0; $i < $n; $i++ ) {
135                 $meta = mysql_fetch_field( $res, $i );
136                 if( $field == $meta->name ) {
137                         return $meta;
138                 }
139         }
140         return false;