diff --git a/administrator/components/com_patchtester/PatchTester/Controller/DisplayController.php b/administrator/components/com_patchtester/PatchTester/Controller/DisplayController.php index 6396d69..63616b9 100644 --- a/administrator/components/com_patchtester/PatchTester/Controller/DisplayController.php +++ b/administrator/components/com_patchtester/PatchTester/Controller/DisplayController.php @@ -95,7 +95,15 @@ class DisplayController extends \JControllerBase // Sanity check - Ensure our classes exist if (!class_exists($viewClass)) { - throw new \RuntimeException(sprintf('The view class for the %1%s view in the %2%s was not found.', $view, $format), 500); + // Try to use a default view + $viewClass = '\\PatchTester\\View\\Default' . ucfirst($format) . 'View'; + + if (!class_exists($viewClass)) + { + throw new \RuntimeException( + sprintf('The view class for the %1$s view in the %2$s was not found.', $view, $format), 500 + ); + } } if (!class_exists($modelClass)) diff --git a/administrator/components/com_patchtester/PatchTester/Controller/FetchController.php b/administrator/components/com_patchtester/PatchTester/Controller/FetchController.php index e8405e5..c3c63d1 100644 --- a/administrator/components/com_patchtester/PatchTester/Controller/FetchController.php +++ b/administrator/components/com_patchtester/PatchTester/Controller/FetchController.php @@ -26,26 +26,50 @@ class FetchController extends DisplayController */ public function execute() { + // We don't want this request to be cached. + header('Pragma: no-cache'); + header('Cache-Control: no-cache'); + header('Expires: -1'); + try { + // Fetch our page from the session + $page = \JFactory::getSession()->get('com_patchtester_fetcher_page', 1); + // TODO - Decouple the model and context? $model = new PullsModel('com_patchtester.fetch', null, \JFactory::getDbo()); // Initialize the state for the model $model->setState($this->initializeState($model)); - $model->requestFromGithub(); - - $msg = \JText::_('COM_PATCHTESTER_FETCH_SUCCESSFUL'); - $type = 'message'; + $status = $model->requestFromGithub($page); } catch (\Exception $e) { - $msg = $e->getMessage(); - $type = 'error'; + $response = new \JResponseJson($e); + + echo json_encode($response); + + $this->getApplication()->close(1); } - $this->getApplication()->enqueueMessage($msg, $type); - $this->getApplication()->redirect(\JRoute::_('index.php?option=com_patchtester', false)); + // Update the UI and session now + if (isset($status['page'])) + { + \JFactory::getSession()->set('com_patchtester_fetcher_page', $status['page']); + $message = \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', $status['page']); + unset($status['page']); + } + else + { + $status['header'] = \JText::_('COM_PATCHTESTER_FETCH_SUCCESSFUL', true); + $message = \JText::_('COM_PATCHTESTER_FETCH_COMPLETE_CLOSE_WINDOW', true); + } + + $response = new \JResponseJson($status, $message, false, true); + + echo json_encode($response); + + $this->getApplication()->close(); } } diff --git a/administrator/components/com_patchtester/PatchTester/Controller/StartfetchController.php b/administrator/components/com_patchtester/PatchTester/Controller/StartfetchController.php new file mode 100644 index 0000000..a492aa8 --- /dev/null +++ b/administrator/components/com_patchtester/PatchTester/Controller/StartfetchController.php @@ -0,0 +1,105 @@ +getApplication()->close(1); + } + + // Make sure we can fetch the data from GitHub - throw an error on < 10 available requests + $github = Helper::initializeGithub(); + $rate = $github->authorization->getRateLimit(); + + // If over the API limit, we can't build this list + if ($rate->resources->core->remaining < 10) + { + $response = new \JResponseJson( + new \Exception( + \JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($rate->resources->core->reset)), + 429 + ) + ); + + echo json_encode($response); + + $this->getApplication()->close(1); + } + + // TODO - Decouple the model and context? + $model = new PullsModel('com_patchtester.fetch', null, \JFactory::getDbo()); + + // Initialize the state for the model + $model->setState($this->initializeState($model)); + + try + { + // Sanity check, ensure there aren't any applied patches + if (count($model->getAppliedPatches()) >= 1) + { + $response = new \JResponseJson(new \Exception(\JText::_('COM_PATCHTESTER_ERROR_APPLIED_PATCHES'), 500)); + + echo json_encode($response); + + $this->getApplication()->close(1); + } + } + catch (\Exception $e) + { + $response = new \JResponseJson($e); + + echo json_encode($response); + + $this->getApplication()->close(1); + } + + // We're able to successfully pull data, prepare our environment + \JFactory::getSession()->set('com_patchtester_fetcher_page', 1); + + $response = new \JResponseJson( + array('complete' => false, 'header' => \JText::_('COM_PATCHTESTER_FETCH_PROCESSING', true)), + \JText::sprintf('COM_PATCHTESTER_FETCH_PAGE_NUMBER', 1), + false, + true + ); + + echo json_encode($response); + + $this->getApplication()->close(); + } +} diff --git a/administrator/components/com_patchtester/PatchTester/Model/FetchModel.php b/administrator/components/com_patchtester/PatchTester/Model/FetchModel.php new file mode 100644 index 0000000..cc4189a --- /dev/null +++ b/administrator/components/com_patchtester/PatchTester/Model/FetchModel.php @@ -0,0 +1,18 @@ +authorization->getRateLimit(); - // If over the API limit, we can't build this list - if ($rate->resources->core->remaining == 0) + // If on page 1, dump the old data + if ($page === 1) { - throw new \RuntimeException( - \JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', \JFactory::getDate($rate->resources->core->reset)) + $this->getDb()->truncateTable('#__patchtester_pulls'); + } + + try + { + // TODO - Option to configure the batch size + $pulls = $github->pulls->getList( + $this->getState()->get('github_user'), $this->getState()->get('github_repo'), 'open', $page, 100 ); } - - // Sanity check, ensure there aren't any applied patches - if (count($this->getAppliedPatches()) >= 1) + catch (\DomainException $e) { - throw new \RuntimeException(\JText::_('COM_PATCHTESTER_ERROR_APPLIED_PATCHES')); + throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_ERROR_GITHUB_FETCH', $e->getMessage())); } - $pulls = array(); - $page = 0; + $count = is_array($pulls) ? count($pulls) : 0; - do + // If there are no pulls to insert then bail, assume we're finished + if ($count === 0 || empty($pulls)) { - $page++; - - try - { - $items = $github->pulls->getList($this->getState()->get('github_user'), $this->getState()->get('github_repo'), 'open', $page, 100); - } - catch (\DomainException $e) - { - throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_ERROR_GITHUB_FETCH', $e->getMessage())); - } - - $count = is_array($items) ? count($items) : 0; - - if ($count) - { - $pulls = array_merge($pulls, $items); - } - } - while ($count); - - // Dump the old data now - $this->getDb()->truncateTable('#__patchtester_pulls'); - - // If there are no pulls to insert then bail - if (empty($pulls)) - { - return; + return array('complete' => true); } $data = array(); @@ -382,6 +361,9 @@ class PullsModel extends \JModelDatabase { throw new \RuntimeException(\JText::sprintf('COM_PATCHTESTER_ERROR_INSERT_DATABASE', $e->getMessage())); } + + // Need to make another request + return array('complete' => false, 'page' => ($page + 1)); } /** diff --git a/administrator/components/com_patchtester/PatchTester/View/Fetch/tmpl/default.php b/administrator/components/com_patchtester/PatchTester/View/Fetch/tmpl/default.php new file mode 100644 index 0000000..b9e8865 --- /dev/null +++ b/administrator/components/com_patchtester/PatchTester/View/Fetch/tmpl/default.php @@ -0,0 +1,20 @@ + + +