Skip to content

Commit

Permalink
Force updates on resources from manifest (#3179)
Browse files Browse the repository at this point in the history
* Added command dkan-force-resources-update to force updates on items from manifest.
* Fix coding standards.
  • Loading branch information
dharizza authored Aug 14, 2020
1 parent 56524ab commit e20e557
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
51 changes: 51 additions & 0 deletions modules/dkan/dkan_periodic_updates/dkan_periodic_updates.drush.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* @file
* Code for dkan_periodic_updates drush commands.
*/

/**
* Implements hook_drush_command().
*/
function dkan_periodic_updates_drush_command() {
$commands['dkan-force-resources-update'] = array(
'description' => 'Force resources from manifest to be updated.',
'aliases' => array('dfru'),
);

return $commands;
}

/**
* Drush command logic.
*/
function drush_dkan_periodic_updates_dkan_force_resources_update() {
$active = variable_get('dkan_periodic_updates_status');
if ($active) {
$items = [];
$file = variable_get('dkan_periodic_updates_manifest');
$file = file_load($file);
// Get items from manifest.
if ($file) {
$handle = fopen($file->uri, 'r');
$headers = fgetcsv($handle, 0, ',');

while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$resource_data = array_combine($headers, $data);
$items[$resource_data['resource_id']]['file_url'] = $resource_data['file_url'];
$datastore = $resource_data['import_to_datastore'] === 'Y' ? TRUE : FALSE;
$items[$resource_data['resource_id']]['datastore'] = $datastore;
}
fclose($handle);
}
else {
watchdog('dkan_periodic_updates', 'No manifest found.');
return [];
}

if (!empty($items)) {
dkan_periodic_updates_execute_update($items);
}
}
}
44 changes: 30 additions & 14 deletions modules/dkan/dkan_periodic_updates/dkan_periodic_updates.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
/**
* Implements hook_permission().
*/
function dkan_periodic_updates_permission()
{
function dkan_periodic_updates_permission() {
return [
'administer periodic updates' => [
'title' => t('administer periodic updates')
'title' => t('administer periodic updates'),
],
];
}
Expand Down Expand Up @@ -42,7 +41,6 @@ function dkan_periodic_updates_menu() {
return $items;
}


/**
* Page callback for admin/dkan/periodic-updates.
*/
Expand Down Expand Up @@ -196,6 +194,9 @@ function dkan_periodic_updates_cron() {
}
}

/**
* Get items that need to be updated.
*/
function dkan_periodic_updates_get_items_to_update($file) {
$file = file_load($file);
if ($file) {
Expand All @@ -217,11 +218,13 @@ function dkan_periodic_updates_get_items_to_update($file) {
$to_update[$resource_data['resource_id']]['file_url'] = $resource_data['file_url'];
}
break;

case 'weekly':
if ($time_passed->days >= 7) {
$to_update[$resource_data['resource_id']]['file_url'] = $resource_data['file_url'];
}
break;

default:
if ($time_passed->days >= 1) {
$to_update[$resource_data['resource_id']]['file_url'] = $resource_data['file_url'];
Expand All @@ -243,7 +246,8 @@ function dkan_periodic_updates_get_items_to_update($file) {
}
fclose($handle);
return $to_update;
} else {
}
else {
watchdog('dkan_periodic_updates', 'No manifest found.');
return [];
}
Expand All @@ -262,7 +266,8 @@ function dkan_periodic_updates_execute_update($resources) {
if (empty($nids)) {
watchdog('dkan_periodic_updates', 'There is no node with UUID !uuid.', ['!uuid' => $uuid], WATCHDOG_WARNING);
variable_set('dkan_periodic_updates_message_' . $uuid, 'No node found for the UUID specified.');
} else {
}
else {
foreach ($nids as $nid) {
$node = node_load($nid);
$updated = FALSE;
Expand Down Expand Up @@ -302,7 +307,8 @@ function dkan_periodic_updates_execute_update($resources) {
try {
_dkan_datastore_resource_drop($node->nid);
$import = _dkan_datastore_resource_import($node->nid);
} catch (\Exception $e) {
}
catch (\Exception $e) {
$message = 'Error importing to the datastore: ' . htmlspecialchars($e->getMessage());
watchdog('error', $message, [], WATCHDOG_ERROR);
variable_set('dkan_periodic_updates_message_' . $uuid, $message);
Expand All @@ -312,7 +318,8 @@ function dkan_periodic_updates_execute_update($resources) {
variable_del('dkan_periodic_updates_message_' . $uuid);
}
}
} else {
}
else {
watchdog('dkan_periodic_updates', 'Node with nid !nid not found.', ['!nid' => $nid], WATCHDOG_WARNING);
variable_set('dkan_periodic_updates_message_' . $uuid, 'Node with nid ' . $nid . ' was not found.');
}
Expand All @@ -329,15 +336,22 @@ function dkan_periodic_updates_state() {
$active = variable_get('dkan_periodic_updates_status');
$output = [];
$output['info'] = [
'#markup' => '<p>' . t('The updates are evaluated on every cron run. If a resource from your manifest hasn\'t been updated yet, it may be updated on the next cron run.') . '</p>',
'#markup' => '<p>' . t("The updates are evaluated on every cron run. If a resource from your manifest hasn't been updated yet, it may be updated on the next cron run.") . "</p>",
];
if ($active) {
$file = variable_get('dkan_periodic_updates_manifest');
$file = file_load($file);
if ($file) {
$handle = fopen($file->uri, 'r');
$headers = fgetcsv($handle, 0, ',');
$table_headers = [t('Destination Resource ID'), t('Source File URL'), t('Update frequency'), t('Status'), t('Last update'), t('Import to datastore')];
$table_headers = [
t('Destination Resource ID'),
t('Source File URL'),
t('Update frequency'),
t('Status'),
t('Last update'),
t('Import to datastore'),
];

while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$resource_data = array_combine($headers, $data);
Expand All @@ -346,7 +360,7 @@ function dkan_periodic_updates_state() {
if (empty($last_updated)) {
$last_updated = ' - ';
if (empty($status)) {
$status = t('Resource hasn\'t been updated.');
$status = t("Resource hasn't been updated.");
}
}
else {
Expand All @@ -366,6 +380,7 @@ function dkan_periodic_updates_state() {
case 'weekly':
$frequency = $resource_data['frequency'];
break;

default:
$frequency = 'daily';
}
Expand All @@ -377,7 +392,8 @@ function dkan_periodic_updates_state() {
'#header' => $table_headers,
'#rows' => $table_rows,
];
} else {
}
else {
$output['state'] = [
'#markup' => '<p class="alert alert-warning">' . t('No manifest was found.') . '</p>',
];
Expand All @@ -394,7 +410,7 @@ function dkan_periodic_updates_state() {

/**
* Helper function to get file contents.
*/
*/
function dkan_periodic_updates_curl_get_contents($url, $path) {
$url = preg_replace('/ /', '%20', $url);
$fp = fopen($path, 'w');
Expand Down Expand Up @@ -447,7 +463,7 @@ function dkan_periodic_updates_create_file($file_url, $download = FALSE, $nid =
$files = file_load_multiple(array(), array('uri' => $file_url));
$file = reset($files);
if (!$file) {
$file = file_save((object)[
$file = file_save((object) [
'filename' => drupal_basename($file_url),
'uri' => $file_url,
'status' => FILE_STATUS_PERMANENT,
Expand Down

0 comments on commit e20e557

Please sign in to comment.