removing unreliable version check before ALTER TABLE
[lhc/web/wiklou.git] / update.php
1 <?
2
3 # Update already-installed software
4 #
5
6 if ( ! ( is_readable( "./LocalSettings.php" )
7 && is_readable( "./AdminSettings.php" ) ) ) {
8 print "A copy of your installation's LocalSettings.php\n" .
9 "and AdminSettings.php must exist in this source directory.\n";
10 exit();
11 }
12
13 $IP = "./includes";
14 include_once( "./LocalSettings.php" );
15 include_once( "./AdminSettings.php" );
16
17 if ( $wgUseTeX && ( ! is_executable( "./math/texvc" ) ) ) {
18 print "To use math functions, you must first compile texvc by\n" .
19 "running \"make\" in the math directory.\n";
20 exit();
21 }
22
23 umask( 000 );
24 set_time_limit( 0 );
25
26 include_once( "Version.php" );
27 include_once( "{$IP}/Setup.php" );
28 $wgTitle = Title::newFromText( "Update script" );
29 $wgCommandLineMode = true;
30 $wgAlterSpecs = array();
31
32 do_revision_updates();
33
34 alter_ipblocks();
35
36 #
37 # Run ALTER TABLE queries.
38 #
39 if ( count( $wgAlterSpecs ) ) {
40 $rconn = mysql_connect( $wgDBserver, $wgDBadminuser, $wgDBadminpassword );
41 mysql_select_db( $wgDBname );
42 print "\n";
43 foreach ( $wgAlterSpecs as $table => $specs ) {
44 $sql = "ALTER TABLE $table $specs";
45 print "$sql;\n";
46 $res = mysql_query( $sql, $rconn );
47 if ( $res === false ) {
48 print "MySQL error: " . mysql_error( $rconn ) . "\n";
49 }
50 }
51 mysql_close( $rconn );
52 }
53
54
55 #
56 # Copy files into installation directories
57 #
58 print "Copying files...\n";
59
60 copyfile( ".", "wiki.phtml", $IP );
61 copyfile( ".", "redirect.phtml", $IP );
62 copyfile( ".", "texvc.phtml", $IP );
63
64 copydirectory( "./includes", $IP );
65 copydirectory( "./stylesheets", $wgStyleSheetDirectory );
66
67 copyfile( "./images", "wiki.png", $wgUploadDirectory );
68 copyfile( "./languages", "Language.php", $IP );
69 copyfile( "./languages", "Language" . ucfirst( $wgLanguageCode ) . ".php", $IP );
70
71 $fp = fopen( $wgDebugLogFile, "w" );
72 if ( false === $fp ) {
73 print "Could not create log file \"{$wgDebugLogFile}\".\n";
74 exit();
75 }
76 $d = date( "Y-m-d H:i:s" );
77 fwrite( $fp, "Wiki debug log file created {$d}\n\n" );
78 fclose( $fp );
79
80 if ( $wgUseTeX ) {
81 copyfile( "./math", "texvc", "{$IP}/math", 0775 );
82 copyfile( "./math", "texvc_test", "{$IP}/math", 0775 );
83 copyfile( "./math", "texvc_tex", "{$IP}/math", 0775 );
84 }
85
86 copyfile( ".", "Version.php", $IP );
87
88 print "Done.\n";
89 exit();
90
91 #
92 #
93 #
94
95 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
96 global $wgInstallOwner, $wgInstallGroup;
97
98 $d = "{$ddir}/{$name}";
99 if ( copy( "{$sdir}/{$name}", $d ) ) {
100 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
101 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
102 chmod( $d, $perms );
103 # print "Copied \"{$name}\" to \"{$ddir}\".\n";
104 } else {
105 print "Failed to copy file \"{$name}\" to \"{$ddir}\".\n";
106 exit();
107 }
108 }
109
110 function copydirectory( $source, $dest ) {
111 $handle = opendir( $source );
112 while ( false !== ( $f = readdir( $handle ) ) ) {
113 if ( "." == $f{0} ) continue;
114 # Windows turned all my CVS->cvs :(
115 if ( !strcasecmp ( "CVS", $f ) ) continue;
116 copyfile( $source, $f, $dest );
117 }
118 }
119
120 function readconsole() {
121 $fp = fopen( "php://stdin", "r" );
122 $resp = trim( fgets( $fp ) );
123 fclose( $fp );
124 return $resp;
125 }
126
127 function do_revision_updates() {
128 global $wgSoftwareRevision;
129 if ( $wgSoftwareRevision < 1001 ) { update_passwords(); }
130 }
131
132 function update_passwords() {
133 $fname = "Update script: update_passwords()";
134 print "\nIt appears that you need to update the user passwords in your\n" .
135 "database. If you have already done this (if you've run this update\n" .
136 "script once before, for example), doing so again will make all your\n" .
137 "user accounts inaccessible, so be sure you only do this once.\n" .
138 "Update user passwords? (yes/no) ";
139
140 $resp = readconsole();
141 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
142
143 $sql = "SELECT user_id,user_password FROM user";
144 $source = wfQuery( $sql, fname );
145
146 while ( $row = mysql_fetch_object( $source ) ) {
147 $id = $row->user_id;
148 $oldpass = $row->user_password;
149 $newpass = md5( "{$id}-{$oldpass}" );
150
151 $sql = "UPDATE user SET user_password='{$newpass}' " .
152 "WHERE user_id={$id}";
153 wfQuery( $sql, $fname );
154 }
155 }
156
157 function alter_ipblocks() {
158 global $wgAlterSpecs;
159
160 if ( field_exists( "ipblocks", "ipb_id" ) ) {
161 return;
162 }
163
164 if ( array_key_exists( "ipblocks", $wgAlterSpecs ) ) {
165 $wgAlterSpecs["ipblocks"] .= ",";
166 }
167
168 $wgAlterSpecs["ipblocks"] .=
169 "ADD ipb_auto tinyint(1) NOT NULL default '0', ".
170 "ADD ipb_id int(8) NOT NULL auto_increment,".
171 "ADD PRIMARY KEY (ipb_id)";
172 }
173
174 function field_exists( $table, $field ) {
175 $fname = "Update script: field_exists";
176 $res = wfQuery( "DESCRIBE $table", $fname );
177 $found = false;
178
179 while ( $row = wfFetchObject( $res ) ) {
180 if ( $row->Field == $field ) {
181 $found = true;
182 break;
183 }
184 }
185 return $found;
186 }
187
188 ?>