Misc fixes
[mediawiki.git] / install.php
blob71d27ec42042995e7b6247ee4c7e8ee13cdc4cb4
1 <?
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;
10 # Install software and create new empty database.
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();
21 $DP = "./includes";
22 include_once( "./LocalSettings.php" );
23 include_once( "./AdminSettings.php" );
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();
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) ";
37 $response = readconsole();
38 if ( ! ( "Y" == $response{0} || "y" == $response{0} ) ) { exit(); }
41 $wgCommandLineMode = true;
42 umask( 000 );
43 set_time_limit( 0 );
46 # Make the necessary directories
48 $dirs = array( $IP, $wgUploadDirectory, $wgStyleSheetDirectory, $wgTmpDirectory );
49 foreach ( $dirs as $d ) { makedirectory( $d ); }
52 # Copy files into installation directories
54 print "Copying files...\n";
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 );
62 copydirectory( "./includes", $IP );
63 copydirectory( "./stylesheets", $wgStyleSheetDirectory );
65 copyfile( "./images", "wiki.png", $wgUploadDirectory );
66 copyfile( "./languages", "Language.php", $IP );
67 copyfile( "./languages", "Language" . ucfirst( $wgLanguageCode ) . ".php", $IP );
69 $fp = fopen( $wgDebugLogFile, "w" );
70 if ( false === $fp ) {
71 print "Could not create log file \"{$wgDebugLogFile}\".\n";
72 exit();
74 $d = date( "Y-m-d H:i:s" );
75 fwrite( $fp, "Wiki debug log file created {$d}\n\n" );
76 fclose( $fp );
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 );
86 copyfile( ".", "Version.php", $IP );
89 # Make and initialize database
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) ";
95 $response = readconsole();
96 if ( ! ( "Y" == $response{0} || "y" == $response{0} ) ) { exit(); }
98 print "\nYou should have already created a root password for the database.\n" .
99 "Enter the root password here: ";
101 $rootpw = readconsole();
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();
109 # Include rest of code to get things like internationalized messages.
111 include_once( "{$IP}/Setup.php" );
112 $wgTitle = Title::newFromText( "Installation script" );
114 # Now do the actual database creation
116 print "Creating database...\n";
117 dbsource( $rconn, "./maintenance/database.sql" );
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" );
125 populatedata(); # Needs internationalized messages
127 print "Adding indexes...\n";
128 dbsource( $rconn, "./maintenance/indexes.sql" );
130 print "Done.\nBrowse \"{$wgServer}{$wgScript}\" to test,\n" .
131 "or \"run WikiSuite -b -o\" in test suite.\n";
132 exit();
135 # Functions used above:
137 function makedirectory( $d ) {
138 global $wgInstallOwner, $wgInstallGroup;
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();
154 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
155 global $wgInstallOwner, $wgInstallGroup;
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();
169 function copydirectory( $source, $dest ) {
170 $handle = opendir( $source );
171 while ( false !== ( $f = readdir( $handle ) ) ) {
172 if ( "." == $f{0} ) continue;
173 if ( "CVS" == $f ) continue;
174 copyfile( $source, $f, $dest );
178 function readconsole() {
179 $fp = fopen( "php://stdin", "r" );
180 $resp = trim( fgets( $fp, 1024 ) );
181 fclose( $fp );
182 return $resp;
186 # Read and execute SQL commands from a file
188 function dbsource( $conn, $fname ) {
189 $fp = fopen( $fname, "r" );
190 if ( false === $fp ) {
191 print "Could not open \"{$fname}\".\n";
192 exit();
195 $cmd = "";
196 $done = false;
198 while ( ! feof( $fp ) ) {
199 $line = trim( fgets( $fp, 1024 ) );
200 $sl = strlen( $line ) - 1;
202 if ( $sl < 0 ) { continue; }
203 if ( "-" == $line{0} && "-" == $line{1} ) { continue; }
205 if ( ";" == $line{$sl} ) {
206 $done = true;
207 $line = substr( $line, 0, $sl );
210 if ( "" != $cmd ) { $cmd .= " "; }
211 $cmd .= $line;
213 if ( $done ) {
214 $cmd = replacevars( $cmd );
215 $res = mysql_query( $cmd, $conn );
217 if ( false === $res ) {
218 print "Query \"{$cmd}\" failed.\n";
219 exit();
222 $cmd = "";
223 $done = false;
226 fclose( $fp );
229 function replacevars( $ins ) {
230 $varnames = array(
231 "wgDBserver", "wgDBname", "wgDBintlname", "wgDBuser",
232 "wgDBpassword", "wgDBsqluser", "wgDBsqlpassword",
233 "wgDBadminuser", "wgDBadminpassword"
236 foreach ( $varnames as $var ) {
237 global $$var;
238 $ins = str_replace( '{$' . $var . '}', $$var, $ins );
240 return $ins;
243 function populatedata() {
244 global $wgDBadminpassword;
245 $fname = "Installation script: populatedata()";
247 $sql = "DELETE FROM site_stats";
248 wfQuery( $sql, $fname );
250 $sql = "INSERT INTO site_stats (ss_row_id,ss_total_views," .
251 "ss_total_edits,ss_good_articles) VALUES (1,0,0,0)";
252 wfQuery( $sql, $fname );
254 $sql = "DELETE FROM user";
255 wfQuery( $sql, $fname );
257 $u = User::newFromName( "WikiSysop" );
258 if ( 0 == $u->idForName() ) {
259 $u->addToDatabase();
260 $u->setPassword( $wgDBadminpassword );
261 $u->addRight( "sysop" );
262 $u->saveSettings();
264 $u = User::newFromName( "WikiDeveloper" );
265 if ( 0 == $u->idForName() ) {
266 $u->addToDatabase();
267 $u->setPassword( $wgDBadminpassword );
268 $u->addRight( "sysop" );
269 $u->addRight( "developer" );
270 $u->saveSettings();
273 $wns = Namespace::getWikipedia();
274 $ulp = addslashes( wfMsg( "uploadlogpage" ) );
275 $dlp = addslashes( wfMsg( "dellogpage" ) );
277 $sql = "DELETE FROM cur";
278 wfQuery( $sql, $fname );
280 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
281 "cur_restrictions) VALUES ({$wns},'{$ulp}','" .
282 wfStrencode( wfMsg( "uploadlogpagetext" ) ) . "','sysop')";
283 wfQuery( $sql );
285 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
286 "cur_restrictions) VALUES ({$wns},'{$dlp}','" .
287 wfStrencode( wfMsg( "dellogpagetext" ) ) . "','sysop')";
288 wfQuery( $sql );
290 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text) " .
291 "VALUES (0,'" . wfStrencode( wfMsg( "mainpage" ) ) . "','" .
292 wfStrencode( wfMsg( "mainpagetext" ) ) . "')";
293 wfQuery( $sql );