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