Project

General

Profile

1 21985 antonis.le
<?php
2
/**
3
* includestatsnumber plugin
4
* This plugin allows you to insert a statistics number to any joomla article
5
* Author: dimitra
6
* License: GNU/GPL http://www.gnu.org/copyleft/gpl.html
7
*/
8
9
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );
10
11
//jimport( 'joomla.event.plugin' );
12
//jimport( 'joomla.language.helper');
13
jimport( 'joomla.plugin.plugin' );
14
jimport( 'joomla.log.log' );
15
16 26210 stefania.m
$GLOBALS['PREDIS_ROOT'] = dirname(__FILE__) . DS . 'predis';
17
18 21985 antonis.le
class plgContentIncludestatsnumbers extends JPlugin {
19
	const CACHE_GROUP = "openaire.stats.plugin";
20
	private $cache;
21
	private $codes = array();
22
	private $db;
23
24
	public function __construct(&$subject, $config) {
25
		parent::__construct($subject, $config);
26
		JLog :: addLogger(array('text_file' => 'openaire.log'), JLog :: ALL, array('openaire'));
27 26210 stefania.m
28
		if (!class_exists("Predis\Client"))
29
			require $GLOBALS['PREDIS_ROOT'] . DS ."autoload.php";
30
31
		try {
32
			$this->cache = new Predis\Client(array(
33
				"scheme" => $this->params->get('cachescheme'),
34
       				"host" => $this->params->get('cacheserver'),
35
       				"port" => $this->params->get('cacheport')
36
				"persistent" => 1));
37
38
			$this->cache->connect();
39
40
		} catch(Exception $e) {
41
			JLog :: add("Error connecting to Redis server: ".$e->getMessage(), JLog :: ERROR, 'openaire');
42
			$this->cache = null;
43
		}
44 21985 antonis.le
45
		/*OVERALL*/
46
		/*1*/ $this->codes[] = "{include_number PUBS}";
47
		/*2*/ $this->codes[] = "{include_number OAPUBS}";
48
		/*3*/ $this->codes[] = "{include_number NOAPUBS}";
49
		/*4*/ $this->codes[] = "{include_number FPUBS}";
50
		/*5*/ $this->codes[] = "{include_number PROJ}";
51
		/*6*/ $this->codes[] = "{include_number DATASRC}";
52
		/*7*/ $this->codes[] = "{include_number DATASRC-PUBS}";
53
		/*8*/ $this->codes[] = "{include_number DATASRC-PUBREPS}";
54
		/*9*/ $this->codes[] = "{include_number DATASRC-PUBOAJ}";
55
		/*10*/$this->codes[] = "{include_number DATASRC-PUBAGGR}";
56
		/*11*/$this->codes[] = "{include_number FUNDERS}";
57
58
		/*FOR FP7*/
59
		/*12*/$this->codes[] = "{include_number FP7PUBSTOTAL}";
60
		/*13*/$this->codes[] = "{include_number FP7PROJPUBS}";
61
		/*14*/$this->codes[] = "{include_number FP7PROJTOTAL}";
62
		/*15*/$this->codes[] = "{include_number FP7OAPUBS}";
63
		/*16*/$this->codes[] = "{include_number FP7RESPUBS}";
64
		/*17*/$this->codes[] = "{include_number FP7EMBPUBS}";
65
66
		/*18*/$this->codes[] = "{include_number SC39FP7PUBSTOTAL}";
67
		/*19*/$this->codes[] = "{include_number SC39FP7PROJPUBS}";
68
		/*20*/$this->codes[] = "{include number SC39FP7PROJTOTAL}";
69
		/*21*/$this->codes[] = "{include_number SC39FP7OAPUBS}";
70
71
		/*ERC*/
72
		/*22*/$this->codes[] = "{include_number ERCPUBSTOTAL}";
73
		/*23*/$this->codes[] = "{include_number ERCPROJPUBS}";
74
		/*24*/$this->codes[] = "{include_number ERCPROJTOTAL}";
75
		/*25*/$this->codes[] = "{include_number ERCOAPUBS}";
76
		/*26*/$this->codes[] = "{include_number ERCRESPUBS}";
77
		/*27*/$this->codes[] = "{include_number ERCEMBPUBS}";
78
79
		/*WT*/
80
		/*28*/$this->codes[] = "{include_number WTPUBSTOTAL}";
81
		/*29*/$this->codes[] = "{include_number WTPROJPUBS}";
82
		/*30*/$this->codes[] = "{include_number WTPROJTOTAL}";
83
		/*31*/$this->codes[] = "{include_number WTOAPUBS}";
84
		/*32*/$this->codes[] = "{include_number WTRESPUBS}";
85
		/*33*/$this->codes[] = "{include_number WTEMBPUBS}";
86 23843 antonis.le
87
		// Latest Natalias requests
88
		/*34*/$this->codes[] = "{include_number DATASRC-WITHPUBS}";
89
		/*35*/$this->codes[] = "{include_number ORG-WITHPUBS}";
90
		/*36*/$this->codes[] = "{include_number DATA-TOTAL}";
91 21985 antonis.le
92
		JLog :: add('Include number Plugin!', JLog :: DEBUG, 'openaire');
93
	}
94
95
	public function onContentPrepare( $context, &$article, &$params, $page = 0 ) {
96 23844 antonis.le
		$regex_base = '\{(include_number)\s+(PUBS|OAPUBS|NOAPUBS|FPUBS|PROJ|DATASRC|DATASRC-PUBS|DATASRC-PUBREPS|DATASRC-PUBOAJ|DATASRC-PUBAGGR|FUNDERS|FP7PUBSTOTAL|FP7PROJPUBS|FP7PROJTOTAL|FP7OAPUBS|FP7RESPUBS|FP7EMBPUBS|SC39FP7PUBSTOTAL|SC39FP7PROJPUBS|SC39FP7PROJTOTAL|SC39FP7OAPUBS|ERCPUBSTOTAL|ERCPROJTOTAL|ERCPROJPUBS|ERCOAPUBS|ERCRESPUBS|ERCEMBPUBS|WTPUBSTOTAL|WTPROJPUBS|WTPROJTOTAL|WTOAPUBS|WTRESPUBS|WTEMBPUBS|DATASRC-WITHPUBS|ORG-WITHPUBS|DATA-TOTAL)\}';
97 21985 antonis.le
		$regex = "/$regex_base/";
98
99
		$contents = $article->text;
100
		$found = preg_match_all($regex, $contents, $matches);
101
		if (!$found){ return true;}
102
103
		try {
104
			$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');
105
			$this->db = new PDO($str);
106
		} catch(Exception $e){
107
			JLog :: add('Could not connect to database: ' . $e->getMessage(), JLog :: ERROR, 'openaire');
108
			return false;
109
		}
110
		$cnt = count($matches);
111
		JLog :: add( "matches found: ".$cnt, JLog :: DEBUG, 'openaire');
112
113
		$result = NULL;
114
		if ($this -> cache != null) {
115 26210 stefania.m
116
			if($this->cache->exists("STATS_NUMBERS")) {
117
				$result = $this -> cache -> hgetall("STATS_NUMBERS");
118
				JLog :: add("Found in cache!", JLog :: DEBUG, 'openaire');
119
			} else {
120
				$this->cache->quit();
121 23844 antonis.le
				JLog :: add("Not found in cache!", JLog :: DEBUG, 'openaire');
122 21985 antonis.le
				$result = $this->makequeries(true);
123
			}
124 26210 stefania.m
125
		} else {//make the queries but don't cache the result
126 21985 antonis.le
			$result = $this->makequeries(false);
127
		}
128
129
		$article->text = str_replace($this->codes, $result, $article->text);
130
131
		return true;
132
	}
133
134
	private function makequeries($addflag){
135
		JLog :: add("Not found in cache!", JLog :: DEBUG, 'openaire');
136
		$temp = array();
137
		/*OVERALL*/
138
139
		/*1*/ $qpubs = "SELECT count(*) FROM result where type='publication'";
140
		/*2*/ $qoapubs = "SELECT count(*) FROM result WHERE access_mode='Open Access' and type='publication'";
141
		/*3*/ $qnoapubs = "SELECT count(*) FROM result WHERE access_mode='Closed Access' and type='publication'";
142
		/*4*/ $qfpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects where result.result_projects = result_projects.id and type='publication'";
143
		/*5*/ $qproj = "SELECT count(*) FROM project";
144
		/*6*/ $qdatasrc = "SELECT count(*) FROM datasource";
145
146
		/*7*/ $qdtsrcpubs = "SELECT count(*) from datasource where compatibility!='non compatible' and compatibility!='unknown'";
147
		/*8*/ $qdtsrcpubreps = "SELECT count(*) FROM datasource WHERE type='Repository'";
148
		/*9*/ $qdtsrcoaj = "SELECT count(*) FROM datasource WHERE type='Aggregated Journal'";
149
		/*10*/$qdtsrcpubaggr = "SELECT count(*) FROM datasource WHERE type='Aggregated Repository'";
150
		/*11*/$qfunders = "SELECT count(distinct funding_lvl0) FROM project WHERE funding_lvl0 !=''";
151
152
		/*FP7*/
153
		/*12: total number of fp7 pubs */
154
		$qfp7pubstotal = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id and type='publication' and result_projects.project = project.id and funding_lvl0 = 'FP7'";
155
                /*13: number of fp7 projects with publications*/
156
		$qfp7projpubs = "SELECT count(distinct project.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id and type='publication' and result_projects.project = project.id and funding_lvl0='FP7'";
157
		/*14: total number of fp7 projects*/
158
		$qfp7projtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'FP7'";
159
		/*15: fp7 open access pubs*/
160
		$qfp7oapubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result_projects = result_projects.id AND result_projects.project = project.id and type='publication' and funding_lvl0 = 'FP7' and access_mode='Open Access'";
161
                /*16: fp7 restricted pubs*/
162
		$qfp7respubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects=result_projects.id AND result_projects.project = project.id and funding_lvl0 = 'FP7' and access_mode='Restricted' and type='publication'";
163
                /*17: fp7 pubs in embargo*/
164
		$qfp7embpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id AND result_projects.project = project.id and funding_lvl0 = 'FP7' and access_mode='Embargo' and type='publication'";
165
                /*18: total number of fp7 pubs with sc39*/
166
		$qsc39fp7pubstotal = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result_projects.project = project.id and funding_lvl0 = 'FP7' and sc39='yes' and result.result_projects = result_projects.id and type='publication'";
167
		/*19: fp7 projs with sc39 and pubs*/
168
		$qsc39fp7projpubs = "SELECT count(distinct project.id) FROM result, result_projects, project WHERE result_projects.project=project.id and funding_lvl0 = 'FP7' and sc39='yes' and result.result_projects = result_projects.id and type='publication'";
169
		/*20: total number of fp7 proj with sc39*/
170
		$qsc39fp7projtotal = "SELECT count(number) from project where funding_lvl0='FP7' and sc39='yes'";
171
		/*21: open access fp7 pubs with sc39*/
172
		$qsc39fp7oapubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id AND result_projects.project = project.id and funding_lvl0 = 'FP7' and access_mode='Open Access' and sc39='yes' and type='publication'";
173
174
		/*ERC*/
175
		/*22: total number of erc pubs*/
176
		$qercpubstotal = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result_projects.project = project.id and funding_lvl2 = 'ERC' and result.result_projects = result_projects.id and type='publication'";
177
                /*23: erc projects with pubs*/
178
		$qercprojpubs = "SELECT count(distinct project.id) FROM result, project, result_projects where result_projects.project = project.id and project.funding_lvl2='ERC' and result.result_projects = result_projects.id and type='publication'";
179
	        /*24: total erc projects*/
180
		$qercprojtotal = "SELECT count(id) FROM project WHERE funding_lvl2 = 'ERC'";
181
		/*25: erc open access pubs*/
182
		$qercoapubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects= result_projects.id AND result_projects.project = project.id and funding_lvl2 = 'ERC' and access_mode='Open Access' and type='publication'";
183
                /*26: erc restricted pubs*/
184
		$qercrespubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id AND result_projects.project = project.id and funding_lvl2 = 'ERC' and access_mode='Restricted' and type='publication'";
185
                /*27: erc embargo pubs*/
186
		$qercembpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects=result_projects.id AND result_projects.project = project.id and funding_lvl2 = 'ERC' and access_mode='Embargo' and type='publication'";
187
188
		/*WT*/
189
		/*28: total number of wt pubs*/
190
		$qwtpubstotal = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result_projects.project = project.id and funding_lvl0 = 'WT' and result.result_projects = result_projects.id and type='publication'";
191
                /*29: wt projects with pubs*/
192
		$qwtprojpubs = "SELECT count(distinct project.id) FROM result, project, result_projects where result_projects.project = project.id and project.funding_lvl0='WT' and result.result_projects = result_projects.id and type='publication'";
193
	        /*30: total wt projects*/
194
		$qwtprojtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'WT'";
195
		/*31: wt open access pubs*/
196
		$qwtoapubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects= result_projects.id AND result_projects.project = project.id and funding_lvl0 = 'WT' and access_mode='Open Access' and type='publication'";
197
                /*32: wt restricted pubs*/
198
		$qwtrespubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id AND result_projects.project = project.id and funding_lvl0 = 'WT' and access_mode='Restricted' and type='publication'";
199
                /*33: wt embargo pubs*/
200
		$qwtembpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects=result_projects.id AND result_projects.project = project.id and funding_lvl0 = 'WT' and access_mode='Embargo' and type='publication'";
201
202 23843 antonis.le
		/*Natalia's extra stuff */
203
		/*34: datasources with publication results */
204
		$datasrc_withpubs = "SELECT count(distinct rd.datasource) from result_datasources rd join result r on r.id=rd.id where r.type='publication'";
205
		/*35: organisations with publication results */
206
		$org_withpubs = "select count(*) from (select dor.organization as organization from datasource_organizations dor join result_datasources rd on rd.datasource=dor.id join result r on r.id=rd.id where r.type='publication' union select por.organization as organization from project_organizations por join result_projects rp on rp.project=por.id join result r on r.id=rp.id where r.type='publication') as foo";
207
		/*36: number of datasets */
208
		$data_total = "select count(id) from result where type='dataset';";
209 21985 antonis.le
210 23843 antonis.le
211 21985 antonis.le
		/*1*/ $temp['pubs'] = $this->doQuery($qpubs);
212
               	/*2*/ $temp['oapubs'] = $this->doQuery($qoapubs);
213
               	/*3*/ $temp['noapubs'] = $this->doQuery($qnoapubs);
214
              	/*4*/ $temp['fpubs'] = $this->doQuery($qfpubs);
215
                /*5*/ $temp['proj'] = $this->doQuery($qproj);
216
                /*6*/ $temp['datasrc'] = $this->doQuery($qdatasrc);
217
                /*7*/ $temp['dtsrcpubs'] = $this->doQuery($qdtsrcpubs);
218
                /*8*/ $temp['dtsrcpubreps'] = $this->doQuery($qdtsrcpubreps);
219
                /*9*/ $temp['dtsrcoaj'] = $this->doQuery($qdtsrcoaj);
220
                /*10*/$temp['dtsrcpubaggr'] = $this->doQuery($qdtsrcpubaggr);
221
                /*11*/$temp['funders'] = $this->doQuery($qfunders);
222
223
		/*FP7*/
224
                /*12*/$temp['fp7pubstotal'] = $this->doQuery($qfp7pubstotal);
225
                /*13*/$temp['fp7projpubs'] = $this->doQuery($qfp7projpubs);
226
                /*14*/$temp['fp7projtotal'] = $this->doQuery($qfp7projtotal);
227
                /*15*/$temp['fp7oapubs'] = $this->doQuery($qfp7oapubs);
228
                /*16*/$temp['fp7respubs'] = $this->doQuery($qfp7respubs);
229
                /*17*/$temp['fp7embpubs'] = $this->doQuery($qfp7embpubs);
230
231
		/*18*/$temp['sc39fp7pubstotal'] = $this->doQuery($qsc39fp7pubstotal);
232
                /*19*/$temp['sc39fp7projpubs'] = $this->doQuery($qsc39fp7projpubs);
233
		/*20*/$temp['sc39fp7projtotal'] = $this->doQuery($qsc39fp7projtotal);
234
                /*21*/$temp['sc39fp7oapubs'] = $this->doQuery($qsc39fp7oapubs);
235
236
		/*ERC*/
237
		/*22*/$temp['ercpubstotal'] = $this->doQuery($qercpubstotal);
238
                /*23*/$temp['ercprojpubs'] = $this->doQuery($qercprojpubs);
239
		/*24*/$temp['ercprojtotal'] = $this->doQuery($qercprojtotal);
240
                /*25*/$temp['ercoapubs'] = $this->doQuery($qercoapubs);
241
                /*26*/$temp['ercrespubs'] = $this->doQuery($qercrespubs);
242
                /*27*/$temp['ercembpubs'] = $this->doQuery($qercembpubs);
243
244
		/*WT*/
245
		/*28*/$temp['wtpubstotal'] = $this->doQuery($qwtpubstotal);
246
                /*29*/$temp['wtprojpubs'] = $this->doQuery($qwtprojpubs);
247
		/*30*/$temp['wtprojtotal'] = $this->doQuery($qwtprojtotal);
248
                /*31*/$temp['wtoapubs'] = $this->doQuery($qwtoapubs);
249
                /*32*/$temp['wtrespubs'] = $this->doQuery($qwtrespubs);
250
                /*33*/$temp['wtembpubs'] = $this->doQuery($qwtembpubs);
251
252 23843 antonis.le
		/*34*/$temp['datasrc_withpubs'] = $this->doQuery($datasrc_withpubs);
253
		/*35*/$temp['org_withpubs'] = $this->doQuery($org_withpubs);
254
		/*36*/$temp['data_total'] = $this->doQuery($data_total);
255
256 21985 antonis.le
		if($addflag){
257
			JLog:: add("params to store: ".print_r($temp, TRUE), JLog :: ERROR, 'openaire');
258 26210 stefania.m
259
			$this->cache = new Predis\Client(array(
260
				"scheme" => $this->params->get('cachescheme'),
261
				"host" => $this->params->get('cacheserver'),
262
				"port" => $this->params->get('cacheport')));
263
264
			$this->cache->connect();
265
			$this->cache->hmset("STATS_NUMBERS", $temp);
266
			$this->cache->save();
267
			$this->cache->quit();
268 21985 antonis.le
		}
269
		return $temp;
270
	}
271
272
	private function doQuery($query){
273
		$stmt = $this->db->query($query);
274
                if(!$stmt){
275
                        $arr = $this->db->errorInfo();
276
                        JLog :: add("Error executing query: ".$query." ".$arr[2], JLog :: ERROR, 'openaire');
277
                        return "-";
278
                }
279
                $t = $stmt->fetch();
280
                return number_format($t[0]);
281
	}
282
}
283
284
?>