Upport some of the bad-title fixes; return NULL for bad titles
[mediawiki.git] / install.php
blob585e2cff459f3fa1a55884d81d8e4f5079862024
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" );
24 include_once( "./maintenance/InitialiseMessages.php" );
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();
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) ";
38 $response = readconsole();
39 if ( ! ( "Y" == $response{0} || "y" == $response{0} ) ) { exit(); }
42 $wgCommandLineMode = true;
43 umask( 000 );
44 set_time_limit( 0 );
47 # Make the necessary directories
49 $dirs = array( $IP, $wgUploadDirectory, $wgStyleSheetDirectory, $wgTmpDirectory );
50 foreach ( $dirs as $d ) { makedirectory( $d ); }
53 # Copy files into installation directories
55 print "Copying files...\n";
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 );
63 copydirectory( "./includes", $IP );
64 copydirectory( "./stylesheets", $wgStyleSheetDirectory );
66 copyfile( "./images", "wiki.png", $wgUploadDirectory );
67 copyfile( "./languages", "Language.php", $IP );
68 copyfile( "./languages", "Language" . ucfirst( $wgLanguageCode ) . ".php", $IP );
70 $fp = fopen( $wgDebugLogFile, "w" );
71 if ( false === $fp ) {
72 print "Could not create log file \"{$wgDebugLogFile}\".\n";
73 exit();
75 $d = date( "Y-m-d H:i:s" );
76 fwrite( $fp, "Wiki debug log file created {$d}\n\n" );
77 fclose( $fp );
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 );
87 copyfile( ".", "Version.php", $IP );
90 # Make and initialize database
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) ";
96 $response = readconsole();
97 if ( ! ( "Y" == $response{0} || "y" == $response{0} ) ) { exit(); }
99 print "\nYou should have already created a root password for the database.\n" .
100 "Enter the root password here: ";
102 $rootpw = readconsole();
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();
110 # Include rest of code to get things like internationalized messages.
112 include_once( "{$IP}/Setup.php" );
113 $wgTitle = Title::newFromText( "Installation script" );
115 # Now do the actual database creation
117 print "Creating database...\n";
118 dbsource( $rconn, "./maintenance/database.sql" );
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" );
126 populatedata(); # Needs internationalized messages
128 print "Adding indexes...\n";
129 dbsource( $rconn, "./maintenance/indexes.sql" );
131 print "Done.\nBrowse \"{$wgServer}{$wgScript}\" to test,\n" .
132 "or \"run WikiSuite -b -o\" in test suite.\n";
133 exit();
136 # Functions used above:
138 function makedirectory( $d ) {
139 global $wgInstallOwner, $wgInstallGroup;
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();
155 function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
156 global $wgInstallOwner, $wgInstallGroup;
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();
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 );
180 function readconsole() {
181 $fp = fopen( "php://stdin", "r" );
182 $resp = trim( fgets( $fp, 1024 ) );
183 fclose( $fp );
184 return $resp;
188 # Read and execute SQL commands from a file
190 function dbsource( $conn, $fname ) {
191 $fp = fopen( $fname, "r" );
192 if ( false === $fp ) {
193 print "Could not open \"{$fname}\".\n";
194 exit();
197 $cmd = "";
198 $done = false;
200 while ( ! feof( $fp ) ) {
201 $line = trim( fgets( $fp, 1024 ) );
202 $sl = strlen( $line ) - 1;
204 if ( $sl < 0 ) { continue; }
205 if ( "-" == $line{0} && "-" == $line{1} ) { continue; }
207 if ( ";" == $line{$sl} ) {
208 $done = true;
209 $line = substr( $line, 0, $sl );
212 if ( "" != $cmd ) { $cmd .= " "; }
213 $cmd .= $line;
215 if ( $done ) {
216 $cmd = replacevars( $cmd );
217 $res = mysql_query( $cmd, $conn );
219 if ( false === $res ) {
220 print "Query \"{$cmd}\" failed.\n";
221 exit();
224 $cmd = "";
225 $done = false;
228 fclose( $fp );
231 function replacevars( $ins ) {
232 $varnames = array(
233 "wgDBserver", "wgDBname", "wgDBintlname", "wgDBuser",
234 "wgDBpassword", "wgDBsqluser", "wgDBsqlpassword",
235 "wgDBadminuser", "wgDBadminpassword"
238 foreach ( $varnames as $var ) {
239 global $$var;
240 $ins = str_replace( '{$' . $var . '}', $$var, $ins );
242 return $ins;
245 function populatedata() {
246 global $wgDBadminpassword;
247 $fname = "Installation script: populatedata()";
249 $sql = "DELETE FROM site_stats";
250 wfQuery( $sql, DB_WRITE, $fname );
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 );
256 $sql = "DELETE FROM user";
257 wfQuery( $sql, DB_WRITE, $fname );
259 $u = User::newFromName( "WikiSysop" );
260 if ( 0 == $u->idForName() ) {
261 $u->addToDatabase();
262 $u->setPassword( $wgDBadminpassword );
263 $u->addRight( "sysop" );
264 $u->saveSettings();
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();
275 $wns = Namespace::getWikipedia();
276 $ulp = addslashes( wfMsgNoDB( "uploadlogpage" ) );
277 $dlp = addslashes( wfMsgNoDB( "dellogpage" ) );
279 $sql = "DELETE FROM cur";
280 wfQuery( $sql, DB_WRITE, $fname );
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 );
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 );
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 );
299 initialiseMessages();