diff options
Diffstat (limited to 'lib/plugins/popularity')
-rw-r--r-- | lib/plugins/popularity/action.php | 42 | ||||
-rw-r--r-- | lib/plugins/popularity/admin.php | 73 | ||||
-rw-r--r-- | lib/plugins/popularity/helper.php | 194 |
3 files changed, 136 insertions, 173 deletions
diff --git a/lib/plugins/popularity/action.php b/lib/plugins/popularity/action.php index d5ec0f5c5..fac610735 100644 --- a/lib/plugins/popularity/action.php +++ b/lib/plugins/popularity/action.php @@ -5,44 +5,49 @@ * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) */ -require_once(DOKU_PLUGIN.'action.php'); -require_once(DOKU_PLUGIN.'popularity/admin.php'); - -class action_plugin_popularity extends Dokuwiki_Action_Plugin { +class action_plugin_popularity extends DokuWiki_Action_Plugin +{ /** * @var helper_plugin_popularity */ - var $helper; + protected $helper; - function __construct(){ + public function __construct() + { $this->helper = $this->loadHelper('popularity', false); } - /** - * Register its handlers with the dokuwiki's event controller - */ - function register(Doku_Event_Handler $controller) { - $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, '_autosubmit', array()); + /** @inheritdoc */ + public function register(Doku_Event_Handler $controller) + { + $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'autosubmit', array()); } - function _autosubmit(Doku_Event &$event, $param){ + /** + * Event handler + * + * @param Doku_Event $event + * @param $param + */ + public function autosubmit(Doku_Event &$event, $param) + { //Do we have to send the data now - if ( !$this->helper->isAutosubmitEnabled() || $this->_isTooEarlyToSubmit() ){ + if (!$this->helper->isAutosubmitEnabled() || $this->isTooEarlyToSubmit()) { return; } //Actually send it - $status = $this->helper->sendData( $this->helper->gatherAsString() ); + $status = $this->helper->sendData($this->helper->gatherAsString()); - if ( $status !== '' ){ + if ($status !== '') { //If an error occured, log it - io_saveFile( $this->helper->autosubmitErrorFile, $status ); + io_saveFile($this->helper->autosubmitErrorFile, $status); } else { //If the data has been sent successfully, previous log of errors are useless @unlink($this->helper->autosubmitErrorFile); //Update the last time we sent data - touch ( $this->helper->autosubmitFile ); + touch($this->helper->autosubmitFile); } $event->stopPropagation(); @@ -53,7 +58,8 @@ class action_plugin_popularity extends Dokuwiki_Action_Plugin { * Check if it's time to send autosubmit data * (we should have check if autosubmit is enabled first) */ - function _isTooEarlyToSubmit(){ + protected function isTooEarlyToSubmit() + { $lastSubmit = $this->helper->lastSentTime(); return $lastSubmit + 24*60*60*30 > time(); } diff --git a/lib/plugins/popularity/admin.php b/lib/plugins/popularity/admin.php index 0cf174e0d..61d8cc3bf 100644 --- a/lib/plugins/popularity/admin.php +++ b/lib/plugins/popularity/admin.php @@ -5,43 +5,44 @@ * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Andreas Gohr <andi@splitbrain.org> */ -// must be run within Dokuwiki -if(!defined('DOKU_INC')) die(); +class admin_plugin_popularity extends DokuWiki_Admin_Plugin +{ -/** - * All DokuWiki plugins to extend the admin function - * need to inherit from this class - */ -class admin_plugin_popularity extends DokuWiki_Admin_Plugin { + /** @var helper_plugin_popularity */ + protected $helper; + protected $sentStatus = null; /** - * @var helper_plugin_popularity + * admin_plugin_popularity constructor. */ - var $helper; - var $sentStatus = null; - - function __construct(){ + public function __construct() + { $this->helper = $this->loadHelper('popularity', false); } /** * return prompt for admin menu + * @param $language + * @return string */ - function getMenuText($language) { + public function getMenuText($language) + { return $this->getLang('name'); } /** * return sort order for position in admin menu */ - function getMenuSort() { + public function getMenuSort() + { return 2000; } /** * Accessible for managers */ - function forAdminOnly() { + public function forAdminOnly() + { return false; } @@ -49,18 +50,19 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin { /** * handle user request */ - function handle() { + public function handle() + { global $INPUT; //Send the data - if ( $INPUT->has('data') ){ - $this->sentStatus = $this->helper->sendData( $INPUT->str('data') ); - if ( $this->sentStatus === '' ){ + if ($INPUT->has('data')) { + $this->sentStatus = $this->helper->sendData($INPUT->str('data')); + if ($this->sentStatus === '') { //Update the last time we sent the data - touch ( $this->helper->popularityLastSubmitFile ); + touch($this->helper->popularityLastSubmitFile); } //Deal with the autosubmit option - $this->_enableAutosubmit( $INPUT->has('autosubmit') ); + $this->enableAutosubmit($INPUT->has('autosubmit')); } } @@ -68,9 +70,10 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin { * Enable or disable autosubmit * @param bool $enable If TRUE, it will enable autosubmit. Else, it will disable it. */ - function _enableAutosubmit( $enable ){ - if ( $enable ){ - io_saveFile( $this->helper->autosubmitFile, ' '); + protected function enableAutosubmit($enable) + { + if ($enable) { + io_saveFile($this->helper->autosubmitFile, ' '); } else { @unlink($this->helper->autosubmitFile); } @@ -79,17 +82,18 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin { /** * Output HTML form */ - function html() { + public function html() + { global $INPUT; - if ( ! $INPUT->has('data') ){ + if (! $INPUT->has('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) ){ + if ($this->helper->isAutoSubmitEnabled()) { + if (file_exists($this->helper->autosubmitErrorFile)) { echo $this->getLang('autosubmitError'); - echo io_readFile( $this->helper->autosubmitErrorFile ); + echo io_readFile($this->helper->autosubmitErrorFile); } } @@ -98,12 +102,12 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin { //Print the last time the data was sent $lastSent = $this->helper->lastSentTime(); - if ( $lastSent !== 0 ){ + if ($lastSent !== 0) { echo $this->getLang('lastSent') . ' ' . datetime_h($lastSent); } } else { //If we just submitted the form - if ( $this->sentStatus === '' ){ + if ($this->sentStatus === '') { //If we successfully sent the data echo $this->locale_xhtml('submitted'); } else { @@ -122,9 +126,10 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin { * @param string $data The popularity data, if it has already been computed. NULL otherwise. * @return string The form, as an html string */ - function buildForm($submissionMode, $data = null){ + protected function buildForm($submissionMode, $data = null) + { $url = ($submissionMode === 'browser' ? $this->helper->submitUrl : script()); - if ( is_null($data) ){ + if (is_null($data)) { $data = $this->helper->gatherAsString(); } @@ -135,7 +140,7 @@ class admin_plugin_popularity extends DokuWiki_Admin_Plugin { .'</textarea><br />'; //If we submit via the server, we give the opportunity to suscribe to the autosubmission option - if ( $submissionMode !== 'browser' ){ + if ($submissionMode !== 'browser') { $form .= '<label for="autosubmit">' .'<input type="checkbox" name="autosubmit" id="autosubmit" ' .($this->helper->isAutosubmitEnabled() ? 'checked' : '' ) diff --git a/lib/plugins/popularity/helper.php b/lib/plugins/popularity/helper.php index b81ab7005..4537976ae 100644 --- a/lib/plugins/popularity/helper.php +++ b/lib/plugins/popularity/helper.php @@ -1,36 +1,43 @@ <?php + +use dokuwiki\HTTP\DokuHTTPClient; +use dokuwiki\Extension\Event; + /** * Popularity Feedback Plugin * * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) */ - -class helper_plugin_popularity extends Dokuwiki_Plugin { +class helper_plugin_popularity extends Dokuwiki_Plugin +{ /** * The url where the data should be sent */ - var $submitUrl = 'http://update.dokuwiki.org/popularity.php'; + public $submitUrl = 'http://update.dokuwiki.org/popularity.php'; /** * Name of the file which determine if the the autosubmit is enabled, * and when it was submited for the last time */ - var $autosubmitFile; + public $autosubmitFile; /** * File where the last error which happened when we tried to autosubmit, will be log */ - var $autosubmitErrorFile; + public $autosubmitErrorFile; /** * Name of the file which determine when the popularity data was manually * submitted for the last time * (If this file doesn't exist, the data has never been sent) */ - var $popularityLastSubmitFile; + public $popularityLastSubmitFile; - - function __construct(){ + /** + * helper_plugin_popularity constructor. + */ + public function __construct() + { global $conf; $this->autosubmitFile = $conf['cachedir'].'/autosubmit.txt'; $this->autosubmitErrorFile = $conf['cachedir'].'/autosubmitError.txt'; @@ -38,46 +45,12 @@ class helper_plugin_popularity extends Dokuwiki_Plugin { } /** - * Return methods of this helper - * - * @return array with methods description - */ - function getMethods(){ - $result = array(); - $result[] = array( - 'name' => 'isAutoSubmitEnabled', - 'desc' => 'Check if autosubmit is enabled', - 'params' => array(), - 'return' => array('result' => 'bool') - ); - $result[] = array( - 'name' => 'sendData', - 'desc' => 'Send the popularity data', - 'params' => array('data' => 'string'), - 'return' => array() - ); - $result[] = array( - 'name' => 'gatherAsString', - 'desc' => 'Gather the popularity data', - 'params' => array(), - 'return' => array('data' => 'string') - ); - $result[] = array( - 'name' => 'lastSentTime', - 'desc' => 'Compute the last time popularity data was sent', - 'params' => array(), - 'return' => array('data' => 'int') - ); - return $result; - - } - - /** * Check if autosubmit is enabled * * @return boolean TRUE if we should send data once a month, FALSE otherwise */ - function isAutoSubmitEnabled(){ + public function isAutoSubmitEnabled() + { return file_exists($this->autosubmitFile); } @@ -87,11 +60,12 @@ class helper_plugin_popularity extends Dokuwiki_Plugin { * @param string $data The popularity data * @return string An empty string if everything worked fine, a string describing the error otherwise */ - function sendData($data){ + public function sendData($data) + { $error = ''; $httpClient = new DokuHTTPClient(); $status = $httpClient->sendRequest($this->submitUrl, array('data' => $data), 'POST'); - if ( ! $status ){ + if (! $status) { $error = $httpClient->error; } return $error; @@ -102,7 +76,8 @@ class helper_plugin_popularity extends Dokuwiki_Plugin { * * @return int */ - function lastSentTime(){ + public function lastSentTime() + { $manualSubmission = @filemtime($this->popularityLastSubmitFile); $autoSubmission = @filemtime($this->autosubmitFile); @@ -114,13 +89,14 @@ class helper_plugin_popularity extends Dokuwiki_Plugin { * * @return string The popularity data as a string */ - function gatherAsString(){ - $data = $this->_gather(); + public function gatherAsString() + { + $data = $this->gather(); $string = ''; - foreach($data as $key => $val){ - if(is_array($val)) foreach($val as $v){ + foreach ($data as $key => $val) { + if (is_array($val)) foreach ($val as $v) { $string .= hsc($key)."\t".hsc($v)."\n"; - }else{ + } else { $string .= hsc($key)."\t".hsc($val)."\n"; } } @@ -132,7 +108,8 @@ class helper_plugin_popularity extends Dokuwiki_Plugin { * * @return array The popularity data as an array */ - function _gather(){ + protected function gather() + { global $conf; /** @var $auth DokuWiki_Auth_Plugin */ global $auth; @@ -156,81 +133,81 @@ class helper_plugin_popularity extends Dokuwiki_Plugin { // number and size of pages $list = array(); - search($list,$conf['datadir'],array($this,'_search_count'),array('all'=>false),''); + search($list, $conf['datadir'], array($this, 'searchCountCallback'), array('all'=>false), ''); $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']; + 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)); + search($list, $conf['mediadir'], array($this, 'searchCountCallback'), 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']; + 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)); + search($list, $conf['cachedir'], array($this, 'searchCountCallback'), 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']; + 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)); + search($list, $conf['indexdir'], array($this, 'searchCountCallback'), 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']; + 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)); + search($list, $conf['metadir'], array($this, 'searchCountCallback'), 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']; + 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)); + search($list, $conf['olddir'], array($this, 'searchCountCallback'), 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']; + 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')){ + if ($auth && $auth->canDo('getUserCount')) { $data['user_count'] = $auth->getUserCount(); } // 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); + 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; } @@ -240,7 +217,7 @@ class helper_plugin_popularity extends Dokuwiki_Plugin { $data['plugin'] = plugin_list(); // pcre info - if(defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION; + 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'); @@ -249,27 +226,33 @@ class helper_plugin_popularity extends Dokuwiki_Plugin { $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_memory'] = php_to_byte(ini_get('memory_limit')); $data['php_exectime'] = $phptime; $data['php_extension'] = get_loaded_extensions(); // plugin usage data - $this->_add_plugin_usage_data($data); + $this->addPluginUsageData($data); return $data; } - protected function _add_plugin_usage_data(&$data){ + /** + * Triggers event to let plugins add their own data + * + * @param $data + */ + protected function addPluginUsageData(&$data) + { $pluginsData = array(); - trigger_event('PLUGIN_POPULARITY_DATA_SETUP', $pluginsData); - foreach($pluginsData as $plugin => $d){ - if ( is_array($d) ) { - foreach($d as $key => $value){ - $data['plugin_' . $plugin . '_' . $key] = $value; - } - } else { - $data['plugin_' . $plugin] = $d; - } + Event::createAndTrigger('PLUGIN_POPULARITY_DATA_SETUP', $pluginsData); + foreach ($pluginsData as $plugin => $d) { + if (is_array($d)) { + foreach ($d as $key => $value) { + $data['plugin_' . $plugin . '_' . $key] = $value; + } + } else { + $data['plugin_' . $plugin] = $d; + } } } @@ -284,57 +267,26 @@ class helper_plugin_popularity extends Dokuwiki_Plugin { * @param array $opts option array as given to search() * @return bool */ - function _search_count(&$data,$base,$file,$type,$lvl,$opts){ + public function searchCountCallback(&$data, $base, $file, $type, $lvl, $opts) + { // traverse - if($type == 'd'){ - if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl; + 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'){ + 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; + 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://php.net/manual/en/ini.core.php#79564 - * - * @param string $v - * @return int|string - */ - function _to_byte($v){ - $l = substr($v, -1); - $ret = substr($v, 0, -1); - switch(strtoupper($l)){ - /** @noinspection PhpMissingBreakStatementInspection */ - case 'P': - $ret *= 1024; - /** @noinspection PhpMissingBreakStatementInspection */ - case 'T': - $ret *= 1024; - /** @noinspection PhpMissingBreakStatementInspection */ - case 'G': - $ret *= 1024; - /** @noinspection PhpMissingBreakStatementInspection */ - case 'M': - $ret *= 1024; - case 'K': - $ret *= 1024; - break; - } - return $ret; - } } |