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