Since the language can be independent of the config now, it needs to be a part of the cache key
Since the language can be independent of the config now, it needs to be a part of the cache key

<?php <?php
/** /**
* GitPHP ControllerBase * GitPHP ControllerBase
* *
* Base class that all controllers extend * Base class that all controllers extend
* *
* @author Christopher Han <xiphux@gmail.com * @author Christopher Han <xiphux@gmail.com
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
   
/** /**
* ControllerBase class * ControllerBase class
* *
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
* @abstract * @abstract
*/ */
abstract class GitPHP_ControllerBase abstract class GitPHP_ControllerBase
{ {
   
/** /**
* tpl * tpl
* *
* Smarty instance * Smarty instance
* *
* @access protected * @access protected
*/ */
protected $tpl; protected $tpl;
   
/** /**
* project * project
* *
* Current project * Current project
* *
* @access protected * @access protected
*/ */
protected $project; protected $project;
   
/** /**
* params * params
* *
* Parameters * Parameters
* *
* @access protected * @access protected
*/ */
protected $params = array(); protected $params = array();
   
/** /**
* headers * headers
* *
* Headers * Headers
* *
* @access protected * @access protected
*/ */
protected $headers = array(); protected $headers = array();
   
/** /**
* __construct * __construct
* *
* Constructor * Constructor
* *
* @access public * @access public
* @return mixed controller object * @return mixed controller object
* @throws Exception on invalid project * @throws Exception on invalid project
*/ */
public function __construct() public function __construct()
{ {
require_once(GitPHP_Config::GetInstance()->GetValue('smarty_prefix', 'lib/smarty/libs/') . 'Smarty.class.php'); require_once(GitPHP_Config::GetInstance()->GetValue('smarty_prefix', 'lib/smarty/libs/') . 'Smarty.class.php');
$this->tpl = new Smarty; $this->tpl = new Smarty;
$this->tpl->plugins_dir[] = GITPHP_INCLUDEDIR . 'smartyplugins'; $this->tpl->plugins_dir[] = GITPHP_INCLUDEDIR . 'smartyplugins';
   
if (GitPHP_Config::GetInstance()->GetValue('cache', false)) { if (GitPHP_Config::GetInstance()->GetValue('cache', false)) {
$this->tpl->caching = 2; $this->tpl->caching = 2;
if (GitPHP_Config::GetInstance()->HasKey('cachelifetime')) { if (GitPHP_Config::GetInstance()->HasKey('cachelifetime')) {
$this->tpl->cache_lifetime = GitPHP_Config::GetInstance()->GetValue('cachelifetime'); $this->tpl->cache_lifetime = GitPHP_Config::GetInstance()->GetValue('cachelifetime');
} }
} }
   
if (isset($_GET['p'])) { if (isset($_GET['p'])) {
$this->project = GitPHP_ProjectList::GetInstance()->GetProject(str_replace(chr(0), '', $_GET['p'])); $this->project = GitPHP_ProjectList::GetInstance()->GetProject(str_replace(chr(0), '', $_GET['p']));
if (!$this->project) { if (!$this->project) {
throw new GitPHP_MessageException(sprintf(GitPHP_Resource::GetInstance()->translate('Invalid project %1$s'), $_GET['p']), true); throw new GitPHP_MessageException(sprintf(GitPHP_Resource::GetInstance()->translate('Invalid project %1$s'), $_GET['p']), true);
} }
} }
   
if (isset($_GET['s'])) if (isset($_GET['s']))
$this->params['search'] = $_GET['s']; $this->params['search'] = $_GET['s'];
if (isset($_GET['st'])) if (isset($_GET['st']))
$this->params['searchtype'] = $_GET['st']; $this->params['searchtype'] = $_GET['st'];
   
$this->ReadQuery(); $this->ReadQuery();
} }
   
/** /**
* GetTemplate * GetTemplate
* *
* Gets the template for this controller * Gets the template for this controller
* *
* @access protected * @access protected
* @abstract * @abstract
* @return string template filename * @return string template filename
*/ */
protected abstract function GetTemplate(); protected abstract function GetTemplate();
   
/** /**
* GetCacheKey * GetCacheKey
* *
* Gets the cache key for this controller * Gets the cache key for this controller
* *
* @access protected * @access protected
* @abstract * @abstract
* @return string cache key * @return string cache key
*/ */
protected abstract function GetCacheKey(); protected abstract function GetCacheKey();
   
/** /**
* GetCacheKeyPrefix * GetCacheKeyPrefix
* *
* Get the prefix for all cache keys * Get the prefix for all cache keys
* *
* @access private * @access private
* @return string cache key prefix * @return string cache key prefix
*/ */
private function GetCacheKeyPrefix() private function GetCacheKeyPrefix()
{ {
$cacheKeyPrefix = ''; $cacheKeyPrefix = GitPHP_Resource::GetLocale();
   
$projList = GitPHP_ProjectList::GetInstance(); $projList = GitPHP_ProjectList::GetInstance();
if ($projList) { if ($projList) {
$cacheKeyPrefix = sha1(serialize($projList->GetConfig())); $cacheKeyPrefix .= '|' . sha1(serialize($projList->GetConfig()));
unset($projList); unset($projList);
} }
if ($this->project) { if ($this->project) {
$cacheKeyPrefix .= '|' . sha1($this->project->GetProject()); $cacheKeyPrefix .= '|' . sha1($this->project->GetProject());
} }
return $cacheKeyPrefix; return $cacheKeyPrefix;
} }
   
/** /**
* GetFullCacheKey * GetFullCacheKey
* *
* Get the full cache key * Get the full cache key
* *
* @access protected * @access protected
* @return string full cache key * @return string full cache key
*/ */
protected function GetFullCacheKey() protected function GetFullCacheKey()
{ {
$cacheKey = $this->GetCacheKeyPrefix(); $cacheKey = $this->GetCacheKeyPrefix();
   
$subCacheKey = $this->GetCacheKey(); $subCacheKey = $this->GetCacheKey();
   
if (!empty($subCacheKey)) if (!empty($subCacheKey))
$cacheKey .= '|' . $subCacheKey; $cacheKey .= '|' . $subCacheKey;
   
return $cacheKey; return $cacheKey;
} }
   
/** /**
* GetName * GetName
* *
* Gets the name of this controller's action * Gets the name of this controller's action
* *
* @abstract * @abstract
* @access public * @access public
* @param boolean $local true if caller wants the localized action name * @param boolean $local true if caller wants the localized action name
* @return string action name * @return string action name
*/ */
public abstract function GetName($local = false); public abstract function GetName($local = false);
   
/** /**
* ReadQuery * ReadQuery
* *
* Read query into parameters * Read query into parameters
* *
* @abstract * @abstract
* @access prtected * @access prtected
*/ */
protected abstract function ReadQuery(); protected abstract function ReadQuery();
   
/** /**
* SetParam * SetParam
* *
* Set a parameter * Set a parameter
* *
* @access protected * @access protected
* @param string $key key to set * @param string $key key to set
* @param mixed $value value to set * @param mixed $value value to set
*/ */
public function SetParam($key, $value) public function SetParam($key, $value)
{ {
if (empty($key)) if (empty($key))
return; return;
   
if (empty($value)) if (empty($value))
unset($this->params[$key]); unset($this->params[$key]);
   
$this->params[$key] = $value; $this->params[$key] = $value;
} }
   
/** /**
* LoadHeaders * LoadHeaders
* *
* Loads headers for this template * Loads headers for this template
* *
* @access protected * @access protected
*/ */
protected function LoadHeaders() protected function LoadHeaders()
{ {
} }
   
/** /**
* LoadData * LoadData
* *
* Loads data for this template * Loads data for this template
* *
* @access protected * @access protected
* @abstract * @abstract
*/ */
protected abstract function LoadData(); protected abstract function LoadData();
   
/** /**
* LoadCommonData * LoadCommonData
* *
* Loads common data used by all templates * Loads common data used by all templates
* *
* @access private * @access private
*/ */
private function LoadCommonData() private function LoadCommonData()
{ {
global $gitphp_version, $gitphp_appstring; global $gitphp_version, $gitphp_appstring;
   
$this->tpl->assign('version', $gitphp_version); $this->tpl->assign('version', $gitphp_version);
$this->tpl->assign('stylesheet', GitPHP_Config::GetInstance()->GetValue('stylesheet', 'gitphp.css')); $this->tpl->assign('stylesheet', GitPHP_Config::GetInstance()->GetValue('stylesheet', 'gitphp.css'));
$this->tpl->assign('javascript', GitPHP_Config::GetInstance()->GetValue('javascript', true)); $this->tpl->assign('javascript', GitPHP_Config::GetInstance()->GetValue('javascript', true));
$this->tpl->assign('pagetitle', GitPHP_Config::GetInstance()->GetValue('title', $gitphp_appstring)); $this->tpl->assign('pagetitle', GitPHP_Config::GetInstance()->GetValue('title', $gitphp_appstring));
$this->tpl->assign('homelink', GitPHP_Config::GetInstance()->GetValue('homelink', GitPHP_Resource::GetInstance()->translate('projects'))); $this->tpl->assign('homelink', GitPHP_Config::GetInstance()->GetValue('homelink', GitPHP_Resource::GetInstance()->translate('projects')));
$this->tpl->assign('action', $this->GetName()); $this->tpl->assign('action', $this->GetName());
$this->tpl->assign('actionlocal', $this->GetName(true)); $this->tpl->assign('actionlocal', $this->GetName(true));
if ($this->project) if ($this->project)
$this->tpl->assign('project', $this->project); $this->tpl->assign('project', $this->project);
if (GitPHP_Config::GetInstance()->GetValue('search', true)) if (GitPHP_Config::GetInstance()->GetValue('search', true))
$this->tpl->assign('enablesearch', true); $this->tpl->assign('enablesearch', true);
if (GitPHP_Config::GetInstance()->GetValue('filesearch', true)) if (GitPHP_Config::GetInstance()->GetValue('filesearch', true))
$this->tpl->assign('filesearch', true); $this->tpl->assign('filesearch', true);
if (isset($this->params['search'])) if (isset($this->params['search']))
$this->tpl->assign('search', $this->params['search']); $this->tpl->assign('search', $this->params['search']);
if (isset($this->params['searchtype'])) if (isset($this->params['searchtype']))
$this->tpl->assign('searchtype', $this->params['searchtype']); $this->tpl->assign('searchtype', $this->params['searchtype']);
$this->tpl->assign('resources', GitPHP_Resource::GetInstance()); $this->tpl->assign('resources', GitPHP_Resource::GetInstance());
} }
   
/** /**
* RenderHeaders * RenderHeaders
* *
* Renders any special headers * Renders any special headers
* *
* @access public * @access public
*/ */
public function RenderHeaders() public function RenderHeaders()
{ {
$this->LoadHeaders(); $this->LoadHeaders();
   
if (count($this->headers) > 0) { if (count($this->headers) > 0) {
foreach ($this->headers as $hdr) { foreach ($this->headers as $hdr) {
header($hdr); header($hdr);
} }
} }
} }
   
/** /**
* Render * Render
* *
* Renders the output * Renders the output
* *
* @access public * @access public
*/ */
public function Render() public function Render()
{ {
if ((GitPHP_Config::GetInstance()->GetValue('cache', false) == true) && (GitPHP_Config::GetInstance()->GetValue('cacheexpire', true) === true)) if ((GitPHP_Config::GetInstance()->GetValue('cache', false) == true) && (GitPHP_Config::GetInstance()->GetValue('cacheexpire', true) === true))
$this->CacheExpire(); $this->CacheExpire();
   
if (!$this->tpl->is_cached($this->GetTemplate(), $this->GetFullCacheKey())) { if (!$this->tpl->is_cached($this->GetTemplate(), $this->GetFullCacheKey())) {
$this->tpl->clear_all_assign(); $this->tpl->clear_all_assign();
$this->LoadCommonData(); $this->LoadCommonData();
$this->LoadData(); $this->LoadData();
} }
   
$this->tpl->display($this->GetTemplate(), $this->GetFullCacheKey()); $this->tpl->display($this->GetTemplate(), $this->GetFullCacheKey());
} }
   
/** /**
* CacheExpire * CacheExpire
* *
* Expires the cache * Expires the cache
* *
* @access public * @access public
* @param boolean $expireAll expire the whole cache * @param boolean $expireAll expire the whole cache
*/ */
public function CacheExpire($expireAll = false) public function CacheExpire($expireAll = false)
{ {
if ($expireAll) { if ($expireAll) {
$this->tpl->clear_all_cache(); $this->tpl->clear_all_cache();
return; return;
} }
   
if (!$this->project) if (!$this->project)
return; return;
   
$headList = $this->project->GetHeads(); $headList = $this->project->GetHeads();
   
if (count($headList) > 0) { if (count($headList) > 0) {
$age = $headList[0]->GetCommit()->GetAge(); $age = $headList[0]->GetCommit()->GetAge();
   
$this->tpl->clear_cache(null, $this->GetCacheKeyPrefix(), null, $age); $this->tpl->clear_cache(null, $this->GetCacheKeyPrefix(), null, $age);
$this->tpl->clear_cache('projectlist.tpl', sha1(serialize(GitPHP_ProjectList::GetInstance()->GetConfig())), null, $age); $this->tpl->clear_cache('projectlist.tpl', sha1(serialize(GitPHP_ProjectList::GetInstance()->GetConfig())), null, $age);
} }
} }
   
} }
   
comments