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