2016-03-26 22:08:01 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Patch testing component for the Joomla! CMS
|
|
|
|
*
|
2018-09-01 14:32:23 +00:00
|
|
|
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2018 Open Source Matters, Inc. All rights reserved.
|
2016-03-26 22:08:01 +00:00
|
|
|
* @license GNU General Public License version 2 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PatchTester\Controller;
|
|
|
|
|
2017-08-17 23:22:01 +00:00
|
|
|
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;
|
2019-09-12 07:04:05 +00:00
|
|
|
use PatchTester\Helper;
|
2016-03-26 22:08:01 +00:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$hasErrors = false;
|
|
|
|
|
2017-08-17 23:22:01 +00:00
|
|
|
$pullModel = new PullModel(null, Factory::getDbo());
|
|
|
|
$pullsModel = new PullsModel($this->context, null, Factory::getDbo());
|
|
|
|
$testsModel = new TestsModel(null, Factory::getDbo());
|
2016-03-26 22:08:01 +00:00
|
|
|
|
2019-09-12 07:04:05 +00:00
|
|
|
// Get the CIServer Registry
|
|
|
|
$ciSettings = Helper::initializeCISettings();
|
|
|
|
|
|
|
|
// Get PatchChain as array, remove any EOL set by php
|
|
|
|
$patchChainPath = $ciSettings->get('folder.backups') . '/' . $ciSettings->get('zip.chain.name');
|
2019-09-12 08:52:34 +00:00
|
|
|
$patchChain = array_reverse(json_decode(file_get_contents($patchChainPath)));
|
2019-09-12 07:04:05 +00:00
|
|
|
|
2016-03-26 22:08:01 +00:00
|
|
|
// Check the applied patches in the database first
|
|
|
|
$appliedPatches = $testsModel->getAppliedPatches();
|
|
|
|
|
2019-09-12 07:04:05 +00:00
|
|
|
if (count($patchChain) && count($appliedPatches))
|
|
|
|
{
|
|
|
|
// Get only the pull_id and remove all occurrences with patchChain
|
2019-09-12 08:52:34 +00:00
|
|
|
$appliedPatches = array_map(function($patch) { return $patch->id; }, $appliedPatches);
|
|
|
|
$patchChain = array_map(function($patch) { return $patch->id; }, $patchChain);
|
2019-09-12 07:04:05 +00:00
|
|
|
$appliedPatches = array_diff($appliedPatches, $patchChain);
|
|
|
|
}
|
|
|
|
|
2016-03-26 22:08:01 +00:00
|
|
|
if (count($appliedPatches))
|
|
|
|
{
|
|
|
|
$revertErrored = false;
|
|
|
|
|
|
|
|
// Let's try to cleanly revert all applied patches
|
|
|
|
foreach ($appliedPatches as $patch)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-09-12 07:04:05 +00:00
|
|
|
$pullModel->revertWithGitHub($patch);
|
2016-03-26 22:08:01 +00:00
|
|
|
}
|
|
|
|
catch (\RuntimeException $e)
|
|
|
|
{
|
|
|
|
$revertErrored = true;
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 07:04:05 +00:00
|
|
|
}
|
2016-03-26 22:08:01 +00:00
|
|
|
|
2019-09-12 07:04:05 +00:00
|
|
|
if (count($patchChain))
|
|
|
|
{
|
|
|
|
$revertErrored = false;
|
|
|
|
|
|
|
|
// Let's try to cleanly revert all applied patches with ci
|
|
|
|
foreach ($patchChain as $patch)
|
2016-03-26 22:08:01 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-09-12 07:04:05 +00:00
|
|
|
$pullModel->revertWithCIServer($patch);
|
2016-03-26 22:08:01 +00:00
|
|
|
}
|
|
|
|
catch (\RuntimeException $e)
|
|
|
|
{
|
2019-09-12 07:04:05 +00:00
|
|
|
$revertErrored = true;
|
2016-03-26 22:08:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 07:04:05 +00:00
|
|
|
// 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'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 22:08:01 +00:00
|
|
|
// 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'
|
2016-03-26 22:08:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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');
|
2016-03-26 22:08:01 +00:00
|
|
|
|
|
|
|
if (count($backups))
|
|
|
|
{
|
|
|
|
foreach ($backups as $file)
|
|
|
|
{
|
2019-08-28 01:20:49 +00:00
|
|
|
if (!File::delete(JPATH_COMPONENT . '/backups/' . $file))
|
2016-03-26 22:08:01 +00:00
|
|
|
{
|
|
|
|
$this->getApplication()->enqueueMessage(
|
2019-08-28 01:20:49 +00:00
|
|
|
Text::sprintf('COM_PATCHTESTER_ERROR_CANNOT_DELETE_FILE', JPATH_COMPONENT . '/backups/' . $file), 'error'
|
2016-03-26 22:08:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$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(
|
2017-08-17 23:22:01 +00:00
|
|
|
'COM_PATCHTESTER_RESET_HAS_ERRORS', JPATH_COMPONENT . '/backups', Factory::getDbo()->replacePrefix('#__patchtester_tests')
|
2016-03-26 22:08:01 +00:00
|
|
|
);
|
|
|
|
$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';
|
2016-03-26 22:08:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
$msg = $e->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));
|
2016-03-26 22:08:01 +00:00
|
|
|
}
|
|
|
|
}
|