Created
March 30, 2020 14:00
-
-
Save jraddaoui/25c5cc824ffd32e1c5892aabaeb39840 to your computer and use it in GitHub Desktop.
Script to be executed with `php symfony tools:run` to fix the PREMIS right basis terms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
print("Have you created a backup of the database? [y/N]: "); | |
flush(); | |
ob_flush(); | |
$confirmation = trim(fgets(STDIN)); | |
if ($confirmation !== 'y') | |
{ | |
exit (0); | |
} | |
print("Checking PREMIS right basis terms:\n"); | |
$configuration = ProjectConfiguration::getApplicationConfiguration( | |
'qubit', 'cli', false | |
); | |
$terms = array( | |
'Copyright' => QubitTerm::RIGHT_BASIS_COPYRIGHT_ID, | |
'License' => QubitTerm::RIGHT_BASIS_LICENSE_ID, | |
'Statute' => QubitTerm::RIGHT_BASIS_STATUTE_ID, | |
'Policy' => QubitTerm::RIGHT_BASIS_POLICY_ID, | |
'Donor' => null | |
); | |
foreach ($terms as $name => $id) | |
{ | |
print("- $name:"); | |
if (!isset($id)) | |
{ | |
print(" correct.\n"); | |
continue; | |
} | |
$criteria = new Criteria; | |
$criteria->addJoin(QubitTerm::ID, QubitTermI18n::ID); | |
$criteria->add(QubitTermI18n::NAME, $name); | |
$criteria->add(QubitTermI18n::CULTURE, 'en'); | |
$criteria->add(QubitTerm::TAXONOMY_ID, QubitTaxonomy::RIGHT_BASIS_ID); | |
if (null === $term = QubitTerm::getOne($criteria)) | |
{ | |
print(" not found.\n"); | |
continue; | |
} | |
if ($term->id == $id) | |
{ | |
print(" correct.\n"); | |
continue; | |
} | |
print(" updating ..."); | |
QubitMigrate::bumpTerm($id, $configuration); | |
$connection = Propel::getConnection(); | |
$connection->beginTransaction(); | |
try | |
{ | |
$connection->exec('SET FOREIGN_KEY_CHECKS = 0'); | |
$foreignKeys = QubitMigrate::findForeignKeys( | |
array(QubitObject::TABLE_NAME, QubitTerm::TABLE_NAME), | |
$configuration | |
); | |
foreach ($foreignKeys as $item) | |
{ | |
QubitPdo::modify( | |
"UPDATE $item[table] SET $item[column] = ? WHERE $item[column] = ?", | |
array($id, $term->id) | |
); | |
} | |
$connection->exec('SET FOREIGN_KEY_CHECKS = 1'); | |
} | |
catch (Exception $e) | |
{ | |
$connection->rollback(); | |
throw $e; | |
} | |
$connection->commit(); | |
print(" done!\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment