33
0
mirror of https://github.com/joomla-extensions/patchtester.git synced 2024-11-17 18:45:10 +00:00
patchtester/administrator/components/com_patchtester/PatchTester/Controller/ResetController.php

156 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/**
* Patch testing component for the Joomla! CMS
*
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later
*/
namespace PatchTester\Controller;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
2019-08-28 01:20:49 +00:00
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\Filesystem\File;
use PatchTester\Model\PullModel;
use PatchTester\Model\PullsModel;
use PatchTester\Model\TestsModel;
/**
* Controller class to reset the system state
*
* @since 2.0
*/
class ResetController extends AbstractController
{
/**
* Execute the controller.
*
* @return void Redirects the application
*
* @since 2.0
*/
2020-03-31 18:19:05 +00:00
public function execute(): void
{
try
{
2019-11-09 11:56:52 +00:00
$hasErrors = false;
$revertErrored = false;
$pullModel = new PullModel(null, Factory::getDbo());
$pullsModel = new PullsModel($this->context, null, Factory::getDbo());
$testsModel = new TestsModel(null, Factory::getDbo());
// Check the applied patches in the database first
2020-03-31 18:19:05 +00:00
$appliedPatches = $testsModel->getAppliedPatches();
$params = ComponentHelper::getParams('com_patchtester');
// Decide based on repository settings whether patch will be applied through Github or CIServer
if ((bool) $params->get('ci_switch', 1))
{
// Let's try to cleanly revert all applied patches with ci
2020-03-31 18:19:05 +00:00
foreach ($appliedPatches as $patch)
{
try
{
$pullModel->revertWithCIServer($patch->id);
}
catch (\RuntimeException $e)
{
$revertErrored = true;
}
}
}
else
{
// Let's try to cleanly revert all applied patches
foreach ($appliedPatches as $patch)
{
try
{
$pullModel->revertWithGitHub($patch->id);
}
catch (\RuntimeException $e)
{
$revertErrored = true;
}
}
}
// If we errored out reverting patches, we'll need to truncate the table
if ($revertErrored)
{
try
{
$testsModel->truncateTable();
}
catch (\RuntimeException $e)
{
$hasErrors = true;
$this->getApplication()->enqueueMessage(
Text::sprintf('COM_PATCHTESTER_ERROR_TRUNCATING_PULLS_TABLE', $e->getMessage()), 'error'
);
}
}
// Now truncate the pulls table
try
{
$pullsModel->truncateTable();
}
catch (\RuntimeException $e)
{
$hasErrors = true;
$this->getApplication()->enqueueMessage(
2019-08-28 01:20:49 +00:00
Text::sprintf('COM_PATCHTESTER_ERROR_TRUNCATING_TESTS_TABLE', $e->getMessage()), 'error'
);
}
// Check the backups directory to see if any .txt files remain; clear them if so
2019-08-28 01:20:49 +00:00
$backups = Folder::files(JPATH_COMPONENT . '/backups', '.txt');
if (count($backups))
{
foreach ($backups as $file)
{
2019-08-28 01:20:49 +00:00
if (!File::delete(JPATH_COMPONENT . '/backups/' . $file))
{
$this->getApplication()->enqueueMessage(
2019-08-28 01:20:49 +00:00
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', JPATH_COMPONENT . '/backups/' . $file), 'error'
);
$hasErrors = true;
}
}
}
// Processing completed, inform the user of a success or fail
if ($hasErrors)
{
2019-08-28 01:20:49 +00:00
$msg = Text::sprintf(
'COM_PATCHTESTER_RESET_HAS_ERRORS', JPATH_COMPONENT . '/backups', Factory::getDbo()->replacePrefix('#__patchtester_tests')
);
$type = 'warning';
}
else
{
2019-08-28 01:20:49 +00:00
$msg = Text::_('COM_PATCHTESTER_RESET_OK');
2016-03-28 11:05:10 +00:00
$type = 'notice';
}
}
catch (\Exception $exception)
{
$msg = $exception->getMessage();
$type = 'error';
}
$this->getApplication()->enqueueMessage($msg, $type);
2019-08-28 01:20:49 +00:00
$this->getApplication()->redirect(Route::_('index.php?option=com_patchtester', false));
}
}