'stats.log'), JLog :: ALL, array('stats')); if(!class_exists("Predis\Client")) { require $GLOBALS['PREDIS_ROOT']. DS ."autoload.php"; } try { //Predis\Autoloader::register(); $this->cache = new Predis\Client(array( "scheme" => $this->params->get('cachescheme'), "host" => $this->params->get('cacheserver'), "port" => $this->params->get('cacheport'))); $this->cache->connect(); } catch(Exception $e) { JLog :: add("Error connecting to Redis server: ".$e->getMessage(), JLog :: ERROR, 'stats'); $this->cache = null; } JLog :: add('Include country number Plugin!', JLog :: DEBUG, 'stats'); } 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); //JLog::add("found matches in cache: " . print_r($matches, true), JLog::INFO, 'openaire'); 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, 'stats'); return false; } } return true; } private function getStatistic($match) { JLog::add("!!!getting stats for match: " . $match[0], JLog::INFO, 'stats'); $res = 0; if ($this->cache != null) { if($this->cache->exists(base64_encode($match[0]))) { $res = $this->cache->hget(base64_encode($match[0]),'results'); JLog::add("!!!$res: ". $res , JLog::INFO, 'stats'); $res= str_replace('[','',$res); $res= str_replace(']','',$res); JLog::add("!!!cleaned: ".$res , JLog::INFO, 'stats'); } else { $query=$this->getQuery($match[2], $match[3]); $res = $this->makeQuery($query); JLog::add("!!!decoded key: " .json_decode($res), JLog::INFO, 'stats'); JLog::add("!!!added in cache -> key: " . base64_encode($match[0]) . " results " . $res . " query " . $query ." persistent true fetchMode 3", JLog::INFO, 'stats'); if (!$this->cache->hmset(base64_encode($match[0]),array("results" =>$res,"query" =>$query ,"persistent" => 'true',"fetchMode" => '3'))){ JLog::add("!!!Error adding key: " . $match[0], JLog::ERROR, 'openaire'); } else { $this->cache->save(); } } } else { $query=$this->getQuery($match[2], $match[3]); $res = $this->makeQuery($query); $res= str_replace('[','',$res); $res= str_replace(']','',$res); } return $res; } private function getQuery($type, $country) { switch ($type) { case "PUB": $query=" SELECT count(result.number) as field0 FROM result JOIN (select distinct result_datasources.id, datasource.name from result_datasources join datasource on datasource.id=result_datasources.datasource join datasource_organizations on datasource_organizations.id=datasource.datasource_organizations join organization on organization.id=datasource_organizations.organization and (organization.country='".$country."') ) as result_datasources ON result.result_datasources = result_datasources.id ;" ; 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("generated query for country stats: " . $query, JLog::INFO, 'stats'); return $query ; } private function makeQuery($query) { $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); JLog::add("Executing query: " . $query, JLog::INFO, 'stats'); $res = $this->doQuery($query); return $res; } private function doQuery($query){ $stmt = $this->db->query($query); if (!$stmt) { $arr = $this->db->errorInfo(); JLog :: add("Error executing query: ".$query." ".$arr[2], JLog :: ERROR, 'stats'); return "-"; } $t = $stmt->fetch(); return number_format($t[0]); } } ?>