Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update IO LODs when normalizing LODs, refs #10698 #545

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 45 additions & 37 deletions lib/task/taxonomy/taxonomyNormalizeTask.class.php
Expand Up @@ -24,7 +24,7 @@
* @subpackage task
* @author Mike Cantelon <mike@artefactual.com>
*/
class taxonomyNormalizeTask extends sfBaseTask
class taxonomyNormalizeTask extends arBaseTask
{
protected function configure()
{
Expand Down Expand Up @@ -65,21 +65,23 @@ protected function configure()

protected function execute($arguments = array(), $options = array())
{
sfContext::createInstance($this->configuration);
parent::execute($arguments, $options);

$this->log("Normalizing for '". $options['culture'] ."' culture...");

// look up taxonomy ID using name
$taxonomyId = $this->getTaxonomyIdByName($arguments['taxonomy-name'], $options['culture']);
if (!$taxonomyId)
// Look up taxonomy ID using name
$this->taxonomyId = $this->getTaxonomyIdByName($arguments['taxonomy-name'], $options['culture']);
if (!$this->taxonomyId)
{
throw new sfException("A taxonomy named '". $arguments['taxonomy-name'] ."' not found for culture '". $options['culture'] ."'.");
}

// determine taxonomy term usage then normalize
$this->log("Normalizing for '". $options['culture'] ."' culture...");

// Determine taxonomy term usage then normalize
$names = array();
$this->populateTaxonomyNameUsage($names, $taxonomyId, $options['culture']);
$this->populateTaxonomyNameUsage($names, $options['culture']);
$this->normalizeTaxonomy($names);

$this->log("Please, rebuild the search index if changes have been made.");
}

protected function getTaxonomyIdByName($name, $culture)
Expand All @@ -90,36 +92,41 @@ protected function getTaxonomyIdByName($name, $culture)

$statement = QubitFlatfileImport::sqlQuery($sql, array($culture, $name));

if($object = $statement->fetch(PDO::FETCH_OBJ))
if ($object = $statement->fetch(PDO::FETCH_OBJ))
{
return $object->id;
} else {
}
else
{
return false;
}
}

protected function populateTaxonomyNameUsage(&$names, $taxonomyId, $culture)
protected function populateTaxonomyNameUsage(&$names, $culture)
{
$sql = "SELECT t.id, i.name FROM term t \r
INNER JOIN term_i18n i ON t.id=i.id \r
WHERE t.taxonomy_id=? AND i.culture=? \r
$sql = "SELECT t.id, i.name FROM term t
INNER JOIN term_i18n i ON t.id=i.id
WHERE t.taxonomy_id=:id AND i.culture=:culture
ORDER BY t.id";

$statement = QubitFlatfileImport::sqlQuery($sql, array($taxonomyId, $culture));
$params = array(':id' => $this->taxonomyId, ':culture' => $culture);

while($object = $statement->fetch(PDO::FETCH_OBJ))
$terms = QubitPdo::fetchAll($sql, $params, array('fetchMode' => PDO::FETCH_OBJ));

foreach ($terms as $term)
{
if (!isset($names[$object->name]))
if (!isset($names[$term->name]))
{
$names[$object->name] = array();
$names[$term->name] = array();
}
array_push($names[$object->name], $object->id);

array_push($names[$term->name], $term->id);
}
}

protected function normalizeTaxonomy($names)
{
foreach($names as $name => $usage)
foreach ($names as $name => $usage)
{
if (count($usage) > 1)
{
Expand All @@ -134,27 +141,28 @@ protected function normalizeTaxonomyTerm($name, $usage)

$this->log("Normalizing terms with name '". $name ."'...");

// cycle through usage and change to point to selected term
if (count($usage))
// Cycle through usage and change to point to selected term
foreach ($usage as $id)
{
// delete now unused terms
foreach($usage as $id)
{
$this->log("Changing object term relations to term ". $id ." to ". $selected_id .".");

$sql = "UPDATE object_term_relation \r
SET term_id=? WHERE term_id=?";
$this->log("Changing object term relations from term ". $id ." to ". $selected_id .".");

$statement = QubitFlatfileImport::sqlQuery($sql, array($selected_id, $id));
$sql = "UPDATE object_term_relation SET term_id=:newId WHERE term_id=:oldId";
$params = array(':newId' => $selected_id, ':oldId' => $id);
QubitPdo::modify($sql, $params);

$this->log("Deleting term ID ". $id .".");
if ($this->taxonomyId == QubitTaxonomy::LEVEL_OF_DESCRIPTION_ID)
{
$this->log("Changing level of descriptions from term ". $id ." to ". $selected_id .".");

// delete taxonomy term
$term = QubitTerm::getById($id);
$term->delete();
$sql = "UPDATE information_object SET level_of_description_id=:newId WHERE level_of_description_id=:oldId";
QubitPdo::modify($sql, $params);
}
} else {
print "Already normalized.\n";

$this->log("Deleting term ID ". $id .".");

// Delete taxonomy term
$term = QubitTerm::getById($id);
$term->delete();
}
}
}