示例#1
0
 /**
  * Update a record table for a set of harvesters and repositories
  * 
  * @param string $table
  * @param array $allowedRepositoryIdStrings
  * @param array $authGroupIdStrings	The ids of the groups to check view authorization for,
  *	May be one of the following or another group Id string:
  *		edu.middlebury.agents.everyone
  *		edu.middlebury.agents.all_agents
  *	If empty, all assets in the specified repositories will be added regardless of
  *	their visibility.
  *
  * @return void
  * @access public
  * @since 3/9/07
  */
 function updateTable($table, $allowedRepositoryIdStrings, $authGroupIdStrings)
 {
     ArgumentValidator::validate($table, StringValidatorRule::getRule());
     ArgumentValidator::validate($allowedRepositoryIdStrings, ArrayValidatorRuleWithRule::getRule(StringValidatorRule::getRule()));
     ArgumentValidator::validate($authGroupIdStrings, ArrayValidatorRuleWithRule::getRule(StringValidatorRule::getRule()));
     $harmoni = Harmoni::instance();
     $config = $harmoni->getAttachedData('OAI_CONFIG');
     $repositoryManager = Services::getService('Repository');
     $authorizationManager = Services::getService('AuthZ');
     $idManager = Services::getService("IdManager");
     $dbc = Services::getService("DatabaseManager");
     $authGroupIds = array();
     foreach ($authGroupIdStrings as $id) {
         $authGroupIds[] = $idManager->getId($id);
     }
     $baseCheckQuery = new SelectQuery();
     $baseCheckQuery->addTable('oai_' . $table);
     $baseCheckQuery->addColumn('datestamp');
     $baseCheckQuery->addColumn('deleted');
     $baseUpdateQuery = new UpdateQuery();
     $baseUpdateQuery->setTable('oai_' . $table);
     $baseUpdateColumns = array('datestamp', 'deleted', 'oai_set', 'dc_title', 'dc_description');
     $dcUpdateColumns = array('datestamp', 'deleted', 'oai_set', 'dc_title', 'dc_description', 'dc_creator', 'dc_subject', 'dc_contributor', 'dc_publisher', 'dc_date', 'dc_type', 'dc_format', 'dc_identifier', 'dc_source', 'dc_language', 'dc_relation', 'dc_coverage', 'dc_rights');
     $baseInsertQuery = new InsertQuery();
     $baseInsertQuery->setTable('oai_' . $table);
     $baseInsertColumns = array('datestamp', 'oai_identifier', 'deleted', 'oai_set', 'dc_title', 'dc_description');
     $dcInsertColumns = array('datestamp', 'oai_identifier', 'deleted', 'oai_set', 'dc_title', 'dc_description', 'dc_creator', 'dc_subject', 'dc_contributor', 'dc_publisher', 'dc_date', 'dc_type', 'dc_format', 'dc_identifier', 'dc_source', 'dc_language', 'dc_relation', 'dc_coverage', 'dc_rights');
     $baseDeleteQuery = new UpdateQuery();
     $baseDeleteQuery->setTable('oai_' . $table);
     $baseDeleteQuery->addValue('deleted', 'true');
     $baseDeleteQuery->addRawValue('datestamp', 'NOW()');
     $baseUndeleteQuery = new UpdateQuery();
     $baseUndeleteQuery->setTable('oai_' . $table);
     $baseUndeleteQuery->addValue('deleted', 'false');
     $baseUndeleteQuery->addRawValue('datestamp', 'NOW()');
     $forceUpdate = false;
     $repositories = $repositoryManager->getRepositories();
     $r = 0;
     if (count($allowedRepositoryIdStrings)) {
         $numR = count($allowedRepositoryIdStrings);
     } else {
         $numR = $repositories->count();
     }
     $numUpdates = 0;
     $numDeleted = 0;
     $message = _('Updating OAI records for repository (%1 of %2) : ');
     $message = str_replace('%2', $numR, $message);
     $instituteId = $idManager->getId('edu.middlebury.agents.users');
     $viewId = $idManager->getId('edu.middlebury.authorization.view');
     require_once HARMONI . "/utilities/Timer.class.php";
     $timer = new Timer();
     $timer->start();
     $existingRepositoryIds = array();
     while ($repositories->hasNext()) {
         $updatesInRepository = 0;
         $repository = $repositories->next();
         $repositoryId = $repository->getId();
         // Only work with allowed repositories
         if (count($allowedRepositoryIdStrings) && !in_array($repositoryId->getIdString(), $allowedRepositoryIdStrings)) {
             continue;
         }
         $r++;
         $existingRepositoryIds[] = $repositoryId->getIdString();
         $assets = $repository->getAssets();
         $status = new CLIStatusStars(str_replace('%1', $r, $message) . $repository->getDisplayName());
         $status->initializeStatistics($assets->count());
         $existingAssetIds = array();
         while ($assets->hasNext()) {
             $asset = $assets->next();
             $assetId = $asset->getId();
             $existingAssetIds[] = $assetId->getIdString();
             try {
                 $modificationDate = $asset->getModificationDate();
             } catch (UnimplementedException $e) {
                 $modificationDate = DateAndTime::now();
             }
             $query = $baseCheckQuery->copy();
             $query->addWhereEqual("oai_set", $repositoryId->getIdString());
             $query->addWhereEqual("oai_identifier", $assetId->getIdString());
             $result = $dbc->query($query, $config->getProperty('OAI_DBID'));
             if (!$result->getNumberOfRows()) {
                 // 					printpre("Doesn't exist:\t".$asset->getDisplayName()."");
                 $query = $baseInsertQuery->copy();
                 $query->addValue('oai_set', $repositoryId->getIdString());
                 $query->addValue('oai_identifier', $assetId->getIdString());
             } else {
                 // 					printpre("Exists:\t".$asset->getDisplayName()."");
                 if ($modificationDate->isGreaterThan(DateAndTime::fromString($result->field('datestamp'))) || $forceUpdate) {
                     // 						printpre("\tUpdating:\t".$asset->getDisplayName());
                     $query = $baseUpdateQuery->copy();
                     $query->addWhereEqual("oai_set", $repositoryId->getIdString());
                     $query->addWhereEqual("oai_identifier", $assetId->getIdString());
                 } else {
                     $query = null;
                 }
             }
             if ($query) {
                 $query->addRawValue('datestamp', 'NOW()');
             }
             $isCurrentlyDeleted = $result->getNumberOfRows() && $result->field('deleted') == 'true' ? true : false;
             $result->free();
             if (!count($authGroupIds)) {
                 $isVisible = true;
             } else {
                 $isVisible = false;
                 try {
                     foreach ($authGroupIds as $id) {
                         if ($authorizationManager->isAuthorized($id, $viewId, $assetId)) {
                             $isVisible = true;
                             break;
                         }
                     }
                 } catch (UnknownIdException $e) {
                     $isVisible = true;
                 }
             }
             if ($query) {
                 //Add the data fields
                 // Deleted
                 if ($isVisible) {
                     $query->addValue('deleted', 'false');
                 } else {
                     $query->addValue('deleted', 'true');
                 }
                 $query->addValue('dc_title', $asset->getDisplayName());
                 $query->addValue('dc_description', $asset->getDescription());
                 $this->addDublinCoreValues($asset, $query);
                 $dbc->query($query, $config->getProperty('OAI_DBID'));
                 $updatesInRepository++;
                 $numUpdates++;
             } else {
                 if ($isCurrentlyDeleted && $isVisible) {
                     $query = $baseUndeleteQuery->copy();
                 } else {
                     if (!$isCurrentlyDeleted && !$isVisible) {
                         $query = $baseDeleteQuery->copy();
                     } else {
                         $query = null;
                     }
                 }
                 if ($query) {
                     $query->addWhereEqual("oai_set", $repositoryId->getIdString());
                     $query->addWhereEqual("oai_identifier", $assetId->getIdString());
                     $dbc->query($query, $config->getProperty('OAI_DBID'));
                     $updatesInRepository++;
                     $numUpdates++;
                 }
             }
             $status->updateStatistics();
         }
         // Update any missing assets as deleted
         $query = $baseDeleteQuery->copy();
         $query->addWhereEqual("oai_set", $repositoryId->getIdString());
         if (count($existingAssetIds)) {
             $query->addWhereEqual("deleted", "false");
             $query->addWhereNotIn("oai_identifier", $existingAssetIds);
         }
         $result = $dbc->query($query, $config->getProperty('OAI_DBID'));
         if ($result->getNumberOfRows()) {
             $updatesInRepository = $updatesInRepository + $result->getNumberOfRows();
             $numUpdates = $numUpdates + $result->getNumberOfRows();
         }
         print OAI_UPDATE_OUTPUT_HTML ? "<pre>" : "\n";
         print "Elapsed Time:\t";
         $timer->end();
         printf("%1.2f", $timer->printTime());
         print " seconds";
         print OAI_UPDATE_OUTPUT_HTML ? "</pre>" : "";
         print OAI_UPDATE_OUTPUT_HTML ? "<pre>" : "\n";
         print "Updates: " . $updatesInRepository;
         print OAI_UPDATE_OUTPUT_HTML ? "</pre>" : "\n";
     }
     // Update any missing repositories as deleted
     $query = $baseDeleteQuery->copy();
     $query->addWhereEqual("deleted", "false");
     if (count($existingRepositoryIds)) {
         $query->addWhereNotIn("oai_set", $existingRepositoryIds);
     }
     $result = $dbc->query($query, $config->getProperty('OAI_DBID'));
     if ($result->getNumberOfRows()) {
         $updatesInRepository = $updatesInRepository + $result->getNumberOfRows();
         $numUpdates = $numUpdates + $result->getNumberOfRows();
     }
     print OAI_UPDATE_OUTPUT_HTML ? "<pre>" : "\n";
     print "Total Updates:\t" . $numUpdates;
     print OAI_UPDATE_OUTPUT_HTML ? "</pre>" : "\n";
 }