'openaire.log'), JLog :: ALL, array('openaire')); if (!class_exists("Predis\Client")) require $GLOBALS['PREDIS_ROOT'] . DS ."autoload.php"; try { $this->cache = new Predis\Client(array( "scheme" => $this->params->get('cachescheme'), "host" => $this->params->get('cacheserver'), "port" => $this->params->get('cacheport'), "persistent" => 1, "read_write_timeout" => 0)); $this->cache->connect(); } catch(Exception $e) { JLog :: add("Error connecting to Redis server: ".$e->getMessage(), JLog :: ERROR, 'openaire'); $this->cache = null; } JLog :: add('Include country number Plugin!', JLog :: DEBUG, 'openaire'); } public function onContentPrepare( $context, &$article, &$params, $page = 0 ) { $regex_base = '\{(include_countrynumber)\s+([[:alpha:]]+)\s+([[:alpha:]\s]+)\}'; $regex = "/$regex_base/"; $contents = $article->text; $found = preg_match_all($regex, $contents, $matches, PREG_SET_ORDER); if (!$found) { return true; } foreach ($matches as $match) { try { $result = $this->getStatistic($match); $article->text = str_replace($match[0], $result, $article->text); } catch (Exception $e) { JLog :: add('Error getting log for: '. $match[0] . '. ' . $e->getMessage(), JLog :: ERROR, 'openaire'); return false; } } return true; } private function getStatistic($match) { JLog::add("getting stats for match: " . $match[0], JLog::INFO, 'openaire'); $res = 0; $res = $this->makeQuery($match[2], $match[3]); return $res; } private function makeQuery($type, $country) { $res = 0; $str = 'pgsql:host='.$this->params->get('dbhost').';port='.$this->params->get('dbport').';dbname='.$this->params->get('dbname').';user='.$this->params->get('dbuser').';password='.$this->params->get('dbpass'); $this->db = new PDO($str); switch ($type) { case "PUB": $query = "select count (rd.id) from result_datasources rd join datasource d on d.id=rd.datasource join organization_datasources od on od.datasource=d.id join organization o on o.id=od.id where o.country='" . $country . "'"; break; case "DATASRC": $query = "select count(distinct d.id) from datasource d join datasource_results dr on dr.id=d.id join datasource_organizations dos on dos.id=d.id join organization o on o.id=dos.organization where o.country='" . $country . "'"; break; } JLog::add("Executing query: " . $query, JLog::INFO, 'openaire'); $res = $this->doQuery($query); return $res; } private function doQuery($query){ if($this->cache != null){ $myKey = md5($query); if($this->cache->exists($myKey)) { $results = $this->cache->hget($myKey, "results"); $results = json_decode($results, true); return number_format($results[0][0]); } else { $results = $this->doQueryNoCache($query); if(isset($_GET['persistent'])) $persistent = $_GET['persistent']; else $persistent = true; //default value is true if not provided if(($this->cache->hmset($myKey, array("query" => $query, "results" => json_encode($results), "persistent" => $persistent, "fetchMode" => PDO::FETCH_BOTH))) == false) { JLog :: add("Error adding key : ".$myKey." in cache", JLog :: ERROR, 'openaire'); } else { $this->cache->save(); } return number_format($results[0][0]); } } else { $results = $this->doQueryNoCache($query); $results = json_decode($results, true); return number_format($results[0][0]); } } function doQueryNoCache($query) { $stmt = $this->db->query($query); if(!$stmt) { $arr = $this->db->errorInfo(); JLog :: add("Error executing query: ".$query." ".$arr[2], JLog :: ERROR, 'openaire'); return null; } return $stmt->fetchAll(); } } ?>