From 0d322a394eec9177b4a24f1fb9f2ab64c04beed7 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Mon, 6 Oct 2008 00:45:18 +0000 Subject: [PATCH] * Allow $wgDiff3=false * 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 | 11 ++++ includes/DefaultSettings.php | 12 +++-- includes/GlobalFunctions.php | 2 +- includes/WebStart.php | 35 +++++++++---- includes/db/Database.php | 12 ++++- includes/db/DatabasePostgres.php | 57 ++++++++++----------- includes/db/DatabaseSqlite.php | 27 +++++++--- includes/parser/Parser.php | 7 +-- includes/templates/PHP4.php | 87 ++++++++++++++++++++++++++++++++ 9 files changed, 196 insertions(+), 54 deletions(-) create mode 100644 extension-test.php5 create mode 100644 includes/templates/PHP4.php diff --git a/extension-test.php5 b/extension-test.php5 new file mode 100644 index 0000000000..fd7f218263 --- /dev/null +++ b/extension-test.php5 @@ -0,0 +1,11 @@ += 0 ) { + echo 'y'.'e'.'s'; +} + +?> diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 128a141844..f9d3ced611 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 */ diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 6034237b57..49d86a7757 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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; } diff --git a/includes/WebStart.php b/includes/WebStart.php index 411c211c68..c569bb2ae0 100644 --- a/includes/WebStart.php +++ b/includes/WebStart.php @@ -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' ); diff --git a/includes/db/Database.php b/includes/db/Database.php index 1fa170752d..bfd46bf78d 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -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( '!\[\]!', '', $this->mPHPError ); + $error = preg_replace( '!^.*?:(.*)$!', '$1', $error ); + return $error; + } else { + return false; + } } protected function connectionErrorHandler( $errno, $errstr ) { diff --git a/includes/db/DatabasePostgres.php b/includes/db/DatabasePostgres.php index 590c682d2a..e85cc874d7 100644 --- a/includes/db/DatabasePostgres.php +++ b/includes/db/DatabasePostgres.php @@ -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 "
  • Checking the version of Postgres..."; $version = $this->getServerVersion(); $PGMINVER = '8.1'; - if ($this->numeric_version < $PGMINVER) { - print "FAILED. Required version is $PGMINVER. You have $this->numeric_version ($version)
  • \n"; + if ($version < $PGMINVER) { + print "FAILED. Required version is $PGMINVER. You have $version\n"; dieout(""); } print "version $this->numeric_version is OK.\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) diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index 4ba0f1f1dc..814f9bc2c4 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -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; } diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 52b86928fa..276140176b 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -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 index 0000000000..aa19367a46 --- /dev/null +++ b/includes/templates/PHP4.php @@ -0,0 +1,87 @@ + + + + + MediaWiki <?php echo htmlspecialchars( $wgVersion ); ?> + + + + + The MediaWiki logo + +

    MediaWiki

    +
    +

    + MediaWiki requires PHP 5.0.0 or higher. You are running PHP + . +

    +You may be able to use MediaWiki using a .php5 file extension.

    "; + $downloadOther = false; + } +} +if ( $downloadOther ) { +?> +

    Please consider upgrading your copy of PHP. PHP 4 is at the end of its +lifecycle and will not receive further security updates.

    +

    If for some reason you really really need to run MediaWiki on PHP 4, you will need to +download version 1.6.x +from our website.

    + + +
    + + -- 2.20.1