2011-10-11 13:02:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-06-12 18:42:45 +00:00
|
|
|
* @package PatchTester
|
|
|
|
* @copyright Copyright (C) 2011 Ian MacLennan, Inc. All rights reserved.
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Methods supporting pull requests.
|
|
|
|
*
|
2012-06-12 18:42:45 +00:00
|
|
|
* @package PatchTester
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
2012-09-09 01:53:44 +00:00
|
|
|
class PatchtesterModelPull extends JModelLegacy
|
2011-10-11 13:02:57 +00:00
|
|
|
{
|
2013-07-12 02:03:26 +00:00
|
|
|
/**
|
|
|
|
* @var JHttp
|
|
|
|
*/
|
|
|
|
protected $transport;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request).
|
|
|
|
*
|
|
|
|
* @since 12.2
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function __construct($config = array())
|
|
|
|
{
|
|
|
|
parent::__construct($config);
|
|
|
|
|
|
|
|
// Set up the JHttp object
|
|
|
|
$options = new JRegistry;
|
|
|
|
$options->set('userAgent', 'JPatchTester/1.0');
|
|
|
|
$options->set('timeout', 120);
|
|
|
|
|
|
|
|
$this->transport = JHttpFactory::getHttp($options, 'curl');
|
|
|
|
}
|
|
|
|
|
2011-10-13 16:44:25 +00:00
|
|
|
/**
|
|
|
|
* Method to auto-populate the model state.
|
|
|
|
*
|
|
|
|
* Note. Calling getState in this method will result in recursion.
|
|
|
|
*
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function populateState()
|
|
|
|
{
|
|
|
|
// Load the parameters.
|
|
|
|
$params = JComponentHelper::getParams('com_patchtester');
|
|
|
|
$this->setState('params', $params);
|
2011-10-15 18:47:02 +00:00
|
|
|
$this->setState('github_user', $params->get('org', 'joomla'));
|
|
|
|
$this->setState('github_repo', $params->get('repo', 'joomla-cms'));
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2011-10-13 16:44:25 +00:00
|
|
|
parent::populateState();
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
|
2011-10-11 20:51:12 +00:00
|
|
|
protected function parsePatch($patch)
|
|
|
|
{
|
|
|
|
$state = 0;
|
|
|
|
$files = array();
|
|
|
|
|
2011-10-15 01:13:59 +00:00
|
|
|
$lines = explode("\n", $patch);
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
foreach ($lines AS $line)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
switch ($state)
|
|
|
|
{
|
|
|
|
case 0:
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, 'diff --git') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$state = 1;
|
|
|
|
}
|
|
|
|
$file = new stdClass;
|
|
|
|
$file->action = 'modified';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, 'index') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->index = substr($line, 6);
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, '---') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->old = substr($line, 6);
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, '+++') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->new = substr($line, 6);
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, 'new file mode') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->action = 'added';
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, 'deleted file mode') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$file->action = 'deleted';
|
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (strpos($line, '@@') === 0)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
$state = 0;
|
|
|
|
$files[] = $file;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
2011-10-11 13:02:57 +00:00
|
|
|
public function apply($id)
|
|
|
|
{
|
2013-07-12 01:49:58 +00:00
|
|
|
jimport('joomla.filesystem.file');
|
2011-10-11 13:02:57 +00:00
|
|
|
|
|
|
|
$table = JTable::getInstance('tests', 'PatchTesterTable');
|
2011-10-15 01:13:59 +00:00
|
|
|
$github = new JGithub;
|
2011-10-11 13:02:57 +00:00
|
|
|
$pull = $github->pulls->get($this->getState('github_user'), $this->getState('github_repo'), $id);
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (is_null($pull->head->repo))
|
|
|
|
{
|
2011-10-15 01:13:59 +00:00
|
|
|
throw new Exception(JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
|
2011-10-11 20:51:12 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 02:03:26 +00:00
|
|
|
$patch = $this->transport->get($pull->diff_url)->body;
|
2011-10-15 01:09:27 +00:00
|
|
|
|
2011-10-11 20:51:12 +00:00
|
|
|
$files = $this->parsePatch($patch);
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->old))
|
|
|
|
{
|
2011-10-15 01:13:59 +00:00
|
|
|
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S'), $file->old));
|
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
if ($file->action == 'added' || $file->action == 'modified')
|
|
|
|
{
|
2011-10-11 13:02:57 +00:00
|
|
|
|
|
|
|
// if the backup file already exists, we can't apply the patch
|
2012-06-12 18:42:45 +00:00
|
|
|
if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt'))
|
|
|
|
{
|
2011-10-15 01:13:59 +00:00
|
|
|
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new));
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->old))
|
|
|
|
{
|
2011-10-15 01:13:59 +00:00
|
|
|
throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S'), $file->old));
|
2011-10-11 20:51:12 +00:00
|
|
|
}
|
|
|
|
|
2011-10-15 01:09:27 +00:00
|
|
|
$url = 'https://raw.github.com/' . $pull->head->user->login . '/' . $pull->head->repo->name . '/' .
|
|
|
|
$pull->head->ref . '/' . $file->new;
|
|
|
|
|
2013-07-12 02:03:26 +00:00
|
|
|
$file->body = $this->transport->get($url)->body;
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
|
|
|
|
// at this point, we have ensured that we have all the new files and there are no conflicts
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
foreach ($files as $file)
|
2011-10-11 13:02:57 +00:00
|
|
|
{
|
|
|
|
// we only create a backup if the file already exists
|
2012-06-12 18:42:45 +00:00
|
|
|
if ($file->action == 'deleted' || (file_exists(JPATH_ROOT . '/' . $file->new) && $file->action == 'modified'))
|
|
|
|
{
|
|
|
|
if (!JFile::copy(JPath::clean(JPATH_ROOT . '/' . $file->old), JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'))
|
|
|
|
{
|
2011-10-15 01:13:59 +00:00
|
|
|
throw new Exception(sprintf('Can not copy file %s to %s'
|
2012-06-12 18:42:45 +00:00
|
|
|
, JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'));
|
2011-10-15 01:13:59 +00:00
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 20:51:12 +00:00
|
|
|
switch ($file->action)
|
|
|
|
{
|
|
|
|
case 'modified':
|
|
|
|
case 'added':
|
2012-06-12 18:42:45 +00:00
|
|
|
if (!JFile::write(JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body))
|
|
|
|
{
|
2011-10-15 01:13:59 +00:00
|
|
|
throw new Exception(sprintf('Can not write the file: %s', JPATH_ROOT . '/' . $file->new));
|
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'deleted':
|
2012-06-12 18:42:45 +00:00
|
|
|
if (!JFile::delete(JPATH::clean(JPATH_ROOT . '/' . $file->old)))
|
|
|
|
{
|
2011-10-15 01:13:59 +00:00
|
|
|
throw new Exception(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old));
|
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
break;
|
2011-10-11 16:04:33 +00:00
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
2011-10-15 01:13:59 +00:00
|
|
|
|
2011-10-11 13:02:57 +00:00
|
|
|
$table->pull_id = $pull->number;
|
|
|
|
$table->data = json_encode($files);
|
|
|
|
$table->patched_by = JFactory::getUser()->id;
|
|
|
|
$table->applied = 1;
|
|
|
|
$version = new JVersion;
|
|
|
|
$table->applied_version = $version->getShortVersion();
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (!$table->store())
|
|
|
|
{
|
2011-10-15 01:13:59 +00:00
|
|
|
throw new Exception($table->getError());
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
2011-10-15 01:13:59 +00:00
|
|
|
|
|
|
|
return true;
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function revert($id)
|
|
|
|
{
|
2013-07-12 01:49:58 +00:00
|
|
|
jimport('joomla.filesystem.file');
|
|
|
|
|
2011-10-11 13:02:57 +00:00
|
|
|
$table = JTable::getInstance('tests', 'PatchTesterTable');
|
|
|
|
|
|
|
|
$table->load($id);
|
|
|
|
|
|
|
|
// we don't want to restore files from an older version
|
|
|
|
$version = new JVersion;
|
2012-06-12 18:42:45 +00:00
|
|
|
|
|
|
|
if ($table->applied_version != $version->getShortVersion())
|
|
|
|
{
|
|
|
|
$table->delete();
|
2012-06-12 20:07:26 +00:00
|
|
|
|
|
|
|
return $this;
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$files = json_decode($table->data);
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
if (!$files)
|
|
|
|
{
|
2012-06-12 20:07:26 +00:00
|
|
|
throw new Exception(sprintf(JText::_('%s - Error retrieving table data (%s)')
|
2012-06-12 18:42:45 +00:00
|
|
|
, __METHOD__, htmlentities($table->data)));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
switch ($file->action)
|
|
|
|
{
|
2011-10-11 20:51:12 +00:00
|
|
|
case 'deleted':
|
|
|
|
case 'modified':
|
2012-06-12 18:42:45 +00:00
|
|
|
if (!JFile::copy(
|
|
|
|
JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'
|
|
|
|
, JPATH_ROOT . '/' . $file->old)
|
|
|
|
)
|
|
|
|
{
|
2012-06-12 20:07:26 +00:00
|
|
|
throw new Exception(sprintf(
|
|
|
|
JText::_('Can not copy file %s to %s')
|
2012-06-12 18:42:45 +00:00
|
|
|
, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'
|
|
|
|
, JPATH_ROOT . '/' . $file->old));
|
2011-10-15 01:13:59 +00:00
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
|
|
|
|
if (!JFile::delete(JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'))
|
|
|
|
{
|
2012-06-12 20:07:26 +00:00
|
|
|
throw new Exception(sprintf(
|
|
|
|
JText::_('Can not delete the file: %s')
|
2012-06-12 18:42:45 +00:00
|
|
|
, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt'));
|
2011-10-15 01:13:59 +00:00
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'added':
|
2012-06-12 18:42:45 +00:00
|
|
|
if (!JFile::delete(JPath::clean(JPATH_ROOT . '/' . $file->new)))
|
|
|
|
{
|
2012-06-12 20:07:26 +00:00
|
|
|
throw new Exception(sprintf(
|
|
|
|
JText::_('Can not delete the file: %s')
|
|
|
|
, JPATH_ROOT . '/' . $file->new));
|
2011-10-15 01:13:59 +00:00
|
|
|
}
|
2011-10-11 20:51:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
$table->delete();
|
2011-10-11 13:02:57 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|