Add DiscussionThreading info to export-0.4.xsd. TODO: Test against real data to make...
[lhc/web/wiklou.git] / install-utils.inc
index 3ff06ec..234b12c 100644 (file)
@@ -1,5 +1,12 @@
 <?php
 
+/**
+ * This file contains functions used by the install script (config/index.php)
+ * and maintenance scripts. It is not loaded in normal web requests.
+ *
+ * @file
+ */
+
 function install_version_checks() {
        # We dare not turn output buffer _off_ since this will break completely
        # if PHP is globally configured to run through a gzip filter.
@@ -11,7 +18,9 @@ function install_version_checks() {
                die( -1 );
        }
        if( version_compare( phpversion(), '5.0.0' ) < 0 ) {
-               echo "PHP 5.0.0 or higher is required. ABORTING.\n";
+               echo "PHP 5.0.0 or higher is required. If PHP 5 is available only when \n".
+   "PHP files have a .php5 extension, please navigate to <a href=\"index.php5\">index.php5</a> \n".
+   "to continue installation. ABORTING.\n";
                die( -1 );
        }
        
@@ -24,6 +33,16 @@ function install_version_checks() {
                        "or higher. ABORTING. (http://bugs.php.net/bug.php?id=34879 for details)\n";
                die( -1 );
        }
+       
+       $test = new PhpXmlBugTester();
+       if( !$test->ok ) {
+               echo "Your system has a combination of PHP and libxml2 versions which is buggy\n" .
+                       "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
+                       "Upgrade to PHP 5.2.9 or later and libxml2 2.7.2 or later!\n" .
+                       "ABORTING (http://bugs.php.net/bug.php?id=45996 for details).\n";
+               die( -1 );
+       }
+       
 
        global $wgCommandLineMode;
        $wgCommandLineMode = true;
@@ -31,32 +50,25 @@ function install_version_checks() {
        @set_time_limit( 0 );
 }
 
-function copyfile( $sdir, $name, $ddir, $perms = 0664 ) {
-       copyfileto( $sdir, $name, $ddir, $name, $perms );
-}
-
-function copyfileto( $sdir, $sname, $ddir, $dname, $perms = 0664 ) {
-       global $wgInstallOwner, $wgInstallGroup;
-
-       $d = "{$ddir}/{$dname}";
-       if ( copy( "{$sdir}/{$sname}", $d ) ) {
-               if ( isset( $wgInstallOwner ) ) { chown( $d, $wgInstallOwner ); }
-               if ( isset( $wgInstallGroup ) ) { chgrp( $d, $wgInstallGroup ); }
-               chmod( $d, $perms );
-               # print "Copied \"{$sname}\" to \"{$d}\".\n";
-       } else {
-               print "Failed to copy file \"{$sname}\" to \"{$ddir}/{$dname}\".\n";
-               exit();
+/**
+ * Test for PHP+libxml2 bug which breaks XML input subtly with certain versions.
+ * http://bugs.php.net/bug.php?id=45996
+ * Known fixed with PHP 5.2.9 + libxml2-2.7.3
+ */
+class PhpXmlBugTester {
+       private $parsedData = '';
+       public $ok = false;
+       public function __construct() {
+               $charData = '<b>c</b>';
+               $xml = '<a>' . htmlspecialchars( $charData ) . '</a>';
+               
+               $parser = xml_parser_create();
+               xml_set_character_data_handler( $parser, array( $this, 'chardata' ) );
+               $parsedOk = xml_parse($parser, $xml, true);
+               $this->ok = $parsedOk && ($this->parsedData == $charData);
        }
-}
-
-function copydirectory( $source, $dest ) {
-       $handle = opendir( $source );
-       while ( false !== ( $f = readdir( $handle ) ) ) {
-               $fullname = "$source/$f";
-               if ( $f{0} != '.' && is_file( $fullname ) ) {
-                       copyfile( $source, $f, $dest );
-               }
+       public function chardata($parser, $data) {
+               $this->parsedData .= $data;
        }
 }
 
@@ -74,30 +86,76 @@ function readconsole( $prompt = '' ) {
                return readline( $prompt );
        } else {
                if ( $isatty ) {
-                       print $prompt;
-               }
-               if ( feof( STDIN ) ) {
-                       return false;
+                       $st = readlineEmulation( $prompt );
+               } else {
+                       if ( feof( STDIN ) ) {
+                               $st = false;
+                       } else {
+                               $st = fgets(STDIN, 1024);
+                       }
                }
-               $st = fgets(STDIN, 1024);
                if ($st === false) return false;
                $resp = trim( $st );
                return $resp;
        }
 }
 
+function findExecutable( $name ) {
+       $paths = explode( PATH_SEPARATOR, getenv( "PATH" ) );
+       foreach( $paths as $path ) {
+               $full = $path . DIRECTORY_SEPARATOR . $name;
+               if( file_exists( $full ) ) {
+                       if( wfIsWindows() || is_executable( $full ) ) {
+                               return $full;
+                       }
+               }
+       }
+       return false;
+}
+
+function readlineEmulation( $prompt ) {
+       $bash = "bash";
+       if( !wfIsWindows() && findExecutable( $bash ) ) {
+               $retval = false;
+               $encPrompt = wfEscapeShellArg( $prompt );
+               $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
+               $encCommand = wfEscapeShellArg( $command );
+               $line = wfShellExec( "$bash -c $encCommand", $retval );
+               
+               if( $retval == 0 ) {
+                       return $line;
+               } elseif( $retval == 127 ) {
+                       // Couldn't execute bash even though we thought we saw it.
+                       // Shell probably spit out an error message, sorry :(
+                       // Fall through to fgets()...
+               } else {
+                       // EOF/ctrl+D
+                       return false;
+               }
+       }
+       
+       // Fallback... we'll have no editing controls, EWWW
+       if ( feof( STDIN ) ) {
+               return false;
+       }
+       print $prompt;
+       return fgets(STDIN, 1024);
+}
+
+
 #
 # Read and execute SQL commands from a file
 #
 function dbsource( $fname, $db = false ) {
+       wfDeprecated( __METHOD__ );
        if ( !$db ) {
                // Try $wgDatabase, which is used in the install and update scripts
                global $wgDatabase;
                if ( isset( $wgDatabase ) ) {
-                       $db =& $wgDatabase;
+                       $db = $wgDatabase;
                } else {
                        // No? Well, we must be outside of those scripts, so use the standard method
-                       $db =& wfGetDB( DB_MASTER );
+                       $db = wfGetDB( DB_MASTER );
                }
        }
        $error = $db->sourceFile( $fname );
@@ -107,47 +165,32 @@ function dbsource( $fname, $db = false ) {
        }
 }
 
-# Obsolete, use Database::fieldExists()
-function field_exists( $table, $field ) {
-       $fname = 'Update script: field_exists';
-       $db =& wfGetDB( DB_SLAVE );
-       $res = $db->query( "DESCRIBE $table", $fname );
-       $found = false;
-
-       while ( $row = $db->fetchObject( $res ) ) {
-               if ( $row->Field == $field ) {
-                       $found = true;
-                       break;
-               }
-       }
-       return $found;
-}
-
-# Obsolete Database::tableExists()
-function table_exists( $db ) {
-       global $wgDBname;
-       $res = mysql_list_tables( $wgDBname );
-       if( !$res ) {
-               echo "** " . mysql_error() . "\n";
-               return false;
-       }
-       for( $i = mysql_num_rows( $res ) - 1; $i--; $i > 0 ) {
-               if( mysql_tablename( $res, $i ) == $db ) return true;
-       }
-       return false;
-}
-
-# Obsolete, use Database:fieldInfo()
-function field_info( $table, $field ) {
-       $res = mysql_query( "SELECT * FROM $table LIMIT 1" );
-       $n = mysql_num_fields( $res );
-       for( $i = 0; $i < $n; $i++ ) {
-               $meta = mysql_fetch_field( $res, $i );
-               if( $field == $meta->name ) {
-                       return $meta;
-               }
-       }
-       return false;
+/**
+ * Get the value of session.save_path
+ *
+ * Per http://www.php.net/manual/en/ref.session.php#ini.session.save-path,
+ * this might have some additional preceding parts which need to be
+ * ditched
+ *
+ * @return string
+ */
+function mw_get_session_save_path() {
+       $path = ini_get( 'session.save_path' );
+       $path = substr( $path, strrpos( $path, ';' ) );
+       return $path;
 }
 
-?>
+/**
+ * Is dl() available to us?
+ *
+ * According to http://www.php.net/manual/en/function.dl.php, dl()
+ * is *not* available when `enable_dl` is off, or under `safe_mode`
+ *
+ * @return bool
+ */
+function mw_have_dl() {
+       return function_exists( 'dl' )
+               && is_callable( 'dl' )
+               && wfIniGetBool( 'enable_dl' )
+               && !wfIniGetBool( 'safe_mode' );
+}
\ No newline at end of file