Files
MokoSuiteBackup/source/packages/plg_task_mokosuitebackup/src/Extension/MokoSuiteBackupTask.php
T
Jonathan Miller 5a672454ad 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
2026-06-22 09:27:14 -05:00

149 lines
4.6 KiB
PHP

<?php
/**
* @package MokoSuiteBackup
* @subpackage plg_task_mokosuitebackup
* @author Moko Consulting <hello@mokoconsulting.tech>
* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE
*
* Joomla Scheduled Task plugin for MokoSuiteBackup.
*
* Registers a "Run Backup Profile" task type with com_scheduler.
* Admins can create multiple scheduled tasks in System > Scheduled Tasks,
* each pointing to a different backup profile — just like Akeeba Backup Pro.
*/
namespace Joomla\Plugin\Task\MokoSuiteBackup\Extension;
defined('_JEXEC') or die;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Task\Status;
use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
use Joomla\Event\Event;
use Joomla\Event\SubscriberInterface;
final class MokoSuiteBackupTask extends CMSPlugin implements SubscriberInterface
{
use TaskPluginTrait;
protected $autoloadLanguage = true;
/**
* Task map — each entry registers a task type in System > Scheduled Tasks.
*
* The admin can create multiple task instances, each with its own profile_id,
* so different backup profiles run on different schedules.
*/
protected const TASKS_MAP = [
'mokosuitebackup.run_profile' => [
'langConstPrefix' => 'PLG_TASK_MOKOJOOMBACKUP_TASK_RUN_PROFILE',
'method' => 'runBackupProfile',
'form' => 'run_profile',
],
'mokosuitebackup.snapshot' => [
'langConstPrefix' => 'PLG_TASK_MOKOJOOMBACKUP_TASK_RUN_SNAPSHOT',
'method' => 'runContentSnapshot',
'form' => 'run_snapshot',
],
];
public static function getSubscribedEvents(): array
{
return [
'onTaskOptionsList' => 'advertiseRoutines',
'onExecuteTask' => 'standardRoutineHandler',
'onContentPrepareForm' => 'enhanceTaskItemForm',
];
}
/**
* Execute a backup using the profile selected in the scheduled task.
*
* @param ExecuteTaskEvent $event The task execution event
*
* @return int Status::OK on success, Status::KNOCKOUT on failure
*/
private function runBackupProfile(ExecuteTaskEvent $event): int
{
$params = $event->getArgument('params');
$profileId = (int) ($params->profile_id ?? 1);
// Load the backup engine from the component
$engineFile = JPATH_ADMINISTRATOR . '/components/com_mokosuitebackup/src/Engine/BackupEngine.php';
if (!file_exists($engineFile)) {
$this->logTask('MokoSuiteBackup component not installed — cannot run backup.');
return Status::KNOCKOUT;
}
// The autoloader should handle this via namespace, but ensure class is available
if (!class_exists('\\Joomla\\Component\\MokoSuiteBackup\\Administrator\\Engine\\BackupEngine')) {
require_once $engineFile;
}
$engine = new \Joomla\Component\MokoSuiteBackup\Administrator\Engine\BackupEngine();
$result = $engine->run($profileId, 'Scheduled task backup', 'scheduled');
if ($result['success']) {
$this->logTask('Backup complete: ' . $result['message']);
return Status::OK;
}
$this->logTask('Backup failed: ' . $result['message']);
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;
}
}