Skip to content

Commit

Permalink
Bulk unpublish/archive harvested datasets (#3737)
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder authored Jan 21, 2022
1 parent df9ed61 commit d206040
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 120 deletions.
149 changes: 85 additions & 64 deletions modules/harvest/src/Commands/HarvestCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public function __construct(Service $service, LoggerChannelInterface $logger) {
* List available harvests.
*
* @command dkan:harvest:list
* @aliases dkan-harvest:list
* @deprecated dkan-harvest:list is deprecated and will be removed in a future Dkan release. Use dkan:harvest:list instead.
*
* @usage dkan:harvest:list
* List available harvests.
Expand All @@ -70,8 +68,6 @@ function ($id) {
*
* @command dkan:harvest:register
* @usage dkan-harvest:register '{"identifier":"example","extract":{"type":"\\Harvest\\ETL\\Extract\\DataJson","uri":"https://source/data.json"},"transforms":[],"load":{"type":"\\Drupal\\harvest\\Load\\Dataset"}}'
* @aliases dkan-harvest:register
* @deprecated dkan-harvest:register is deprecated and will be removed in a future Dkan release. Use dkan:harvest:register instead.
*/
public function register($harvest_plan) {
try {
Expand All @@ -90,8 +86,6 @@ public function register($harvest_plan) {
* Deregister a harvest.
*
* @command dkan:harvest:deregister
* @aliases dkan-harvest:deregister
* @deprecated dkan-harvest:deregister is deprecated and will be removed in a future Dkan release. Use dkan:harvest:deregister instead.
*/
public function deregister($id) {
try {
Expand All @@ -113,8 +107,6 @@ public function deregister($id) {
* The harvest id.
*
* @command dkan:harvest:run
* @aliases dkan-harvest:run
* @deprecated dkan-harvest:run is deprecated and will be removed in a future Dkan release. Use dkan:harvest:run instead.
*
* @usage dkan:harvest:run
* Runs a harvest.
Expand All @@ -136,8 +128,6 @@ public function run($id) {
* Run all pending harvests.
*
* @command dkan:harvest:run-all
* @aliases dkan-harvest:run-all
* @deprecated dkan-harvest:run-all is deprecated and will be removed in a future Dkan release. Use dkan:harvest:run-all instead.
*
* @usage dkan:harvest:run-all
* Runs all pending harvests.
Expand All @@ -155,65 +145,94 @@ public function runAll() {
/**
* Give information about a previous harvest run.
*
* @param string $id
* @param string $harvestId
* The harvest id.
* @param string $run_id
* @param string $runId
* The run's id.
*
* @command dkan:harvest:info
* @aliases dkan-harvest:info
* @deprecated dkan-harvest:info is deprecated and will be removed in a future Dkan release. Use dkan:harvest:info instead.
*/
public function info($id, $run_id = NULL) {
$run_ids = [];

if (!isset($run_id)) {
$run_ids = $this->harvestService
->getAllHarvestRunInfo($id);
}
else {
$run_ids = [$run_id];
}
public function info($harvestId, $runId = NULL) {
$this->validateHarvestId($harvestId);
$runIds = $runId ? [$runId] : $this->harvestService->getAllHarvestRunInfo($harvestId);

$run_infos = [];
foreach ($run_ids as $run_id) {
$run = $this->harvestService
->getHarvestRunInfo($id, $run_id);
foreach ($runIds as $id) {
$run = $this->harvestService->getHarvestRunInfo($harvestId, $id);
$result = json_decode($run, TRUE);

$run_infos[] = [$run_id, $result];
$runs[] = [$id, $result];
}

$this->renderHarvestRunsInfo($run_infos);
$this->renderHarvestRunsInfo($runs ?? []);
}

/**
* Revert a harvest, i.e. remove all of its harvested entities.
*
* @param string $id
* @param string $harvestId
* The source to revert.
*
* @command dkan:harvest:revert
* @aliases dkan-harvest:revert
* @deprecated dkan-harvest:revert is deprecated and will be removed in a future Dkan release. Use dkan:harvest:revert instead.
*
* @usage dkan:harvest:revert
* Removes harvested entities.
*/
public function revert($id) {
public function revert($harvestId) {
$this->validateHarvestId($harvestId);
$result = $this->harvestService->revertHarvest($harvestId);
(new ConsoleOutput())->write("{$result} items reverted for the '{$harvestId}' harvest plan." . PHP_EOL);
}

$result = $this->harvestService
->revertHarvest($id);
/**
* Archive all harvested datasets for a single harvest.
*
* @param string $harvestId
* The source to archive harvests for.
*
* @command dkan:harvest:archive
*
* @usage dkan:harvest:archive
* Archives harvested entities.
*/
public function archive($harvestId) {
$this->validateHarvestId($harvestId);
$result = $this->harvestService->archive($harvestId);
if (empty($result)) {
(new ConsoleOutput())->write("No items available to archive for the '{$harvestId}' harvest plan." . PHP_EOL);
}
foreach ($result as $id) {
(new ConsoleOutput())->write("Archived dataset {$id} from harvest '{$harvestId}'." . PHP_EOL);
}
}

(new ConsoleOutput())->write("{$result} items reverted for the '{$id}' harvest plan." . PHP_EOL);
/**
* Archive all harvested datasets for a single harvest.
*
* @param string $harvestId
* The source to archive harvests for.
*
* @command dkan:harvest:publish
*
* @usage dkan:harvest:publish
* Publishes harvested entities.
*/
public function publish($harvestId) {
$this->validateHarvestId($harvestId);
$result = $this->harvestService->publish($harvestId);
if (empty($result)) {
(new ConsoleOutput())->write("No items available to publish for the '{$harvestId}' harvest plan." . PHP_EOL);
}
foreach ($result as $id) {
(new ConsoleOutput())->write("Published dataset {$id} from harvest '{$harvestId}'." . PHP_EOL);
}
}

/**
* Show status of of a particular harvest run.
*
* @param string $harvest_id
* @param string $harvestId
* The id of the harvest source.
* @param string $run_id
* @param string $runId
* The run's id. Optional. Show the status for the latest run if not
* provided.
*
Expand All @@ -222,44 +241,37 @@ public function revert($id) {
* @usage dkan:harvest:status
* test 1599157120
*/
public function status($harvest_id, $run_id = NULL) {
// Validate the harvest id.
$harvest_id_all = $this->harvestService->getAllHarvestIds();

if (array_search($harvest_id, $harvest_id_all) === FALSE) {
(new ConsoleOutput())->writeln("<error>harvest id $harvest_id not found.</error>");
return DrushCommands::EXIT_FAILURE;
}
public function status($harvestId, $runId = NULL) {
$this->validateHarvestId($harvestId);

// No run_id provided, get the latest run_id.
// Validate run_id.
$run_id_all = $this->harvestService->getAllHarvestRunInfo($harvest_id);
$allRunIds = $this->harvestService->getAllHarvestRunInfo($harvestId);

if (empty($run_id_all)) {
(new ConsoleOutput())->writeln("<error>No Run IDs found for harvest id $harvest_id</error>");
if (empty($allRunIds)) {
(new ConsoleOutput())->writeln("<error>No Run IDs found for harvest id $harvestId</error>");
return DrushCommands::EXIT_FAILURE;
}

if (empty($run_id)) {
if (empty($runId)) {
// Get the last run_id from the array.
$run_id = end($run_id_all);
reset($run_id_all);
$runId = end($allRunIds);
reset($allRunIds);
}

if (array_search($run_id, $run_id_all) === FALSE) {
(new ConsoleOutput())->writeln("<error>Run ID $run_id not found for harvest id $harvest_id</error>");
if (array_search($runId, $allRunIds) === FALSE) {
(new ConsoleOutput())->writeln("<error>Run ID $runId not found for harvest id $harvestId</error>");
return DrushCommands::EXIT_FAILURE;
}

$run = $this->harvestService
->getHarvestRunInfo($harvest_id, $run_id);
$run = $this->harvestService->getHarvestRunInfo($harvestId, $runId);

if (empty($run)) {
(new ConsoleOutput())->writeln("<error>No status found for harvest id $harvest_id and run id $run_id</error>");
(new ConsoleOutput())->writeln("<error>No status found for harvest id $harvestId and run id $runId</error>");
return DrushCommands::EXIT_FAILURE;
}

$this->renderStatusTable($harvest_id, $run_id, json_decode($run, TRUE));
$this->renderStatusTable($harvestId, $runId, json_decode($run, TRUE));
}

/**
Expand All @@ -275,11 +287,7 @@ public function status($harvest_id, $run_id = NULL) {
* @alias dkan:harvest:orphan
*/
public function orphanDatasets(string $harvestId) : int {

if (!in_array($harvestId, $this->harvestService->getAllHarvestIds())) {
$this->logger()->error("Harvest id {$harvestId} not found.");
return DrushCommands::EXIT_FAILURE;
}
$this->validateHarvestId($harvestId);

try {
$orphans = $this->harvestService->getOrphanIdsFromCompleteHarvest($harvestId);
Expand All @@ -296,4 +304,17 @@ public function orphanDatasets(string $harvestId) : int {
}
}

/**
* Throw error if Harvest ID does not exist.
*
* @param string $harvestId
* The Harvest ID.
*/
private function validateHarvestId($harvestId) {
if (!in_array($harvestId, $this->harvestService->getAllHarvestIds())) {
$this->logger()->error("Harvest id {$harvestId} not found.");
return DrushCommands::EXIT_FAILURE;
}
}

}
Loading

0 comments on commit d206040

Please sign in to comment.