添加 Piwik 到代码库中。

+YUCHENG HU+

git-svn-id: https://svn.code.sf.net/p/hawebs/svn@480 a2543c7e-f6e9-4f8a-8bff-1ffc34733512
This commit is contained in:
YuCheng Hu
2010-07-10 04:49:54 +00:00
parent 31303f574e
commit a7909e345e
4 changed files with 419 additions and 0 deletions
@@ -0,0 +1,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: ExampleFeedburner.php 2265 2010-06-03 17:46:05Z vipsoft $
*
* @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'))
);
}
}
}
@@ -0,0 +1,57 @@
<script type="text/javascript">
var idSite = {$idSite};
{literal}
$(document).ready(function(){
function getName()
{
return $("#feedburnerName").val();
}
$("#feedburnerSubmit").click( function(){
var feedburnerName = getName();
$.get('?module=ExampleFeedburner&action=saveFeedburnerName&idSite='+idSite+'&name='+feedburnerName);
piwik.dashboardObject.reloadEnclosingWidget($(this));
});
});
</script>
<style>
.metric { font-weight:bold;text-align:left; }
.feedburner td { padding:0px 3px; }
</style>
{/literal}
{if !is_array($fbStats)}
{$fbStats}
{else}
<table class='feedburner' align="center" cellpadding="2" style='text-align:center'>
<tr>
<td></td>
<td style="text-decoration:underline;">Previous</td>
<td style="text-decoration:underline;">Yesterday</td>
<td></td>
</tr>
<tr>
<td class='metric'>Circulation</td>
<td>{$fbStats[0][0]}</td>
<td>{$fbStats[0][1]}</td>
<td>{$fbStats[0][2]}</td>
</tr>
<tr>
<td class='metric'>Hits</td>
<td>{$fbStats[1][0]}</td>
<td>{$fbStats[1][1]}</td>
<td>{$fbStats[1][2]}</td>
</tr>
<tr>
<td class='metric'>Reach</td>
<td>{$fbStats[2][0]}</td>
<td>{$fbStats[2][1]}</td>
<td>{$fbStats[2][2]}</td>
</tr>
</table>
{/if}
<center>
<input id="feedburnerName" type="text" value="{$feedburnerFeedName}" />
<input id="feedburnerSubmit" type="submit" value="ok" />
</center>
@@ -0,0 +1,190 @@
<?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: ExamplePlugin.php 2265 2010-06-03 17:46:05Z vipsoft $
*
* @category Piwik_Plugins
* @package Piwik_ExamplePlugin
*/
/**
*
* @package Piwik_ExamplePlugin
*/
class Piwik_ExamplePlugin extends Piwik_Plugin
{
/**
* Return information about this plugin.
*
* @see Piwik_Plugin
*
* @return array
*/
public function getInformation()
{
return array(
'description' => 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! <br />";
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! <br /> You can output whatever you want in widgets, and put them on dashboard or everywhere on the web (in your blog, website, etc.).
<br />Widgets can include graphs, tables, flash, text, images, etc.
<br />It's very easy to create a new plugin and widgets in Piwik. Have a look at this example file (/plugins/ExamplePlugin/ExamplePlugin.php).
<div id='happycoding'><i>Happy coding!</i></div>
<div id='jsenabled'>You can easily use Jquery in widgets</div>
<script type=\"text/javascript\">$('#happycoding').hide().fadeIn(5000);$('#jsenabled').hide().css({'color':'red'}).fadeIn(10000);</script>";
}
/**
* Embed Piwik.org blog using widgetbox.com widget code
*/
function blogPiwik()
{
echo '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash" width="400px" height="343px" id="InsertWidget_0bf84c7c-70b5-41c1-adbc-6f4f823c598c" align="middle"><param name="movie" value="http://widgetserver.com/syndication/flash/wrapper/InsertWidget.swf"/><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="menu" value="false" /><param name="flashvars" value="r=2&appId=0bf84c7c-70b5-41c1-adbc-6f4f823c598c" /> <embed src="http://widgetserver.com/syndication/flash/wrapper/InsertWidget.swf" name="InsertWidget_0bf84c7c-70b5-41c1-adbc-6f4f823c598c" width="400px" height="343px" quality="high" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" align="middle" flashvars="r=2&appId=0bf84c7c-70b5-41c1-adbc-6f4f823c598c" /></object>';
}
function photostreamMatt()
{
echo '<iframe align=center src=http://www.flickr.com/slideShow/index.gne?user_id=34965144@N00&set_id=72157602308487455 frameBorder=0 width=380 scrolling=no height=500></iframe> ';
}
/**
* 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 .= '<i>This page aims to list the different functions you can use when programming plugins for Piwik.</i><br />';
$out .= '<b>Be careful, the following APIs may change in the near future as Piwik is still in development.</b><br />';
$out .= '<h2>General</h2>';
$out .= '<h3>Accessible from your plugin controller</h3>';
$out .= '<code>$this->date</code> = current selected <b>Piwik_Date</b> object (<a href="http://dev.piwik.org/trac/browser/trunk/core/Date.php">class</a>)<br />';
$out .= '<code>$period = Piwik_Common::getRequestVar("period");</code> - Get the current selected period<br />';
$out .= '<code>$idSite = Piwik_Common::getRequestVar("idSite");</code> - Get the selected idSite<br />';
$out .= '<code>$site = new Piwik_Site($idSite);</code> - Build the Piwik_Site object (<a href="http://dev.piwik.org/trac/browser/trunk/core/Site.php">class</a>)<br />';
$out .= '<code>$this->str_date</code> = current selected date in YYYY-MM-DD format<br />';
$out .= '<h3>Misc</h3>';
$out .= '<code>Piwik_AddMenu( $mainMenuName, $subMenuName, $url );</code> - Adds an entry to the menu in the Piwik interface (See the example in the <a href="http://dev.piwik.org/trac/browser/trunk/plugins/UserCountry/UserCountry.php#L146">UserCountry Plugin file</a>)<br />';
$out .= '<code>Piwik_AddWidget( $widgetCategory, $widgetName, $controllerName, $controllerAction, $customParameters = array());</code> - 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 <a href="http://dev.piwik.org/trac/browser/trunk/plugins/UserCountry/UserCountry.php#L143">UserCountry Plugin file</a> or any other plugin)<br />';
$out .= '<code>Piwik_Common::prefixTable("site")</code> = <b>' . Piwik_Common::prefixTable("site") . '</b><br />';
$out .= '<h2>User access</h2>';
$out .= '<code>Piwik::getCurrentUserLogin()</code> = <b>' . Piwik::getCurrentUserLogin() . '</b><br />';
$out .= '<code>Piwik::isUserHasSomeAdminAccess()</code> = <b>' . self::boolToString(Piwik::isUserHasSomeAdminAccess()) . '</b><br />';
$out .= '<code>Piwik::isUserHasAdminAccess( array $idSites = array(1,2) )</code> = <b>' . self::boolToString(Piwik::isUserHasAdminAccess(array(1,2) )) . '</b><br />';
$out .= '<code>Piwik::isUserHasViewAccess( array $idSites = array(1) ) </code> = <b>' . self::boolToString(Piwik::isUserHasViewAccess(array(1))) . '</b><br />';
$out .= '<code>Piwik::isUserIsSuperUser()</code> = <b>' . self::boolToString(Piwik::isUserIsSuperUser()) . '</b><br />';
$out .= '<h2>Execute SQL queries</h2>';
$txtQuery = "SELECT token_auth FROM ".Piwik_Common::prefixTable('user')." WHERE login = ?";
$result = Piwik_FetchOne($txtQuery, array('anonymous'));
$out .= '<code>Piwik_FetchOne("'.$txtQuery.'", array("anonymous"))</code> = <b>' . var_export($result,true) . '</b><br />';
$out .= '<br />';
$query = Piwik_Query($txtQuery, array('anonymous'));
$fetched = $query->fetch();
$token_auth = $fetched['token_auth'];
$out .= '<code>$query = Piwik_Query("'.$txtQuery.'", array("anonymous"))</code><br />';
$out .= '<code>$fetched = $query->fetch();</code><br />';
$out .= 'At this point, we have: <code>$fetched[\'token_auth\'] == <b>'.var_export($token_auth,true) . '</b></code><br />';
$out .= '<h2>Example Sites information API</h2>';
$out .= '<code>Piwik_SitesManager_API::getInstance()->getSitesWithViewAccess()</code> = <b><pre>' .var_export(Piwik_SitesManager_API::getInstance()->getSitesWithViewAccess(),true) . '</pre></b><br />';
$out .= '<code>Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess()</code> = <b><pre>' .var_export(Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess(),true) . '</pre></b><br />';
$out .= '<h2>Example API Users information</h2>';
$out .= 'View the list of API methods you can call on <a href="http://dev.piwik.org/trac/wiki/API/Reference#Methods">API reference</a><br />';
$out .= 'For example you can try <code>Piwik_UsersManager_API::getInstance()->getUsersSitesFromAccess("view");</code> or <code>Piwik_UsersManager_API::getInstance()->deleteUser("userToDelete");</code><br />';
$out .= '<h2>Javascript in Piwik</h2>';
$out .= '<h3>i18n internationalization</h3>';
$out .= 'In order to translate strings within Javascript code, you can use the javascript function _pk_translate( token );.
<ul><li>The "token" parameter is the string unique key found in the translation file. For this token string to be available in Javascript, you must
suffix your token by "_js" in the language file. For example, you can add <code>\'Goals_AddGoal_js\' => \'Add Goal\',</code> in the lang/en.php file</li>
<li>You then need to instruct Piwik to load your Javascript translations for your plugin; by default, all translation strings are not loaded in Javascript for performance reasons. This can be done by calling the Smarty modifier before any your javascript includes that would be using the translations, eg.
<code>{loadJavascriptTranslations plugins=\'$YOUR_PLUGIN_NAME\'}</code>. In our previous example, the $YOUR_PLUGIN_NAME being Goals, we would write <code>{loadJavascriptTranslations plugins=\'Goals\'}</code>
</li><li>You can then print this string from your JS code by doing <code>_pk_translate(\'Goals_AddGoal_js\');</code>.
</li></ul>';
$out .= '<h3>Reload a widget in the dashboard</h3>';
$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 <code>piwik.dashboardObject.reloadEnclosingWidget($(this));</code>.';
$out .= '<h2>Smarty plugins</h2>';
$out .= 'There are some builtin plugins for Smarty especially developped for Piwik. <br />
You can find them on the <a href="http://dev.piwik.org/trac/browser/trunk/core/SmartyPlugins">SVN at /trunk/core/SmartyPlugins</a>. <br />
More documentation to come about smarty plugins.<br />';
echo $out;
}
static private function boolToString($bool)
{
return $bool ? "true" : "false";
}
}
@@ -0,0 +1,19 @@
<?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: en.php 1816 2010-01-29 21:27:47Z vipsoft $
*
* @category Piwik_Plugins
* @package Piwik_ExamplePlugin
*/
$translations = array(
'ExamplePlugin_PluginDescription' => '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',
);