*/ // must be run within Dokuwiki if(!defined('DOKU_INC')) die(); /** * All DokuWiki plugins to extend the admin function * need to inherit from this class */ class admin_plugin_statistics extends DokuWiki_Admin_Plugin { /** @var string the currently selected page */ protected $opt = ''; /** @var string from date in YYYY-MM-DD */ protected $from = ''; /** @var string to date in YYYY-MM-DD */ protected $to = ''; /** @var int Offset to use when displaying paged data */ protected $start = 0; /** @var string MySQL timelimit statement */ protected $tlimit = ''; /** @var helper_plugin_statistics */ protected $hlp; /** * Available statistic pages */ protected $pages = array( 'dashboard' => 1, 'content' => array( 'page', 'edits', 'images', 'downloads', 'history', ), 'users' => array( 'topuser', 'topeditor', 'topgroup', 'topgroupedit', 'seenusers', ), 'links' => array( 'referer', 'newreferer', 'outlinks', ), 'search' => array( 'searchengines', 'searchphrases', 'searchwords', 'internalsearchphrases', 'internalsearchwords', ), 'technology' => array( 'browsers', 'os', 'countries', 'resolution', 'viewport', ), ); /** @var array keeps a list of all real content pages, generated from above array */ protected $allowedpages = array(); /** * Initialize the helper */ public function __construct() { $this->hlp = plugin_load('helper', 'statistics'); // build a list of pages foreach($this->pages as $key => $val) { if(is_array($val)) { $this->allowedpages = array_merge($this->allowedpages, $val); }else { $this->allowedpages[] = $key; } } } /** * Access for managers allowed */ public function forAdminOnly() { return false; } /** * return sort order for position in admin menu */ public function getMenuSort() { return 350; } /** * handle user request */ public function handle() { $this->opt = preg_replace('/[^a-z]+/', '', $_REQUEST['opt']); if(!in_array($this->opt, $this->allowedpages)) $this->opt = 'dashboard'; $this->start = (int) $_REQUEST['s']; $this->setTimeframe($_REQUEST['f'], $_REQUEST['t']); } /** * set limit clause */ public function setTimeframe($from, $to) { // swap if wrong order if($from > $to) list($from, $to) = array($to, $from); $this->tlimit = $this->hlp->Query()->mktlimit($from, $to); $this->from = $from; $this->to = $to; } /** * Output the Statistics */ function html() { echo '
'; echo '

' . $this->getLang('menu') . '

'; $this->html_timeselect(); tpl_flush(); $method = 'html_' . $this->opt; if(method_exists($this, $method)) { echo '
'; echo '

' . $this->getLang($this->opt) . '

'; $this->$method(); echo '
'; } echo '
'; } /** * Return the TOC * * @return array */ function getTOC() { $toc = array(); foreach($this->pages as $key => $info) { if (is_array($info)) { $toc[] = html_mktocitem( '', $this->getLang($key), 1, '' ); foreach($info as $page) { $toc[] = html_mktocitem( '?do=admin&page=statistics&opt=' . $page . '&f=' . $this->from . '&t=' . $this->to, $this->getLang($page), 2, '' ); } } else { $toc[] = html_mktocitem( '?do=admin&page=statistics&opt=' . $key . '&f=' . $this->from . '&t=' . $this->to, $this->getLang($key), 1, '' ); } } return $toc; } function html_graph($name, $width, $height) { $url = DOKU_BASE . 'lib/plugins/statistics/img.php?img=' . $name . '&f=' . $this->from . '&t=' . $this->to; echo ''; } /** * Outputs pagination links * * @param int $limit * @param int $next */ function html_pager($limit, $next) { echo '
'; if($this->start > 0) { $go = max($this->start - $limit, 0); echo ''; } if($next) { $go = $this->start + $limit; echo ''; } echo '
'; } /** * Print the time selection menu */ function html_timeselect() { $today = date('Y-m-d'); $last1 = date('Y-m-d', time() - (60 * 60 * 24)); $last7 = date('Y-m-d', time() - (60 * 60 * 24 * 7)); $last30 = date('Y-m-d', time() - (60 * 60 * 24 * 30)); echo '
'; echo '' . $this->getLang('time_select') . ' '; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; echo ''; echo '
'; } /** * Print an introductionary screen */ function html_dashboard() { echo '

