2011-10-11 13:02:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2013-07-13 02:26:21 +00:00
|
|
|
* @package PatchTester
|
|
|
|
*
|
|
|
|
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 Open Source Matters, Inc. All rights reserved.
|
|
|
|
* @license GNU General Public License version 2 or later
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
|
|
|
/**
|
2013-07-13 02:26:21 +00:00
|
|
|
* Methods supporting a list of pull requests.
|
2011-10-11 13:02:57 +00:00
|
|
|
*
|
2013-07-13 02:26:21 +00:00
|
|
|
* @package PatchTester
|
|
|
|
* @since 1.0
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
|
|
|
class PatchtesterModelPulls extends JModelList
|
|
|
|
{
|
2013-07-13 00:31:00 +00:00
|
|
|
/**
|
|
|
|
* Github object
|
|
|
|
*
|
2013-07-13 02:26:21 +00:00
|
|
|
* @var PTGithub
|
|
|
|
* @since 2.0
|
2013-07-13 00:31:00 +00:00
|
|
|
*/
|
|
|
|
protected $github;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object containing the rate limit data
|
|
|
|
*
|
2013-07-13 02:26:21 +00:00
|
|
|
* @var object
|
|
|
|
* @since 2.0
|
2013-07-13 00:31:00 +00:00
|
|
|
*/
|
|
|
|
protected $rate;
|
2011-10-11 13:02:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2012-06-12 18:42:45 +00:00
|
|
|
* @param array $config An optional associative array of configuration settings.
|
|
|
|
*
|
|
|
|
* @see JController
|
2013-07-13 02:26:21 +00:00
|
|
|
* @since 1.0
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
|
|
|
public function __construct($config = array())
|
|
|
|
{
|
2012-06-12 18:42:45 +00:00
|
|
|
if (empty($config['filter_fields']))
|
|
|
|
{
|
2011-10-11 13:02:57 +00:00
|
|
|
$config['filter_fields'] = array(
|
|
|
|
'id', 'id',
|
|
|
|
'title', 'title',
|
|
|
|
'updated_at', 'updated_at',
|
|
|
|
'user', 'user'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($config);
|
2013-07-13 00:31:00 +00:00
|
|
|
|
|
|
|
// Set up the Github object
|
2013-07-13 01:06:39 +00:00
|
|
|
$params = JComponentHelper::getParams('com_patchtester');
|
|
|
|
|
|
|
|
$options = new JRegistry;
|
|
|
|
|
|
|
|
// Set the username and password if set in the params
|
|
|
|
if ($params->get('gh_user', '') && $params->get('gh_password'))
|
|
|
|
{
|
|
|
|
$options->set('api.username', $params->get('gh_user', ''));
|
|
|
|
$options->set('api.password', $params->get('gh_password', ''));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Display a message about the lowered API limit without credentials
|
|
|
|
JFactory::getApplication()->enqueueMessage(JText::_('COM_PATCHTESTER_NO_CREDENTIALS'), 'notice');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->github = new PTGithub($options);
|
2013-07-13 00:31:00 +00:00
|
|
|
|
|
|
|
// Store the rate data for reuse during this request cycle
|
|
|
|
$this->rate = $this->github->account->getRateLimit()->rate;
|
|
|
|
|
|
|
|
// Check the API rate limit, display a message if over
|
|
|
|
if ($this->rate->remaining == 0)
|
|
|
|
{
|
2013-07-13 01:06:39 +00:00
|
|
|
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_PATCHTESTER_API_LIMIT_LIST', JFactory::getDate($this->rate->reset)), 'notice');
|
2013-07-13 00:31:00 +00:00
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to auto-populate the model state.
|
|
|
|
*
|
2013-07-13 02:26:21 +00:00
|
|
|
* @param string $ordering An optional ordering field.
|
|
|
|
* @param string $direction An optional direction (asc|desc).
|
2011-10-11 13:02:57 +00:00
|
|
|
*
|
2013-07-13 02:26:21 +00:00
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @note Calling getState() in this method will result in recursion.
|
|
|
|
* @since 1.0
|
2011-10-11 13:02:57 +00:00
|
|
|
*/
|
|
|
|
protected function populateState($ordering = null, $direction = null)
|
|
|
|
{
|
2011-10-13 17:58:22 +00:00
|
|
|
// Load the filter state.
|
2012-06-12 18:42:45 +00:00
|
|
|
$search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
|
2011-10-13 17:58:22 +00:00
|
|
|
$this->setState('filter.search', $search);
|
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
$searchId = $this->getUserStateFromRequest($this->context . '.filter.searchid', 'filter_searchid');
|
2011-10-15 07:10:25 +00:00
|
|
|
$this->setState('filter.searchid', $searchId);
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
// Load the parameters.
|
|
|
|
$params = JComponentHelper::getParams('com_patchtester');
|
2011-10-13 17:58:22 +00:00
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
$this->setState('params', $params);
|
|
|
|
$this->setState('github_user', $params->get('org', 'joomla'));
|
|
|
|
$this->setState('github_repo', $params->get('repo', 'joomla-cms'));
|
2011-10-13 17:58:22 +00:00
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
// List state information.
|
|
|
|
parent::populateState('number', 'desc');
|
2012-09-09 01:53:44 +00:00
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
// GitHub's default list limit is 30
|
|
|
|
$this->setState('list.limit', 30);
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a list of applied patches
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2011-10-11 13:02:57 +00:00
|
|
|
public function getAppliedPatches()
|
|
|
|
{
|
2013-07-13 02:26:21 +00:00
|
|
|
$db = $this->getDbo();
|
|
|
|
$query = $db->getQuery(true)
|
|
|
|
->select('*')
|
|
|
|
->from('#__patchtester_tests')
|
|
|
|
->where('applied = 1');
|
|
|
|
|
|
|
|
$db->setQuery($query);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$tests = $db->loadObjectList('pull_id');
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
catch (RuntimeException $e)
|
|
|
|
{
|
|
|
|
$this->setError($e->getMessage());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
|
|
|
* Method to get an array of data items.
|
|
|
|
*
|
|
|
|
* @return mixed An array of data items on success, false on failure.
|
|
|
|
*
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2011-10-11 13:02:57 +00:00
|
|
|
public function getItems()
|
|
|
|
{
|
2012-06-12 18:42:45 +00:00
|
|
|
if ($this->getState('github_user') == '' || $this->getState('github_repo') == '')
|
|
|
|
{
|
2011-10-11 13:02:57 +00:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2011-10-13 17:58:22 +00:00
|
|
|
$this->ordering = $this->getState('list.ordering', 'title');
|
|
|
|
$this->orderDir = $this->getState('list.direction', 'asc');
|
2012-09-09 01:53:44 +00:00
|
|
|
|
2012-06-12 18:42:45 +00:00
|
|
|
try
|
|
|
|
{
|
2013-07-14 19:34:29 +00:00
|
|
|
$cacheFile = JPATH_CACHE . '/patchtester.json';
|
|
|
|
$params = $this->getState('params');
|
|
|
|
|
|
|
|
// Check if caching is enabled
|
|
|
|
if ($params->get('cache', 1) == 1)
|
2011-10-15 01:13:59 +00:00
|
|
|
{
|
2013-07-14 19:34:29 +00:00
|
|
|
// Fetch cache time from component parameters and convert to seconds
|
|
|
|
$cacheTime = $params->get('cache_lifetime', 60);
|
|
|
|
$cacheTime = $cacheTime * 60;
|
2012-06-12 18:42:45 +00:00
|
|
|
|
2013-07-14 19:34:29 +00:00
|
|
|
// Cache files expired?
|
|
|
|
if (!file_exists($cacheFile) || (time() - @filemtime($cacheFile) > $cacheTime))
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2013-07-14 19:34:29 +00:00
|
|
|
// Do a request to the GitHub API for new data
|
|
|
|
$pulls = $this->requestFromGithub();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Render from the cached data
|
|
|
|
$pulls = json_decode(file_get_contents($cacheFile));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No caching, request from GitHub
|
|
|
|
$pulls = $this->requestFromGithub();
|
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
|
2013-07-14 19:34:29 +00:00
|
|
|
return $pulls;
|
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
|
2012-06-12 18:42:45 +00:00
|
|
|
|
2013-07-14 19:34:29 +00:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
|
2013-07-14 19:34:29 +00:00
|
|
|
/**
|
|
|
|
* Method to request new data from GitHub
|
|
|
|
*
|
|
|
|
* @return array Pull request data
|
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
|
|
|
protected function requestFromGithub()
|
|
|
|
{
|
|
|
|
// If over the API limit, we can't build this list
|
|
|
|
if ($this->rate->remaining > 0)
|
|
|
|
{
|
|
|
|
$page = $this->getPagination()->pagesCurrent;
|
|
|
|
$search = $this->getState('filter.search');
|
|
|
|
$searchId = $this->getState('filter.searchid');
|
2012-06-12 18:42:45 +00:00
|
|
|
|
2013-07-14 19:34:29 +00:00
|
|
|
$pulls = $this->github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page);
|
|
|
|
usort($pulls, array($this, 'sortItems'));
|
|
|
|
|
|
|
|
foreach ($pulls as $i => &$pull)
|
|
|
|
{
|
|
|
|
if ($search && false === strpos($pull->title, $search))
|
|
|
|
{
|
|
|
|
unset($pulls[$i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (isset($matches[1]))
|
|
|
|
{
|
|
|
|
$pulls[$i]->joomlacode_issue = (int) $matches[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preg_match('#(http://joomlacode[-\w\./\?\S]+)#', $pull->body, $matches);
|
2012-06-12 18:42:45 +00:00
|
|
|
|
|
|
|
if (isset($matches[1]))
|
|
|
|
{
|
2013-07-14 19:34:29 +00:00
|
|
|
preg_match('#tracker_item_id=([0-9]+)#', $matches[1], $matches);
|
2012-06-12 18:42:45 +00:00
|
|
|
|
|
|
|
if (isset($matches[1]))
|
|
|
|
{
|
2013-07-14 19:34:29 +00:00
|
|
|
$pulls[$i]->joomlacode_issue = (int) $matches[1];
|
2012-06-12 18:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-13 17:58:22 +00:00
|
|
|
}
|
2013-07-14 19:34:29 +00:00
|
|
|
|
|
|
|
// If caching is enabled, save the request data
|
|
|
|
$params = $this->getState('params');
|
|
|
|
|
|
|
|
if ($params->get('cache') == 1)
|
2013-07-13 00:31:00 +00:00
|
|
|
{
|
2013-07-14 19:34:29 +00:00
|
|
|
$data = json_encode($pulls);
|
|
|
|
file_put_contents(JPATH_CACHE . '/patchtester.json', $data);
|
2013-07-13 00:31:00 +00:00
|
|
|
}
|
2012-06-12 18:42:45 +00:00
|
|
|
}
|
2013-07-14 19:34:29 +00:00
|
|
|
else
|
2012-06-12 18:42:45 +00:00
|
|
|
{
|
2013-07-14 19:34:29 +00:00
|
|
|
$pulls = array();
|
2011-10-13 02:13:19 +00:00
|
|
|
}
|
2013-07-14 19:34:29 +00:00
|
|
|
|
|
|
|
return $pulls;
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
|
|
|
* Method to sort the items array
|
|
|
|
*
|
|
|
|
* @param object $a First sort object
|
|
|
|
* @param object $b Second sort object
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2011-10-11 13:02:57 +00:00
|
|
|
public function sortItems($a, $b)
|
|
|
|
{
|
2011-10-13 17:58:22 +00:00
|
|
|
switch ($this->ordering)
|
|
|
|
{
|
|
|
|
case 'title' :
|
|
|
|
return ($this->orderDir == 'asc') ? strcasecmp($a->title, $b->title) : strcasecmp($b->title, $a->title);
|
|
|
|
|
|
|
|
case 'number' :
|
|
|
|
default :
|
2012-06-12 18:42:45 +00:00
|
|
|
return ($this->orderDir == 'asc') ? $b->number < $a->number : $b->number > $a->number;
|
2011-10-13 17:58:22 +00:00
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|
2012-09-09 01:53:44 +00:00
|
|
|
|
2013-07-13 02:26:21 +00:00
|
|
|
/**
|
|
|
|
* Method to get the total number of items for the data set.
|
|
|
|
*
|
|
|
|
* @return integer The total number of items available in the data set.
|
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
|
|
|
public function getTotal()
|
|
|
|
{
|
|
|
|
if ($this->rate->remaining > 0)
|
|
|
|
{
|
|
|
|
return $this->github->repos->get('joomla', 'joomla-cms')->open_issues_count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2011-10-11 13:02:57 +00:00
|
|
|
}
|