2010-05-07 12:25:01 +00:00
|
|
|
<?php
|
2010-08-21 18:20:09 +00:00
|
|
|
/**
|
|
|
|
|
* PostgreSQL-specific installer.
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Deployment
|
|
|
|
|
*/
|
2010-12-01 20:22:45 +00:00
|
|
|
|
2010-07-29 18:36:39 +00:00
|
|
|
/**
|
|
|
|
|
* Class for setting up the MediaWiki database using Postgres.
|
2010-12-06 20:04:28 +00:00
|
|
|
*
|
2010-07-29 18:36:39 +00:00
|
|
|
* @ingroup Deployment
|
|
|
|
|
* @since 1.17
|
|
|
|
|
*/
|
2010-07-20 09:30:33 +00:00
|
|
|
class PostgresInstaller extends DatabaseInstaller {
|
2010-05-07 12:25:01 +00:00
|
|
|
|
|
|
|
|
protected $globalNames = array(
|
|
|
|
|
'wgDBserver',
|
|
|
|
|
'wgDBport',
|
|
|
|
|
'wgDBname',
|
|
|
|
|
'wgDBuser',
|
|
|
|
|
'wgDBpassword',
|
|
|
|
|
'wgDBmwschema',
|
|
|
|
|
'wgDBts2schema',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var $minimumVersion = '8.1';
|
2011-01-07 18:24:56 +00:00
|
|
|
private $ts2MaxVersion = '8.3'; // Doing ts2 is not necessary in PG > 8.3
|
2010-05-07 12:25:01 +00:00
|
|
|
|
|
|
|
|
function getName() {
|
|
|
|
|
return 'postgres';
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 13:52:05 +00:00
|
|
|
public function isCompiled() {
|
2010-07-07 02:53:19 +00:00
|
|
|
return self::checkExtension( 'pgsql' );
|
2010-05-07 12:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getConnectForm() {
|
|
|
|
|
return
|
2010-12-06 20:04:28 +00:00
|
|
|
$this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) .
|
2010-05-07 12:25:01 +00:00
|
|
|
$this->getTextBox( 'wgDBport', 'config-db-port' ) .
|
2010-11-04 23:21:24 +00:00
|
|
|
Html::openElement( 'fieldset' ) .
|
|
|
|
|
Html::element( 'legend', array(), wfMsg( 'config-db-wiki-settings' ) ) .
|
2010-12-06 20:04:28 +00:00
|
|
|
$this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) .
|
|
|
|
|
$this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) .
|
2010-05-07 12:25:01 +00:00
|
|
|
$this->getTextBox( 'wgDBts2schema', 'config-db-ts2-schema' ) .
|
2010-11-04 23:21:24 +00:00
|
|
|
Html::closeElement( 'fieldset' ) .
|
2010-05-07 12:25:01 +00:00
|
|
|
$this->getInstallUserBox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function submitConnectForm() {
|
|
|
|
|
// Get variables from the request
|
2010-12-06 20:04:28 +00:00
|
|
|
$newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport',
|
2010-05-07 12:25:01 +00:00
|
|
|
'wgDBname', 'wgDBmwschema', 'wgDBts2schema' ) );
|
|
|
|
|
|
|
|
|
|
// Validate them
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
if ( !strlen( $newValues['wgDBname'] ) ) {
|
|
|
|
|
$status->fatal( 'config-missing-db-name' );
|
|
|
|
|
} elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) {
|
|
|
|
|
$status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] );
|
|
|
|
|
}
|
|
|
|
|
if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) {
|
|
|
|
|
$status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] );
|
|
|
|
|
}
|
|
|
|
|
if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBts2schema'] ) ) {
|
|
|
|
|
$status->fatal( 'config-invalid-ts2schema', $newValues['wgDBts2schema'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Submit user box
|
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
$status->merge( $this->submitInstallUserBox() );
|
|
|
|
|
}
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to connect
|
2010-10-17 19:35:13 +00:00
|
|
|
$status->merge( $this->getConnection() );
|
2010-05-07 12:25:01 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check version
|
2010-08-17 03:07:27 +00:00
|
|
|
$version = $this->db->getServerVersion();
|
2010-05-07 12:25:01 +00:00
|
|
|
if ( version_compare( $version, $this->minimumVersion ) < 0 ) {
|
|
|
|
|
return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version );
|
|
|
|
|
}
|
2010-08-17 03:07:27 +00:00
|
|
|
|
|
|
|
|
$this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) );
|
|
|
|
|
$this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) );
|
2010-05-07 12:25:01 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-17 03:07:27 +00:00
|
|
|
function getConnection() {
|
2010-05-07 12:25:01 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
|
|
|
|
try {
|
2010-08-17 03:07:27 +00:00
|
|
|
$this->db = new DatabasePostgres(
|
2010-05-07 12:25:01 +00:00
|
|
|
$this->getVar( 'wgDBserver' ),
|
|
|
|
|
$this->getVar( '_InstallUser' ),
|
|
|
|
|
$this->getVar( '_InstallPassword' ),
|
2011-01-07 18:24:56 +00:00
|
|
|
false );
|
2010-08-17 03:07:27 +00:00
|
|
|
$status->value = $this->db;
|
2010-05-07 12:25:01 +00:00
|
|
|
} catch ( DBConnectionError $e ) {
|
|
|
|
|
$status->fatal( 'config-connection-error', $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-07 18:24:56 +00:00
|
|
|
protected function canCreateAccounts() {
|
|
|
|
|
$status = $this->getConnection();
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$conn = $status->value;
|
|
|
|
|
|
|
|
|
|
$superuser = $this->getVar( '_InstallUser' );
|
|
|
|
|
|
|
|
|
|
$rights = $conn->query( 'pg_catalog.pg_user',
|
|
|
|
|
'SELECT
|
|
|
|
|
CASE WHEN usesuper IS TRUE THEN
|
|
|
|
|
CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
|
|
|
|
|
ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
|
|
|
|
|
END AS rights',
|
|
|
|
|
array( 'usename' => $superuser ), __METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if( !$rights ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( $rights != 1 && $rights != 3 ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-10 16:06:16 +00:00
|
|
|
public function preInstall() {
|
2011-01-07 18:24:56 +00:00
|
|
|
$commitCB = array(
|
2010-11-10 16:06:16 +00:00
|
|
|
'name' => 'pg-commit',
|
|
|
|
|
'callback' => array( $this, 'commitChanges' ),
|
|
|
|
|
);
|
2011-01-07 18:24:56 +00:00
|
|
|
$userCB = array(
|
|
|
|
|
'name' => 'user',
|
|
|
|
|
'callback' => array( $this, 'setupUser' ),
|
|
|
|
|
);
|
|
|
|
|
$ts2CB = array(
|
|
|
|
|
'name' => 'pg-ts2',
|
|
|
|
|
'callback' => array( $this, 'setupTs2' ),
|
|
|
|
|
);
|
|
|
|
|
$this->parent->addInstallStep( $commitCB, 'interwiki' );
|
|
|
|
|
$this->parent->addInstallStep( $userCB );
|
2010-11-10 16:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
2010-05-07 12:25:01 +00:00
|
|
|
function setupDatabase() {
|
2010-11-10 16:06:16 +00:00
|
|
|
$status = $this->getConnection();
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
$conn = $status->value;
|
|
|
|
|
|
|
|
|
|
// Make sure that we can write to the correct schema
|
|
|
|
|
// If not, Postgres will happily and silently go to the next search_path item
|
|
|
|
|
$schema = $this->getVar( 'wgDBmwschema' );
|
|
|
|
|
$ctest = 'mediawiki_test_table';
|
2010-12-04 15:14:08 +00:00
|
|
|
$safeschema = $conn->addIdentifierQuotes( $schema );
|
2010-11-10 16:06:16 +00:00
|
|
|
if ( $conn->tableExists( $ctest, $schema ) ) {
|
2010-12-15 20:59:29 +00:00
|
|
|
$conn->query( "DROP TABLE $safeschema.$ctest" );
|
2010-11-10 16:06:16 +00:00
|
|
|
}
|
2010-12-15 20:59:29 +00:00
|
|
|
$res = $conn->query( "CREATE TABLE $safeschema.$ctest(a int)" );
|
2010-11-10 16:06:16 +00:00
|
|
|
if ( !$res ) {
|
|
|
|
|
$status->fatal( 'config-install-pg-schema-failed',
|
|
|
|
|
$this->getVar( 'wgDBuser'), $schema );
|
|
|
|
|
}
|
2010-12-15 20:59:29 +00:00
|
|
|
$conn->query( "DROP TABLE $safeschema.$ctest" );
|
2011-01-07 18:24:56 +00:00
|
|
|
|
|
|
|
|
$dbName = $this->getVar( 'wgDBname' );
|
|
|
|
|
if( !$conn->selectDB( $dbName ) ) {
|
|
|
|
|
$safedb = $conn->addIdentifierQuotes( $dbName );
|
|
|
|
|
$safeuser = $conn->addQuotes( $this->getVar( 'wgDBuser' ) );
|
|
|
|
|
$conn->query( "CREATE DATABASE $safedb OWNER $safeuser", __METHOD__ );
|
|
|
|
|
$conn->selectDB( $dbName );
|
|
|
|
|
}
|
2010-11-10 16:06:16 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-07 18:24:56 +00:00
|
|
|
/**
|
|
|
|
|
* Ts2 isn't needed in newer versions of Postgres, so wrap it in a nice big
|
|
|
|
|
* version check and skip it if we're new. Maybe we can bump $minimumVersion
|
|
|
|
|
* one day and render this obsolete :)
|
|
|
|
|
*
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
function setupTs2() {
|
|
|
|
|
if( version_compare( $this->db->getServerVersion(), $this->ts2MaxVersion, '<' ) ) {
|
|
|
|
|
if ( !$this->db->tableExists( 'pg_ts_cfg', $wgDBts2schema ) ) {
|
2011-01-07 18:41:55 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
|
'config-install-pg-ts2-failed',
|
|
|
|
|
$this->getVar( 'wgDBname' ),
|
|
|
|
|
'http://www.devx.com/opensource/Article/21674/0/page/2'
|
|
|
|
|
);
|
2011-01-07 18:24:56 +00:00
|
|
|
}
|
|
|
|
|
$safeuser = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
|
|
|
|
|
foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) {
|
|
|
|
|
$sql = "GRANT SELECT ON pg_ts_$table TO $safeuser";
|
|
|
|
|
$this->db->query( $sql, __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 20:15:45 +00:00
|
|
|
function commitChanges() {
|
2010-11-10 16:06:16 +00:00
|
|
|
$this->db->query( 'COMMIT' );
|
2010-12-01 20:15:45 +00:00
|
|
|
return Status::newGood();
|
2010-05-07 12:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-07 18:24:56 +00:00
|
|
|
function setupUser() {
|
|
|
|
|
if ( !$this->getVar( '_CreateDBAccount' ) ) {
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$status = $this->getConnection();
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$db = $this->getVar( 'wgDBname' );
|
|
|
|
|
$this->db->selectDB( $db );
|
|
|
|
|
$safeuser = $this->db->addQuotes( $this->getVar( 'wgDBuser' ) );
|
|
|
|
|
$safepass = $this->db->addQuotes( $this->getVar( 'wgDBpassword' ) );
|
|
|
|
|
$res = $this->db->query( "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass", __METHOD__ );
|
|
|
|
|
if ( $res !== true ) {
|
|
|
|
|
$status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-07 12:25:01 +00:00
|
|
|
function getLocalSettings() {
|
|
|
|
|
$port = $this->getVar( 'wgDBport' );
|
|
|
|
|
$schema = $this->getVar( 'wgDBmwschema' );
|
|
|
|
|
$ts2 = $this->getVar( 'wgDBts2schema' );
|
|
|
|
|
return
|
|
|
|
|
"# Postgres specific settings
|
|
|
|
|
\$wgDBport = \"{$port}\";
|
|
|
|
|
\$wgDBmwschema = \"{$schema}\";
|
|
|
|
|
\$wgDBts2schema = \"{$ts2}\";";
|
|
|
|
|
}
|
2010-08-22 10:37:27 +00:00
|
|
|
|
2010-09-07 20:57:53 +00:00
|
|
|
public function preUpgrade() {
|
|
|
|
|
global $wgDBuser, $wgDBpassword;
|
|
|
|
|
|
|
|
|
|
# Normal user and password are selected after this step, so for now
|
|
|
|
|
# just copy these two
|
|
|
|
|
$wgDBuser = $this->getVar( '_InstallUser' );
|
|
|
|
|
$wgDBpassword = $this->getVar( '_InstallPassword' );
|
2010-08-22 10:37:27 +00:00
|
|
|
}
|
2010-12-27 15:41:11 +00:00
|
|
|
|
|
|
|
|
private function setupPLpgSQL() {
|
|
|
|
|
$rows = $this->numRows(
|
|
|
|
|
$this->db->query( "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'" )
|
|
|
|
|
);
|
|
|
|
|
if ( $rows < 1 ) {
|
|
|
|
|
// plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
|
|
|
|
|
$SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
|
|
|
|
|
"WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
|
|
|
|
|
$rows = $this->numRows( $this->db->query( $SQL ) );
|
|
|
|
|
global $wgDBname;
|
|
|
|
|
if ( $rows >= 1 ) {
|
|
|
|
|
$result = $this->db->query( 'CREATE LANGUAGE plpgsql' );
|
|
|
|
|
if ( !$result ) {
|
2011-01-06 22:45:34 +00:00
|
|
|
return Status::newFatal( 'config-pg-no-plpgsql', $wgDBname );
|
2010-12-27 15:41:11 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2011-01-06 22:45:34 +00:00
|
|
|
return Status::newFatal( 'config-pg-no-plpgsql', $wgDBname );
|
2010-12-27 15:41:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
2010-05-07 12:25:01 +00:00
|
|
|
}
|