Project

General

Profile

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
		if (!$found) {
53
			return true;
54
		}
55
		
56
		foreach ($matches as $match) {
57
			try {
58
				$result = $this->getStatistic($match);
59
				$article->text = str_replace($match[0], $result, $article->text);
60
			} catch (Exception $e) {
61
				JLog :: add('Error getting log for: '. $match[0] . '. ' . $e->getMessage(), JLog :: ERROR, 'openaire');
62
				
63
				return false;
64
			}
65
		}
66
		
67
		return true;
68
	}
69
	
70
	private function getStatistic($match) {
71
		JLog::add("getting stats for match: " . $match[0], JLog::INFO, 'openaire');
72
		$res = 0;
73
		
74
		$res = $this->makeQuery($match[2], $match[3]);		
75
		
76
		return $res;
77
	}
78
	
79
	private function makeQuery($type, $country) {
80
		$res = 0;
81
		$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');
82
		$this->db = new PDO($str);
83
		
84
		switch ($type) {
85
			case "PUB":
86
				$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 . "'";
87
				break;
88
			case "DATASRC":
89
				$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 . "'";
90
				break;
91
		}
92
		
93
		JLog::add("Executing query: " . $query, JLog::INFO, 'openaire');
94
		
95
		$res = $this->doQuery($query);
96
		
97
		return $res;
98
	}
99
	
100
	private function doQuery($query){
101
	
102
		if($this->cache != null){
103
		
104
			$myKey = md5($query);
105
				
106
			if($this->cache->exists($myKey)) { 
107
			
108
				$results = $this->cache->hget($myKey, "results");
109
				$results = json_decode($results, true);
110
				return number_format($results[0][0]);
111
				
112
			} else {
113
			
114
				$results = $this->doQueryNoCache($query);
115
				
116
				if(isset($_GET['persistent']))
117
  					$persistent = $_GET['persistent'];
118
				else
119
  					$persistent = true; //default value is true if not provided
120
  
121
  				if(($this->cache->hmset($myKey, array("query" => $query, "results" => json_encode($results), "persistent" => $persistent, "fetchMode" => PDO::FETCH_BOTH))) == false) {
122
  					JLog :: add("Error adding key : ".$myKey." in cache", JLog :: ERROR, 'openaire');
123
  				} else {
124
  					$this->cache->save();
125
  				}
126
  				return number_format($results[0][0]);
127
			}
128
		} else {
129
			$results = $this->doQueryNoCache($query);
130
			$results = json_decode($results, true);
131
			
132
			return number_format($results[0][0]);
133
		}
134
	}
135
	
136
	function doQueryNoCache($query) {
137
		
138
		$stmt = $this->db->query($query); 
139
		if(!$stmt) {
140
			$arr = $this->db->errorInfo();
141
			JLog :: add("Error executing query: ".$query." ".$arr[2], JLog :: ERROR, 'openaire');
142
			return array(0 => array("count" => 0, 0 => 0));
143
		}
144
		return $stmt->fetchAll();
145
	}
146
}
147

    
148
?>
(1-1/4)