' . $this->getLang('intro_dashboard') . '

'; // general info echo '
'; $result = $this->hlp->Query()->aggregate($this->tlimit); echo ''; echo ''; echo '
'; $this->html_graph('dashboardviews', 700, 280); $this->html_graph('dashboardwiki', 700, 280); echo '
'; // top pages today echo '
'; echo '

' . $this->getLang('dash_mostpopular') . '

'; $result = $this->hlp->Query()->pages($this->tlimit, $this->start, 15); $this->html_resulttable($result); echo '' . $this->getLang('more') . ''; echo '
'; // top referer today echo '
'; echo '

' . $this->getLang('dash_newincoming') . '

'; $result = $this->hlp->Query()->newreferer($this->tlimit, $this->start, 15); $this->html_resulttable($result); echo '' . $this->getLang('more') . ''; echo '
'; // top searches today echo '
'; echo '

' . $this->getLang('dash_topsearch') . '

'; $result = $this->hlp->Query()->searchphrases(true, $this->tlimit, $this->start, 15); $this->html_resulttable($result); echo '' . $this->getLang('more') . ''; echo '
'; } function html_history() { echo '

' . $this->getLang('intro_history') . '

'; $this->html_graph('history_page_count', 600, 200); $this->html_graph('history_page_size', 600, 200); $this->html_graph('history_media_count', 600, 200); $this->html_graph('history_media_size', 600, 200); } function html_countries() { echo '

' . $this->getLang('intro_countries') . '

'; $this->html_graph('countries', 400, 200); $result = $this->hlp->Query()->countries($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_page() { echo '

' . $this->getLang('intro_page') . '

'; $result = $this->hlp->Query()->pages($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_edits() { echo '

' . $this->getLang('intro_edits') . '

'; $result = $this->hlp->Query()->edits($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_images() { echo '

' . $this->getLang('intro_images') . '

'; $result = $this->hlp->Query()->imagessum($this->tlimit); echo '

'; echo sprintf($this->getLang('trafficsum'), $result[0]['cnt'], filesize_h($result[0]['filesize'])); echo '

'; $result = $this->hlp->Query()->images($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_downloads() { echo '

' . $this->getLang('intro_downloads') . '

'; $result = $this->hlp->Query()->downloadssum($this->tlimit); echo '

'; echo sprintf($this->getLang('trafficsum'), $result[0]['cnt'], filesize_h($result[0]['filesize'])); echo '

'; $result = $this->hlp->Query()->downloads($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_browsers() { echo '

' . $this->getLang('intro_browsers') . '

'; $this->html_graph('browsers', 400, 200); $result = $this->hlp->Query()->browsers($this->tlimit, $this->start, 150, true); $this->html_resulttable($result, '', 150); } function html_topuser(){ echo '

' . $this->getLang('intro_topuser') . '

'; $this->html_graph('topuser', 400, 200); $result = $this->hlp->Query()->topuser($this->tlimit, $this->start, 150, true); $this->html_resulttable($result, '', 150); } function html_topeditor(){ echo '

' . $this->getLang('intro_topeditor') . '

'; $this->html_graph('topeditor', 400, 200); $result = $this->hlp->Query()->topeditor($this->tlimit, $this->start, 150, true); $this->html_resulttable($result, '', 150); } function html_topgroup(){ echo '

' . $this->getLang('intro_topgroup') . '

'; $this->html_graph('topgroup', 400, 200); $result = $this->hlp->Query()->topgroup($this->tlimit, $this->start, 150, true); $this->html_resulttable($result, '', 150); } function html_topgroupedit(){ echo '

' . $this->getLang('intro_topgroupedit') . '

'; $this->html_graph('topgroupedit', 400, 200); $result = $this->hlp->Query()->topgroupedit($this->tlimit, $this->start, 150, true); $this->html_resulttable($result, '', 150); } function html_os() { echo '

' . $this->getLang('intro_os') . '

'; $this->html_graph('os', 400, 200); $result = $this->hlp->Query()->os($this->tlimit, $this->start, 150, true); $this->html_resulttable($result, '', 150); } function html_referer() { $result = $this->hlp->Query()->aggregate($this->tlimit); $all = $result['search'] + $result['external'] + $result['direct']; if($all) { printf( '

