Autoblocker privacy protection
[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 #
35 # Copy files into installation directories
36 #
37 print "Copying files...\n";
38
39 copyfile( ".", "wiki.phtml", $IP );
40 copyfile( ".", "redirect.phtml", $IP );
41 copyfile( ".", "texvc.phtml", $IP );
42
43 copydirectory( "./includes", $IP );
44 copydirectory( "./stylesheets", $wgStyleSheetDirectory );
45
46 copyfile( "./images", "wiki.png", $wgUploadDirectory );
47 copyfile( "./languages", "Language.php", $IP );
48 copyfile( "./languages", "Language" . ucfirst( $wgLanguageCode ) . ".php", $IP );
49
50 $fp = fopen( $wgDebugLogFile, "w" );
51 if ( false === $fp ) {
52 print "Could not create log file \"{$wgDebugLogFile}\".\n";
53 exit();
54 }
55 $d = date( "Y-m-d H:i:s" );
56 fwrite( $fp, "Wiki debug log file created {$d}\n\n" );
57 fclose( $fp );
58
59 if ( $wgUseTeX ) {
60 copyfile( "./math", "texvc", "{$IP}/math", 0775 );
61 copyfile( "./math", "texvc_test", "{$IP}/math", 0775 );
62 copyfile( "./math", "texvc_tex", "{$IP}/math", 0775 );
63 }
64
65 copyfile( ".", "Version.php", $IP );
66
67 print "Done.\n";
68 exit();
69
70 #
71 #
72 #
73
74 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
75 global $wgInstallOwner, $wgInstallGroup;
76
77 $d = "{$ddir}/{$name}";
78 if ( copy( "{$sdir}/{$name}", $d ) ) {
79 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
80 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
81 chmod( $d, $perms );
82 # print "Copied \"{$name}\" to \"{$ddir}\".\n";
83 } else {
84 print "Failed to copy file \"{$name}\" to \"{$ddir}\".\n";
85 exit();
86 }
87 }
88
89 function copydirectory( $source, $dest ) {
90 $handle = opendir( $source );
91 while ( false !== ( $f = readdir( $handle ) ) ) {
92 if ( "." == $f{0} ) continue;
93 # Windows turned all my CVS->cvs :(
94 if ( !strcasecmp ( "CVS", $f ) ) continue;
95 copyfile( $source, $f, $dest );
96 }
97 }
98
99 function readconsole() {
100 $fp = fopen( "php://stdin", "r" );
101 $resp = trim( fgets( $fp ) );
102 fclose( $fp );
103 return $resp;
104 }
105
106 function do_revision_updates() {
107 global $wgSoftwareRevision, $wgAlterSpecs, $wgDBserver, $wgDBadminuser;
108 global $wgDBadminpassword, $wgDBname;
109
110 if ( $wgSoftwareRevision < 1001 ) { update_passwords(); }
111 if ( $wgSoftwareRevision < 1002 ) { alter_ipblocks(); }
112
113 # Run ALTER TABLE queries.
114
115 if ( count( $wgAlterSpecs ) ) {
116 $rconn = mysql_connect( $wgDBserver, $wgDBadminuser, $wgDBadminpassword );
117 mysql_select_db( $wgDBname );
118 print "\n";
119 foreach ( $wgAlterSpecs as $table => $specs ) {
120 $sql = "ALTER TABLE $table $specs";
121 print "$sql;\n";
122 $res = mysql_query( $sql, $rconn );
123 if ( $res === false ) {
124 print "MySQL error: " . mysql_error( $rconn ) . "\n";
125 }
126 }
127 mysql_close( $rconn );
128 }
129 }
130
131 function update_passwords() {
132 $fname = "Update script: update_passwords()";
133 print "\nIt appears that you need to update the user passwords in your\n" .
134 "database. If you have already done this (if you've run this update\n" .
135 "script once before, for example), doing so again will make all your\n" .
136 "user accounts inaccessible, so be sure you only do this once.\n" .
137 "Update user passwords? (yes/no) ";
138
139 $resp = readconsole();
140 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
141
142 $sql = "SELECT user_id,user_password FROM user";
143 $source = wfQuery( $sql, fname );
144
145 while ( $row = mysql_fetch_object( $source ) ) {
146 $id = $row->user_id;
147 $oldpass = $row->user_password;
148 $newpass = md5( "{$id}-{$oldpass}" );
149
150 $sql = "UPDATE user SET user_password='{$newpass}' " .
151 "WHERE user_id={$id}";
152 wfQuery( $sql, $fname );
153 }
154 }
155
156 function alter_ipblocks() {
157 global $wgAlterSpecs;
158 $fname = "Update script: alter_ipblocks";
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 ?>