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