From a7909e345ed4726557eb97d36499a5ea264a6a11 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Sat, 10 Jul 2010 04:49:54 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Piwik=20=E5=88=B0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=BA=93=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit +YUCHENG HU+ git-svn-id: https://svn.code.sf.net/p/hawebs/svn@480 a2543c7e-f6e9-4f8a-8bff-1ffc34733512 --- .../ExampleFeedburner/ExampleFeedburner.php | 153 ++++++++++++++ .../templates/feedburner.tpl | 57 ++++++ .../plugins/ExamplePlugin/ExamplePlugin.php | 190 ++++++++++++++++++ .../trunk/plugins/ExamplePlugin/lang/en.php | 19 ++ 4 files changed, 419 insertions(+) create mode 100644 oss/piwik/trunk/plugins/ExampleFeedburner/ExampleFeedburner.php create mode 100644 oss/piwik/trunk/plugins/ExampleFeedburner/templates/feedburner.tpl create mode 100644 oss/piwik/trunk/plugins/ExamplePlugin/ExamplePlugin.php create mode 100644 oss/piwik/trunk/plugins/ExamplePlugin/lang/en.php diff --git a/oss/piwik/trunk/plugins/ExampleFeedburner/ExampleFeedburner.php b/oss/piwik/trunk/plugins/ExampleFeedburner/ExampleFeedburner.php new file mode 100644 index 00000000..633e3e9f --- /dev/null +++ b/oss/piwik/trunk/plugins/ExampleFeedburner/ExampleFeedburner.php @@ -0,0 +1,153 @@ + 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 = ''; + $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')) + ); + } + } +} diff --git a/oss/piwik/trunk/plugins/ExampleFeedburner/templates/feedburner.tpl b/oss/piwik/trunk/plugins/ExampleFeedburner/templates/feedburner.tpl new file mode 100644 index 00000000..812dcdc2 --- /dev/null +++ b/oss/piwik/trunk/plugins/ExampleFeedburner/templates/feedburner.tpl @@ -0,0 +1,57 @@ + + + +{/literal} + +{if !is_array($fbStats)} + {$fbStats} +{else} + + + + + + + + + + + + + + + + + + + + + + + + + +
PreviousYesterday
Circulation{$fbStats[0][0]}{$fbStats[0][1]}{$fbStats[0][2]}
Hits{$fbStats[1][0]}{$fbStats[1][1]}{$fbStats[1][2]}
Reach{$fbStats[2][0]}{$fbStats[2][1]}{$fbStats[2][2]}
+{/if} + +
+ + +
diff --git a/oss/piwik/trunk/plugins/ExamplePlugin/ExamplePlugin.php b/oss/piwik/trunk/plugins/ExamplePlugin/ExamplePlugin.php new file mode 100644 index 00000000..5240c2b9 --- /dev/null +++ b/oss/piwik/trunk/plugins/ExamplePlugin/ExamplePlugin.php @@ -0,0 +1,190 @@ + Piwik_Translate('ExamplePlugin_PluginDescription'), + 'author' => 'Piwik', + 'author_homepage' => 'http://piwik.org/', + 'version' => '0.1', + 'translationAvailable' => true, + ); + } + + public function getListHooksRegistered() + { + return array( +// 'Controller.renderView' => 'addUniqueVisitorsColumnToGivenReport', + ); + } + + function addUniqueVisitorsColumnToGivenReport($notification) + { + $view = $notification->getNotificationInfo(); + $view = $view['view']; + if($view->getCurrentControllerName() == 'Referers' + && $view->getCurrentControllerAction() == 'getWebsites') + { + $view->addColumnToDisplay('nb_uniq_visitors'); + } + } + + function postLoad() + { + // we register the widgets so they appear in the "Add a new widget" window in the dashboard + // Note that the first two parameters can be either a normal string, or an index to a translation string + Piwik_AddWidget('ExamplePlugin_exampleWidgets', 'ExamplePlugin_exampleWidget', 'ExamplePlugin', 'exampleWidget'); + Piwik_AddWidget('ExamplePlugin_exampleWidgets', 'ExamplePlugin_blogPiwikRss', 'ExamplePlugin', 'blogPiwik'); + Piwik_AddWidget('ExamplePlugin_exampleWidgets', 'ExamplePlugin_photostreamMatt', 'ExamplePlugin', 'photostreamMatt'); + } +} + +/** + * + * @package Piwik_ExamplePlugin + */ +class Piwik_ExamplePlugin_Controller extends Piwik_Controller +{ + /** + * Go to /piwik/?module=ExamplePlugin&action=helloWorld to execute this method + * + */ + function helloWorld() + { + echo "Hello world!
"; + echo "Happy coding with Piwik :)"; + } + + /** + * See the result on piwik/?module=ExamplePlugin&action=exampleWidget + * or in the dashboard > Add a new widget + * + */ + function exampleWidget() + { + echo "Hello world!
You can output whatever you want in widgets, and put them on dashboard or everywhere on the web (in your blog, website, etc.). +
Widgets can include graphs, tables, flash, text, images, etc. +
It's very easy to create a new plugin and widgets in Piwik. Have a look at this example file (/plugins/ExamplePlugin/ExamplePlugin.php). +
Happy coding!
+
You can easily use Jquery in widgets
+ "; + } + + /** + * Embed Piwik.org blog using widgetbox.com widget code + */ + function blogPiwik() + { + echo ' '; + } + + function photostreamMatt() + { + echo ' '; + } + + /** + * This method displays a text containing an help about "How to build plugins for Piwik". + * This help is then used on http://dev.piwik.org/trac/wiki/Plugins/GlobalFunctions + * + */ + function index() + { + $out = ''; + $out .= 'This page aims to list the different functions you can use when programming plugins for Piwik.
'; + $out .= 'Be careful, the following APIs may change in the near future as Piwik is still in development.
'; + + $out .= '

General

'; + $out .= '

Accessible from your plugin controller

'; + + $out .= '$this->date = current selected Piwik_Date object (class)
'; + $out .= '$period = Piwik_Common::getRequestVar("period"); - Get the current selected period
'; + $out .= '$idSite = Piwik_Common::getRequestVar("idSite"); - Get the selected idSite
'; + $out .= '$site = new Piwik_Site($idSite); - Build the Piwik_Site object (class)
'; + $out .= '$this->str_date = current selected date in YYYY-MM-DD format
'; + + $out .= '

Misc

'; + $out .= 'Piwik_AddMenu( $mainMenuName, $subMenuName, $url ); - Adds an entry to the menu in the Piwik interface (See the example in the UserCountry Plugin file)
'; + $out .= 'Piwik_AddWidget( $widgetCategory, $widgetName, $controllerName, $controllerAction, $customParameters = array()); - Adds a widget that users can add in the dashboard, or export using the Widgets link at the top of the screen. See the example in the UserCountry Plugin file or any other plugin)
'; + $out .= 'Piwik_Common::prefixTable("site") = ' . Piwik_Common::prefixTable("site") . '
'; + + + $out .= '

