feat: scheduled task for automated content snapshots (#56)
Add mokosuitebackup.snapshot task type for com_scheduler with params for content_types and description_format ([date]/[datetime] placeholders). Closes #56
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
* Task form: configure content snapshot parameters.
|
||||
* This form appears in System > Scheduled Tasks when creating a
|
||||
* "MokoSuiteBackup: Run Content Snapshot" task.
|
||||
-->
|
||||
<form>
|
||||
<fieldset name="run_snapshot">
|
||||
<field name="content_types" type="checkboxes" label="Content Types" default="articles,categories,modules">
|
||||
<option value="articles">Articles</option>
|
||||
<option value="categories">Categories</option>
|
||||
<option value="modules">Modules</option>
|
||||
</field>
|
||||
<field name="description_format" type="text" label="Description Format" default="[date] Scheduled snapshot" hint="Use [date], [datetime] placeholders" />
|
||||
</fieldset>
|
||||
</form>
|
||||
@@ -43,6 +43,11 @@ final class MokoSuiteBackupTask extends CMSPlugin implements SubscriberInterface
|
||||
'method' => 'runBackupProfile',
|
||||
'form' => 'run_profile',
|
||||
],
|
||||
'mokosuitebackup.snapshot' => [
|
||||
'langConstPrefix' => 'PLG_TASK_MOKOJOOMBACKUP_TASK_RUN_SNAPSHOT',
|
||||
'method' => 'runContentSnapshot',
|
||||
'form' => 'run_snapshot',
|
||||
],
|
||||
];
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
@@ -93,4 +98,51 @@ final class MokoSuiteBackupTask extends CMSPlugin implements SubscriberInterface
|
||||
|
||||
return Status::KNOCKOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a content snapshot using the configured content types.
|
||||
*
|
||||
* @param ExecuteTaskEvent $event The task execution event
|
||||
*
|
||||
* @return int Status::OK on success, Status::KNOCKOUT on failure
|
||||
*/
|
||||
private function runContentSnapshot(ExecuteTaskEvent $event): int
|
||||
{
|
||||
$params = $event->getArgument('params');
|
||||
$contentTypes = (array) ($params->content_types ?? ['articles', 'categories', 'modules']);
|
||||
$descFormat = (string) ($params->description_format ?? '[date] Scheduled snapshot');
|
||||
|
||||
// Resolve placeholders in the description
|
||||
$description = str_replace(
|
||||
['[date]', '[datetime]'],
|
||||
[date('Y-m-d'), date('Y-m-d H:i:s')],
|
||||
$descFormat
|
||||
);
|
||||
|
||||
// Load the snapshot engine from the component
|
||||
$engineFile = JPATH_ADMINISTRATOR . '/components/com_mokosuitebackup/src/Engine/SnapshotEngine.php';
|
||||
|
||||
if (!file_exists($engineFile)) {
|
||||
$this->logTask('MokoSuiteBackup component not installed — cannot create snapshot.');
|
||||
|
||||
return Status::KNOCKOUT;
|
||||
}
|
||||
|
||||
if (!class_exists('\\Joomla\\Component\\MokoSuiteBackup\\Administrator\\Engine\\SnapshotEngine')) {
|
||||
require_once $engineFile;
|
||||
}
|
||||
|
||||
$engine = new \Joomla\Component\MokoSuiteBackup\Administrator\Engine\SnapshotEngine();
|
||||
$result = $engine->create($contentTypes, $description);
|
||||
|
||||
if ($result['success']) {
|
||||
$this->logTask('Snapshot complete: ' . $result['message']);
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
$this->logTask('Snapshot failed: ' . $result['message']);
|
||||
|
||||
return Status::KNOCKOUT;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user