1
|
<?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
|
$GLOBALS['PREDIS_ROOT'] = dirname(__FILE__) . DS . 'predis';
|
16
|
|
17
|
class plgContentIncludestatsnumberscountry extends JPlugin {
|
18
|
private $cache;
|
19
|
private $codes = array();
|
20
|
private $db;
|
21
|
|
22
|
public function __construct(&$subject, $config) {
|
23
|
parent::__construct($subject, $config);
|
24
|
JLog :: addLogger(array('text_file' => 'openaire.log'), JLog :: ALL, array('openaire'));
|
25
|
|
26
|
if (!class_exists("Predis\Client"))
|
27
|
require $GLOBALS['PREDIS_ROOT'] . DS ."autoload.php";
|
28
|
|
29
|
try {
|
30
|
$this->cache = new Predis\Client(array(
|
31
|
"scheme" => $this->params->get('cachescheme'),
|
32
|
"host" => $this->params->get('cacheserver'),
|
33
|
"port" => $this->params->get('cacheport')));
|
34
|
|
35
|
$this->cache->connect();
|
36
|
|
37
|
} catch(Exception $e) {
|
38
|
JLog :: add("Error connecting to Redis server: ".$e->getMessage(), JLog :: ERROR, 'openaire');
|
39
|
$this->cache = null;
|
40
|
}
|
41
|
|
42
|
JLog :: add('Include country number Plugin!', JLog :: DEBUG, 'openaire');
|
43
|
}
|
44
|
|
45
|
public function onContentPrepare( $context, &$article, &$params, $page = 0 ) {
|
46
|
$regex_base = '\{(include_countrynumber)\s+([[:alpha:]]+)\s+([[:alpha:]\s]+)\}';
|
47
|
$regex = "/$regex_base/";
|
48
|
|
49
|
$contents = $article->text;
|
50
|
$found = preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
|
51
|
|
52
|
JLog::add("found matches: " . print_r($matches, true), JLog::INFO, 'openaire');
|
53
|
|
54
|
if (!$found) {
|
55
|
return true;
|
56
|
}
|
57
|
|
58
|
foreach ($matches as $match) {
|
59
|
try {
|
60
|
$result = $this->getStatistic($match);
|
61
|
$article->text = str_replace($match[0], $result, $article->text);
|
62
|
} catch (Exception $e) {
|
63
|
JLog :: add('Error getting log for: '. $match[0] . '. ' . $e->getMessage(), JLog :: ERROR, 'openaire');
|
64
|
|
65
|
return false;
|
66
|
}
|
67
|
}
|
68
|
|
69
|
return true;
|
70
|
}
|
71
|
|
72
|
private function getStatistic($match) {
|
73
|
JLog::add("getting stats for match: " . $match[0], JLog::INFO, 'openaire');
|
74
|
$res = 0;
|
75
|
|
76
|
if ($this->cache != null) {
|
77
|
|
78
|
if($this->cache->exists(base64_encode($match[0]))) {
|
79
|
$res = $this->cache->get(base64_encode($match[0]));
|
80
|
} else {
|
81
|
$res = $this->makeQuery($match[2], $match[3]);
|
82
|
|
83
|
JLog::add("adding in cache key: " . $match[0] . " value " . $res, JLog::INFO, 'openaire');
|
84
|
if (!$this->cache->set(base64_encode($match[0]), $res)) {
|
85
|
JLog::add("Error adding key: " . $match[0], JLog::ERROR, 'openaire');
|
86
|
} else {
|
87
|
$this->cache->save();
|
88
|
}
|
89
|
}
|
90
|
} else {
|
91
|
$res = $this->makeQuery($match[2], $match[3]);
|
92
|
}
|
93
|
|
94
|
return $res;
|
95
|
}
|
96
|
|
97
|
private function makeQuery($type, $country) {
|
98
|
$res = 0;
|
99
|
$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');
|
100
|
$this->db = new PDO($str);
|
101
|
|
102
|
switch ($type) {
|
103
|
case "PUB":
|
104
|
$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 . "'";
|
105
|
break;
|
106
|
case "DATASRC":
|
107
|
$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 . "'";
|
108
|
break;
|
109
|
}
|
110
|
|
111
|
JLog::add("Executing query: " . $query, JLog::INFO, 'openaire');
|
112
|
|
113
|
$res = $this->doQuery($query);
|
114
|
|
115
|
return $res;
|
116
|
}
|
117
|
|
118
|
private function doQuery($query){
|
119
|
$stmt = $this->db->query($query);
|
120
|
|
121
|
if (!$stmt) {
|
122
|
$arr = $this->db->errorInfo();
|
123
|
JLog :: add("Error executing query: ".$query." ".$arr[2], JLog :: ERROR, 'openaire');
|
124
|
|
125
|
return "-";
|
126
|
}
|
127
|
|
128
|
$t = $stmt->fetch();
|
129
|
|
130
|
return number_format($t[0]);
|
131
|
}
|
132
|
}
|
133
|
|
134
|
?>
|