/**
* Module cache helper
*
* Caching modes:
* To be set in XML:
* 'static' One cache file for all pages with the same module parameters
* 'itemid' Changes on itemid change, to be called from inside the module:
* 'safeuri' Id created from $cacheparams->modeparams array,
* 'id' Module sets own cache id's
*
* @param object $module Module object
* @param object $moduleparams Module parameters
* @param object $cacheparams Module cache parameters - id or URL parameters, depending on the module cache mode
*
* @return string
*
* @see InputFilter::clean()
* @since 1.6
*/
public static function moduleCache($module, $moduleparams, $cacheparams)
{
if (!isset($cacheparams->modeparams)) {
$cacheparams->modeparams = null;
}
if (!isset($cacheparams->cachegroup)) {
$cacheparams->cachegroup = $module->module;
}
if (!isset($cacheparams->cachesuffix)) {
$cacheparams->cachesuffix = '';
}
$user = Factory::getUser();
$app = Factory::getApplication();
/** @var CallbackController $cache */
$cache = Factory::getContainer()->get(CacheControllerFactoryInterface::class)->createCacheController('callback', ['defaultgroup' => $cacheparams->cachegroup]);
// Turn cache off for internal callers if parameters are set to off and for all logged in users
$ownCacheDisabled = $moduleparams->get('owncache') === 0 || $moduleparams->get('owncache') === '0';
$cacheDisabled = $moduleparams->get('cache') === 0 || $moduleparams->get('cache') === '0';
if ($ownCacheDisabled || $cacheDisabled || $app->get('caching') == 0 || $user->get('id')) {
$cache->setCaching(false);
}
// Module cache is set in seconds, global cache in minutes, setLifeTime works in minutes
$cache->setLifeTime($moduleparams->get('cache_time', $app->get('cachetime') * 60) / 60);
$wrkaroundoptions = array('nopathway' => 1, 'nohead' => 0, 'nomodules' => 1, 'modulemode' => 1, 'mergehead' => 1);
$wrkarounds = true;
$view_levels = md5(serialize($user->getAuthorisedViewLevels()));
switch ($cacheparams->cachemode) {
case 'id':
$ret = $cache->get(array($cacheparams->class, $cacheparams->method), $cacheparams->methodparams, $cacheparams->modeparams . $cacheparams->cachesuffix, $wrkarounds, $wrkaroundoptions);
break;
case 'safeuri':
$secureid = null;
if (\is_array($cacheparams->modeparams)) {
$input = $app->input;
$uri = $input->getArray();
$safeuri = new \stdClass();
$noHtmlFilter = InputFilter::getInstance();
foreach ($cacheparams->modeparams as $key => $value) {
// Use int filter for id/catid to clean out spamy slugs
if (isset($uri[$key])) {
$safeuri->{$key} = $noHtmlFilter->clean($uri[$key], $value);
}
}
}
$secureid = md5(serialize(array($safeuri, $cacheparams->method, $moduleparams)));
$ret = $cache->get(array($cacheparams->class, $cacheparams->method), $cacheparams->methodparams, $module->id . $view_levels . $secureid . $cacheparams->cachesuffix, $wrkarounds, $wrkaroundoptions);
break;
case 'static':
$ret = $cache->get(array($cacheparams->class, $cacheparams->method), $cacheparams->methodparams, $module->module . md5(serialize($cacheparams->methodparams)) . $cacheparams->cachesuffix, $wrkarounds, $wrkaroundoptions);
break;
case 'itemid':
default:
$ret = $cache->get(array($cacheparams->class, $cacheparams->method), $cacheparams->methodparams, $module->id . $view_levels . $app->input->getInt('Itemid', null) . $cacheparams->cachesuffix, $wrkarounds, $wrkaroundoptions);
break;
}
return $ret;
}