Project

General

Profile

« Previous | Next » 

Revision 33898

Added by Eri Katsari almost 10 years ago

updated numbers plugin for new backend

View differences:

modules/uoa-joomla/trunk/joomla-2.5.3/includestatsnumberscountry/includestatsnumberscountry.php
14 14

  
15 15
$GLOBALS['PREDIS_ROOT'] = dirname(__FILE__) . DS . 'predis';
16 16

  
17
 
17 18
class plgContentIncludestatsnumberscountry extends JPlugin {
18 19
	private $cache;
19 20
	private $codes = array();
......
23 24
		parent::__construct($subject, $config);
24 25
		JLog :: addLogger(array('text_file' => 'openaire.log'), JLog :: ALL, array('openaire'));
25 26
		
26
		if (!class_exists("Predis\Client"))
27
			require $GLOBALS['PREDIS_ROOT'] . DS ."autoload.php";
27
		if(!class_exists("Predis\Client")) {
28
			require $GLOBALS['PREDIS_ROOT'] . DS ."autoload.php";				
29
		}
28 30
		
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')));
31
			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')));
34 38
        			
35 39
        		$this->cache->connect();
36 40
		
37
		} catch(Exception $e) {
38
			JLog :: add("Error connecting to Redis server: ".$e->getMessage(), JLog :: ERROR, 'openaire');
39
			$this->cache = null;
40
		}
41
			} catch(Exception $e) {
42
				JLog :: add("Stats Plugin:  Error connecting to Redis server: ".$e->getMessage(), JLog :: ERROR, 'openaire');
43
				$this->cache = null;
44
			}
41 45
		
42
		JLog :: add('Include country number Plugin!', JLog :: DEBUG, 'openaire');
46
		
47
		JLog :: add('Stats Plugin:  Include country number Plugin!', JLog :: DEBUG, 'openaire');
43 48
	}
44 49

  
45 50
	public function onContentPrepare( $context, &$article, &$params, $page = 0 ) {
......
49 54
		$contents = $article->text;
50 55
		$found = preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
51 56
		
52
		JLog::add("found matches: " . print_r($matches, true), JLog::INFO, 'openaire');	
57
		 //JLog::add("found matches in cache: " . print_r($matches, true), JLog::INFO, 'openaire');	
53 58
		
54 59
		if (!$found) {
55 60
			return true;
......
70 75
	}
71 76
	
72 77
	private function getStatistic($match) {
73
		JLog::add("getting stats for match: " . $match[0], JLog::INFO, 'openaire');
78
	 JLog::add("Stats Plugin: getting stats for match: " . $match[0], JLog::INFO, 'openaire');
74 79
		$res = 0;
75 80
		
76 81
		if ($this->cache != null) {
77 82
		
78 83
			if($this->cache->exists(base64_encode($match[0]))) {
79
				$res = $this->cache->get(base64_encode($match[0]));	
84
		        $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
      
80 95
			} 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');
96
			  
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');
86 106
				} else {
87 107
					$this->cache->save();
88 108
				}
89 109
			}
90 110
		} else {
91
			$res = $this->makeQuery($match[2], $match[3]);		
111
			 	
112
    		        $query=$this->getQuery($match[2], $match[3]);
113
                        $res = $this->makeQuery($query);
114
                         $res= str_replace('[','',$res);
115
                         $res= str_replace(']','',$res);
116
 
92 117
		}
93 118
		
94 119
		return $res;
95 120
	}
96 121
	
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
		
122
private function getQuery($type, $country) {
123

  
102 124
		switch ($type) {
103 125
			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 . "'";
126
				$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 . "'";
105 127
				break;
106 128
			case "DATASRC":
107 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 . "'";
108 130
				break;
109
		}
131
}
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);
110 144
		
111
		JLog::add("Executing query: " . $query, JLog::INFO, 'openaire');
112
		
145
	 
146
		JLog::add("Stats Plugin:  Executing query: " . $query, JLog::INFO, 'openaire');
113 147
		$res = $this->doQuery($query);
114 148
		
115 149
		return $res;
......
120 154
		
121 155
		if (!$stmt) {
122 156
			$arr = $this->db->errorInfo();
123
			JLog :: add("Error executing query: ".$query." ".$arr[2], JLog :: ERROR, 'openaire');
157
			JLog :: add("Stats Plugin:  Error executing query: ".$query." ".$arr[2], JLog :: ERROR, 'openaire');
124 158
			
125 159
			return "-";
126 160
		}
127 161
		
128 162
		$t = $stmt->fetch();
129

  
130
		return number_format($t[0]);
163
      
164
	 	return number_format($t[0]);
131 165
	}
132 166
}
133 167

  

Also available in: Unified diff