From ef89ec541c7a3b249695ce8d9427f663455f6c6d Mon Sep 17 00:00:00 2001 From: Greg Sabino Mullane Date: Tue, 27 Jun 2006 15:37:35 +0000 Subject: [PATCH] Add concept of a distinct port separate from a server. Add some functions to DatabasePostgres.php. --- config/index.php | 11 +++--- includes/Database.php | 19 +++++----- includes/DatabaseOracle.php | 2 +- includes/DatabasePostgres.php | 62 +++++++++++++++++++++++++------- includes/DefaultSettings.php | 3 +- includes/Setup.php | 1 + maintenance/convertLinks.inc | 10 +++--- maintenance/rebuildall.php | 2 +- maintenance/rebuildtextindex.php | 2 +- maintenance/tables.sql | 2 +- maintenance/update.php | 2 +- tests/LocalTestSettings.sample | 3 ++ tests/RunTests.php | 10 +++--- 13 files changed, 87 insertions(+), 42 deletions(-) diff --git a/config/index.php b/config/index.php index 5deff4df54..04bccf928a 100644 --- a/config/index.php +++ b/config/index.php @@ -476,13 +476,13 @@ print "
  • Environment check DBserver = importPost( "DBserver", "localhost" ); + $conf->DBport = importPost( "DBport", "5432" ); $conf->DBname = importPost( "DBname", "wikidb" ); $conf->DBuser = importPost( "DBuser", "wikiuser" ); $conf->DBpassword = importPost( "DBpassword" ); $conf->DBpassword2 = importPost( "DBpassword2" ); $conf->DBprefix = importPost( "DBprefix" ); $conf->DBschema = importPost( "DBschema", "mediawiki" ); - $conf->DBport = importPost( "DBport", "5432" ); $conf->DBmysql5 = (importPost( "DBmysql5" ) == "true") ? "true" : "false"; $conf->RootUser = importPost( "RootUser", "root" ); $conf->RootPW = importPost( "RootPW", "-" ); @@ -620,7 +620,7 @@ error_reporting( E_ALL ); # Attempt to connect echo( "
  • Attempting to connect to database server as $db_user..." ); - $wgDatabase = Database::newFromParams( $wgDBserver, $db_user, $db_pass, '', 1 ); + $wgDatabase = Database::newFromParams( $wgDBserver, $wgDBport, $db_user, $db_pass, '', 1 ); # Check the connection and respond to errors if( $wgDatabase->isOpen() ) { @@ -664,7 +664,7 @@ error_reporting( E_ALL ); } else /* not mysql */ { echo( "
  • Attempting to connect to database server as $wgDBuser..." ); - $wgDatabase = $dbc->newFromParams($wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, 1); + $wgDatabase = $dbc->newFromParams($wgDBserver, $wgDBport, $wgDBuser, $wgDBpassword, $wgDBname, 1); if (!$wgDatabase->isOpen()) { print " error: " . $wgDatabase->lastError() . "
  • \n"; } else { @@ -728,7 +728,7 @@ error_reporting( E_ALL ); # Create user if required if ( $conf->Root ) { - $conn = $dbc->newFromParams( $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, 1 ); + $conn = $dbc->newFromParams( $wgDBserver, $wgDBport, $wgDBuser, $wgDBpassword, $wgDBname, 1 ); if ( $conn->isOpen() ) { print "
  • DB user account ok
  • \n"; $conn->close(); @@ -785,7 +785,7 @@ error_reporting( E_ALL ); # Set up the "regular user" account *if we can, and if we need to* if( $conf->Root ) { # See if we need to - $wgDatabase2 = $dbc->newFromParams( $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, 1 ); + $wgDatabase2 = $dbc->newFromParams( $wgDBserver, $wgDBport, $wgDBuser, $wgDBpassword, $wgDBname, 1 ); if( $wgDatabase2->isOpen() ) { # Nope, just close the test connection and continue $wgDatabase2->close(); @@ -1340,6 +1340,7 @@ if ( \$wgCommandLineMode ) { \$wgEmailAuthentication = $eauthent; \$wgDBserver = \"{$slconf['DBserver']}\"; +\$wgDBport = \"{$slconf['DBport']}\"; \$wgDBname = \"{$slconf['DBname']}\"; \$wgDBuser = \"{$slconf['DBuser']}\"; \$wgDBpassword = \"{$slconf['DBpassword']}\"; diff --git a/includes/Database.php b/includes/Database.php index fc2d67d77e..8af3ce146b 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -240,7 +240,7 @@ class Database { protected $mLastQuery = ''; - protected $mServer, $mUser, $mPassword, $mConn = null, $mDBname; + protected $mServer, $mPort, $mUser, $mPassword, $mConn = null, $mDBname; protected $mOut, $mOpened = false; protected $mFailFunction; @@ -371,6 +371,7 @@ class Database { /**@{{ * @param string $server database server host + * @param string $port database server port * @param string $user database user name * @param string $password database user password * @param string $dbname database name @@ -381,8 +382,8 @@ class Database { * @param $flags * @param $tablePrefix String: database table prefixes. By default use the prefix gave in LocalSettings.php */ - function __construct( $server = false, $user = false, $password = false, $dbName = false, - $failFunction = false, $flags = 0, $tablePrefix = 'get from global' ) { + function __construct( $server = false, $port = false, $user = false, $password = false, $dbName = false, + $failFunction = false, $flags = 0, $tablePrefix = 'get from global') { global $wgOut, $wgDBprefix, $wgCommandLineMode; # Can't get a reference if it hasn't been set yet @@ -416,8 +417,8 @@ class Database { $this->mTablePrefix = $tablePrefix; } - if ( $server ) { - $this->open( $server, $user, $password, $dbName ); + if ( $server or $port) { + $this->open( $server, $port, $user, $password, $dbName ); } } @@ -426,17 +427,17 @@ class Database { * @param failFunction * @param $flags */ - static function newFromParams( $server, $user, $password, $dbName, + static function newFromParams( $server, $port, $user, $password, $dbName, $failFunction = false, $flags = 0 ) { - return new Database( $server, $user, $password, $dbName, $failFunction, $flags ); + return new Database( $server, $port, $user, $password, $dbName, $failFunction, $flags ); } /** * Usually aborts on failure * If the failFunction is set to a non-zero integer, returns success */ - function open( $server, $user, $password, $dbName ) { + function open( $server, $port, $user, $password, $dbName ) { global $wguname; # Test for missing mysql.so @@ -1923,7 +1924,7 @@ class Database { */ protected function replaceVars( $ins ) { $varnames = array( - 'wgDBserver', 'wgDBname', 'wgDBintlname', 'wgDBuser', + 'wgDBserver', 'wgDBport', 'wgDBname', 'wgDBintlname', 'wgDBuser', 'wgDBpassword', 'wgDBsqluser', 'wgDBsqlpassword', 'wgDBadminuser', 'wgDBadminpassword', ); diff --git a/includes/DatabaseOracle.php b/includes/DatabaseOracle.php index d5d7379dc6..8e2b542d2a 100644 --- a/includes/DatabaseOracle.php +++ b/includes/DatabaseOracle.php @@ -40,7 +40,7 @@ class DatabaseOracle extends Database { Database::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix ); } - /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false, + /* static */ function newFromParams( $server = false, $port = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0, $tablePrefix = 'get from global' ) { return new DatabaseOracle( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix ); diff --git a/includes/DatabasePostgres.php b/includes/DatabasePostgres.php index e904949aab..0635847ae7 100644 --- a/includes/DatabasePostgres.php +++ b/includes/DatabasePostgres.php @@ -7,9 +7,6 @@ * than MySQL ones, some of them should be moved to parent * Database class. * - * STATUS: Working PG implementation of MediaWiki - * TODO: Installer support - * * @package MediaWiki */ @@ -18,31 +15,28 @@ */ require_once( 'Database.php' ); -/** - * - * @package MediaWiki - */ class DatabasePostgres extends Database { var $mInsertId = NULL; var $mLastResult = NULL; - function DatabasePostgres($server = false, $user = false, $password = false, $dbName = false, + function DatabasePostgres($server = false, $port = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0 ) { - Database::__construct( $server, $user, $password, $dbName, $failFunction, $flags ); + Database::__construct( $server, $port, $user, $password, $dbName, $failFunction, $flags ); } - static function newFromParams( $server = false, $user = false, $password = false, $dbName = false, + static function newFromParams( $server = false, $port = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0) { - return new DatabasePostgres( $server, $user, $password, $dbName, $failFunction, $flags ); + return new DatabasePostgres( $server, $port, $user, $password, $dbName, $failFunction, $flags ); } /** * Usually aborts on failure * If the failFunction is set to a non-zero integer, returns success */ - function open( $server, $user, $password, $dbName ) { + function open( $server, $port, $user, $password, $dbName ) { + # Test for PostgreSQL support, to avoid suppressed fatal error if ( !function_exists( 'pg_connect' ) ) { throw new DBConnectionError( $this, "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" ); @@ -56,6 +50,7 @@ class DatabasePostgres extends Database { $this->mPassword = $password; $this->mDBname = $dbName; $this->mSchemas = array($wgDBschema,'public'); + $this->mPort = $port; $success = false; @@ -65,6 +60,9 @@ class DatabasePostgres extends Database { if ($server!=false && $server!="") { $hstring="host=$server "; } + if ($port!=false && $port != '') { + $hstring .= "port=$port "; + } @$this->mConn = pg_connect("$hstring dbname=$dbName user=$user password=$password"); if ( $this->mConn == false ) { @@ -197,7 +195,7 @@ class DatabasePostgres extends Database { $n = pg_num_fields( $res ); for( $i = 0; $i < $n; $i++ ) { // FIXME - throw new DBUnexpectedError($this, "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" ); + throw new DBUnexpectedError($this, "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgres" ); $meta = mysql_fetch_field( $res, $i ); if( $field == $meta->name ) { return $meta; @@ -439,6 +437,44 @@ class DatabasePostgres extends Database { $searchpath=$this->makeList($schemas,LIST_NAMES); $this->query("SET search_path = $searchpath"); } + + + /** + * Query whether a given table exists + */ + function tableExists( $table ) { + global $wgDBschema; + $safetable = preg_replace("/'/", "''", $table); + $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n WHERE c.relnamespace = n.oid " + . "AND c.relname = '$safetable' AND n.nspname = '$wgDBschema'"; + $res = $this->query($SQL); + if( $res ) { + $this->freeResult( $res ); + return true; + } else { + return false; + } + } + + function fieldExists( $table, $field, $fname = 'DatabasePostgres::fieldExists' ) { + global $wgDBschema; + $safetable = preg_replace("/'/", "''", $table); + $safecol = preg_replace("/'/", "''", $field); + $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n, pg_attribute a " + . "WHERE c.relnamespace = n.oid AND c.relname = '$safetable' AND n.nspname = '$wgDBschema' " + . "AND a.attrelid = c.oid AND a.attname = '$safecol'"; + $res = $this->query($SQL, $fname); + if( $res ) { + $this->freeResult( $res ); + return true; + } else { + return false; + } + } + + + + } ?> diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index b5ec472b19..132b9374b2 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -433,12 +433,13 @@ $wgSMTP = false; $wgDBserver = 'localhost'; /** name of the database */ $wgDBname = 'wikidb'; +/** port number for databases that use them (e.g. postgres) */ +$wgDBport = 5432; /** */ $wgDBconnection = ''; /** Database username */ $wgDBuser = 'wikiuser'; /** Database type - * "mysql" for working code and "PostgreSQL" for development/broken code */ $wgDBtype = "mysql"; /** Search type diff --git a/includes/Setup.php b/includes/Setup.php index ea70968df1..5c7ae1b8e5 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -147,6 +147,7 @@ wfProfileIn( $fname.'-database' ); if ( !$wgDBservers ) { $wgDBservers = array(array( 'host' => $wgDBserver, + 'port' => $wgDBport, 'user' => $wgDBuser, 'password' => $wgDBpassword, 'dbname' => $wgDBname, diff --git a/maintenance/convertLinks.inc b/maintenance/convertLinks.inc index f0d2c439fe..df682f115f 100644 --- a/maintenance/convertLinks.inc +++ b/maintenance/convertLinks.inc @@ -8,14 +8,14 @@ /** */ function convertLinks() { global $wgDBtype; - if( $wgDBtype == 'PostgreSQL' ) { + if( $wgDBtype == 'Postgres' ) { print "Links table already ok on PostgreSQL.\n"; return; } print "Converting links table to ID-ID...\n"; - global $wgLang, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname; + global $wgLang, $wgDBserver, $wgDBport, $wgDBadminuser, $wgDBadminpassword, $wgDBname; global $noKeys, $logPerformance, $fh; $numRows = $tuplesAdded = $numBadLinks = $curRowsRead = 0; #counters etc @@ -147,7 +147,7 @@ function convertLinks() { #-------------------------------------------------------------------- if ( $overwriteLinksTable ) { - $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname ); + $dbConn = Database::newFromParams( $wgDBserver, $wgDBport, $wgDBadminuser, $wgDBadminpassword, $wgDBname ); if (!($dbConn->isOpen())) { print "Opening connection to database failed.\n"; return; @@ -174,9 +174,9 @@ function convertLinks() { #-------------------------------------------------------------------- function createTempTable() { - global $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname; + global $wgDBserver, $wgDBport, $wgDBadminuser, $wgDBadminpassword, $wgDBname; global $noKeys; - $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname ); + $dbConn = Database::newFromParams( $wgDBserver, $wgDBport, $wgDBadminuser, $wgDBadminpassword, $wgDBname ); if (!($dbConn->isOpen())) { print "Opening connection to database failed.\n"; diff --git a/maintenance/rebuildall.php b/maintenance/rebuildall.php index 7c44e30028..5765465d95 100644 --- a/maintenance/rebuildall.php +++ b/maintenance/rebuildall.php @@ -15,7 +15,7 @@ require_once( "refreshLinks.inc" ); require_once( "rebuildtextindex.inc" ); require_once( "rebuildrecentchanges.inc" ); -$database = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname ); +$database = Database::newFromParams( $wgDBserver, $wgDBport, wgDBadminuser, $wgDBadminpassword, $wgDBname ); print "** Rebuilding fulltext search index (if you abort this will break searching; run this script again to fix):\n"; dropTextIndex( $database ); diff --git a/maintenance/rebuildtextindex.php b/maintenance/rebuildtextindex.php index 54672d21d5..dc8bd102eb 100644 --- a/maintenance/rebuildtextindex.php +++ b/maintenance/rebuildtextindex.php @@ -13,7 +13,7 @@ require_once( "commandLine.inc" ); require_once( "rebuildtextindex.inc" ); $wgTitle = Title::newFromText( "Rebuild text index script" ); -$database = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname ); +$database = Database::newFromParams( $wgDBserver, $wgDBport, $wgDBadminuser, $wgDBadminpassword, $wgDBname ); dropTextIndex( $database ); rebuildTextIndex( $database ); diff --git a/maintenance/tables.sql b/maintenance/tables.sql index 5dd851f530..fca4d974e5 100644 --- a/maintenance/tables.sql +++ b/maintenance/tables.sql @@ -995,4 +995,4 @@ CREATE TABLE /*$wgDBprefix*/querycache_info ( UNIQUE KEY ( qci_type ) -) TYPE=InnoDB; \ No newline at end of file +) TYPE=InnoDB; diff --git a/maintenance/update.php b/maintenance/update.php index 8643aa7927..a73cf5e60b 100644 --- a/maintenance/update.php +++ b/maintenance/update.php @@ -32,7 +32,7 @@ if( !isset( $wgDBadminuser ) || !isset( $wgDBadminpassword ) ) { # Attempt to connect to the database as a privileged user # This will vomit up an error if there are permissions problems -$wgDatabase = $dbc->newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname, 1 ); +$wgDatabase = $dbc->newFromParams( $wgDBserver, $wgDBport, $wgDBadminuser, $wgDBadminpassword, $wgDBname, 1 ); if( !$wgDatabase->isOpen() ) { # Appears to have failed diff --git a/tests/LocalTestSettings.sample b/tests/LocalTestSettings.sample index 7ada2fda91..d915feda5a 100644 --- a/tests/LocalTestSettings.sample +++ b/tests/LocalTestSettings.sample @@ -12,16 +12,19 @@ ini_set('include_path',ini_get('include_path').':'.$IP); $testOptions = array( 'mysql3' => array( 'server' => null, + 'port' => null, 'user' => null, 'password' => null, 'database' => null ), 'mysql4' => array( 'server' => null, + 'port' => null, 'user' => null, 'password' => null, 'database' => null ), 'postgres' => array( 'server' => null, + 'port' => null, 'user' => null, 'password' => null, 'database' => null ), diff --git a/tests/RunTests.php b/tests/RunTests.php index ec04fc037f..8b4f1c762e 100644 --- a/tests/RunTests.php +++ b/tests/RunTests.php @@ -14,13 +14,15 @@ require_once( 'PHPUnit.php' ); $testOptions = array( 'mysql4' => array( - 'server' => null, - 'user' => null, + 'server' => null, + 'port' => null, + 'user' => null, 'password' => null, 'database' => null ), 'postgres' => array( - 'server' => null, - 'user' => null, + 'server' => null, + 'port' => null + 'user' => null, 'password' => null, 'database' => null ), ); -- 2.20.1