'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'))); $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); JLog::add("found matches: " . 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, 'openaire'); return false; } } return true; } private function getStatistic($match) { JLog::add("getting stats for match: " . $match[0], JLog::INFO, 'openaire'); $res = 0; if ($this->cache != null) { if($this->cache->exists(base64_encode($match[0]))) { $res = $this->cache->get(base64_encode($match[0])); } else { $res = $this->makeQuery($match[2], $match[3]); JLog::add("adding in cache key: " . $match[0] . " value " . $res, JLog::INFO, 'openaire'); if (!$this->cache->set(base64_encode($match[0]), $res)) { JLog::add("Error adding key: " . $match[0], JLog::ERROR, 'openaire'); } else { $this->cache->save(); } } } else { $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){ $stmt = $this->db->query($query); if (!$stmt) { $arr = $this->db->errorInfo(); JLog :: add("Error executing query: ".$query." ".$arr[2], JLog :: ERROR, 'openaire'); return "-"; } $t = $stmt->fetch(); return number_format($t[0]); } } ?>