Skip to content

Instantly share code, notes, and snippets.

@mattsches
Last active December 19, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsches/08f6a8dddc41366b5402 to your computer and use it in GitHub Desktop.
Save mattsches/08f6a8dddc41366b5402 to your computer and use it in GitHub Desktop.
Den ersten Entwurf des Piwik-Plugins (vgl. http://board.s9y.org/viewtopic.php?f=4&t=19403) etwas angepasst: PHP5 benötigt; PEAR-HTTP-Request eingebaut und in eigene Methode gezogen; "site_id" konfigurierbar.
<?php # $Id$
require_once S9Y_PEAR_PATH . 'HTTP/Request.php';
@serendipity_plugin_api::load_language(dirname(__FILE__));
class serendipity_plugin_piwik extends serendipity_plugin {
public $title = PLUGIN_SIDEBAR_PIWIK_NAME;
protected $token;
protected $url;
protected $entries;
protected $entries_max;
protected $entries_remove;
protected $dependencies;
/**
* @param serendipity_property_bag $propbag
* @return void
*/
public function introspect(&$propbag)
{
$this->title = $this->get_config('title', $this->title);
$propbag->add('name', PLUGIN_SIDEBAR_PIWIK_NAME);
$propbag->add('description', PLUGIN_SIDEBAR_PIWIK_DESC);
$propbag->add('stackable', true);
$propbag->add('author', 'Bernd Distler');
$propbag->add('version', '0.2');
$propbag->add('requirements', array(
'serendipity' => '1.3.1', // don't know about that
'smarty' => '2.6.7',
'php' => '5.1.0'
));
$propbag->add('configuration', array('title','token','site_id','url','entries','entries_max','entries_remove'));
$propbag->add('groups', array('FRONTEND_EXTERNAL_SERVICES'));
$this->dependencies = array('serendipity_event_piwik' => 'remove');
}
/**
* @param string $name
* @param serendipity_property_bag $propbag
* @return bool
*/
public function introspect_config_item($name, &$propbag)
{
switch($name) {
case 'title':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_SIDEBAR_PIWIK_TITLE);
$propbag->add('description', PLUGIN_SIDEBAR_PIWIK_TITLE);
$propbag->add('default', '');
break;
case 'token':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_SIDEBAR_PIWIK_TOKEN);
$propbag->add('description', PLUGIN_SIDEBAR_PIWIK_TOKEN);
$propbag->add('default', '');
break;
case 'site_id':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_SIDEBAR_PIWIK_SITEID);
$propbag->add('description', PLUGIN_SIDEBAR_PIWIK_SITEID);
$propbag->add('default', '');
break;
case 'url':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_SIDEBAR_PIWIK_URL);
$propbag->add('description', PLUGIN_SIDEBAR_PIWIK_URL);
$propbag->add('default', 'http://example.org/piwik/');
break;
case 'entries':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_SIDEBAR_PIWIK_ENTRIES);
$propbag->add('description', PLUGIN_SIDEBAR_PIWIK_ENTRIES);
$propbag->add('default', 'true');
break;
case 'entries_max':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_SIDEBAR_PIWIK_ENTRIES_MAX);
$propbag->add('description', PLUGIN_SIDEBAR_PIWIK_ENTRIES_MAX);
$propbag->add('default', '5');
break;
case 'entries_remove':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_SIDEBAR_PIWIK_ENTRIES_REMOVE);
$propbag->add('description', PLUGIN_SIDEBAR_PIWIK_ENTRIES_REMOVE);
$propbag->add('default', '');
break;
}
return true;
}
/**
* @param string $title
* @return void
*/
public function generate_content(&$title) {
$title = $this->get_config('title', $title ? $title : $this->title);
$token = $this->get_config('token', $this->token);
$site_id = $this->get_config('site_id', 1);
$url = $this->get_config('url', $this->url);
$entries = $this->get_config('entries', $this->entries);
$entries_max = $this->get_config('entries_max', $this->entries_max);
$entries_remove = $this->get_config('entries_remove', $this->entries_remove);
$error = false;
$piwik_array_pagesurls = array();
$piwik_array_pagestitles = array();
if ($entries) {
/* most viewed entries of current week */
$api_url = $url;
$api_url .= "?module=API&method=Actions.getPageUrls";
$api_url .= "&idSite=" . $site_id . "&period=week&date=today";
$api_url .= "&format=PHP&filter_limit=" . $entries_max;
$api_url .= "&flat=1&disableLink=1";
$api_url .= "&token_auth=$token";
try {
$piwik_array_pagesurls = unserialize($this->requestPiwikData($api_url));
} catch (Exception $e) {
$error = true;
}
$api_url = $url;
$api_url .= "?module=API&method=Actions.getPageTitles";
$api_url .= "&idSite=" . $site_id . "&period=week&date=today";
$api_url .= "&format=PHP&filter_limit=" . $entries_max;
$api_url .= "&flat=1&disableLink=1";
$api_url .= "&token_auth=$token";
try {
$piwik_array_pagestitles = unserialize($this->requestPiwikData($api_url));
} catch (Exception $e) {
$error = true;
}
if ($error) {
return;
}
for ($i = 0; $i < count($piwik_array_pagesurls); $i++) {
$piwik_array_pagesurls[$i]['label'] = $piwik_array_pagestitles[$i]['label'];
}
$entries_view = PLUGIN_SIDEBAR_PIWIK_ENTRIES_VIEWS;
echo "\n<ol>\n";
foreach ($piwik_array_pagesurls as $row) {
$piwik_content_pageurl = htmlspecialchars(
html_entity_decode(urldecode($row['url']), ENT_QUOTES),
ENT_QUOTES
);
$piwik_content_pagelabel = htmlspecialchars(
html_entity_decode(urldecode($row['label']), ENT_QUOTES),
ENT_QUOTES
);
$piwik_content_pagelabel = str_replace($entries_remove, "", $piwik_content_pagelabel);
$piwik_content_hits = $row['nb_visits'];
echo ' <li><a href="' . $piwik_content_pageurl . '" title="' . $piwik_content_hits . ' ' . $entries_view . '">' . $piwik_content_pagelabel . "</a></li>\n";
}
echo "\n</ol>\n";
}
}
/**
* @param $api_url
* @return bool|mixed|string
*/
protected function requestPiwikData($api_url)
{
serendipity_request_start();
$req = new HTTP_Request($api_url);
if (PEAR::isError($req->sendRequest()) || $req->getResponseCode() != '200') {
$piwik_fetched = file_get_contents($api_url);
} else {
$piwik_fetched = $req->getResponseBody();
}
serendipity_request_end();
return $piwik_fetched;
}
}
/* vim: set sts=4 ts=4 expandtab : */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment