'stats.log'), JLog :: ALL, array('stats')); $regex = '\{include_number\s+[A-Za-z0-9_ ]+\s*\}'; $regex = "/$regex/"; preg_match_all($regex, $article->text, $matches, PREG_SET_ORDER); if ($matches) { foreach ($matches as $match) { preg_match('/\s+[A-Za-z0-9_ ]+\s*/', $match[0], $id_matches); $number = $this->getNumberFromSearch(trim($id_matches[0])); $article->text = str_replace($match[0], $number, $article->text); } } return true; } public function getNumberFromSearch($id) { try { $splits = explode("_", $id); $id = $splits[0]; $round = $splits[sizeof($splits) - 1] == 'ROUND' ? true : false; $type = $splits[1] && $splits[1] == 'TYPE' && $splits[2] ? $splits[2] : null; $compatibility = $splits[1] && $splits[1] == 'COMPATIBILITY' && $splits[2] ? $splits[2] : null; // Generate a unique cache key based on your data and context $cacheKey = md5($this->CACHE_GROUP . $id . ($type ? '_' . $type : '') . ($compatibility ? '_' . $compatibility : '')); // JLog:: add('[Stats-plugin] $cacheKey: ' . $cacheKey, JLog :: DEBUG, 'stats'); // Generate a cache key based on your specific criteria $cache = Factory::getCache('plg_include_number', 'output'); $cachedData = $cache->get($cacheKey); $config = Factory::getConfig(); $cacheEnabled = $config->get('caching') && $config->get('cachetime') > 0; if ($cachedData && $cacheEnabled) { JLog:: add('[Stats-plugin] Number: ' . $id . ($type ? '_' . $type : '') . ($compatibility ? '_' . $compatibility : '') . " found in cache", JLog :: DEBUG, 'stats'); return $this->format($cachedData, $round); } else { JLog:: add('[Stats-plugin] Try to get number: ' . $id. ($type ? '_' . $type : '') . ($compatibility ? '_' . $compatibility : ''), JLog :: DEBUG, 'stats'); // If data not found in cache, fetch and store it if ($id == "DATASETS") { $url = 'datasets/count?format=json'; } else if ($id == "PUBLICATIONS") { $url = 'publications/count?format=json'; } else if ($id == "SOFTWARE") { $url = 'software/count?format=json'; } else if ($id == "OTHER") { $url = 'other/count?format=json'; } else if ($id == "RESULTS") { $url = 'results/count?format=json'; } else if ($id == "PROJECTS") { $url = 'projects/count?format=json'; } else if ($id == "DATASOURCES") { $url = 'datasources/count?format=json' . $this->createUrlforDatasources($type, $compatibility); } else if ($id == "FUNDERS") { $url = 'projects/?format=json&refine=true&page=1&size=0&fields=funder&sf=funder'; } else { return '?'; } JLog:: add('[Stats-plugin] Try to get number: ' . $this->params->get('search_api_url') . $url, JLog :: DEBUG, 'stats'); $response = json_decode(file_get_contents($this->params->get('search_api_url') . $url)); $number = null; if ($id == "FUNDERS" && $response->refineResults) { $funders = $response->refineResults->funder; $number = sizeof($funders); } else { if ($response->code == 200) { $number = $response->total; } else { JLog:: add('[Stats-plugin] Fail to get number: ' . $id, JLog :: ERROR, 'stats'); } } JLog:: add('[Stats-plugin] number ' . $id . ($type ? '_' . $type : '') . ($compatibility ? '_' . $compatibility : '')." is ".$number, JLog :: DEBUG, 'stats'); if ($number) { $cache->store($number,$cacheKey); return $this->format($number, $round); } } } catch (Exception $e) { JLog:: add('[Stats-plugin] $exception: ' . $e->getMessage(), JLog :: ERROR, 'stats'); } return "?"; } function createUrlforDatasources($type, $compatibility) { return ($type ? ('&fq='.urlencode('datasourcetypename exact "' . $type . '"')) : "") . ($compatibility ? ('&fq='.urlencode('datasourcecompatibilityname exact "' . $compatibility . '"')) : ""); } function format($n, $round) { // first strip any formatting; $n = (0 + str_replace(",", "", $n)); // is this a number? if (!is_numeric($n)) return $n; if (!$round) return number_format($n); // now filter it; if ($n > 1000000000000) return round(($n / 1000000000000), 1) . ' '. $this->params->get('trillion'); else if ($n > 1000000000) return round(($n / 1000000000), 1) . ' '. $this->params->get('billion'); else if ($n > 1000000) return round(($n / 1000000), 1) . ' '. $this->params->get('million'); else if ($n > 1000) return round(($n / 1000), 1) . ' '. $this->params->get('thousand'); return number_format($n); } } ?>