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