Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExampleFeedburner.php « ExampleFeedburner « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca7b582ce135fe77c0b038d676908cc6428910af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
 * Piwik - Open source web analytics
 * 
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
 * @version $Id$
 * 
 * @category Piwik_Plugins
 * @package Piwik_ExampleFeedburner
 */

/**
 *
 * @package Piwik_ExampleFeedburner
 */
class Piwik_ExampleFeedburner extends Piwik_Plugin
{
	/**
	 * Return information about this plugin.
	 *
	 * @see Piwik_Plugin
	 *
	 * @return array
	 */
	public function getInformation()
	{
		return array(
			'description' => Piwik_Translate('ExampleFeedburner_PluginDescription'),
			'author' => 'Piwik',
			'author_homepage' => 'http://piwik.org/',
			'version' => '0.1',
		);
	}

	function install()
	{
		try{
			Piwik_Exec('ALTER TABLE '.Piwik_Common::prefixTable('site'). " ADD `feedburnerName` VARCHAR( 100 ) DEFAULT NULL");
		} catch(Exception $e){
			// mysql code error 1060: column already exists
			// if there is another error we throw the exception, otherwise it is OK as we are simply reinstalling the plugin
			if(!Zend_Registry::get('db')->isErrNo($e, '1060'))
			{
				throw $e;
			}
		}
	}
	
	function uninstall()
	{
		Piwik_Query('ALTER TABLE '.Piwik_Common::prefixTable('site'). " DROP `feedburnerName`");
	}
}

Piwik_AddWidget('Example Widgets', 'Feedburner statistics', 'ExampleFeedburner', 'feedburner');

/**
 *
 * @package Piwik_ExampleFeedburner
 */
class Piwik_ExampleFeedburner_Controller extends Piwik_Controller
{

	/**
	 * Simple feedburner statistics output
	 *
	 */
	function feedburner()
	{
		$view = Piwik_View::factory('feedburner');
		$idSite = Piwik_Common::getRequestVar('idSite',1,'int');
		$feedburnerFeedName = Piwik_FetchOne('SELECT feedburnerName FROM '.Piwik_Common::prefixTable('site').
								' WHERE idsite = ?', $idSite );
		if(empty($feedburnerFeedName))
		{
			$feedburnerFeedName = 'Piwik';
		}
		$view->feedburnerFeedName = $feedburnerFeedName;
		$view->idSite = $idSite;
		$view->fbStats = $this->getFeedData($feedburnerFeedName);
		echo $view->render();
	}
	

	/**
	 * Returns array of counts and images based on Feedburner URI
	 * 
	 * @param string $uri
	 * @return array()
	 */
	protected function getFeedData($uri)
	{
		// Awareness API only supports yesterday and back;
		// we get stats for previous two days;
		// @see http://code.google.com/apis/feedburner/awareness_api.html#dates
		$yesterday = Piwik_Date::factory('-1 day', 'America/Los_Angeles');
		$beforeYesterday = Piwik_Date::factory('-2 day', 'America/Los_Angeles');
		
		//create url to gather XML feed from
		$url = 'https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri='.$uri.'&dates='.$beforeYesterday->toString().','.$yesterday->toString();
		$data = '';
		try {
			$data = Piwik_Http::sendHttpRequest($url, 5);
			$xml = new SimpleXMLElement($data);
		} catch(Exception $e) {
			return "Error parsing the data for feed $uri. Fetched data was: \n'". $data."'";
		}
		
		if(count($xml->feed->entry) != 2) {
			return "Error fetching the Feedburner stats. Expected XML, Got: \n" . strip_tags($data);
		}
		$data = array();
		$i = 0;
		foreach($xml->feed->entry as $feedDay){
			$data[0][$i] = (int)$feedDay['circulation'];
			$data[1][$i] = (int)$feedDay['hits'];
			$data[2][$i] = (int)$feedDay['reach'];
			$i++;
		}
	
		foreach($data as $key => $value) {
			if( $value[0] == $value[1]) {
				$img = 'stop.png';
			} else if($value[0] < $value[1]) {
				$img = 'arrow_up.png';
			} else {
				$img = 'arrow_down.png';
			}
			
			$prefixImage = '<img alt="" src="./plugins/MultiSites/images/';
			$suffixImage = '" />';
			$data[$key][2] = $prefixImage . $img . $suffixImage;
		}
		return $data;
	}
	
	/**
	 * Function called to save the Feedburner ID entered in the form
	 *
	 */
	function saveFeedburnerName()
	{
		// we save the value in the DB for an authenticated user
		if(Piwik::getCurrentUserLogin() != 'anonymous')
		{
			Piwik_Query('UPDATE '.Piwik_Common::prefixTable('site').' 
						 SET feedburnerName = ? WHERE idsite = ?', 
				array(Piwik_Common::getRequestVar('name','','string'), Piwik_Common::getRequestVar('idSite',1,'int'))
				);
		}
	}
}