* Allow $wgDiff3=false
authorTim Starling <tstarling@users.mediawiki.org>
Mon, 6 Oct 2008 00:45:18 +0000 (00:45 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Mon, 6 Oct 2008 00:45:18 +0000 (00:45 +0000)
* Don't call quickUserCan('edit') unless section edit is enabled
* In DatabasePostgres and DatabaseSqlite: throw an exception on connection error
* In DatabasePostgres: don't send an invalid connection string whenever one of the fields is empty. Use quoting.
* In Database: make the captured PHP error prettier
* Display a descriptive error message when the user navigates to index.php with PHP 4, not a parse error. Check to see if the *.php5 extension works, using file_get_contents().
* The default port number for PostgreSQL is 5432, not blank.
* Better default for $wgDBname

extension-test.php5 [new file with mode: 0644]
includes/DefaultSettings.php
includes/GlobalFunctions.php
includes/WebStart.php
includes/db/Database.php
includes/db/DatabasePostgres.php
includes/db/DatabaseSqlite.php
includes/parser/Parser.php
includes/templates/PHP4.php [new file with mode: 0644]

diff --git a/extension-test.php5 b/extension-test.php5
new file mode 100644 (file)
index 0000000..fd7f218
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+/** 
+ * Test for *.php5 capability in webserver
+ * Used by includes/templates/PHP4.php
+ */
+if ( version_compare( phpversion(), '5.0.0' ) >= 0 ) {
+       echo 'y'.'e'.'s';
+}
+
+?>
index 128a141..f9d3ced 100644 (file)
@@ -27,8 +27,10 @@ if( !defined( 'MEDIAWIKI' ) ) {
  * Create a site configuration object
  * Not used for much in a default install
  */
-require_once( "$IP/includes/SiteConfiguration.php" );
-$wgConf = new SiteConfiguration;
+if ( !defined( 'MW_PHP4' ) ) {
+       require_once( "$IP/includes/SiteConfiguration.php" );
+       $wgConf = new SiteConfiguration;
+}
 
 /** MediaWiki version number */
 $wgVersion                     = '1.14alpha';
@@ -539,10 +541,10 @@ $wgSMTP                           = false;
  */
 /** database host name or ip address */
 $wgDBserver         = 'localhost';
-/** database port number */
-$wgDBport           = '';
+/** database port number (for PostgreSQL) */
+$wgDBport           = 5432;
 /** name of the database */
-$wgDBname           = 'wikidb';
+$wgDBname           = 'my_wiki';
 /** */
 $wgDBconnection     = '';
 /** Database username */
index 6034237..49d86a7 100644 (file)
@@ -1270,7 +1270,7 @@ function wfMerge( $old, $mine, $yours, &$result ){
 
        # This check may also protect against code injection in
        # case of broken installations.
-       if(! file_exists( $wgDiff3 ) ){
+       if( !$wgDiff3 || !file_exists( $wgDiff3 ) ) {
                wfDebug( "diff3 not found\n" );
                return false;
        }
index 411c211..c569bb2 100644 (file)
@@ -74,6 +74,7 @@ if ( $IP === false ) {
        $IP = realpath( '.' );
 }
 
+
 # Start profiler
 require_once( "$IP/StartProfiler.php" );
 wfProfileIn( 'WebStart.php-conf' );
@@ -81,20 +82,36 @@ wfProfileIn( 'WebStart.php-conf' );
 # Load up some global defines.
 require_once( "$IP/includes/Defines.php" );
 
-# LocalSettings.php is the per site customization file. If it does not exit
-# the wiki installer need to be launched or the generated file moved from
-# ./config/ to ./
-if( !file_exists( "$IP/LocalSettings.php" ) ) {
-       require_once( "$IP/includes/DefaultSettings.php" ); # used for printing the version
-       require_once( "$IP/includes/templates/NoLocalSettings.php" );
-       die();
+# Check for PHP 5
+if ( !function_exists( 'version_compare' ) 
+       || version_compare( phpversion(), '5.0.0' ) < 0
+) {
+       define( 'MW_PHP4', '1' );
+       require( "$IP/includes/DefaultSettings.php" );
+       require( "$IP/includes/templates/PHP4.php" );
+       exit;
 }
 
 # Start the autoloader, so that extensions can derive classes from core files
 require_once( "$IP/includes/AutoLoader.php" );
 
-# Include site settings. $IP may be changed (hopefully before the AutoLoader is invoked)
-require_once( "$IP/LocalSettings.php" );
+if ( defined( 'MW_CONFIG_CALLBACK' ) ) {
+       # Use a callback function to configure MediaWiki
+       require_once( "$IP/includes/DefaultSettings.php" );
+       call_user_func( MW_CONFIG_CALLBACK );
+} else {
+       # LocalSettings.php is the per site customization file. If it does not exit
+       # the wiki installer need to be launched or the generated file moved from
+       # ./config/ to ./
+       if( !file_exists( "$IP/LocalSettings.php" ) ) {
+               require_once( "$IP/includes/DefaultSettings.php" ); # used for printing the version
+               require_once( "$IP/includes/templates/NoLocalSettings.php" );
+               die();
+       }
+
+       # Include site settings. $IP may be changed (hopefully before the AutoLoader is invoked)
+       require_once( "$IP/LocalSettings.php" );
+}
 wfProfileOut( 'WebStart.php-conf' );
 
 wfProfileIn( 'WebStart.php-ob_start' );
index 1fa1707..bfd46bf 100644 (file)
@@ -423,12 +423,22 @@ class Database {
 
        protected function installErrorHandler() {
                $this->mPHPError = false;
+               $this->htmlErrors = ini_set( 'html_errors', '0' );
                set_error_handler( array( $this, 'connectionErrorHandler' ) );
        }
 
        protected function restoreErrorHandler() {
                restore_error_handler();
-               return $this->mPHPError;
+               if ( $this->htmlErrors !== false ) {
+                       ini_set( 'html_errors', $this->htmlErrors );
+               }
+               if ( $this->mPHPError ) {
+                       $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError );
+                       $error = preg_replace( '!^.*?:(.*)$!', '$1', $error );
+                       return $error;
+               } else {
+                       return false;
+               }
        }
 
        protected function connectionErrorHandler( $errno,  $errstr ) {
index 590c682..e85cc87 100644 (file)
@@ -78,12 +78,6 @@ class DatabasePostgres extends Database {
                $failFunction = false, $flags = 0 )
        {
 
-               global $wgOut;
-               # Can't get a reference if it hasn't been set yet
-               if ( !isset( $wgOut ) ) {
-                       $wgOut = NULL;
-               }
-               $this->mOut =& $wgOut;
                $this->mFailFunction = $failFunction;
                $this->mFlags = $flags;
                $this->open( $server, $user, $password, $dbName);
@@ -138,10 +132,6 @@ class DatabasePostgres extends Database {
 
                global $wgDBport;
 
-               if (!strlen($user)) { ## e.g. the class is being loaded
-                       return;
-               }
-
                $this->close();
                $this->mServer = $server;
                $this->mPort = $port = $wgDBport;
@@ -149,22 +139,31 @@ class DatabasePostgres extends Database {
                $this->mPassword = $password;
                $this->mDBname = $dbName;
 
-               $hstring="";
+               $connectVars = array(
+                       'dbname' => $dbName,
+                       'user' => $user,
+                       'password' => $password );
                if ($server!=false && $server!="") {
-                       $hstring="host=$server ";
+                       $connectVars['host'] = $server;
                }
                if ($port!=false && $port!="") {
-                       $hstring .= "port=$port ";
+                       $connectVars['port'] = $port;
                }
+               $connectString = $this->makeConnectionString( $connectVars );
 
-               error_reporting( E_ALL );
-               @$this->mConn = pg_connect("$hstring dbname=$dbName user=$user password=$password");
+               $this->installErrorHandler();
+               $this->mConn = pg_connect( $connectString );
+               $phpError = $this->restoreErrorHandler();
 
                if ( $this->mConn == false ) {
                        wfDebug( "DB connection error\n" );
                        wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
                        wfDebug( $this->lastError()."\n" );
-                       return false;
+                       if ( !$this->mFailFunction ) {
+                               throw new DBConnectionError( $this, $phpError );
+                       } else {
+                               return false;
+                       }
                }
 
                $this->mOpened = true;
@@ -189,6 +188,14 @@ class DatabasePostgres extends Database {
                return $this->mConn;
        }
 
+       function makeConnectionString( $vars ) {
+               $s = '';
+               foreach ( $vars as $name => $value ) {
+                       $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
+               }
+               return $s;
+       }
+
 
        function initial_setup($password, $dbName) {
                // If this is the initial connection, setup the schema stuff and possibly create the user
@@ -197,8 +204,8 @@ class DatabasePostgres extends Database {
                print "<li>Checking the version of Postgres...";
                $version = $this->getServerVersion();
                $PGMINVER = '8.1';
-               if ($this->numeric_version < $PGMINVER) {
-                       print "<b>FAILED</b>. Required version is $PGMINVER. You have $this->numeric_version ($version)</li>\n";
+               if ($version < $PGMINVER) {
+                       print "<b>FAILED</b>. Required version is $PGMINVER. You have $version</li>\n";
                        dieout("</ul>");
                }
                print "version $this->numeric_version is OK.</li>\n";
@@ -729,8 +736,7 @@ class DatabasePostgres extends Database {
 
                $table = $this->tableName( $table );
                if (! isset( $wgDBversion ) ) {
-                       $this->getServerVersion();
-                       $wgDBversion = $this->numeric_version;
+                       $wgDBversion = $this->getServerVersion();
                }
 
                if ( !is_array( $options ) )
@@ -1046,17 +1052,10 @@ class DatabasePostgres extends Database {
         * @return string Version information from the database
         */
        function getServerVersion() {
-               $version = pg_fetch_result($this->doQuery("SELECT version()"),0,0);
-               $thisver = array();
-               if (!preg_match('/PostgreSQL (\d+\.\d+)(\S+)/', $version, $thisver)) {
-                       die("Could not determine the numeric version from $version!");
-               }
-               $version = $thisver[1].$thisver[2];
-               $this->numeric_version = $thisver[1];
-               return $version;
+               $versionInfo = pg_version( $this->mConn );
+               return $versionInfo['server'];
        }
 
-
        /**
         * Query whether a given relation exists (in the given schema, or the
         * default mw one if not given)
index 4ba0f1f..814f9bc 100644 (file)
@@ -23,8 +23,6 @@ class DatabaseSqlite extends Database {
                global $wgOut,$wgSQLiteDataDir, $wgSQLiteDataDirMode;
                if ("$wgSQLiteDataDir" == '') $wgSQLiteDataDir = dirname($_SERVER['DOCUMENT_ROOT']).'/data';
                if (!is_dir($wgSQLiteDataDir)) wfMkdirParents( $wgSQLiteDataDir, $wgSQLiteDataDirMode );
-               if (!isset($wgOut)) $wgOut = NULL; # Can't get a reference if it hasn't been set yet
-               $this->mOut =& $wgOut;
                $this->mFailFunction = $failFunction;
                $this->mFlags = $flags;
                $this->mDatabaseFile = "$wgSQLiteDataDir/$dbName.sqlite";
@@ -48,11 +46,28 @@ class DatabaseSqlite extends Database {
                $this->mConn = false;
                if ($dbName) {
                        $file = $this->mDatabaseFile;
-                       if ($this->mFlags & DBO_PERSISTENT) $this->mConn = new PDO("sqlite:$file",$user,$pass,array(PDO::ATTR_PERSISTENT => true));
-                       else $this->mConn = new PDO("sqlite:$file",$user,$pass);
-                       if ($this->mConn === false) wfDebug("DB connection error: $err\n");;
+                       try {
+                               if ( $this->mFlags & DBO_PERSISTENT ) {
+                                       $this->mConn = new PDO( "sqlite:$file", $user, $pass, 
+                                               array( PDO::ATTR_PERSISTENT => true ) );
+                               } else {
+                                       $this->mConn = new PDO( "sqlite:$file", $user, $pass );
+                               }
+                       } catch ( PDOException $e ) {
+                               $err = $e->getMessage();
+                       }
+                       if ( $this->mConn === false ) {
+                               wfDebug( "DB connection error: $err\n" );
+                               if ( !$this->mFailFunction ) {
+                                       throw new DBConnectionError( $this, $err );
+                               } else {
+                                       return false;
+                               }
+
+                       }
                        $this->mOpened = $this->mConn;
-                       $this->mConn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT); # set error codes only, dont raise exceptions
+                       # set error codes only, don't raise exceptions
+                       $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); 
                }
                return $this->mConn;
        }
index 52b8692..2761401 100644 (file)
@@ -3445,10 +3445,11 @@ class Parser
                global $wgMaxTocLevel, $wgContLang;
 
                $doNumberHeadings = $this->mOptions->getNumberHeadings();
-               if( !$this->mTitle->quickUserCan( 'edit' ) ) {
+               $showEditLink = $this->mOptions->getEditSection();
+
+               // Do not call quickUserCan unless necessary
+               if( $showEditLink && !$this->mTitle->quickUserCan( 'edit' ) ) {
                        $showEditLink = 0;
-               } else {
-                       $showEditLink = $this->mOptions->getEditSection();
                }
 
                # Inhibit editsection links if requested in the page
diff --git a/includes/templates/PHP4.php b/includes/templates/PHP4.php
new file mode 100644 (file)
index 0000000..aa19367
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+/**
+ * @file
+ * @ingroup Templates
+ */
+
+if( isset( $_SERVER['REQUEST_URI'] ) ) {
+       $scriptUrl = $_SERVER['REQUEST_URI'];
+} elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
+       // Probably IIS; doesn't set REQUEST_URI
+       $scriptUrl = $_SERVER['SCRIPT_NAME'];
+} else {
+       $scriptUrl = '';
+}
+if ( preg_match( '!^(.*)/config/[^/]*.php$!', $scriptUrl, $m ) ) {
+       $baseUrl = $m[1];
+} elseif ( preg_match( '!^(.*)/[^/]*.php$!', $scriptUrl, $m ) ) {
+       $baseUrl = $m[1];
+} else {
+       $baseUrl = dirname( $baseUrl );
+}
+
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
+       <head>
+               <title>MediaWiki <?php echo htmlspecialchars( $wgVersion ); ?></title>
+               <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
+               <style type='text/css' media='screen, projection'>
+                       html, body {
+                               color: #000;
+                               background-color: #fff;
+                               font-family: sans-serif;
+                               text-align: center;
+                       }
+
+                       p {
+                               text-align: left;
+                               margin-left: 2em;
+                               margin-right: 2em;
+                       }
+
+                       h1 {
+                               font-size: 150%;
+                       }
+               </style>
+       </head>
+       <body>
+               <img src="<?php echo htmlspecialchars( $baseUrl ) ?>/skins/common/images/mediawiki.png" alt='The MediaWiki logo' />
+
+               <h1>MediaWiki <?php echo htmlspecialchars( $wgVersion ); ?></h1>
+               <div class='error'>
+<p>
+                       MediaWiki requires PHP 5.0.0 or higher. You are running PHP
+                       <?php echo htmlspecialchars( phpversion() ); ?>.
+</p>
+<?php
+flush();
+/** 
+ * Test the *.php5 extension
+ */
+$downloadOther = true;
+if ( $baseUrl ) {
+       $testUrl = "$wgServer$baseUrl/extension-test.php5";
+       ini_set( 'allow_url_fopen', '1' );
+       $s = file_get_contents( $testUrl );
+
+       if ( strpos( $s, 'yes' ) !== false ) {
+               $encUrl = htmlspecialchars( str_replace( '.php', '.php5', $scriptUrl ) );
+               echo "<p>You may be able to use MediaWiki using a <a href=\"$encUrl\">.php5</a> file extension.</p>";
+               $downloadOther = false;
+       }
+}
+if ( $downloadOther ) {
+?>
+<p>Please consider upgrading your copy of PHP. PHP 4 is at the end of its
+lifecycle and will not receive further security updates.</p>
+<p>If for some reason you really really need to run MediaWiki on PHP 4, you will need to 
+<a href="http://www.mediawiki.org/wiki/Download">download version 1.6.x</a> 
+from our website. </p>
+<?php
+}
+?>
+
+               </div>
+       </body>
+</html>