mirror of
https://github.com/joomla-extensions/patchtester.git
synced 2024-12-23 11:29:00 +00:00
Implement caching mechanism
This commit is contained in:
parent
cacb1c20ee
commit
0238672232
@ -29,7 +29,7 @@
|
|||||||
<option value="0">JNO</option>
|
<option value="0">JNO</option>
|
||||||
</field>
|
</field>
|
||||||
|
|
||||||
<field name="cache_lifetime" type="password" default="60"
|
<field name="cache_lifetime" type="text" default="60"
|
||||||
description="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_DESC"
|
description="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_DESC"
|
||||||
label="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_LABEL"
|
label="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_LABEL"
|
||||||
/>
|
/>
|
||||||
|
@ -23,4 +23,32 @@ class PatchTesterController extends JControllerLegacy
|
|||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
protected $default_view = 'pulls';
|
protected $default_view = 'pulls';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to purge the cache
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @since 2.0
|
||||||
|
*/
|
||||||
|
public function purge()
|
||||||
|
{
|
||||||
|
// Check for request forgeries
|
||||||
|
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
|
||||||
|
|
||||||
|
jimport('joomla.filesystem.file');
|
||||||
|
|
||||||
|
if (file_exists(JPATH_CACHE . '/patchtester.json') && !JFile::delete(JPATH_CACHE . '/patchtester.json'))
|
||||||
|
{
|
||||||
|
$msg = JText::_('COM_PATCHTESTER_PURGE_FAIL');
|
||||||
|
$msgType = 'error';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$msg = JText::_('COM_PATCHTESTER_PURGE_SUCCESS');
|
||||||
|
$msgType = 'message';
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setRedirect('index.php?option=com_patchtester&view=pulls', $msg, $msgType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,9 @@ COM_PATCHTESTER_NO_CREDENTIALS="No user credentials are saved, this will allow o
|
|||||||
COM_PATCHTESTER_NOT_APPLIED="Not Applied"
|
COM_PATCHTESTER_NOT_APPLIED="Not Applied"
|
||||||
COM_PATCHTESTER_OPEN_IN_GITHUB="Open in GitHub"
|
COM_PATCHTESTER_OPEN_IN_GITHUB="Open in GitHub"
|
||||||
COM_PATCHTESTER_OPEN_IN_JOOMLACODE="Open in JoomlaCode"
|
COM_PATCHTESTER_OPEN_IN_JOOMLACODE="Open in JoomlaCode"
|
||||||
|
COM_PATCHTESTER_PURGE_CACHE="Purge Cache"
|
||||||
|
COM_PATCHTESTER_PURGE_FAIL="Could not purge the cache"
|
||||||
|
COM_PATCHTESTER_PURGE_SUCCESS="Cache purged successfully"
|
||||||
COM_PATCHTESTER_REPO_IS_GONE="The patch could not be applied because the repository is missing"
|
COM_PATCHTESTER_REPO_IS_GONE="The patch could not be applied because the repository is missing"
|
||||||
COM_PATCHTESTER_REVERT_OK="Patch successfully reverted"
|
COM_PATCHTESTER_REVERT_OK="Patch successfully reverted"
|
||||||
COM_PATCHTESTER_REVERT_PATCH="Revert Patch"
|
COM_PATCHTESTER_REVERT_PATCH="Revert Patch"
|
||||||
|
@ -164,64 +164,35 @@ class PatchtesterModelPulls extends JModelList
|
|||||||
|
|
||||||
$this->ordering = $this->getState('list.ordering', 'title');
|
$this->ordering = $this->getState('list.ordering', 'title');
|
||||||
$this->orderDir = $this->getState('list.direction', 'asc');
|
$this->orderDir = $this->getState('list.direction', 'asc');
|
||||||
$search = $this->getState('filter.search');
|
|
||||||
$searchId = $this->getState('filter.searchid');
|
|
||||||
|
|
||||||
$page = $this->getPagination()->pagesCurrent;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// If over the API limit, we can't build this list
|
$cacheFile = JPATH_CACHE . '/patchtester.json';
|
||||||
// TODO - Cache the request data in case of API limiting
|
$params = $this->getState('params');
|
||||||
if ($this->rate->remaining > 0)
|
|
||||||
|
// Check if caching is enabled
|
||||||
|
if ($params->get('cache', 1) == 1)
|
||||||
{
|
{
|
||||||
$pulls = $this->github->pulls->getList($this->getState('github_user'), $this->getState('github_repo'), 'open', $page);
|
// Fetch cache time from component parameters and convert to seconds
|
||||||
usort($pulls, array($this, 'sortItems'));
|
$cacheTime = $params->get('cache_lifetime', 60);
|
||||||
|
$cacheTime = $cacheTime * 60;
|
||||||
|
|
||||||
foreach ($pulls as $i => &$pull)
|
// Cache files expired?
|
||||||
|
if (!file_exists($cacheFile) || (time() - @filemtime($cacheFile) > $cacheTime))
|
||||||
{
|
{
|
||||||
if ($search && false === strpos($pull->title, $search))
|
// Do a request to the GitHub API for new data
|
||||||
{
|
$pulls = $this->requestFromGithub();
|
||||||
unset($pulls[$i]);
|
}
|
||||||
continue;
|
else
|
||||||
}
|
{
|
||||||
|
// Render from the cached data
|
||||||
if ($searchId && $pull->number != $searchId)
|
$pulls = json_decode(file_get_contents($cacheFile));
|
||||||
{
|
|
||||||
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);
|
|
||||||
|
|
||||||
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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$pulls = array();
|
// No caching, request from GitHub
|
||||||
|
$pulls = $this->requestFromGithub();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $pulls;
|
return $pulls;
|
||||||
@ -234,6 +205,83 @@ class PatchtesterModelPulls extends JModelList
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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');
|
||||||
|
|
||||||
|
$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);
|
||||||
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If caching is enabled, save the request data
|
||||||
|
$params = $this->getState('params');
|
||||||
|
|
||||||
|
if ($params->get('cache') == 1)
|
||||||
|
{
|
||||||
|
$data = json_encode($pulls);
|
||||||
|
file_put_contents(JPATH_CACHE . '/patchtester.json', $data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$pulls = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $pulls;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to sort the items array
|
* Method to sort the items array
|
||||||
*
|
*
|
||||||
|
@ -117,6 +117,7 @@ class PatchtesterViewPulls extends JViewLegacy
|
|||||||
protected function addToolbar()
|
protected function addToolbar()
|
||||||
{
|
{
|
||||||
JToolBarHelper::title(JText::_('COM_PATCHTESTER'), 'patchtester');
|
JToolBarHelper::title(JText::_('COM_PATCHTESTER'), 'patchtester');
|
||||||
|
JToolbarHelper::custom('purge', 'delete.png', 'delete_f2.png', 'COM_PATCHTESTER_PURGE_CACHE', false);
|
||||||
JToolBarHelper::preferences('com_patchtester');
|
JToolBarHelper::preferences('com_patchtester');
|
||||||
|
|
||||||
JFactory::getDocument()->addStyleDeclaration(
|
JFactory::getDocument()->addStyleDeclaration(
|
||||||
|
Loading…
Reference in New Issue
Block a user