diff --git a/serendipity_plugin_piwik/ChangeLog b/serendipity_plugin_piwik/ChangeLog new file mode 100644 index 00000000..c8438cb3 --- /dev/null +++ b/serendipity_plugin_piwik/ChangeLog @@ -0,0 +1,18 @@ +0.4.0 + * with Piwik v2 it is possible to use lsat x days for most + read entries instead of current week + * some optimizations for fetching most read entries (smaller arrays) + +0.3.1 + * included some debug-logging + * included "known bugs" into README.markdown + +0.3 + * renaming of language strings + * included some live-statistics + +0.2 + * some changes and optimisations by mattsches, thanks! + +0.1 + * first try, a lot of things could behave wrong diff --git a/serendipity_plugin_piwik/README.markdown b/serendipity_plugin_piwik/README.markdown new file mode 100644 index 00000000..7f652850 --- /dev/null +++ b/serendipity_plugin_piwik/README.markdown @@ -0,0 +1,16 @@ +# serendipity_plugin_piwik + +## Description + +This is just a small sidebar-plugin which can connect to your Piwik-installation. If you want to +use it, you have to set up an user who has at least read-access to piwik. Within user-managment of +piwik you can create a new one, if you don't want to use admin. + +If you don't need one part of the statistics, just disable it. + +If you want to change the order of output, install the plugin twice, it's stackable :) +Now, you can can enable or disable any part of every instance until you are happy ;) + +## known bugs + +In some cases (i think, it only happens if main/index-page is in the top xx-list) page-url and page-title of section "most read entries" are not for the same page. I don't know why, but Piwik gives different output for the fetching of url and title and at the moment i don't know how i could solve this problem. diff --git a/serendipity_plugin_piwik/UTF-8/lang_de.inc.php b/serendipity_plugin_piwik/UTF-8/lang_de.inc.php new file mode 100755 index 00000000..e1fc31a1 --- /dev/null +++ b/serendipity_plugin_piwik/UTF-8/lang_de.inc.php @@ -0,0 +1,38 @@ +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.4.0'); + $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','live_show','live_title','live_minutes','entries_show','entries_title','entries_days','entries_max','entries_remove','debug')); + $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_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_TITLE_DESC); + $propbag->add('default', ''); + break; + case 'token': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_TOKEN_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_TOKEN_DESC); + $propbag->add('default', ''); + break; + case 'site_id': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_SITEID_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_SITEID_DESC); + $propbag->add('default', ''); + break; + case 'url': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_URL_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_URL_DESC); + $propbag->add('default', 'http://example.org/piwik/'); + break; + case 'live_show': + $propbag->add('type', 'boolean'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_LIVE_SHOW_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_LIVE_SHOW_DESC); + $propbag->add('default', 'true'); + break; + case 'live_title': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_LIVE_TITLE_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_LIVE_TITLE_DESC); + $propbag->add('default', ''); + break; + case 'live_minutes': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_LIVE_MINUTES_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_LIVE_MINUTES_DESC); + $propbag->add('default', '30'); + break; + case 'entries_show': + $propbag->add('type', 'boolean'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_ENTRIES_SHOW_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_ENTRIES_SHOW_DESC); + $propbag->add('default', 'true'); + break; + case 'entries_title': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_ENTRIES_TITLE_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_ENTRIES_TITLE_DESC); + $propbag->add('default', ''); + break; + case 'entries_days': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_ENTRIES_DAYS_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_ENTRIES_DAYS_DESC); + $propbag->add('default', '7'); + break; + case 'entries_max': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_ENTRIES_MAX_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_ENTRIES_MAX_DESC); + $propbag->add('default', '5'); + break; + case 'entries_remove': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_ENTRIES_REMOVE_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_ENTRIES_REMOVE_DESC); + $propbag->add('default', ''); + break; + case 'debug': + $propbag->add('type', 'boolean'); + $propbag->add('name', PLUGIN_SIDEBAR_PIWIK_DEBUG_NAME); + $propbag->add('description', PLUGIN_SIDEBAR_PIWIK_DEBUG_DESC); + $propbag->add('default', 'false'); + 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', $this->site_id); + $url = $this->get_config('url', $this->url); + $live_show = $this->get_config('live_show', $this->live_show); + $live_title = $this->get_config('live_title', $this->live_title); + $live_minutes = $this->get_config('live_minutes', $this->live_minutes); + $entries_show = $this->get_config('entries_show', $this->entries_show); + $entries_title = $this->get_config('entries_title', $this->entries_title); + $entries_days = $this->get_config('entries_days', $this->entries_days); + $entries_max = $this->get_config('entries_max', $this->entries_max); + $entries_remove = $this->get_config('entries_remove', $this->entries_remove); + $debug = $this->get_config('debug', $this->debug); + $error = false; + $piwik_array_pagesurls = array(); + $piwik_array_pagestitles = array(); + $piwik_array_live = array(); + + /* live statistics from last xx minutes */ + if ($live_show) { + + $api_url = $url; + $api_url .= "?module=API&method=Live.getCounters"; + $api_url .= "&idSite=" . $site_id . "&lastMinutes=" . $live_minutes; + $api_url .= "&format=PHP"; + $api_url .= "&token_auth=$token"; + if ($debug) {$this->debug_append('Live.getCounters - API-URL: '.$api_url);} + try { + $piwik_array_live = unserialize($this->requestPiwikData($api_url)); + } catch (Exception $e) { + $error = true; + } + if ($error) { + return; + } + + if ($live_title <> '') {echo "\n

