From 98be6429ce6efaa013146af9145aa572816adb89 Mon Sep 17 00:00:00 2001 From: Guillaume Turri <guillaume.turri@gmail.com> Date: Thu, 9 Dec 2010 22:03:59 +0100 Subject: [PATCH] Add support for autosubmitting popularity data FS#2025 --- lib/plugins/popularity/admin.php | 269 +++++++----------------- lib/plugins/popularity/lang/en/lang.php | 4 + 2 files changed, 78 insertions(+), 195 deletions(-) diff --git a/lib/plugins/popularity/admin.php b/lib/plugins/popularity/admin.php index 71ea40799..22ddb245b 100644 --- a/lib/plugins/popularity/admin.php +++ b/lib/plugins/popularity/admin.php @@ -13,21 +13,15 @@ if(!defined('DOKU_INC')) die(); * need to inherit from this class */ class admin_plugin_popularity extends DokuWiki_Admin_Plugin { - var $version = '2010-09-17'; + var $version; + var $helper; + var $sentStatus = null; + function admin_plugin_popularity(){ + $this->helper = $this->loadHelper('popularity', false); - /** - * return some info - */ - function getInfo(){ - return array( - 'author' => 'Andreas Gohr', - 'email' => 'andi@splitbrain.org', - 'date' => $this->version, - 'name' => 'Popularity Feedback Plugin', - 'desc' => 'Send anonymous data about your wiki to the developers.', - 'url' => 'http://www.dokuwiki.org/plugin:popularity', - ); + $pluginInfo = $this->getInfo(); + $this->version = $pluginInfo['date']; } /** @@ -56,204 +50,89 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin { * handle user request */ function handle() { + //Send the data + if ( isset($_REQUEST['data']) ){ + $this->sentStatus = $this->helper->sendData( $_REQUEST['data'] ); + //Deal with the autosubmit option + $this->_enableAutosubmit( isset($_REQUEST['autosubmit']) ); + } } /** - * Output HTML form + * Enable or disable autosubmit + * @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it. */ - function html() { - echo $this->locale_xhtml('intro'); - - flush(); - $data = $this->_gather(); - echo '<form method="post" action="http://update.dokuwiki.org/popularity.php" accept-charset="utf-8">'; - echo '<fieldset style="width: 60%;">'; - echo '<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">'; - foreach($data as $key => $val){ - if(is_array($val)) foreach($val as $v){ - echo hsc($key)."\t".hsc($v)."\n"; - }else{ - echo hsc($key)."\t".hsc($val)."\n"; - } + function _enableAutosubmit( $enable ){ + if ( $enable ){ + io_saveFile( $this->helper->autosubmitFile, ' '); + } else { + @unlink($this->helper->autosubmitFile); } - echo '</textarea><br />'; - echo '<input type="submit" class="button" value="'.$this->getLang('submit').'"/>'; - echo '</fieldset>'; - echo '</form>'; - -// dbg($data); } - /** - * Gather all information + * Output HTML form */ - function _gather(){ - global $conf; - global $auth; - $data = array(); - $phptime = ini_get('max_execution_time'); - @set_time_limit(0); - - // version - $data['anon_id'] = md5(auth_cookiesalt()); - $data['version'] = getVersion(); - $data['popversion'] = $this->version; - $data['language'] = $conf['lang']; - $data['now'] = time(); - - // some config values - $data['conf_useacl'] = $conf['useacl']; - $data['conf_authtype'] = $conf['authtype']; - $data['conf_template'] = $conf['template']; - - // number and size of pages - $list = array(); - search($list,$conf['datadir'],array($this,'_search_count'),'',''); - $data['page_count'] = $list['file_count']; - $data['page_size'] = $list['file_size']; - $data['page_biggest'] = $list['file_max']; - $data['page_smallest'] = $list['file_min']; - $data['page_nscount'] = $list['dir_count']; - $data['page_nsnest'] = $list['dir_nest']; - if($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count']; - $data['page_oldest'] = $list['file_oldest']; - unset($list); - - // number and size of media - $list = array(); - search($list,$conf['mediadir'],array($this,'_search_count'),array('all'=>true)); - $data['media_count'] = $list['file_count']; - $data['media_size'] = $list['file_size']; - $data['media_biggest'] = $list['file_max']; - $data['media_smallest'] = $list['file_min']; - $data['media_nscount'] = $list['dir_count']; - $data['media_nsnest'] = $list['dir_nest']; - if($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count']; - unset($list); - - // number and size of cache - $list = array(); - search($list,$conf['cachedir'],array($this,'_search_count'),array('all'=>true)); - $data['cache_count'] = $list['file_count']; - $data['cache_size'] = $list['file_size']; - $data['cache_biggest'] = $list['file_max']; - $data['cache_smallest'] = $list['file_min']; - if($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count']; - unset($list); - - // number and size of index - $list = array(); - search($list,$conf['indexdir'],array($this,'_search_count'),array('all'=>true)); - $data['index_count'] = $list['file_count']; - $data['index_size'] = $list['file_size']; - $data['index_biggest'] = $list['file_max']; - $data['index_smallest'] = $list['file_min']; - if($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count']; - unset($list); - - // number and size of meta - $list = array(); - search($list,$conf['metadir'],array($this,'_search_count'),array('all'=>true)); - $data['meta_count'] = $list['file_count']; - $data['meta_size'] = $list['file_size']; - $data['meta_biggest'] = $list['file_max']; - $data['meta_smallest'] = $list['file_min']; - if($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count']; - unset($list); - - // number and size of attic - $list = array(); - search($list,$conf['olddir'],array($this,'_search_count'),array('all'=>true)); - $data['attic_count'] = $list['file_count']; - $data['attic_size'] = $list['file_size']; - $data['attic_biggest'] = $list['file_max']; - $data['attic_smallest'] = $list['file_min']; - if($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count']; - $data['attic_oldest'] = $list['file_oldest']; - unset($list); - - // user count - if($auth && $auth->canDo('getUserCount')){ - $data['user_count'] = $auth->getUserCount(); - } + function html() { + if ( ! isset($_REQUEST['data']) ){ + echo $this->locale_xhtml('intro'); + + //If there was an error the last time we tried to autosubmit, warn the user + if ( $this->helper->isAutoSubmitEnabled() ){ + if ( @file_exists($this->helper->autosubmitErrorFile) ){ + echo $this->getLang('autosubmitError'); + echo io_readFile( $this->helper->autosubmitErrorFile ); + } + } - // calculate edits per day - $list = @file($conf['metadir'].'/_dokuwiki.changes'); - $count = count($list); - if($count > 2){ - $first = (int) substr(array_shift($list),0,10); - $last = (int) substr(array_pop($list),0,10); - $dur = ($last - $first)/(60*60*24); // number of days in the changelog - $data['edits_per_day'] = $count/$dur; + flush(); + echo $this->buildForm('server'); + } else { + //If we just submitted the form + if ( $this->sentStatus === '' ){ + //If we successfully sent the data + echo $this->locale_xhtml('submitted'); + } else { + //If we failed to submit the data, try directly with the browser + echo $this->getLang('submissionFailed') . $this->sentStatus . '<br />'; + echo $this->getLang('submitDirectly'); + echo $this->buildForm('browser', $_REQUEST['data']); + } } - unset($list); - - // plugins - $data['plugin'] = plugin_list(); - - // pcre info - if(defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION; - $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit'); - $data['pcre_recursion'] = ini_get('pcre.recursion_limit'); - - // php info - $data['os'] = PHP_OS; - $data['webserver'] = $_SERVER['SERVER_SOFTWARE']; - $data['php_version'] = phpversion(); - $data['php_sapi'] = php_sapi_name(); - $data['php_memory'] = $this->_to_byte(ini_get('memory_limit')); - $data['php_exectime'] = $phptime; - $data['php_extension'] = get_loaded_extensions(); - - return $data; } - function _search_count(&$data,$base,$file,$type,$lvl,$opts){ - // traverse - if($type == 'd'){ - if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl; - $data['dir_count']++; - return true; - } - - //only search txt files if 'all' option not set - if($opts['all'] || substr($file,-4) == '.txt'){ - $size = filesize($base.'/'.$file); - $date = filemtime($base.'/'.$file); - $data['file_count']++; - $data['file_size'] += $size; - if(!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size; - if($data['file_max'] < $size) $data['file_max'] = $size; - if(!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date; - } - - return false; - } - /** - * Convert php.ini shorthands to byte - * - * @author <gilthans dot NO dot SPAM at gmail dot com> - * @link http://de3.php.net/manual/en/ini.core.php#79564 + * Build the form which presents the data to be sent + * @param string $submit How is the data supposed to be sent? (may be: 'browser' or 'server') + * @param string $data The popularity data, if it has already been computed. NULL otherwise. + * @return The form, as an html string */ - function _to_byte($v){ - $l = substr($v, -1); - $ret = substr($v, 0, -1); - switch(strtoupper($l)){ - case 'P': - $ret *= 1024; - case 'T': - $ret *= 1024; - case 'G': - $ret *= 1024; - case 'M': - $ret *= 1024; - case 'K': - $ret *= 1024; - break; + function buildForm($submissionMode, $data = null){ + $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script()); + if ( is_null($data) ){ + $data = $this->helper->gatherAsString(); + } + + $form = '<form method="post" action="'. $url .'" accept-charset="utf-8">' + .'<fieldset style="width: 60%;">' + .'<textarea class="edit" rows="10" cols="80" readonly="readonly" name="data">' + .$data + .'</textarea><br />'; + + //If we submit via the server, we give the opportunity to suscribe to the autosubmission option + if ( $submissionMode !== 'browser' ){ + $form .= '<label for="autosubmit">' + .'<input type="checkbox" name="autosubmit" id="autosubmit" ' + .($this->helper->isAutosubmitEnabled() ? 'checked' : '' ) + .'/>' . $this->getLang('autosubmit') .'<br />' + .'</label>' + .'<input type="hidden" name="do" value="admin">' + .'<input type="hidden" name="page" value="popularity">'; } - return $ret; + $form .= '<input type="submit" class="button" value="'.$this->getLang('submit').'"/>' + .'</fieldset>' + .'</form>'; + return $form; } } diff --git a/lib/plugins/popularity/lang/en/lang.php b/lib/plugins/popularity/lang/en/lang.php index c9912b7ad..eba1a48e2 100644 --- a/lib/plugins/popularity/lang/en/lang.php +++ b/lib/plugins/popularity/lang/en/lang.php @@ -2,3 +2,7 @@ $lang['name'] = 'Popularity Feedback (may take some time to load)'; $lang['submit'] = 'Send Data'; +$lang['autosubmit'] = 'Automatically send data once a month (may be (un)selected later)'; +$lang['submissionFailed'] = 'The data couldn\'t be sent due to the following error:'; +$lang['submitDirectly'] = 'You may sent this data directly, validating the following form'; +$lang['autosubmitError'] = 'The last time we tried to autosubmit the data, we couldn\'t, because of the following error: '; -- GitLab