/** * Set the backtrace HTML * * @param string $backtrace * @return void * @access public * @since 3/6/06 */ function setBacktrace($backtrace) { if (is_array($backtrace)) { ob_start(); HarmoniErrorHandler::printDebugBacktrace($backtrace); $this->_backtrace = HtmlString::getSafeHtml(ob_get_clean()); } else { $this->_backtrace = HtmlString::getSafeHtml($backtrace); } }
/** * Run the update * * @return boolean * @access public * @since 3/24/08 */ function runUpdate($dbIndex) { $prepStatus = new StatusStars("Preparing Migration"); $prepStatus->initializeStatistics(3); // Configure the original Hierarchy and AZ services $context = new OsidContext(); $configuration = new ConfigurationProperties(); $configuration->addProperty('database_index', $dbIndex); $configuration->addProperty('database_name', $_REQUEST['db_name']); $configuration->addProperty('harmoni_db_name', 'migration_db'); Services::startManagerAsService("IdManager", $context, $configuration); Services::startManagerAsService("HierarchyManager", $context, $configuration); Services::startManagerAsService("AuthorizationManager", $context, $configuration); // Agent Manager $configuration = new ConfigurationProperties(); // default agent Flavor is one that can be editted $agentFlavor = "HarmoniEditableAgent"; $agentHierarchyId = "edu.middlebury.authorization.hierarchy"; $configuration->addProperty('hierarchy_id', $agentHierarchyId); $configuration->addProperty('defaultAgentFlavor', $agentFlavor); $configuration->addProperty('database_index', $dbIndex); $configuration->addProperty('database_name', $_REQUEST['db_name']); Services::startManagerAsService("AgentManager", $context, $configuration); // :: Set up PropertyManager :: //the property manager operates in the same context as the AgentManager and is more or less an adjunct to it $configuration->addProperty('database_index', $dbIndex); $configuration->addProperty('database_name', $_REQUEST['db_name']); Services::startManagerAsService("PropertyManager", $context, $configuration); // :: Start the AuthenticationManager OSID Impl. $configuration = new ConfigurationProperties(); $tokenCollectors = array(serialize(new Type("Authentication", "edu.middlebury.harmoni", "Harmoni DB")) => new FormActionNamePassTokenCollector('does not exist')); $configuration->addProperty('token_collectors', $tokenCollectors); Services::startManagerAsService("AuthenticationManager", $context, $configuration); // :: Start and configure the AuthenticationMethodManager $configuration = new ConfigurationProperties(); // set up a Database Authentication Method require_once HARMONI . "/oki2/agentmanagement/AuthNMethods/SQLDatabaseAuthNMethod.class.php"; require_once HARMONI . "/oki2/agentmanagement/AuthNMethods/SQLDatabaseMD5UsernamePasswordAuthNTokens.class.php"; $dbAuthType = new Type("Authentication", "edu.middlebury.harmoni", "Harmoni DB"); $dbMethodConfiguration = new ConfigurationProperties(); $dbMethodConfiguration->addProperty('tokens_class', 'SQLDatabaseMD5UsernamePasswordAuthNTokens'); $dbMethodConfiguration->addProperty('database_id', $dbIndex); $dbMethodConfiguration->addProperty('authentication_table', 'auth_db_user'); $dbMethodConfiguration->addProperty('username_field', 'username'); $dbMethodConfiguration->addProperty('password_field', 'password'); $propertiesFields = array('username' => 'username'); $dbMethodConfiguration->addProperty('properties_fields', $propertiesFields); $dbAuthNMethod = new SQLDatabaseAuthNMethod(); $dbAuthNMethod->assignConfiguration($dbMethodConfiguration); $configuration->addProperty($dbAuthType, $dbAuthNMethod); Services::startManagerAsService("AuthNMethodManager", $context, $configuration); // :: Agent-Token Mapping Manager :: $configuration = new ConfigurationProperties(); $configuration->addProperty('database_id', $dbIndex); $configuration->addProperty('harmoni_db_name', 'migration_db'); Services::startManagerAsService("AgentTokenMappingManager", $context, $configuration); $prepStatus->updateStatistics(); $dbc = Services::getService("DatabaseManager"); try { /********************************************************* * Check for the old tables. They must exist for us to run *********************************************************/ $azTables = array('az_authorization', 'az_function', 'hierarchy', 'j_node_node', 'node', 'node_ancestry'); // Check for old tables $tables = $dbc->getTableList($dbIndex); foreach ($azTables as $table) { if (!in_array($table, $tables)) { throw new Exception("Old AZ table, {$table}, is missing. Can not run Update."); } } /********************************************************* * Create the new tables *********************************************************/ $type = $dbc->getDatabaseType($dbIndex); switch ($type) { case MYSQL: SQLUtils::runSQLfile(HARMONI_BASE . "/SQL/MySQL/AuthZ2.sql", $dbIndex); break; case POSTGRESQL: SQLUtils::runSQLfile(HARMONI_BASE . "/SQL/PostgreSQL/AuthZ2.sql", $dbIndex); break; case ORACLE: SQLUtils::runSQLfile(HARMONI_BASE . "/SQL/PostgreSQL/AuthZ2.sql", $dbIndex); break; default: throw new Exception("Database schemas are not defined for specified database type."); } /********************************************************* * Hierarchy *********************************************************/ $hierarchyMgr1 = Services::getService("Hierarchy"); if (get_class($hierarchyMgr1) == "AuthZ2_HierarchyManager") { throw new OperationFailedException("Original HierarchyManager not configured."); } $hierarchyMgr2 = new AuthZ2_HierarchyManager(); $azMgr2 = new AuthZ2_AuthorizationManager(); $azMgr2->setHierarchyManager($hierarchyMgr2); $hierarchyMgr2->assignConfiguration($hierarchyMgr1->_configuration); /********************************************************* * Authorization *********************************************************/ $azMgr1 = Services::getService("AuthZ"); if (get_class($hierarchyMgr1) == "AuthZ2_AuthorizationManager") { throw new OperationFailedException("Original HierarchyManager not configured."); } $azMgr2->assignConfiguration($azMgr1->_configuration); $prepStatus->updateStatistics(); /********************************************************* * Hierarchies *********************************************************/ $hierarchies = $hierarchyMgr1->getHierarchies(); $prepStatus->updateStatistics(); while ($hierarchies->hasNext()) { $hierarchy = $hierarchies->next(); try { $newHierarchy = $hierarchyMgr2->getHierarchy($hierarchy->getId()); } catch (UnknownIdException $e) { $newHierarchy = $hierarchyMgr2->createHierarchy($hierarchy->getDisplayName(), array(), $hierarchy->getDescription(), $hierarchy->allowsMultipleParents(), $hierarchy->allowsRecursion(), $hierarchy->getId()); } $query = new SelectQuery(); $query->addTable("node"); $query->addColumn("COUNT(*)", "num"); $query->addWhereEqual("fk_hierarchy", $hierarchy->getId()->getIdString()); $dbc = Services::getService("DatabaseManager"); $result = $dbc->query($query); $this->nodeStatus = new StatusStars("Migrating nodes in the '" . $hierarchy->getDisplayName() . "' Hierarchy."); $this->nodeStatus->initializeStatistics($result->field("num")); // Add all of the nodes $nodes = $hierarchy->getRootNodes(); while ($nodes->hasNext()) { $this->addNode($newHierarchy, $nodes->next()); } } /********************************************************* * Authorizations *********************************************************/ $azMgr1 = Services::getService("AuthZ"); if (get_class($hierarchyMgr1) == "AuthZ2_AuthorizationManager") { throw new OperationFailedException("Original HierarchyManager not configured."); } // Add all of the Authorization functions $functionTypes = $azMgr1->getFunctionTypes(); while ($functionTypes->hasNext()) { $oldFunctions = $azMgr1->getFunctions($functionTypes->next()); while ($oldFunctions->hasNext()) { $oldFunction = $oldFunctions->next(); // Get or create the function try { $newFunction = $azMgr2->getFunction($oldFunction->getId()); } catch (UnknownIdException $e) { $newFunction = $azMgr2->createFunction($oldFunction->getId(), $oldFunction->getReferenceName(), $oldFunction->getDescription(), $oldFunction->getFunctionType(), $oldFunction->getQualifierHierarchyId()); } // Get all authorizations for this function. $oldAZs = $azMgr1->getExplicitAZs(null, $oldFunction->getId(), null, false); $status = new StatusStars("Migrating '" . $newFunction->getReferenceName() . "' Authorizations (" . $oldAZs->count() . ")"); $status->initializeStatistics($oldAZs->count()); while ($oldAZs->hasNext()) { $oldAZ = $oldAZs->next(); $status->updateStatistics(); try { $oldQualifier = $oldAZ->getQualifier(); } catch (UnknownIdException $e) { // continue if the qualifier no longer exists. continue; } // Add the new authorization try { $newAZ = $azMgr2->createAuthorization($oldAZ->getAgentId(), $oldAZ->getFunction()->getId(), $oldQualifier->getId()); if ($oldAZ->getExpirationDate()) { $newAZ->updateExpirationDate($oldAZ->getExpirationDate()); } if ($oldAZ->getEffectiveDate()) { $newAZ->updateEffectiveDate($oldAZ->getEffectiveDate()); } } catch (OperationFailedException $e) { } } } } } catch (Exception $e) { printpre($e->getMessage()); HarmoniErrorHandler::printDebugBacktrace($e->getTrace()); printpre("An error has occurred. Removing new tables."); try { $query = new GenericSQLQuery('TRUNCATE az2_implicit_az'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_explicit_az'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_node_ancestry'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_j_node_node'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_function'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_function_type'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_node'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_node_type'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_hierarchy'); } catch (DatabaseException $e) { } $query = new GenericSQLQuery('DROP TABLE az2_implicit_az, az2_explicit_az, az2_function, az2_function_type, az2_node_ancestry, az2_j_node_node, az2_node, az2_node_type, az2_hierarchy;'); $dbc->query($query, $dbIndex); return false; } /********************************************************* * If we have successfully gotten this far, drop the old * hierarchy and AuthZ tables to prevent confusion. *********************************************************/ $query = new GenericSQLQuery('DROP TABLE az_authorization, az_function, hierarchy, j_node_node, node, node_ancestry;'); $dbc->query($query, $dbIndex); print "Success!"; return true; }
/** * Run the update * * @return boolean * @access public * @since 3/24/08 */ function runUpdate() { $prepStatus = new StatusStars("Preparing Migration"); $prepStatus->initializeStatistics(3); $prepStatus->updateStatistics(); $dbc = Services::getService("DatabaseManager"); try { /********************************************************* * Check for the old tables. They must exist for us to run *********************************************************/ $azTables = array('az_authorization', 'az_function', 'hierarchy', 'j_node_node', 'node', 'node_ancestry'); // Check for old tables $tables = $dbc->getTableList(IMPORTER_CONNECTION); foreach ($azTables as $table) { if (!in_array($table, $tables)) { throw new Exception("Old AZ table, {$table}, is missing. Can not run Update."); } } /********************************************************* * Create the new tables *********************************************************/ $type = $dbc->getDatabaseType(IMPORTER_CONNECTION); switch ($type) { case MYSQL: SQLUtils::runSQLfile(HARMONI_BASE . "/SQL/MySQL/AuthZ2.sql", IMPORTER_CONNECTION); break; case POSTGRESQL: SQLUtils::runSQLfile(HARMONI_BASE . "/SQL/PostgreSQL/AuthZ2.sql", IMPORTER_CONNECTION); break; case ORACLE: SQLUtils::runSQLfile(HARMONI_BASE . "/SQL/PostgreSQL/AuthZ2.sql", IMPORTER_CONNECTION); break; default: throw new Exception("Database schemas are not defined for specified database type."); } /********************************************************* * Hierarchy *********************************************************/ $hierarchyMgr1 = Services::getService("Hierarchy"); if (get_class($hierarchyMgr1) == "AuthZ2_HierarchyManager") { throw new OperationFailedException("Original HierarchyManager not configured."); } $hierarchyMgr2 = new AuthZ2_HierarchyManager(); $azMgr2 = new AuthZ2_AuthorizationManager(); $azMgr2->setHierarchyManager($hierarchyMgr2); $hierarchyMgr2->assignConfiguration($hierarchyMgr1->_configuration); /********************************************************* * Authorization *********************************************************/ $azMgr1 = Services::getService("AuthZ"); if (get_class($hierarchyMgr1) == "AuthZ2_AuthorizationManager") { throw new OperationFailedException("Original HierarchyManager not configured."); } $azMgr2->assignConfiguration($azMgr1->_configuration); $prepStatus->updateStatistics(); /********************************************************* * Hierarchies *********************************************************/ $hierarchies = $hierarchyMgr1->getHierarchies(); $prepStatus->updateStatistics(); while ($hierarchies->hasNext()) { $hierarchy = $hierarchies->next(); try { $newHierarchy = $hierarchyMgr2->getHierarchy($hierarchy->getId()); } catch (UnknownIdException $e) { $newHierarchy = $hierarchyMgr2->createHierarchy($hierarchy->getDisplayName(), array(), $hierarchy->getDescription(), $hierarchy->allowsMultipleParents(), $hierarchy->allowsRecursion(), $hierarchy->getId()); } $query = new SelectQuery(); $query->addTable("node"); $query->addColumn("COUNT(*)", "num"); $query->addWhereEqual("fk_hierarchy", $hierarchy->getId()->getIdString()); $dbc = Services::getService("DatabaseManager"); $result = $dbc->query($query); $this->nodeStatus = new StatusStars("Migrating nodes in the '" . $hierarchy->getDisplayName() . "' Hierarchy."); $this->nodeStatus->initializeStatistics($result->field("num")); // Add all of the nodes $nodes = $hierarchy->getRootNodes(); while ($nodes->hasNext()) { $this->addNode($newHierarchy, $nodes->next()); } } /********************************************************* * Authorizations *********************************************************/ $azMgr1 = Services::getService("AuthZ"); if (get_class($hierarchyMgr1) == "AuthZ2_AuthorizationManager") { throw new OperationFailedException("Original HierarchyManager not configured."); } // Add all of the Authorization functions $functionTypes = $azMgr1->getFunctionTypes(); while ($functionTypes->hasNext()) { $oldFunctions = $azMgr1->getFunctions($functionTypes->next()); while ($oldFunctions->hasNext()) { $oldFunction = $oldFunctions->next(); // Get or create the function try { $newFunction = $azMgr2->getFunction($oldFunction->getId()); } catch (UnknownIdException $e) { $newFunction = $azMgr2->createFunction($oldFunction->getId(), $oldFunction->getReferenceName(), $oldFunction->getDescription(), $oldFunction->getFunctionType(), $oldFunction->getQualifierHierarchyId()); } // Get all authorizations for this function. $oldAZs = $azMgr1->getExplicitAZs(null, $oldFunction->getId(), null, false); $status = new StatusStars("Migrating '" . $newFunction->getReferenceName() . "' Authorizations (" . $oldAZs->count() . ")"); $status->initializeStatistics($oldAZs->count()); while ($oldAZs->hasNext()) { $oldAZ = $oldAZs->next(); $status->updateStatistics(); try { $oldQualifier = $oldAZ->getQualifier(); } catch (UnknownIdException $e) { // continue if the qualifier no longer exists. continue; } // Add the new authorization try { $newAZ = $azMgr2->createAuthorization($oldAZ->getAgentId(), $oldAZ->getFunction()->getId(), $oldQualifier->getId()); if ($oldAZ->getExpirationDate()) { $newAZ->updateExpirationDate($oldAZ->getExpirationDate()); } if ($oldAZ->getEffectiveDate()) { $newAZ->updateEffectiveDate($oldAZ->getEffectiveDate()); } } catch (OperationFailedException $e) { } } } } } catch (Exception $e) { printpre($e->getMessage()); HarmoniErrorHandler::printDebugBacktrace($e->getTrace()); printpre("An error has occurred. Removing new tables."); try { $query = new GenericSQLQuery('TRUNCATE az2_implicit_az'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_explicit_az'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_node_ancestry'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_j_node_node'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_function'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_function_type'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_node'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_node_type'); } catch (DatabaseException $e) { } try { $query = new GenericSQLQuery('TRUNCATE az2_hierarchy'); } catch (DatabaseException $e) { } $query = new GenericSQLQuery('DROP TABLE az2_implicit_az, az2_explicit_az, az2_function, az2_function_type, az2_node_ancestry, az2_j_node_node, az2_node, az2_node_type, az2_hierarchy;'); $dbc->query($query, IMPORTER_CONNECTION); return false; } /********************************************************* * If we have successfully gotten this far, drop the old * hierarchy and AuthZ tables to prevent confusion. *********************************************************/ $query = new GenericSQLQuery('DROP TABLE az_authorization, az_function, hierarchy, j_node_node, node, node_ancestry;'); $dbc->query($query, IMPORTER_CONNECTION); return true; }