Created
April 6, 2021 08:04
-
-
Save jraddaoui/c5495f1050e7a49cc601ff00e236f960 to your computer and use it in GitHub Desktop.
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 | |
const TAXONOMY_ID = 37; | |
echo 'Have you created a backup of the database? [y/N]: '; | |
flush(); | |
ob_flush(); | |
$confirmation = trim(fgets(STDIN)); | |
if ('y' !== $confirmation) { | |
exit(0); | |
} | |
QubitSearch::disable(); | |
$data = sfYaml::load( | |
sfConfig::get('sf_data_dir').'/fixtures/taxonomyTerms.yml' | |
); | |
$allTaxonomies = $data['QubitTaxonomy']; | |
$allTerms = $data['QubitTerm']; | |
$taxonomyKey = array_key_first(array_filter($allTaxonomies, function ($data) { | |
return TAXONOMY_ID == $data['id']; | |
})); | |
if (!$taxonomyKey) { | |
echo "Taxonomy not found.\n"; | |
exit(1); | |
} | |
$terms = array_filter($allTerms, function ($data) use ($taxonomyKey) { | |
return | |
( | |
is_int($data['taxonomy_id']) | |
&& TAXONOMY_ID == $data['taxonomy_id'] | |
) | |
|| ( | |
is_string($data['taxonomy_id']) | |
&& $taxonomyKey == $data['taxonomy_id'] | |
); | |
}); | |
foreach ($terms as $termKey => $termData) { | |
if (!isset($termData['name']['en'])) { | |
echo "Term {$termKey} missing English name.\n"; | |
continue; | |
} | |
$sql = 'SELECT * FROM '.QubitTerm::TABLE_NAME.' t | |
INNER JOIN '.QubitTermI18n::TABLE_NAME." ti | |
WHERE ti.culture='en' | |
AND ti.name=? | |
AND t.taxonomy_id=?"; | |
$params = [$termData['name']['en'], TAXONOMY_ID]; | |
if (isset($termData['id'])) { | |
$sql .= ' AND t.id=?'; | |
$params[] = $termData['id']; | |
} | |
if (false !== QubitPdo::fetchOne($sql, $params)) { | |
echo "Term '{$termData['name']['en']}' already exists.\n"; | |
continue; | |
} | |
echo "Creating '{$termData['name']['en']}' term.\n"; | |
$term = new QubitTerm(); | |
$term->taxonomyId = TAXONOMY_ID; | |
if (isset($termData['id'])) { | |
QubitMigrate::bumpTerm($termData['id'], $this->configuration); | |
$term->id = $termData['id']; | |
} | |
$term->parentId = $termData['parent_id']; | |
if (is_string($term->parentId)) { | |
$term->parentId = $allTerms[$term->parentId]['id']; | |
} | |
$term->sourceCulture = 'en'; | |
if (isset($termData['source_culture'])) { | |
$term->sourceCulture = $termData['source_culture']; | |
} | |
foreach ($termData['name'] as $culture => $name) { | |
$term->setName($name, ['culture' => $culture]); | |
} | |
$term->save(); | |
} | |
echo "Done! The search index hasn't been updated.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment