Fix typo
[lhc/web/wiklou.git] / install.php
1 <?
2
3 # Install software and create new empty database.
4 #
5
6 if ( ! ( is_readable( "./LocalSettings.php" )
7 && is_readable( "./AdminSettings.php" ) ) ) {
8 print "You must first create the files LocalSettings.php\n" .
9 "and AdminSettings.php based on the samples in the top\n" .
10 "source directory before running this install script.\n";
11 exit();
12 }
13 if ( $wgUseTeX && ( ! is_executable( "./math/texvc" ) ) ) {
14 print "To use math functions, you must first compile texvc by\n" .
15 "running \"make\" in the math directory.\n";
16 exit();
17 }
18
19 $DP = "./includes";
20 include_once( "./LocalSettings.php" );
21 include_once( "./AdminSettings.php" );
22
23 if ( is_file( "{$IP}/Version.php" ) ) {
24 print "There appears to be an installation of the software\n" .
25 "already present on \"{$IP}\". You may want to run the update\n" .
26 "script instead. If you continue with this installation script,\n" .
27 "that software and all of its data will be overwritten.\n" .
28 "Are you sure you want to do this? (yes/no) ";
29
30 $response = readconsole();
31 if ( ! ( "Y" == $response{0} || "y" == $response{0} ) ) { exit(); }
32 }
33
34 $wgCommandLineMode = true;
35 umask( 000 );
36 set_time_limit( 0 );
37
38 #
39 # Make the necessary directories
40 #
41 $dirs = array( $IP, $wgUploadDirectory, $wgStyleSheetDirectory, $wgTmpDirectory );
42 foreach ( $dirs as $d ) { makedirectory( $d ); }
43
44 #
45 # Copy files into installation directories
46 #
47 print "Copying files...\n";
48
49 copyfile( ".", "LocalSettings.php", $IP );
50 copyfile( ".", "Version.php", $IP );
51 copyfile( ".", "wiki.phtml", $IP );
52 copyfile( ".", "redirect.phtml", $IP );
53 copyfile( ".", "texvc.phtml", $IP );
54
55 copydirectory( "./includes", $IP );
56 copydirectory( "./stylesheets", $wgStyleSheetsDirectory );
57
58 copyfile( "./images", "wiki.png", $wgUploadDirectory );
59 copyfile( "./languages", "Language.php", $IP );
60 copyfile( "./languages", "Language" . ucfirst( $wgLanguageCode ) . ".php", $IP );
61
62 $fp = fopen( $wgDebugLogFile, "w" );
63 if ( false === $fp ) {
64 print "Could not create log file \"{$wgDebugLogFile}\".\n";
65 exit();
66 }
67 $d = date( "Y-m-d H:i:s" );
68 fwrite( $fp, "Wiki debug log file created {$d}\n\n" );
69 fclose( $fp );
70
71 if ( $wgUseTeX ) {
72 makedirectory( "{$IP}/math" );
73 makedirectory( $wgMathDirectory );
74 copyfile( "./math", "texvc", "{$IP}/math", 0775 );
75 copyfile( "./math", "texvc_test", "{$IP}/math", 0775 );
76 copyfile( "./math", "texvc_tex", "{$IP}/math", 0775 );
77 }
78
79 copyfile( ".", "Version.php", $IP );
80
81 #
82 # Make and initialize database
83 #
84 print "\n* * *\nWarning! This script will completely erase the\n" .
85 "existing database \"{$wgDBname}\" and all its contents.\n" .
86 "Are you sure you want to do this? (yes/no) ";
87
88 $response = readconsole();
89 if ( ! ( "Y" == $response{0} || "y" == $response{0} ) ) { exit(); }
90
91 print "\nYou should have already created a root password for the database.\n" .
92 "Enter the root password here: ";
93
94 $rootpw = readconsole();
95
96 $rconn = mysql_connect( $wgDBserver, "root", $rootpw );
97 if ( false === $rconn ) {
98 print "Could not connect to database on \"{$wgDBserver}\" as root.\n";
99 exit();
100 }
101
102 # Include rest of code to get things like internationalized messages.
103 #
104 include_once( "{$IP}/Setup.php" );
105 $wgTitle = Title::newFromText( "Installation script" );
106
107 # Now do the actual database creation
108 #
109 print "Creating database...\n";
110 dbsource( $rconn, "./maintenance/database.sql" );
111
112 mysql_select_db( $wgDBname, $rconn );
113 dbsource( $rconn, "./maintenance/tables.sql" );
114 dbsource( $rconn, "./maintenance/users.sql" );
115 dbsource( $rconn, "./maintenance/initialdata.sql" );
116
117 populatedata(); # Needs internationalized messages
118
119 print "Adding indexes...\n";
120 dbsource( $rconn, "./maintenance/indexes.sql" );
121
122 print "Done.\nBrowse \"{$wgServer}{$wgScript}\" to test,\n" .
123 "or \"run WikiSuite -b -o\" in test suite.\n";
124 exit();
125
126 #
127 # Functions used above:
128 #
129 function makedirectory( $d ) {
130 global $wgInstallOwner, $wgInstallGroup;
131
132 if ( is_dir( $d ) ) {
133 print "Directory \"{$d}\" exists.\n";
134 } else {
135 if ( mkdir( $d, 0777 ) ) {
136 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
137 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
138 print "Directory \"{$d}\" created.\n";
139 } else {
140 print "Could not create directory \"{$d}\".\n";
141 exit();
142 }
143 }
144 }
145
146 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
147 global $wgInstallOwner, $wgInstallGroup;
148
149 $d = "{$ddir}/{$name}";
150 if ( copy( "{$sdir}/{$name}", $d ) ) {
151 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
152 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
153 chmod( $d, $perms );
154 # print "Copied \"{$name}\" to \"{$ddir}\".\n";
155 } else {
156 print "Failed to copy file \"{$name}\" to \"{$ddir}\".\n";
157 exit();
158 }
159 }
160
161 function copydirectory( $source, $dest ) {
162 $handle = opendir( $source );
163 while ( false !== ( $f = readdir( $handle ) ) ) {
164 if ( "." == $f{0} ) continue;
165 if ( "CVS" == $f ) continue;
166 copyfile( $source, $f, $dest );
167 }
168 }
169
170 function readconsole() {
171 $fp = fopen( "php://stdin", "r" );
172 $resp = trim( fgets( $fp ) );
173 fclose( $fp );
174 return $resp;
175 }
176
177 #
178 # Read and execute SQL commands from a file
179 #
180 function dbsource( $conn, $fname ) {
181 $fp = fopen( $fname, "r" );
182 if ( false === $fp ) {
183 print "Could not open \"{$fname}\".\n";
184 exit();
185 }
186
187 $cmd = "";
188 $done = false;
189
190 while ( ! feof( $fp ) ) {
191 $line = trim( fgets( $fp ) );
192 $sl = strlen( $line ) - 1;
193
194 if ( $sl < 0 ) { continue; }
195 if ( "-" == $line{0} && "-" == $line{1} ) { continue; }
196
197 if ( ";" == $line{$sl} ) {
198 $done = true;
199 $line = substr( $line, 0, $sl );
200 }
201
202 if ( "" != $cmd ) { $cmd .= " "; }
203 $cmd .= $line;
204
205 if ( $done ) {
206 $cmd = replacevars( $cmd );
207 $res = mysql_query( $cmd, $conn );
208
209 if ( false === $res ) {
210 print "Query \"{$cmd}\" failed.\n";
211 exit();
212 }
213
214 $cmd = "";
215 $done = false;
216 }
217 }
218 fclose( $fp );
219 }
220
221 function replacevars( $ins ) {
222 $varnames = array(
223 "wgDBserver", "wgDBname", "wgDBintlname", "wgDBuser",
224 "wgDBpassword", "wgDBsqluser", "wgDBsqlpassword",
225 "wgDBadminuser", "wgDBadminpassword"
226 );
227
228 foreach ( $varnames as $var ) {
229 global $$var;
230 $ins = str_replace( '{$' . $var . '}', $$var, $ins );
231 }
232 return $ins;
233 }
234
235 function populatedata() {
236 global $wgDBadminpassword;
237 $fname = "Installation script: populatedata()";
238
239 $sql = "DELETE FROM site_stats";
240 wfQuery( $sql, $fname );
241
242 $sql = "INSERT INTO site_stats (ss_row_id,ss_total_views," .
243 "ss_total_edits,ss_good_articles) VALUES (1,0,0,0)";
244 wfQuery( $sql, $fname );
245
246 $sql = "DELETE FROM user";
247 wfQuery( $sql, $fname );
248
249 $sql = "INSERT INTO user (user_name, user_password, user_rights)" .
250 "VALUES ('WikiSysop','" . User::encryptPassword( $wgDBadminpassword ) .
251 "','sysop'),('WikiDeveloper','" . User::encryptPassword(
252 $wgDBadminpassword ) . "','sysop,developer')";
253 wfQuery( $sql, $fname );
254
255 $wns = Namespace::getWikipedia();
256 $ulp = addslashes( wfMsg( "uploadlogpage" ) );
257 $dlp = addslashes( wfMsg( "dellogpage" ) );
258
259 $sql = "DELETE FROM cur";
260 wfQuery( $sql, $fname );
261
262 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
263 "cur_restrictions) VALUES ({$wns},'{$ulp}','" .
264 wfStrencode( wfMsg( "uploadlogpagetext" ) ) . "','sysop')";
265 wfQuery( $sql );
266
267 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
268 "cur_restrictions) VALUES ({$wns},'{$dlp}','" .
269 wfStrencode( wfMsg( "dellogpagetext" ) ) . "','sysop')";
270 wfQuery( $sql );
271
272 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text) " .
273 "VALUES (0,'" . wfStrencode( wfMsg( "mainpage" ) ) . "','" .
274 wfStrencode( wfMsg( "mainpagetext" ) ) . "')";
275 wfQuery( $sql );
276 }
277
278 ?>