".$live_title."

\n";} + echo '\n"; + + } + + /* most viewed entries of current week */ + if ($entries_show) { + // read Actions.getPageUrls from Piwik + $api_url = $url.'index.php'; + $api_url .= "?module=API&method=Actions.getPageUrls"; + $api_url .= "&idSite=" . $site_id; + if ($entries_days == '0') { + $api_url .= "&period=week&date=today"; + } else { + $api_url .= "&period=range&date=previous".$entries_days; + } + $api_url .= "&format=PHP&filter_limit=" . $entries_max; + $api_url .= "&flat=1&disableLink=1"; + $api_url .= "&showColumns=label,url,nb_visits"; + $api_url .= "&token_auth=$token"; + if ($debug) {$this->debug_append('Actions.getPageUrls - API-URL: '.$api_url);} + try { + $piwik_array_pagesurls = unserialize($this->requestPiwikData($api_url)); + } catch (Exception $e) { + $error = true; + } + + // read Actions.getPageTitles from Piwik + $api_url = $url.'index.php'; + $api_url .= "?module=API&method=Actions.getPageTitles"; + $api_url .= "&idSite=" . $site_id; + if ($entries_days == '0') { + $api_url .= "&period=week&date=today"; + } else { + $api_url .= "&period=range&date=previous".$entries_days; + } + $api_url .= "&format=PHP&filter_limit=" . $entries_max; + $api_url .= "&flat=1&disableLink=1"; + $api_url .= "&showColumns=label"; + $api_url .= "&token_auth=$token"; + if ($debug) {$this->debug_append('Actions.getPageTitles - API-URL: '.$api_url);} + try { + $piwik_array_pagestitles = unserialize($this->requestPiwikData($api_url)); + } catch (Exception $e) { + $error = true; + } + if ($error) { + return; + } + + // take pagetitles from seconed array and write it into first one + for ($i = 0; $i < count($piwik_array_pagesurls); $i++) { + $piwik_array_pagesurls[$i]['label'] = $piwik_array_pagestitles[$i]['label']; + } + + if ($entries_title <> '') {echo "\n

".$entries_title."

\n";} + echo "\n
    \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 '
  1. ' . $piwik_content_pagelabel . "
  2. \n"; + } + echo "\n
\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; + } + + + function debug_append ($debug_message) + { + $logfile = $serendipity['serendipityPath'] . 'templates_c/piwik_debug.log'; + $fp = @fopen($logfile, 'a+'); + fwrite($fp, "\n".date('Y-m-d H:i:s', serendipity_serverOffsetHour()).' '.$debug_message); + fclose($fp); + } +} + +/* vim: set sts=4 ts=4 expandtab : */