View source
<?php
const PROGRESS_AGE = 86400;
function progress_initialize_progress($name, $message) {
progress_remove_progress($name);
$current = microtime(TRUE);
return db_insert('progress')
->fields([
'name' => $name,
'progress' => 0,
'message' => $message,
'start_stamp' => $current,
'current_stamp' => $current,
])
->execute();
}
function progress_set_progress($name, $message, $progress) {
$current = microtime(TRUE);
$result = db_update('progress')
->fields([
'progress' => $progress,
'message' => $message,
'current_stamp' => $current,
])
->condition('name', $name)
->execute();
if (\Drupal::moduleHandler()
->moduleExists('nodejs')) {
$start_stamp = db_query("SELECT start_stamp FROM {progress} WHERE name = :name", [
':name' => $name,
])
->fetchField();
$object = (object) [
'name' => $name,
'progress' => $progress,
'message' => $message,
'current_stamp' => $current,
'start_stamp' => $start_stamp,
];
$message = (object) [
'channel' => 'progress',
'data' => (object) [
'action' => 'setProgress',
'progress' => $object,
'timestamp' => microtime(TRUE),
],
'callback' => 'nodejsProgress',
];
\Drupal::moduleHandler()
->alter('progress_message', $message);
nodejs_send_content_channel_message($message);
}
return $result;
}
function progress_set_progress_start($name, $start) {
return db_update('progress')
->fields([
'start_stamp' => $start,
])
->condition('name', $name)
->execute();
}
function progress_set_intervalled_progress($name, $message, $progress, $interval = NULL) {
static $set = [];
if (isset($interval, $set[$name])) {
if ($set[$name] + $interval > microtime(TRUE)) {
return TRUE;
}
}
$result = progress_set_progress($name, $message, $progress);
$set[$name] = microtime(TRUE);
return $result;
}
function progress_get_progress($name) {
$result = db_query("SELECT name, progress, message, start_stamp, end_stamp, current_stamp FROM {progress} WHERE name = :name", [
':name' => $name,
])
->fetchObject();
if ($result) {
$result->start = $result->start_stamp;
$result->end = $result->end_stamp;
$result->current = $result->current_stamp;
}
return $result;
}
function progress_end_progress($name, $message) {
$current = microtime(TRUE);
return db_update('progress')
->fields([
'progress' => 1,
'message' => $message,
'end_stamp' => $current,
])
->condition('name', $name)
->execute();
}
function progress_remove_progress($name) {
return db_delete('progress')
->condition('name', $name)
->execute();
}
function progress_get_progresses() {
$result = db_select('progress', 'p')
->fields('p', [
'name',
'progress',
'message',
'start_stamp',
'end_stamp',
'current_stamp',
])
->execute();
$progresses = [];
foreach ($result as $progress) {
$progress->start = $progress->start_stamp;
$progress->end = $progress->end_stamp;
$progress->current = $progress->current_stamp;
$progresses[$progress->name] = $progress;
}
return $progresses;
}
function progress_estimate_completion($progress) {
if ($progress->progress) {
$progress->estimate = $progress->start + 1 / $progress->progress * ($progress->current - $progress->start);
}
else {
$progress->estimate = NULL;
}
return $progress->estimate;
}
function progress_cron() {
return db_delete('progress')
->condition('end_stamp', 0, '>')
->condition('end_stamp', time() - \Drupal::config('progress.settings')
->get('progress_age'), '<')
->execute();
}
function progress_cronapi($op, $job = NULL) {
switch ($op) {
case 'list':
return [
'progress_cron' => t('Cleanup old progresses'),
];
}
}