Shqip
[lhc/web/wiklou.git] / install-utils.inc
1 <?
2
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 );
27 }
28
29 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
30 global $wgInstallOwner, $wgInstallGroup;
31
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 }
42 }
43
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 }
52 }
53
54 function readconsole() {
55 $fp = fopen( "php://stdin", "r" );
56 $resp = trim( fgets( $fp, 1024 ) );
57 fclose( $fp );
58 return $resp;
59 }
60
61
62 function replacevars( $ins ) {
63 $varnames = array(
64 "wgDBserver", "wgDBname", "wgDBintlname", "wgDBuser",
65 "wgDBpassword", "wgDBsqluser", "wgDBsqlpassword",
66 "wgDBadminuser", "wgDBadminpassword"
67 );
68
69 foreach ( $varnames as $var ) {
70 global $$var;
71 $ins = str_replace( '{$' . $var . '}', $$var, $ins );
72 }
73 return $ins;
74 }
75
76 #
77 # Read and execute SQL commands from a file
78 #
79 function dbsource( $fname, $conn = false ) {
80 $fp = fopen( $fname, "r" );
81 if ( false === $fp ) {
82 print "Could not open \"{$fname}\".\n";
83 exit();
84 }
85
86 $cmd = "";
87 $done = false;
88
89 while ( ! feof( $fp ) ) {
90 $line = trim( fgets( $fp, 1024 ) );
91 $sl = strlen( $line ) - 1;
92
93 if ( $sl < 0 ) { continue; }
94 if ( "-" == $line{0} && "-" == $line{1} ) { continue; }
95
96 if ( ";" == $line{$sl} ) {
97 $done = true;
98 $line = substr( $line, 0, $sl );
99 }
100
101 if ( "" != $cmd ) { $cmd .= " "; }
102 $cmd .= $line;
103
104 if ( $done ) {
105 $cmd = replacevars( $cmd );
106 if( $conn )
107 $res = mysql_query( $cmd, $conn );
108 else
109 $res = mysql_query( $cmd );
110
111 if ( false === $res ) {
112 print "Query \"{$cmd}\" failed.\n";
113 exit();
114 }
115
116 $cmd = "";
117 $done = false;
118 }
119 }
120 fclose( $fp );
121 }
122
123
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;
136 }
137
138
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;
150 }
151
152 function field_info( $table, $field ) {
153 $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
154 $n = mysql_num_fields( $res );
155 for( $i = 0; $i < $n; $i++ ) {
156 $meta = mysql_fetch_field( $res, $i );
157 if( $field == $meta->name ) {
158 return $meta;
159 }
160 }
161 return false;
162 }
163
164 ?>