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