From edfce5445fc8800d31c34eab5bb7ef6bbe125381 Mon Sep 17 00:00:00 2001 From: Nikolai Plath Date: Tue, 12 Jun 2012 13:42:45 -0500 Subject: [PATCH] Formatting and fixes --- .../components/com_patchtester/access.xml | 8 +- .../components/com_patchtester/config.xml | 52 ++++---- .../components/com_patchtester/controller.php | 16 +-- .../com_patchtester/controllers/pull.php | 33 +++-- .../com_patchtester/models/pull.php | 123 ++++++++++++------ .../com_patchtester/models/pulls.php | 81 ++++++++---- .../com_patchtester/patchtester.php | 11 +- .../com_patchtester/patchtester.xml | 85 ++++++------ .../sql/install.mysql.utf8.sql | 4 +- .../sql/uninstall.mysql.utf8.sql | 2 +- .../com_patchtester/tables/tests.php | 12 +- .../com_patchtester/views/pulls/view.html.php | 17 +-- patchtester/build.sh | 4 + 13 files changed, 269 insertions(+), 179 deletions(-) diff --git a/administrator/components/com_patchtester/access.xml b/administrator/components/com_patchtester/access.xml index fdd2e4c..2690c6a 100644 --- a/administrator/components/com_patchtester/access.xml +++ b/administrator/components/com_patchtester/access.xml @@ -1,7 +1,7 @@ -
- - -
+
+ + +
diff --git a/administrator/components/com_patchtester/config.xml b/administrator/components/com_patchtester/config.xml index e63f0ff..2b3e164 100644 --- a/administrator/components/com_patchtester/config.xml +++ b/administrator/components/com_patchtester/config.xml @@ -1,34 +1,34 @@ -
+
- + - + -
+
-
+
- -
+ +
diff --git a/administrator/components/com_patchtester/controller.php b/administrator/components/com_patchtester/controller.php index 42963ae..68f8622 100644 --- a/administrator/components/com_patchtester/controller.php +++ b/administrator/components/com_patchtester/controller.php @@ -1,8 +1,8 @@ getModel('pull') - ->apply(JRequest::getVar('pull_id')); + try + { + $this->getModel('pull') + ->apply(JRequest::getVar('pull_id')); $msg = 'Patch successfully applied'; $type = 'message'; - } catch (Exception $e) { + } + catch (Exception $e) + { $msg = $e->getMessage(); $type = 'error'; } @@ -35,14 +38,20 @@ class PatchtesterControllerPull extends JController public function revert() { - $model = $this->getModel('pull'); - if ($model->revert(JRequest::getVar('pull_id'))) { + try + { + $this->getModel('pull') + ->revert(JRequest::getVar('pull_id')); + $msg = 'Patch successfully reverted'; $type = 'message'; - } else { - $msg = 'Patch did not revert'; + } + catch (Exception $e) + { + $msg = $e->getMessage() ?: 'Patch did not revert'; $type = 'error'; } + $this->setRedirect(JRoute::_('index.php?option=com_patchtester&view=pulls', false), $msg, $type); } diff --git a/administrator/components/com_patchtester/models/pull.php b/administrator/components/com_patchtester/models/pull.php index 1e82010..b1948ac 100644 --- a/administrator/components/com_patchtester/models/pull.php +++ b/administrator/components/com_patchtester/models/pull.php @@ -1,8 +1,8 @@ index = substr($line, 6); } - if (strpos($line, '---') === 0) { + if (strpos($line, '---') === 0) + { $file->old = substr($line, 6); } - if (strpos($line, '+++') === 0) { + if (strpos($line, '+++') === 0) + { $file->new = substr($line, 6); } - if (strpos($line, 'new file mode') === 0) { + if (strpos($line, 'new file mode') === 0) + { $file->action = 'added'; } - if (strpos($line, 'deleted file mode') === 0) { + if (strpos($line, 'deleted file mode') === 0) + { $file->action = 'deleted'; } - if (strpos($line, '@@') === 0) { + if (strpos($line, '@@') === 0) + { $state = 0; $files[] = $file; } @@ -84,54 +91,62 @@ class PatchtesterModelPull extends JModel public function apply($id) { - jimport('joomla.client.github'); - jimport('joomla.client.curl'); + //@todo Use the JCurl class + require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/curl.php'; $table = JTable::getInstance('tests', 'PatchTesterTable'); $github = new JGithub; $pull = $github->pulls->get($this->getState('github_user'), $this->getState('github_repo'), $id); - if (is_null($pull->head->repo)) { + if (is_null($pull->head->repo)) + { throw new Exception(JText::_('COM_PATCHTESTER_REPO_IS_GONE')); } - $patch = JCurl::getAdapter($pull->diff_url) - ->fetch()->body; + $patch = PTCurl::getAdapter($pull->diff_url) + ->fetch()->body; $files = $this->parsePatch($patch); - foreach($files AS $file) { - if ($file->action == 'deleted' && ! file_exists(JPATH_ROOT . '/' . $file->old)) { + foreach ($files as $file) + { + if ($file->action == 'deleted' && !file_exists(JPATH_ROOT . '/' . $file->old)) + { throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_DELETED_DOES_NOT_EXIST_S'), $file->old)); } - if ($file->action == 'added' || $file->action == 'modified') { + if ($file->action == 'added' || $file->action == 'modified') + { // if the backup file already exists, we can't apply the patch - if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt')) { + if (file_exists(JPATH_COMPONENT . '/backups/' . md5($file->new) . '.txt')) + { throw new Exception(sprintf(JText::_('COM_PATCHTESTER_CONFLICT_S'), $file->new)); } - if ($file->action == 'modified' && ! file_exists(JPATH_ROOT . '/' . $file->old)) { + if ($file->action == 'modified' && !file_exists(JPATH_ROOT . '/' . $file->old)) + { throw new Exception(sprintf(JText::_('COM_PATCHTESTER_FILE_MODIFIED_DOES_NOT_EXIST_S'), $file->old)); } $url = 'https://raw.github.com/' . $pull->head->user->login . '/' . $pull->head->repo->name . '/' . $pull->head->ref . '/' . $file->new; - $file->body = JCurl::getAdapter($url) - ->fetch()->body; + $file->body = PTCurl::getAdapter($url) + ->fetch()->body; } } // at this point, we have ensured that we have all the new files and there are no conflicts - foreach ($files AS $file) + foreach ($files as $file) { // we only create a backup if the file already exists - 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')) { + 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')) + { throw new Exception(sprintf('Can not copy file %s to %s' - , JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')); + , JPATH_ROOT . '/' . $file->old, JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')); } } @@ -139,13 +154,15 @@ class PatchtesterModelPull extends JModel { case 'modified': case 'added': - if( ! JFile::write(JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body)) { + if (!JFile::write(JPath::clean(JPATH_ROOT . '/' . $file->new), $file->body)) + { throw new Exception(sprintf('Can not write the file: %s', JPATH_ROOT . '/' . $file->new)); } break; case 'deleted': - if( ! JFile::delete(JPATH::clean(JPATH_ROOT . '/' . $file->old))) { + if (!JFile::delete(JPATH::clean(JPATH_ROOT . '/' . $file->old))) + { throw new Exception(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->old)); } break; @@ -159,7 +176,8 @@ class PatchtesterModelPull extends JModel $version = new JVersion; $table->applied_version = $version->getShortVersion(); - if ( ! $table->store()) { + if (!$table->store()) + { throw new Exception($table->getError()); } @@ -174,41 +192,64 @@ class PatchtesterModelPull extends JModel // we don't want to restore files from an older version $version = new JVersion; - if ($table->applied_version != $version->getShortVersion()) { + + if ($table->applied_version != $version->getShortVersion()) + { + /* $table->applied = 0; $table->applied_version = ''; $table->store(); + */ + $table->delete(); return true; } $files = json_decode($table->data); - foreach ($files AS $file) { - switch ($file->action) { + if (!$files) + { + throw new Exception(sprintf('%s - Error retrieving table data (%s)' + , __METHOD__, htmlentities($table->data))); + } + + foreach ($files as $file) + { + switch ($file->action) + { case 'deleted': case 'modified': - if ( ! JFile::copy(JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt', JPATH_ROOT . '/' . $file->old)) { + if (!JFile::copy( + JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt' + , JPATH_ROOT . '/' . $file->old) + ) + { throw new Exception(sprintf('Can not copy file %s to %s' - , JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt' - , JPATH_ROOT . '/' . $file->old)); + , JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt' + , JPATH_ROOT . '/' . $file->old)); } - if ( ! JFile::delete(JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')) { + + if (!JFile::delete(JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')) + { throw new Exception(sprintf('Can not delete the file: %s' - , JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')); + , JPATH_COMPONENT . '/backups/' . md5($file->old) . '.txt')); } break; case 'added': - if ( ! JFile::delete(JPath::clean(JPATH_ROOT . '/' . $file->new))) { + if (!JFile::delete(JPath::clean(JPATH_ROOT . '/' . $file->new))) + { throw new Exception(sprintf('Can not delete the file: %s', JPATH_ROOT . '/' . $file->new)); } break; } } + /* $table->applied_version = ''; $table->applied = 0; $table->store(); + */ + $table->delete(); return true; } diff --git a/administrator/components/com_patchtester/models/pulls.php b/administrator/components/com_patchtester/models/pulls.php index a7279c5..05b6fb1 100644 --- a/administrator/components/com_patchtester/models/pulls.php +++ b/administrator/components/com_patchtester/models/pulls.php @@ -1,8 +1,8 @@ getUserStateFromRequest($this->context.'.filter.search', 'filter_search'); + $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search'); $this->setState('filter.search', $search); - $searchId = $this->getUserStateFromRequest($this->context.'.filter.searchid', 'filter_searchid'); + $searchId = $this->getUserStateFromRequest($this->context . '.filter.searchid', 'filter_searchid'); $this->setState('filter.searchid', $searchId); // Load the parameters. @@ -73,9 +75,10 @@ class PatchtesterModelPulls extends JModelList * different modules that might need different sets of data or different * ordering requirements. * - * @param string $id A prefix for the store id. - * @return string A store id. - * @since 1.6 + * @param string $id A prefix for the store id. + * + * @return string A store id. + * @since 1.6 */ protected function getStoreId($id = '') { @@ -86,7 +89,7 @@ class PatchtesterModelPulls extends JModelList { $query = $this->_db->getQuery(true); $query->select('*'); - $query->from('#__tests'); + $query->from('#__patchtester_tests'); $query->where('applied = 1'); $this->_db->setQuery($query); @@ -98,7 +101,8 @@ class PatchtesterModelPulls extends JModelList { jimport('joomla.client.github'); - if ($this->getState('github_user') == '' || $this->getState('github_repo') == '') { + if ($this->getState('github_user') == '' || $this->getState('github_repo') == '') + { return array(); } @@ -107,28 +111,57 @@ class PatchtesterModelPulls extends JModelList $search = $this->getState('filter.search'); $searchId = $this->getState('filter.searchid'); - try { + try + { $github = new JGithub(); - $pulls = $github->pulls->getAll($this->getState('github_user'), $this->getState('github_repo')); + $pulls = $github->pulls->getList($this->getState('github_user'), $this->getState('github_repo')); usort($pulls, array($this, 'sortItems')); - foreach ($pulls AS $i => &$pull) + foreach ($pulls as $i => &$pull) { - if($search && false === strpos($pull->title, $search)) { + if ($search && false === strpos($pull->title, $search)) + { unset($pulls[$i]); continue; } - if($searchId && $pull->number != $searchId) { + + if ($searchId && $pull->number != $searchId) + { unset($pulls[$i]); continue; } + + // Try to find a joomlacode issue number + $pulls[$i]->joomlacode_issue = 0; + $matches = array(); + preg_match('#\[\#([0-9]+)\]#', $pull->title, $matches); - $pull->joomlacode_issue = isset($matches[1]) ? $matches[1] : 0; + + if (isset($matches[1])) + { + $pulls[$i]->joomlacode_issue = (int) $matches[1]; + } + else + { + preg_match('#(http://joomlacode[-\w\./\?\S]+)#', $pull->body, $matches); + + if (isset($matches[1])) + { + preg_match('#tracker_item_id=([0-9]+)#', $matches[1], $matches); + + if (isset($matches[1])) + { + $pulls[$i]->joomlacode_issue = (int) $matches[1]; + } + } + } } return $pulls; - } catch (Exception $e) { + } + catch (Exception $e) + { JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error'); return array(); @@ -144,7 +177,7 @@ class PatchtesterModelPulls extends JModelList case 'number' : default : - return ($this->orderDir == 'asc') ? $b->number < $a->number : $b->number > $a->number; + return ($this->orderDir == 'asc') ? $b->number < $a->number : $b->number > $a->number; } } } diff --git a/administrator/components/com_patchtester/patchtester.php b/administrator/components/com_patchtester/patchtester.php index c8a73a3..1bdd04a 100644 --- a/administrator/components/com_patchtester/patchtester.php +++ b/administrator/components/com_patchtester/patchtester.php @@ -1,8 +1,8 @@ authorise('core.manage', 'com_patchtester')) { +if (!JFactory::getUser()->authorise('core.manage', 'com_patchtester')) +{ return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR')); } // Include dependencies jimport('joomla.application.component.controller'); -$controller = JController::getInstance('PatchTester'); +$controller = JController::getInstance('PatchTester'); $controller->execute(JRequest::getCmd('task')); $controller->redirect(); diff --git a/administrator/components/com_patchtester/patchtester.xml b/administrator/components/com_patchtester/patchtester.xml index 4f61fc6..7772393 100644 --- a/administrator/components/com_patchtester/patchtester.xml +++ b/administrator/components/com_patchtester/patchtester.xml @@ -1,51 +1,52 @@ - com_patchtester - Ian MacLennan - October 2011 - (C) 2011 Ian MacLennan. All rights reserved. - - GNU General Public License version 2 or later; see - LICENSE.txt - ianlenmac@gmail.com - http://github.com/ianmacl - 1.0alpha - COM_PATCHTESTER_XML_DESCRIPTION + com_patchtester + Ian MacLennan + October 2011 + (C) 2011 Ian MacLennan. All rights reserved. + + GNU General Public License version 2 or later; see + LICENSE.txt + + ianlenmac@gmail.com + http://github.com/ianmacl + 1.0alpha + COM_PATCHTESTER_XML_DESCRIPTION - - - sql/install.mysql.utf8.sql - + + + sql/install.mysql.utf8.sql + - - sql/uninstall.mysql.utf8.sql - - + + sql/uninstall.mysql.utf8.sql + + - - patchtester.php - - - com_patchtester - - access.xml - config.xml - controller.php - patchtester.php - assets - controllers - models - sql - tables - views - language - - + + patchtester.php + + + com_patchtester + + access.xml + config.xml + controller.php + patchtester.php + assets + controllers + models + sql + tables + views + language + + - - - https://raw.github.com/ianmacl/patchtester/master/updates/patchtester.xml - + + + https://raw.github.com/ianmacl/patchtester/master/updates/patchtester.xml + diff --git a/administrator/components/com_patchtester/sql/install.mysql.utf8.sql b/administrator/components/com_patchtester/sql/install.mysql.utf8.sql index 373db1c..51d8357 100644 --- a/administrator/components/com_patchtester/sql/install.mysql.utf8.sql +++ b/administrator/components/com_patchtester/sql/install.mysql.utf8.sql @@ -1,7 +1,7 @@ -CREATE TABLE IF NOT EXISTS `#__tests` ( +CREATE TABLE IF NOT EXISTS `#__patchtester_tests` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pull_id` int(11) NOT NULL, - `data` text NOT NULL, + `data` longtext NOT NULL, `patched_by` int(11) NOT NULL, `applied` int(11) NOT NULL, `applied_version` varchar(25) NOT NULL, diff --git a/administrator/components/com_patchtester/sql/uninstall.mysql.utf8.sql b/administrator/components/com_patchtester/sql/uninstall.mysql.utf8.sql index 390b128..9740617 100644 --- a/administrator/components/com_patchtester/sql/uninstall.mysql.utf8.sql +++ b/administrator/components/com_patchtester/sql/uninstall.mysql.utf8.sql @@ -1 +1 @@ -DROP TABLE IF EXISTS `#__tests` +DROP TABLE IF EXISTS `#__patchtester_tests` diff --git a/administrator/components/com_patchtester/tables/tests.php b/administrator/components/com_patchtester/tables/tests.php index 9cac601..f9cc080 100644 --- a/administrator/components/com_patchtester/tables/tests.php +++ b/administrator/components/com_patchtester/tables/tests.php @@ -1,8 +1,8 @@ state = $this->get('State'); - $this->items = $this->get('Items'); - $this->patches = $this->get('AppliedPatches'); + $this->state = $this->get('State'); + $this->items = $this->get('Items'); + $this->patches = $this->get('AppliedPatches'); // Check for errors. - if (count($errors = $this->get('Errors'))) { + if (count($errors = $this->get('Errors'))) + { JError::raiseError(500, implode("\n", $errors)); return false; } diff --git a/patchtester/build.sh b/patchtester/build.sh index 3870ee4..968765b 100755 --- a/patchtester/build.sh +++ b/patchtester/build.sh @@ -1,3 +1,4 @@ +#!/bin/sh rm -rf site rm -rf admin rm -rf ../patchtester.tar.bz2 @@ -6,6 +7,7 @@ cp -r ../components/com_patchtester site rm -rf admin/backups/*.txt mv admin/patchtester.xml patchtester.xml tar jcf ../com_patchtester.tar.bz2 site admin patchtester.xml +tar zcf ../com_patchtester.tar.gz site admin patchtester.xml rm -rf github mkdir github @@ -15,5 +17,7 @@ cp ../libraries/joomla/client/curl.php github cp -r ../libraries/joomla/client/github github cp github.xml github tar jcf ../file_github.tar.bz2 github/* +tar zcf ../file_github.tar.gz github/* tar jcf ../pkg_patchtester.tar.bz2 pkg_patchtester.xml ../com_patchtester.tar.bz2 ../file_github.tar.bz2 +tar zcf ../pkg_patchtester.tar.gz pkg_patchtester_gz.xml ../com_patchtester.tar.gz ../file_github.tar.gz