1 |
26162
|
antonis.le
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* includestatsnumbercountry plugin
|
4 |
|
|
* This plugin allows you to insert a statistics number to any joomla article. This is a
|
5 |
|
|
* modified version that computes statistics per country.
|
6 |
|
|
* Author: dimitra
|
7 |
|
|
* License: GNU/GPL http://www.gnu.org/copyleft/gpl.html
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
|
|
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );
|
11 |
|
|
|
12 |
|
|
jimport( 'joomla.plugin.plugin' );
|
13 |
|
|
jimport( 'joomla.log.log' );
|
14 |
|
|
|
15 |
26210
|
stefania.m
|
$GLOBALS['PREDIS_ROOT'] = dirname(__FILE__) . DS . 'predis';
|
16 |
|
|
|
17 |
33898
|
eri.katsar
|
|
18 |
26162
|
antonis.le
|
class plgContentIncludestatsnumberscountry extends JPlugin {
|
19 |
|
|
private $cache;
|
20 |
|
|
private $codes = array();
|
21 |
|
|
private $db;
|
22 |
|
|
|
23 |
|
|
public function __construct(&$subject, $config) {
|
24 |
|
|
parent::__construct($subject, $config);
|
25 |
|
|
JLog :: addLogger(array('text_file' => 'openaire.log'), JLog :: ALL, array('openaire'));
|
26 |
|
|
|
27 |
33898
|
eri.katsar
|
if(!class_exists("Predis\Client")) {
|
28 |
|
|
require $GLOBALS['PREDIS_ROOT'] . DS ."autoload.php";
|
29 |
|
|
}
|
30 |
26162
|
antonis.le
|
|
31 |
33898
|
eri.katsar
|
try {
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
$this->cache = new Predis\Client(array(
|
35 |
|
|
"scheme" => $this->params->get('cachescheme'),
|
36 |
|
|
"host" => $this->params->get('cacheserver'),
|
37 |
|
|
"port" => $this->params->get('cacheport')));
|
38 |
26210
|
stefania.m
|
|
39 |
|
|
$this->cache->connect();
|
40 |
|
|
|
41 |
33898
|
eri.katsar
|
} catch(Exception $e) {
|
42 |
|
|
JLog :: add("Stats Plugin: Error connecting to Redis server: ".$e->getMessage(), JLog :: ERROR, 'openaire');
|
43 |
|
|
$this->cache = null;
|
44 |
|
|
}
|
45 |
26162
|
antonis.le
|
|
46 |
33898
|
eri.katsar
|
|
47 |
|
|
JLog :: add('Stats Plugin: Include country number Plugin!', JLog :: DEBUG, 'openaire');
|
48 |
26162
|
antonis.le
|
}
|
49 |
|
|
|
50 |
|
|
public function onContentPrepare( $context, &$article, &$params, $page = 0 ) {
|
51 |
|
|
$regex_base = '\{(include_countrynumber)\s+([[:alpha:]]+)\s+([[:alpha:]\s]+)\}';
|
52 |
|
|
$regex = "/$regex_base/";
|
53 |
|
|
|
54 |
|
|
$contents = $article->text;
|
55 |
|
|
$found = preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
|
56 |
|
|
|
57 |
33898
|
eri.katsar
|
//JLog::add("found matches in cache: " . print_r($matches, true), JLog::INFO, 'openaire');
|
58 |
26162
|
antonis.le
|
|
59 |
|
|
if (!$found) {
|
60 |
|
|
return true;
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
foreach ($matches as $match) {
|
64 |
|
|
try {
|
65 |
|
|
$result = $this->getStatistic($match);
|
66 |
|
|
$article->text = str_replace($match[0], $result, $article->text);
|
67 |
|
|
} catch (Exception $e) {
|
68 |
|
|
JLog :: add('Error getting log for: '. $match[0] . '. ' . $e->getMessage(), JLog :: ERROR, 'openaire');
|
69 |
|
|
|
70 |
|
|
return false;
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
return true;
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
private function getStatistic($match) {
|
78 |
33898
|
eri.katsar
|
JLog::add("Stats Plugin: getting stats for match: " . $match[0], JLog::INFO, 'openaire');
|
79 |
26162
|
antonis.le
|
$res = 0;
|
80 |
|
|
|
81 |
|
|
if ($this->cache != null) {
|
82 |
26210
|
stefania.m
|
|
83 |
|
|
if($this->cache->exists(base64_encode($match[0]))) {
|
84 |
33898
|
eri.katsar
|
$res = $this->cache->hget(base64_encode($match[0]),'results');
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
JLog::add(" Stats Plugin: $res: ". $res , JLog::INFO, 'openaire');
|
88 |
|
|
$res= str_replace('[','',$res);
|
89 |
|
|
$res= str_replace(']','',$res);
|
90 |
|
|
JLog::add(" Stats Plugin: cleaned: ".$res , JLog::INFO, 'openaire');
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
// return json_decode($t[0]);
|
94 |
|
|
|
95 |
26210
|
stefania.m
|
} else {
|
96 |
33898
|
eri.katsar
|
|
97 |
|
|
$query=$this->getQuery($match[2], $match[3]);
|
98 |
|
|
$res = $this->makeQuery($query);
|
99 |
|
|
JLog::add("!!!decoded key: " .json_decode($res), JLog::INFO, 'openaire');
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
JLog::add("Stats Plugin: added in cache -> key: " . base64_encode($match[0]) . " results " . $res . " query " . $query ." persistent 1 fetchMode 3", JLog::INFO, 'openaire');
|
103 |
|
|
if (!$this->cache->hmset(base64_encode($match[0]),array("results" =>$res,"query" =>$query ,"persistent" => '1',"fetchMode" => '3'))){
|
104 |
|
|
|
105 |
|
|
JLog::add("Stats Plugin: Error adding key: " . $match[0], JLog::ERROR, 'openaire');
|
106 |
26210
|
stefania.m
|
} else {
|
107 |
|
|
$this->cache->save();
|
108 |
26162
|
antonis.le
|
}
|
109 |
|
|
}
|
110 |
|
|
} else {
|
111 |
33898
|
eri.katsar
|
|
112 |
|
|
$query=$this->getQuery($match[2], $match[3]);
|
113 |
|
|
$res = $this->makeQuery($query);
|
114 |
|
|
$res= str_replace('[','',$res);
|
115 |
|
|
$res= str_replace(']','',$res);
|
116 |
|
|
|
117 |
26162
|
antonis.le
|
}
|
118 |
|
|
|
119 |
|
|
return $res;
|
120 |
|
|
}
|
121 |
|
|
|
122 |
33898
|
eri.katsar
|
private function getQuery($type, $country) {
|
123 |
|
|
|
124 |
26162
|
antonis.le
|
switch ($type) {
|
125 |
|
|
case "PUB":
|
126 |
33898
|
eri.katsar
|
$query = "select count (distinct 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 . "'";
|
127 |
26162
|
antonis.le
|
break;
|
128 |
|
|
case "DATASRC":
|
129 |
|
|
$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 . "'";
|
130 |
|
|
break;
|
131 |
33898
|
eri.katsar
|
}
|
132 |
|
|
|
133 |
|
|
JLog::add("generated query for country stats: " . $query, JLog::INFO, 'openaire');
|
134 |
|
|
|
135 |
|
|
return $query ;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
private function makeQuery($query) {
|
141 |
|
|
$res = 0;
|
142 |
|
|
$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');
|
143 |
|
|
$this->db = new PDO($str);
|
144 |
26162
|
antonis.le
|
|
145 |
33898
|
eri.katsar
|
|
146 |
|
|
JLog::add("Stats Plugin: Executing query: " . $query, JLog::INFO, 'openaire');
|
147 |
26162
|
antonis.le
|
$res = $this->doQuery($query);
|
148 |
|
|
|
149 |
|
|
return $res;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
private function doQuery($query){
|
153 |
|
|
$stmt = $this->db->query($query);
|
154 |
|
|
|
155 |
|
|
if (!$stmt) {
|
156 |
|
|
$arr = $this->db->errorInfo();
|
157 |
33898
|
eri.katsar
|
JLog :: add("Stats Plugin: Error executing query: ".$query." ".$arr[2], JLog :: ERROR, 'openaire');
|
158 |
26162
|
antonis.le
|
|
159 |
|
|
return "-";
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
$t = $stmt->fetch();
|
163 |
33898
|
eri.katsar
|
|
164 |
|
|
return number_format($t[0]);
|
165 |
26162
|
antonis.le
|
}
|
166 |
|
|
}
|
167 |
|
|
|
168 |
26210
|
stefania.m
|
?>
|