' . $this->getLang('intro_referer') . '

', $all, $result['direct'], (100 * $result['direct'] / $all), $result['search'], (100 * $result['search'] / $all), $result['external'], (100 * $result['external'] / $all) ); } $result = $this->hlp->Query()->referer($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_newreferer() { echo '

' . $this->getLang('intro_newreferer') . '

'; $result = $this->hlp->Query()->newreferer($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_outlinks() { echo '

' . $this->getLang('intro_outlinks') . '

'; $result = $this->hlp->Query()->outlinks($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_searchphrases() { echo '

' . $this->getLang('intro_searchphrases') . '

'; $result = $this->hlp->Query()->searchphrases(true, $this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_searchwords() { echo '

' . $this->getLang('intro_searchwords') . '

'; $result = $this->hlp->Query()->searchwords(true, $this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_internalsearchphrases() { echo '

' . $this->getLang('intro_internalsearchphrases') . '

'; $result = $this->hlp->Query()->searchphrases(false, $this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_internalsearchwords() { echo '

' . $this->getLang('intro_internalsearchwords') . '

'; $result = $this->hlp->Query()->searchwords(false, $this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_searchengines() { echo '

' . $this->getLang('intro_searchengines') . '

'; $this->html_graph('searchengines', 400, 200); $result = $this->hlp->Query()->searchengines($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_resolution() { echo '

' . $this->getLang('intro_resolution') . '

'; $this->html_graph('resolution', 650, 490); $result = $this->hlp->Query()->resolution($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_viewport() { echo '

' . $this->getLang('intro_viewport') . '

'; $this->html_graph('viewport', 650, 490); $result = $this->hlp->Query()->viewport($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } function html_seenusers() { echo '

' . $this->getLang('intro_seenusers') . '

'; $result = $this->hlp->Query()->seenusers($this->tlimit, $this->start, 150); $this->html_resulttable($result, '', 150); } /** * Display a result in a HTML table */ function html_resulttable($result, $header = '', $pager = 0) { echo ''; if(is_array($header)) { echo ''; foreach($header as $h) { echo ''; } echo ''; } $count = 0; if(is_array($result)) foreach($result as $row) { echo ''; foreach($row as $k => $v) { if($k == 'res_x') continue; if($k == 'res_y') continue; echo ''; } echo ''; if($pager && ($count == $pager)) break; $count++; } echo '
' . hsc($h) . '
'; if($k == 'page') { echo ''; echo hsc($v); echo ''; } elseif($k == 'media') { echo ''; echo hsc($v); echo ''; } elseif($k == 'filesize') { echo filesize_h($v); } elseif($k == 'url') { $url = hsc($v); $url = preg_replace('/^https?:\/\/(www\.)?/', '', $url); if(strlen($url) > 45) { $url = substr($url, 0, 30) . ' … ' . substr($url, -15); } echo ''; echo $url; echo ''; } elseif($k == 'ilookup') { echo 'Search'; } elseif($k == 'lookup') { echo ''; echo 'Google'; echo ' '; echo ''; echo 'Yahoo!'; echo ' '; echo ''; echo 'Bing'; echo ' '; } elseif($k == 'engine') { include_once(dirname(__FILE__) . '/inc/searchengines.php'); if(isset($SEARCHENGINEINFO[$v])) { echo '' . $SEARCHENGINEINFO[$v][0] . ''; } else { echo hsc(ucwords($v)); } } elseif($k == 'eflag') { $this->html_icon('search', $v); } elseif($k == 'bflag') { $this->html_icon('browser', $v); } elseif($k == 'osflag') { $this->html_icon('os', $v); } elseif($k == 'cflag') { $this->html_icon('flags', $v); } elseif($k == 'html') { echo $v; } else { echo hsc($v); } echo '
'; if($pager) $this->html_pager($pager, count($result) > $pager); } function html_icon($type, $value) { $value = strtolower(preg_replace('/[^\w]+/', '', $value)); $value = str_replace(' ', '_', $value); $file = 'lib/plugins/statistics/ico/' . $type . '/' . $value . '.png'; if($type == 'flags') { $w = 18; $h = 12; } else { $w = 16; $h = 16; } if(file_exists(DOKU_INC . $file)) { echo '' . hsc($value) . ''; } } }