MediaWiki
REL1_20
|
00001 <?php 00030 class PostgresInstaller extends DatabaseInstaller { 00031 00032 protected $globalNames = array( 00033 'wgDBserver', 00034 'wgDBport', 00035 'wgDBname', 00036 'wgDBuser', 00037 'wgDBpassword', 00038 'wgDBmwschema', 00039 ); 00040 00041 protected $internalDefaults = array( 00042 '_InstallUser' => 'postgres', 00043 ); 00044 00045 var $minimumVersion = '8.3'; 00046 var $maxRoleSearchDepth = 5; 00047 00048 protected $pgConns = array(); 00049 00050 function getName() { 00051 return 'postgres'; 00052 } 00053 00054 public function isCompiled() { 00055 return self::checkExtension( 'pgsql' ); 00056 } 00057 00058 function getConnectForm() { 00059 return 00060 $this->getTextBox( 'wgDBserver', 'config-db-host', array(), $this->parent->getHelpBox( 'config-db-host-help' ) ) . 00061 $this->getTextBox( 'wgDBport', 'config-db-port' ) . 00062 Html::openElement( 'fieldset' ) . 00063 Html::element( 'legend', array(), wfMessage( 'config-db-wiki-settings' )->text() ) . 00064 $this->getTextBox( 'wgDBname', 'config-db-name', array(), $this->parent->getHelpBox( 'config-db-name-help' ) ) . 00065 $this->getTextBox( 'wgDBmwschema', 'config-db-schema', array(), $this->parent->getHelpBox( 'config-db-schema-help' ) ) . 00066 Html::closeElement( 'fieldset' ) . 00067 $this->getInstallUserBox(); 00068 } 00069 00070 function submitConnectForm() { 00071 // Get variables from the request 00072 $newValues = $this->setVarsFromRequest( array( 'wgDBserver', 'wgDBport', 00073 'wgDBname', 'wgDBmwschema' ) ); 00074 00075 // Validate them 00076 $status = Status::newGood(); 00077 if ( !strlen( $newValues['wgDBname'] ) ) { 00078 $status->fatal( 'config-missing-db-name' ); 00079 } elseif ( !preg_match( '/^[a-zA-Z0-9_]+$/', $newValues['wgDBname'] ) ) { 00080 $status->fatal( 'config-invalid-db-name', $newValues['wgDBname'] ); 00081 } 00082 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBmwschema'] ) ) { 00083 $status->fatal( 'config-invalid-schema', $newValues['wgDBmwschema'] ); 00084 } 00085 00086 // Submit user box 00087 if ( $status->isOK() ) { 00088 $status->merge( $this->submitInstallUserBox() ); 00089 } 00090 if ( !$status->isOK() ) { 00091 return $status; 00092 } 00093 00094 $status = $this->getPgConnection( 'create-db' ); 00095 if ( !$status->isOK() ) { 00096 return $status; 00097 } 00101 $conn = $status->value; 00102 00103 // Check version 00104 $version = $conn->getServerVersion(); 00105 if ( version_compare( $version, $this->minimumVersion ) < 0 ) { 00106 return Status::newFatal( 'config-postgres-old', $this->minimumVersion, $version ); 00107 } 00108 00109 $this->setVar( 'wgDBuser', $this->getVar( '_InstallUser' ) ); 00110 $this->setVar( 'wgDBpassword', $this->getVar( '_InstallPassword' ) ); 00111 return Status::newGood(); 00112 } 00113 00114 public function getConnection() { 00115 $status = $this->getPgConnection( 'create-tables' ); 00116 if ( $status->isOK() ) { 00117 $this->db = $status->value; 00118 } 00119 return $status; 00120 } 00121 00122 public function openConnection() { 00123 return $this->openPgConnection( 'create-tables' ); 00124 } 00125 00133 protected function openConnectionWithParams( $user, $password, $dbName ) { 00134 $status = Status::newGood(); 00135 try { 00136 $db = new DatabasePostgres( 00137 $this->getVar( 'wgDBserver' ), 00138 $user, 00139 $password, 00140 $dbName); 00141 $status->value = $db; 00142 } catch ( DBConnectionError $e ) { 00143 $status->fatal( 'config-connection-error', $e->getMessage() ); 00144 } 00145 return $status; 00146 } 00147 00153 protected function getPgConnection( $type ) { 00154 if ( isset( $this->pgConns[$type] ) ) { 00155 return Status::newGood( $this->pgConns[$type] ); 00156 } 00157 $status = $this->openPgConnection( $type ); 00158 00159 if ( $status->isOK() ) { 00163 $conn = $status->value; 00164 $conn->clearFlag( DBO_TRX ); 00165 $conn->commit( __METHOD__ ); 00166 $this->pgConns[$type] = $conn; 00167 } 00168 return $status; 00169 } 00170 00196 protected function openPgConnection( $type ) { 00197 switch ( $type ) { 00198 case 'create-db': 00199 return $this->openConnectionToAnyDB( 00200 $this->getVar( '_InstallUser' ), 00201 $this->getVar( '_InstallPassword' ) ); 00202 case 'create-schema': 00203 return $this->openConnectionWithParams( 00204 $this->getVar( '_InstallUser' ), 00205 $this->getVar( '_InstallPassword' ), 00206 $this->getVar( 'wgDBname' ) ); 00207 case 'create-tables': 00208 $status = $this->openPgConnection( 'create-schema' ); 00209 if ( $status->isOK() ) { 00213 $conn = $status->value; 00214 $safeRole = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) ); 00215 $conn->query( "SET ROLE $safeRole" ); 00216 } 00217 return $status; 00218 default: 00219 throw new MWException( "Invalid special connection type: \"$type\"" ); 00220 } 00221 } 00222 00223 public function openConnectionToAnyDB( $user, $password ) { 00224 $dbs = array( 00225 'template1', 00226 'postgres', 00227 ); 00228 if ( !in_array( $this->getVar( 'wgDBname' ), $dbs ) ) { 00229 array_unshift( $dbs, $this->getVar( 'wgDBname' ) ); 00230 } 00231 $conn = false; 00232 $status = Status::newGood(); 00233 foreach ( $dbs as $db ) { 00234 try { 00235 $conn = new DatabasePostgres( 00236 $this->getVar( 'wgDBserver' ), 00237 $user, 00238 $password, 00239 $db ); 00240 } catch ( DBConnectionError $error ) { 00241 $conn = false; 00242 $status->fatal( 'config-pg-test-error', $db, 00243 $error->getMessage() ); 00244 } 00245 if ( $conn !== false ) { 00246 break; 00247 } 00248 } 00249 if ( $conn !== false ) { 00250 return Status::newGood( $conn ); 00251 } else { 00252 return $status; 00253 } 00254 } 00255 00256 protected function getInstallUserPermissions() { 00257 $status = $this->getPgConnection( 'create-db' ); 00258 if ( !$status->isOK() ) { 00259 return false; 00260 } 00264 $conn = $status->value; 00265 $superuser = $this->getVar( '_InstallUser' ); 00266 00267 $row = $conn->selectRow( '"pg_catalog"."pg_roles"', '*', 00268 array( 'rolname' => $superuser ), __METHOD__ ); 00269 return $row; 00270 } 00271 00272 protected function canCreateAccounts() { 00273 $perms = $this->getInstallUserPermissions(); 00274 if ( !$perms ) { 00275 return false; 00276 } 00277 return $perms->rolsuper === 't' || $perms->rolcreaterole === 't'; 00278 } 00279 00280 protected function isSuperUser() { 00281 $perms = $this->getInstallUserPermissions(); 00282 if ( !$perms ) { 00283 return false; 00284 } 00285 return $perms->rolsuper === 't'; 00286 } 00287 00288 public function getSettingsForm() { 00289 if ( $this->canCreateAccounts() ) { 00290 $noCreateMsg = false; 00291 } else { 00292 $noCreateMsg = 'config-db-web-no-create-privs'; 00293 } 00294 $s = $this->getWebUserBox( $noCreateMsg ); 00295 00296 return $s; 00297 } 00298 00299 public function submitSettingsForm() { 00300 $status = $this->submitWebUserBox(); 00301 if ( !$status->isOK() ) { 00302 return $status; 00303 } 00304 00305 $same = $this->getVar( 'wgDBuser' ) === $this->getVar( '_InstallUser' ); 00306 00307 if ( $same ) { 00308 $exists = true; 00309 } else { 00310 // Check if the web user exists 00311 // Connect to the database with the install user 00312 $status = $this->getPgConnection( 'create-db' ); 00313 if ( !$status->isOK() ) { 00314 return $status; 00315 } 00316 $exists = $status->value->roleExists( $this->getVar( 'wgDBuser' ) ); 00317 } 00318 00319 // Validate the create checkbox 00320 if ( $this->canCreateAccounts() && !$same && !$exists ) { 00321 $create = $this->getVar( '_CreateDBAccount' ); 00322 } else { 00323 $this->setVar( '_CreateDBAccount', false ); 00324 $create = false; 00325 } 00326 00327 if ( !$create && !$exists ) { 00328 if ( $this->canCreateAccounts() ) { 00329 $msg = 'config-install-user-missing-create'; 00330 } else { 00331 $msg = 'config-install-user-missing'; 00332 } 00333 return Status::newFatal( $msg, $this->getVar( 'wgDBuser' ) ); 00334 } 00335 00336 if ( !$exists ) { 00337 // No more checks to do 00338 return Status::newGood(); 00339 } 00340 00341 // Existing web account. Test the connection. 00342 $status = $this->openConnectionToAnyDB( 00343 $this->getVar( 'wgDBuser' ), 00344 $this->getVar( 'wgDBpassword' ) ); 00345 if ( !$status->isOK() ) { 00346 return $status; 00347 } 00348 00349 // The web user is conventionally the table owner in PostgreSQL 00350 // installations. Make sure the install user is able to create 00351 // objects on behalf of the web user. 00352 if ( $same || $this->canCreateObjectsForWebUser() ) { 00353 return Status::newGood(); 00354 } else { 00355 return Status::newFatal( 'config-pg-not-in-role' ); 00356 } 00357 } 00358 00364 protected function canCreateObjectsForWebUser() { 00365 if ( $this->isSuperUser() ) { 00366 return true; 00367 } 00368 00369 $status = $this->getPgConnection( 'create-db' ); 00370 if ( !$status->isOK() ) { 00371 return false; 00372 } 00373 $conn = $status->value; 00374 $installerId = $conn->selectField( '"pg_catalog"."pg_roles"', 'oid', 00375 array( 'rolname' => $this->getVar( '_InstallUser' ) ), __METHOD__ ); 00376 $webId = $conn->selectField( '"pg_catalog"."pg_roles"', 'oid', 00377 array( 'rolname' => $this->getVar( 'wgDBuser' ) ), __METHOD__ ); 00378 00379 return $this->isRoleMember( $conn, $installerId, $webId, $this->maxRoleSearchDepth ); 00380 } 00381 00390 protected function isRoleMember( $conn, $targetMember, $group, $maxDepth ) { 00391 if ( $targetMember === $group ) { 00392 // A role is always a member of itself 00393 return true; 00394 } 00395 // Get all members of the given group 00396 $res = $conn->select( '"pg_catalog"."pg_auth_members"', array( 'member' ), 00397 array( 'roleid' => $group ), __METHOD__ ); 00398 foreach ( $res as $row ) { 00399 if ( $row->member == $targetMember ) { 00400 // Found target member 00401 return true; 00402 } 00403 // Recursively search each member of the group to see if the target 00404 // is a member of it, up to the given maximum depth. 00405 if ( $maxDepth > 0 ) { 00406 if ( $this->isRoleMember( $conn, $targetMember, $row->member, $maxDepth - 1 ) ) { 00407 // Found member of member 00408 return true; 00409 } 00410 } 00411 } 00412 return false; 00413 } 00414 00415 public function preInstall() { 00416 $createDbAccount = array( 00417 'name' => 'user', 00418 'callback' => array( $this, 'setupUser' ), 00419 ); 00420 $commitCB = array( 00421 'name' => 'pg-commit', 00422 'callback' => array( $this, 'commitChanges' ), 00423 ); 00424 $plpgCB = array( 00425 'name' => 'pg-plpgsql', 00426 'callback' => array( $this, 'setupPLpgSQL' ), 00427 ); 00428 $schemaCB = array( 00429 'name' => 'schema', 00430 'callback' => array( $this, 'setupSchema' ) 00431 ); 00432 00433 if( $this->getVar( '_CreateDBAccount' ) ) { 00434 $this->parent->addInstallStep( $createDbAccount, 'database' ); 00435 } 00436 $this->parent->addInstallStep( $commitCB, 'interwiki' ); 00437 $this->parent->addInstallStep( $plpgCB, 'database' ); 00438 $this->parent->addInstallStep( $schemaCB, 'database' ); 00439 } 00440 00441 function setupDatabase() { 00442 $status = $this->getPgConnection( 'create-db' ); 00443 if ( !$status->isOK() ) { 00444 return $status; 00445 } 00446 $conn = $status->value; 00447 00448 $dbName = $this->getVar( 'wgDBname' ); 00449 00450 $exists = $conn->selectField( '"pg_catalog"."pg_database"', '1', 00451 array( 'datname' => $dbName ), __METHOD__ ); 00452 if ( !$exists ) { 00453 $safedb = $conn->addIdentifierQuotes( $dbName ); 00454 $conn->query( "CREATE DATABASE $safedb", __METHOD__ ); 00455 } 00456 return Status::newGood(); 00457 } 00458 00459 function setupSchema() { 00460 // Get a connection to the target database 00461 $status = $this->getPgConnection( 'create-schema' ); 00462 if ( !$status->isOK() ) { 00463 return $status; 00464 } 00465 $conn = $status->value; 00466 00467 // Create the schema if necessary 00468 $schema = $this->getVar( 'wgDBmwschema' ); 00469 $safeschema = $conn->addIdentifierQuotes( $schema ); 00470 $safeuser = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) ); 00471 if( !$conn->schemaExists( $schema ) ) { 00472 try { 00473 $conn->query( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" ); 00474 } catch ( DBQueryError $e ) { 00475 return Status::newFatal( 'config-install-pg-schema-failed', 00476 $this->getVar( '_InstallUser' ), $schema ); 00477 } 00478 } 00479 00480 // Select the new schema in the current connection 00481 $conn->determineCoreSchema( $schema ); 00482 return Status::newGood(); 00483 } 00484 00485 function commitChanges() { 00486 $this->db->commit( __METHOD__ ); 00487 return Status::newGood(); 00488 } 00489 00490 function setupUser() { 00491 if ( !$this->getVar( '_CreateDBAccount' ) ) { 00492 return Status::newGood(); 00493 } 00494 00495 $status = $this->getPgConnection( 'create-db' ); 00496 if ( !$status->isOK() ) { 00497 return $status; 00498 } 00499 $conn = $status->value; 00500 00501 $safeuser = $conn->addIdentifierQuotes( $this->getVar( 'wgDBuser' ) ); 00502 $safepass = $conn->addQuotes( $this->getVar( 'wgDBpassword' ) ); 00503 00504 // Check if the user already exists 00505 $userExists = $conn->roleExists( $this->getVar( 'wgDBuser' ) ); 00506 if ( !$userExists ) { 00507 // Create the user 00508 try { 00509 $sql = "CREATE ROLE $safeuser NOCREATEDB LOGIN PASSWORD $safepass"; 00510 00511 // If the install user is not a superuser, we need to make the install 00512 // user a member of the new user's group, so that the install user will 00513 // be able to create a schema and other objects on behalf of the new user. 00514 if ( !$this->isSuperUser() ) { 00515 $sql .= ' ROLE' . $conn->addIdentifierQuotes( $this->getVar( '_InstallUser' ) ); 00516 } 00517 00518 $conn->query( $sql, __METHOD__ ); 00519 } catch ( DBQueryError $e ) { 00520 return Status::newFatal( 'config-install-user-create-failed', 00521 $this->getVar( 'wgDBuser' ), $e->getMessage() ); 00522 } 00523 } 00524 00525 return Status::newGood(); 00526 } 00527 00528 function getLocalSettings() { 00529 $port = $this->getVar( 'wgDBport' ); 00530 $schema = $this->getVar( 'wgDBmwschema' ); 00531 return 00532 "# Postgres specific settings 00533 \$wgDBport = \"{$port}\"; 00534 \$wgDBmwschema = \"{$schema}\";"; 00535 } 00536 00537 public function preUpgrade() { 00538 global $wgDBuser, $wgDBpassword; 00539 00540 # Normal user and password are selected after this step, so for now 00541 # just copy these two 00542 $wgDBuser = $this->getVar( '_InstallUser' ); 00543 $wgDBpassword = $this->getVar( '_InstallPassword' ); 00544 } 00545 00546 public function createTables() { 00547 $schema = $this->getVar( 'wgDBmwschema' ); 00548 00549 $status = $this->getConnection(); 00550 if ( !$status->isOK() ) { 00551 return $status; 00552 } 00553 00557 $conn = $status->value; 00558 00559 if( $conn->tableExists( 'archive' ) ) { 00560 $status->warning( 'config-install-tables-exist' ); 00561 $this->enableLB(); 00562 return $status; 00563 } 00564 00565 $conn->begin( __METHOD__ ); 00566 00567 if( !$conn->schemaExists( $schema ) ) { 00568 $status->fatal( 'config-install-pg-schema-not-exist' ); 00569 return $status; 00570 } 00571 $error = $conn->sourceFile( $conn->getSchemaPath() ); 00572 if( $error !== true ) { 00573 $conn->reportQueryError( $error, 0, '', __METHOD__ ); 00574 $conn->rollback( __METHOD__ ); 00575 $status->fatal( 'config-install-tables-failed', $error ); 00576 } else { 00577 $conn->commit( __METHOD__ ); 00578 } 00579 // Resume normal operations 00580 if( $status->isOk() ) { 00581 $this->enableLB(); 00582 } 00583 return $status; 00584 } 00585 00586 public function setupPLpgSQL() { 00587 // Connect as the install user, since it owns the database and so is 00588 // the user that needs to run "CREATE LANGAUGE" 00589 $status = $this->getPgConnection( 'create-schema' ); 00590 if ( !$status->isOK() ) { 00591 return $status; 00592 } 00596 $conn = $status->value; 00597 00598 $exists = $conn->selectField( '"pg_catalog"."pg_language"', 1, 00599 array( 'lanname' => 'plpgsql' ), __METHOD__ ); 00600 if ( $exists ) { 00601 // Already exists, nothing to do 00602 return Status::newGood(); 00603 } 00604 00605 // plpgsql is not installed, but if we have a pg_pltemplate table, we 00606 // should be able to create it 00607 $exists = $conn->selectField( 00608 array( '"pg_catalog"."pg_class"', '"pg_catalog"."pg_namespace"' ), 00609 1, 00610 array( 00611 'pg_namespace.oid=relnamespace', 00612 'nspname' => 'pg_catalog', 00613 'relname' => 'pg_pltemplate', 00614 ), 00615 __METHOD__ ); 00616 if ( $exists ) { 00617 try { 00618 $conn->query( 'CREATE LANGUAGE plpgsql' ); 00619 } catch ( DBQueryError $e ) { 00620 return Status::newFatal( 'config-pg-no-plpgsql', $this->getVar( 'wgDBname' ) ); 00621 } 00622 } else { 00623 return Status::newFatal( 'config-pg-no-plpgsql', $this->getVar( 'wgDBname' ) ); 00624 } 00625 return Status::newGood(); 00626 } 00627 }