diff --git a/lib/task/taxonomy/taxonomyPurgeTask.class.php b/lib/task/taxonomy/taxonomyPurgeTask.class.php new file mode 100644 index 0000000000..ec967f4067 --- /dev/null +++ b/lib/task/taxonomy/taxonomyPurgeTask.class.php @@ -0,0 +1,149 @@ +. + */ + +/** + * Purge taxonomy of terms. + * + * @author Mike Cantelon + */ +class taxonomyPurgeTask extends arBaseTask +{ + /** + * @see sfTask + * + * @param mixed $arguments + * @param mixed $options + */ + public function execute($arguments = [], $options = []) + { + parent::execute($arguments, $options); + + // Determine ID of taxonomy to purge + $taxonomyId = $this->determineTaxonomyId($options); + + // Prevent purging of taxonomies that aren't editable + if (in_array($taxonomyId, QubitTaxonomy::$lockedTaxonomies)) { + throw new sfException('Taxonomy is not editable.'); + } + + // Fetch terms in taxonomy + $criteria = new Criteria(); + $criteria->add(QubitTerm::TAXONOMY_ID, $taxonomyId); + + // Delete terms, maintaining count of deleted terms + $termCount = 0; + foreach (QubitTerm::get($criteria) as $term) { + $this->log( + sprintf('Deleting "%s" (ID %d)...', $term->getName(['sourceCulture' => true]), $term->id) + ); + + $term->delete(); + + ++$termCount; + } + + $this->log(sprintf('Done: deleted %d terms.', $termCount)); + } + + /** + * @see sfBaseTask + */ + protected function configure() + { + $this->addOptions([ + new sfCommandOption( + 'application', + null, + sfCommandOption::PARAMETER_OPTIONAL, + 'The application name', + 'qubit' + ), + new sfCommandOption( + 'env', + null, + sfCommandOption::PARAMETER_REQUIRED, + 'The environment', + 'cli' + ), + new sfCommandOption( + 'connection', + null, + sfCommandOption::PARAMETER_REQUIRED, + 'The connection name', + 'propel' + ), + + new sfCommandOption( + 'taxonomy-id', + null, + sfCommandOption::PARAMETER_OPTIONAL, + 'ID of taxonomy' + ), + new sfCommandOption( + 'taxonomy-name', + null, + sfCommandOption::PARAMETER_OPTIONAL, + 'Name of taxonomy' + ), + new sfCommandOption( + 'taxonomy-name-culture', + null, + sfCommandOption::PARAMETER_OPTIONAL, + 'Culture to use for taxonomy name lookup' + ), + ]); + + $this->namespace = 'taxonomy'; + $this->name = 'purge'; + $this->briefDescription = 'Purge taxonomy.'; + $this->detailedDescription = <<<'EOF' +Purge taxonomiy of terms. +EOF; + } + + private function determineTaxonomyId($options) + { + if (ctype_digit($options['taxonomy-id'])) { + $criteria = new Criteria(); + $criteria->add(QubitTaxonomy::ID, $options['taxonomy-id']); + + if (null === QubitTaxonomy::getOne($criteria)) { + throw new sfException('Invalid taxonomy-id.'); + } + + return $options['taxonomy-id']; + } + if (isset($options['taxonomy-name'])) { + $culture = (isset($options['taxonomy-name-culture'])) ? $options['taxonomy-name-culture'] : 'en'; + + $criteria = new Criteria(); + + $criteria->add(QubitTaxonomyI18n::NAME, $options['taxonomy-name']); + $criteria->add(QubitTaxonomyI18n::CULTURE, $culture); + + if (null === $taxonomy = QubitTaxonomyI18n::getOne($criteria)) { + throw new sfException('Invalid taxonomy-name and/or taxonomy-name-culture.'); + } + + return $taxonomy->id; + } + + throw new sfException('Either the taxonomy-id or taxonomy-name must be used to specifiy a taxonomy.'); + } +}