Put a buffer size of 1024 (the default value) into the call to fgets so
[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
124 populatedata(); # Needs internationalized messages
125
126 print "Adding indexes...\n";
127 dbsource( $rconn, "./maintenance/indexes.sql" );
128
129 print "Done.\nBrowse \"{$wgServer}{$wgScript}\" to test,\n" .
130 "or \"run WikiSuite -b -o\" in test suite.\n";
131 exit();
132
133 #
134 # Functions used above:
135 #
136 function makedirectory( $d ) {
137 global $wgInstallOwner, $wgInstallGroup;
138
139 if ( is_dir( $d ) ) {
140 print "Directory \"{$d}\" exists.\n";
141 } else {
142 if ( mkdir( $d, 0777 ) ) {
143 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
144 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
145 print "Directory \"{$d}\" created.\n";
146 } else {
147 print "Could not create directory \"{$d}\".\n";
148 exit();
149 }
150 }
151 }
152
153 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
154 global $wgInstallOwner, $wgInstallGroup;
155
156 $d = "{$ddir}/{$name}";
157 if ( copy( "{$sdir}/{$name}", $d ) ) {
158 if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
159 if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
160 chmod( $d, $perms );
161 # print "Copied \"{$name}\" to \"{$ddir}\".\n";
162 } else {
163 print "Failed to copy file \"{$name}\" to \"{$ddir}\".\n";
164 exit();
165 }
166 }
167
168 function copydirectory( $source, $dest ) {
169 $handle = opendir( $source );
170 while ( false !== ( $f = readdir( $handle ) ) ) {
171 if ( "." == $f{0} ) continue;
172 if ( "CVS" == $f ) continue;
173 copyfile( $source, $f, $dest );
174 }
175 }
176
177 function readconsole() {
178 $fp = fopen( "php://stdin", "r" );
179 $resp = trim( fgets( $fp, 1024 ) );
180 fclose( $fp );
181 return $resp;
182 }
183
184 #
185 # Read and execute SQL commands from a file
186 #
187 function dbsource( $conn, $fname ) {
188 $fp = fopen( $fname, "r" );
189 if ( false === $fp ) {
190 print "Could not open \"{$fname}\".\n";
191 exit();
192 }
193
194 $cmd = "";
195 $done = false;
196
197 while ( ! feof( $fp ) ) {
198 $line = trim( fgets( $fp ) );
199 $sl = strlen( $line ) - 1;
200
201 if ( $sl < 0 ) { continue; }
202 if ( "-" == $line{0} && "-" == $line{1} ) { continue; }
203
204 if ( ";" == $line{$sl} ) {
205 $done = true;
206 $line = substr( $line, 0, $sl );
207 }
208
209 if ( "" != $cmd ) { $cmd .= " "; }
210 $cmd .= $line;
211
212 if ( $done ) {
213 $cmd = replacevars( $cmd );
214 $res = mysql_query( $cmd, $conn );
215
216 if ( false === $res ) {
217 print "Query \"{$cmd}\" failed.\n";
218 exit();
219 }
220
221 $cmd = "";
222 $done = false;
223 }
224 }
225 fclose( $fp );
226 }
227
228 function replacevars( $ins ) {
229 $varnames = array(
230 "wgDBserver", "wgDBname", "wgDBintlname", "wgDBuser",
231 "wgDBpassword", "wgDBsqluser", "wgDBsqlpassword",
232 "wgDBadminuser", "wgDBadminpassword"
233 );
234
235 foreach ( $varnames as $var ) {
236 global $$var;
237 $ins = str_replace( '{$' . $var . '}', $$var, $ins );
238 }
239 return $ins;
240 }
241
242 function populatedata() {
243 global $wgDBadminpassword;
244 $fname = "Installation script: populatedata()";
245
246 $sql = "DELETE FROM site_stats";
247 wfQuery( $sql, $fname );
248
249 $sql = "INSERT INTO site_stats (ss_row_id,ss_total_views," .
250 "ss_total_edits,ss_good_articles) VALUES (1,0,0,0)";
251 wfQuery( $sql, $fname );
252
253 $sql = "DELETE FROM user";
254 wfQuery( $sql, $fname );
255
256 $u = User::newFromName( "WikiSysop" );
257 if ( 0 == $u->idForName() ) {
258 $u->addToDatabase();
259 $u->setPassword( $wgDBadminpassword );
260 $u->addRight( "sysop" );
261 $u->saveSettings();
262 }
263 $u = User::newFromName( "WikiDeveloper" );
264 if ( 0 == $u->idForName() ) {
265 $u->addToDatabase();
266 $u->setPassword( $wgDBadminpassword );
267 $u->addRight( "sysop" );
268 $u->addRight( "developer" );
269 $u->saveSettings();
270 }
271
272 $wns = Namespace::getWikipedia();
273 $ulp = addslashes( wfMsg( "uploadlogpage" ) );
274 $dlp = addslashes( wfMsg( "dellogpage" ) );
275
276 $sql = "DELETE FROM cur";
277 wfQuery( $sql, $fname );
278
279 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
280 "cur_restrictions) VALUES ({$wns},'{$ulp}','" .
281 wfStrencode( wfMsg( "uploadlogpagetext" ) ) . "','sysop')";
282 wfQuery( $sql );
283
284 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
285 "cur_restrictions) VALUES ({$wns},'{$dlp}','" .
286 wfStrencode( wfMsg( "dellogpagetext" ) ) . "','sysop')";
287 wfQuery( $sql );
288
289 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text) " .
290 "VALUES (0,'" . wfStrencode( wfMsg( "mainpage" ) ) . "','" .
291 wfStrencode( wfMsg( "mainpagetext" ) ) . "')";
292 wfQuery( $sql );
293 }
294
295 ?>