User access

'; + $out .= 'Piwik::getCurrentUserLogin() = ' . Piwik::getCurrentUserLogin() . '
'; + $out .= 'Piwik::isUserHasSomeAdminAccess() = ' . self::boolToString(Piwik::isUserHasSomeAdminAccess()) . '
'; + $out .= 'Piwik::isUserHasAdminAccess( array $idSites = array(1,2) ) = ' . self::boolToString(Piwik::isUserHasAdminAccess(array(1,2) )) . '
'; + $out .= 'Piwik::isUserHasViewAccess( array $idSites = array(1) ) = ' . self::boolToString(Piwik::isUserHasViewAccess(array(1))) . '
'; + $out .= 'Piwik::isUserIsSuperUser() = ' . self::boolToString(Piwik::isUserIsSuperUser()) . '
'; + + $out .= '

Execute SQL queries

'; + $txtQuery = "SELECT token_auth FROM ".Piwik_Common::prefixTable('user')." WHERE login = ?"; + $result = Piwik_FetchOne($txtQuery, array('anonymous')); + $out .= 'Piwik_FetchOne("'.$txtQuery.'", array("anonymous")) = ' . var_export($result,true) . '
'; + $out .= '
'; + + $query = Piwik_Query($txtQuery, array('anonymous')); + $fetched = $query->fetch(); + $token_auth = $fetched['token_auth']; + + $out .= '$query = Piwik_Query("'.$txtQuery.'", array("anonymous"))
'; + $out .= '$fetched = $query->fetch();
'; + $out .= 'At this point, we have: $fetched[\'token_auth\'] == '.var_export($token_auth,true) . '
'; + + $out .= '

Example Sites information API

'; + $out .= 'Piwik_SitesManager_API::getInstance()->getSitesWithViewAccess() =
' .var_export(Piwik_SitesManager_API::getInstance()->getSitesWithViewAccess(),true) . '

'; + $out .= 'Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess() =
' .var_export(Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess(),true) . '

'; + + $out .= '

Example API Users information

'; + $out .= 'View the list of API methods you can call on API reference
'; + $out .= 'For example you can try Piwik_UsersManager_API::getInstance()->getUsersSitesFromAccess("view"); or Piwik_UsersManager_API::getInstance()->deleteUser("userToDelete");
'; + + $out .= '

Javascript in Piwik

'; + $out .= '

i18n internationalization

'; + $out .= 'In order to translate strings within Javascript code, you can use the javascript function _pk_translate( token );. + '; + + $out .= '

Reload a widget in the dashboard

'; + $out .= 'It is sometimes useful to reload one widget in the dashboard (for example, every 20 seconds for a real time widget, or after a setting change). + You can easily force your widget to reload in the dashboard by calling the helper function piwik.dashboardObject.reloadEnclosingWidget($(this));.'; + + $out .= '

Smarty plugins

'; + $out .= 'There are some builtin plugins for Smarty especially developped for Piwik.
+ You can find them on the SVN at /trunk/core/SmartyPlugins.
+ More documentation to come about smarty plugins.
'; + + echo $out; + } + + static private function boolToString($bool) + { + return $bool ? "true" : "false"; + } +} diff --git a/oss/piwik/trunk/plugins/ExamplePlugin/lang/en.php b/oss/piwik/trunk/plugins/ExamplePlugin/lang/en.php new file mode 100644 index 00000000..c3fbbd03 --- /dev/null +++ b/oss/piwik/trunk/plugins/ExamplePlugin/lang/en.php @@ -0,0 +1,19 @@ + 'Example Plugin: This plugin shows how to create a very simple plugin, that exports two widgets in the Dashboard.', + 'ExamplePlugin_exampleWidgets' => 'Example Widgets', + 'ExamplePlugin_exampleWidget' => 'Example widget', + 'ExamplePlugin_blogPiwikRss' => 'Blog Piwik RSS', + 'ExamplePlugin_photostreamMatt' => 'Matt\'s Photos', +);