Move over some initial_setup() for Postgres (THIS IS BROKEN, NEEDS REVIEW/WORK FROM...
authorChad Horohoe <demon@users.mediawiki.org>
Fri, 7 Jan 2011 18:24:56 +0000 (18:24 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Fri, 7 Jan 2011 18:24:56 +0000 (18:24 +0000)
* Ported canCreateAccounts() check
* Moved creation of new user to setupUser()
* Moved DB creation (still needs schema stuff) to setupDatabase() where it belongs
* Added ts2 setup

TODO:
* UI for non-admin account (like MysqlInstaller::getSettingsForm) and maybe move ts2 and other "optional" settings there
* Schema creation/setup

includes/installer/Installer.i18n.php
includes/installer/MysqlInstaller.php
includes/installer/PostgresInstaller.php

index c99a614..6588e51 100644 (file)
@@ -452,6 +452,9 @@ Please proceed to the next page.",
 Make sure that the user "$1" can write to the schema "$2".',
        'config-install-pg-commit'        => 'Committing changes',
        'config-pg-no-plpgsql'            => 'You need to install the language PL/pgSQL in the database $1',
+       'config-install-pg-ts2'           => 'Checking for tsearch2',
+       'config-install-pg-ts2-failed'    => "'''FAILED''' tsearch2 must be installed in the database $1
+Please read [http://www.devx.com/opensource/Article/21674/0/page/2 these instructions] or ask on #postgresql on irc.freenode.net</li>\n",
        'config-install-user'             => 'Creating database user',
        'config-install-user-failed'      => 'Granting permission to user "$1" failed: $2',
        'config-install-tables'           => 'Creating tables',
index 82734f2..44b07d9 100644 (file)
@@ -410,7 +410,7 @@ class MysqlInstaller extends DatabaseInstaller {
                $conn = $status->value;
                $dbName = $this->getVar( 'wgDBname' );
                if( !$conn->selectDB( $dbName ) ) {
-                       $conn->query( "CREATE DATABASE `$dbName`", __METHOD__ );
+                       $conn->query( "CREATE DATABASE " . $conn->addIdentifierQuotes( $dbName ), __METHOD__ );
                        $conn->selectDB( $dbName );
                }
                return $status;
index 48f84d6..faac6ce 100644 (file)
@@ -25,6 +25,7 @@ class PostgresInstaller extends DatabaseInstaller {
        );
 
        var $minimumVersion = '8.1';
+       private $ts2MaxVersion = '8.3'; // Doing ts2 is not necessary in PG > 8.3
 
        function getName() {
                return 'postgres';
@@ -99,7 +100,7 @@ class PostgresInstaller extends DatabaseInstaller {
                                $this->getVar( 'wgDBserver' ),
                                $this->getVar( '_InstallUser' ),
                                $this->getVar( '_InstallPassword' ),
-                               $this->getVar( 'wgDBname' ) );
+                               false );
                        $status->value = $this->db;
                } catch ( DBConnectionError $e ) {
                        $status->fatal( 'config-connection-error', $e->getMessage() );
@@ -107,13 +108,50 @@ class PostgresInstaller extends DatabaseInstaller {
                return $status;
        }
 
+       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;
+       }
+
        public function preInstall() {
-               # Add our user callback to installSteps, right before the tables are created.
-               $callback = array(
+               $commitCB = array(
                        'name' => 'pg-commit',
                        'callback' => array( $this, 'commitChanges' ),
                );
-               $this->parent->addInstallStep( $callback, 'interwiki' );
+               $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 );
        }
 
        function setupDatabase() {
@@ -137,16 +175,65 @@ class PostgresInstaller extends DatabaseInstaller {
                                $this->getVar( 'wgDBuser'), $schema );
                }
                $conn->query( "DROP TABLE $safeschema.$ctest" );
-
+               
+               $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 );
+               }
                return $status;
        }
 
+       /**
+        * 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 ) ) {
+                               return Status::newFatal( 'config-install-pg-ts2-failed' );
+                       }
+                       $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();
+       }
+
        function commitChanges() {
                $this->db->query( 'COMMIT' );
-
                return Status::newGood();
        }
 
+       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;
+       }
+
        function getLocalSettings() {
                $port = $this->getVar( 'wgDBport' );
                $schema = $this->getVar( 'wgDBmwschema' );