class Tasks extends InstallTasks {
protected $pdoDriver = 'pgsql';
public function __construct() {
$this->tasks[] = array(
'function' => 'checkEncoding',
'arguments' => array(),
);
$this->tasks[] = array(
'function' => 'checkBinaryOutput',
'arguments' => array(),
);
$this->tasks[] = array(
'function' => 'initializeDatabase',
'arguments' => array(),
);
}
public function name() {
return st('PostgreSQL');
}
public function minimumVersion() {
return '8.3';
}
protected function connect() {
try {
db_set_active();
Database::getConnection();
$this
->pass('Drupal can CONNECT to the database ok.');
} catch (Exception $e) {
if ($e
->getCode() == Connection::DATABASE_NOT_FOUND) {
$connection_info = Database::getConnectionInfo();
$database = $connection_info['default']['database'];
unset($connection_info['default']['database']);
Database::removeConnection('default');
Database::addConnectionInfo('default', 'default', $connection_info['default']);
try {
Database::getConnection()
->createDatabase($database);
Database::closeConnection();
Database::removeConnection('default');
$connection_info['default']['database'] = $database;
Database::addConnectionInfo('default', 'default', $connection_info['default']);
Database::getConnection();
$this
->pass('Drupal can CONNECT to the database ok.');
} catch (DatabaseNotFoundException $e) {
$this
->fail(st('Database %database not found. The server reports the following message when attempting to create the database: %error.', array(
'%database' => $database,
'%error' => $e
->getMessage(),
)));
}
}
else {
$this
->fail(st('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname?</li></ul>', array(
'%error' => $e
->getMessage(),
)));
return FALSE;
}
}
return TRUE;
}
protected function checkEncoding() {
try {
if (db_query('SHOW server_encoding')
->fetchField() == 'UTF8') {
$this
->pass(st('Database is encoded in UTF-8'));
}
else {
$replacements = array(
'%encoding' => 'UTF8',
'%driver' => $this
->name(),
'!link' => '<a href="INSTALL.pgsql.txt">INSTALL.pgsql.txt</a>',
);
$text = 'The %driver database must use %encoding encoding to work with Drupal.';
$text .= 'Recreate the database with %encoding encoding. See !link for more details.';
$this
->fail(st($text, $replacements));
}
} catch (Exception $e) {
$this
->fail(st('Drupal could not determine the encoding of the database was set to UTF-8'));
}
}
function checkBinaryOutput() {
$database_connection = Database::getConnection();
if (version_compare($database_connection
->version(), '9') >= 0) {
if (!$this
->checkBinaryOutputSuccess()) {
$connection_options = $database_connection
->getConnectionOptions();
$query = "ALTER DATABASE \"" . $connection_options['database'] . "\" SET bytea_output = 'escape';";
try {
db_query($query);
} catch (Exception $e) {
}
db_close();
if (!$this
->checkBinaryOutputSuccess()) {
$replacements = array(
'%setting' => 'bytea_output',
'%current_value' => 'hex',
'%needed_value' => 'escape',
'!query' => "<code>" . $query . "</code>",
);
$this
->fail(st("The %setting setting is currently set to '%current_value', but needs to be '%needed_value'. Change this by running the following query: !query", $replacements));
}
}
}
}
protected function checkBinaryOutputSuccess() {
$bytea_output = db_query("SELECT 'encoding'::bytea AS output")
->fetchField();
return $bytea_output == 'encoding';
}
function initializeDatabase() {
try {
db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric) RETURNS numeric AS
\'SELECT CASE WHEN (($1 > $2) OR ($2 IS NULL)) THEN $1 ELSE $2 END;\'
LANGUAGE \'sql\'');
db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric, numeric) RETURNS numeric AS
\'SELECT greatest($1, greatest($2, $3));\'
LANGUAGE \'sql\'');
if (!db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'rand'")
->fetchField()) {
db_query('CREATE OR REPLACE FUNCTION "rand"() RETURNS float AS
\'SELECT random();\'
LANGUAGE \'sql\'');
}
db_query('CREATE OR REPLACE FUNCTION "substring_index"(text, text, integer) RETURNS text AS
\'SELECT array_to_string((string_to_array($1, $2)) [1:$3], $2);\'
LANGUAGE \'sql\'');
db_query('CREATE OR REPLACE FUNCTION "concat"(anynonarray, anynonarray) RETURNS text AS
\'SELECT CAST($1 AS text) || CAST($2 AS text);\'
LANGUAGE \'sql\'
');
db_query('CREATE OR REPLACE FUNCTION "concat"(text, anynonarray) RETURNS text AS
\'SELECT $1 || CAST($2 AS text);\'
LANGUAGE \'sql\'
');
db_query('CREATE OR REPLACE FUNCTION "concat"(anynonarray, text) RETURNS text AS
\'SELECT CAST($1 AS text) || $2;\'
LANGUAGE \'sql\'
');
db_query('CREATE OR REPLACE FUNCTION "concat"(text, text) RETURNS text AS
\'SELECT $1 || $2;\'
LANGUAGE \'sql\'
');
$this
->pass(st('PostgreSQL has initialized itself.'));
} catch (Exception $e) {
$this
->fail(st('Drupal could not be correctly setup with the existing database. Revise any errors.'));
}
}
}