mirror of
https://github.com/joomla-extensions/patchtester.git
synced 2024-12-23 03:19:00 +00:00
Implement caching mechanism
This commit is contained in:
parent
cacb1c20ee
commit
0238672232
@ -29,7 +29,7 @@
|
||||
<option value="0">JNO</option>
|
||||
</field>
|
||||
|
||||
<field name="cache_lifetime" type="password" default="60"
|
||||
<field name="cache_lifetime" type="text" default="60"
|
||||
description="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_DESC"
|
||||
label="COM_PATCHTESTER_FIELD_CACHE_LIFETIME_LABEL"
|
||||
/>
|
||||
|
@ -23,4 +23,32 @@ class PatchTesterController extends JControllerLegacy
|
||||
* @since 1.0
|
||||
*/
|
||||
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_OPEN_IN_GITHUB="Open in GitHub"
|
||||
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_REVERT_OK="Patch successfully reverted"
|
||||
COM_PATCHTESTER_REVERT_PATCH="Revert Patch"
|
||||
|
@ -164,64 +164,35 @@ class PatchtesterModelPulls extends JModelList
|
||||
|
||||
$this->ordering = $this->getState('list.ordering', 'title');
|
||||
$this->orderDir = $this->getState('list.direction', 'asc');
|
||||
$search = $this->getState('filter.search');
|
||||
$searchId = $this->getState('filter.searchid');
|
||||
|
||||
$page = $this->getPagination()->pagesCurrent;
|
||||
|
||||
try
|
||||
{
|
||||
// If over the API limit, we can't build this list
|
||||
// TODO - Cache the request data in case of API limiting
|
||||
if ($this->rate->remaining > 0)
|
||||
$cacheFile = JPATH_CACHE . '/patchtester.json';
|
||||
$params = $this->getState('params');
|
||||
|
||||
// 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);
|
||||
usort($pulls, array($this, 'sortItems'));
|
||||
// Fetch cache time from component parameters and convert to seconds
|
||||
$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))
|
||||
{
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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
|
||||
{
|
||||
$pulls = array();
|
||||
// No caching, request from GitHub
|
||||
$pulls = $this->requestFromGithub();
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
|
@ -117,6 +117,7 @@ class PatchtesterViewPulls extends JViewLegacy
|
||||
protected function addToolbar()
|
||||
{
|
||||
JToolBarHelper::title(JText::_('COM_PATCHTESTER'), 'patchtester');
|
||||
JToolbarHelper::custom('purge', 'delete.png', 'delete_f2.png', 'COM_PATCHTESTER_PURGE_CACHE', false);
|
||||
JToolBarHelper::preferences('com_patchtester');
|
||||
|
||||
JFactory::getDocument()->addStyleDeclaration(
|
||||
|
Loading…
Reference in New Issue
Block a user