1<?php 2/** 3 * Popularity Feedback Plugin 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 */ 7 8class helper_plugin_popularity extends Dokuwiki_Plugin { 9 /** 10 * The url where the data should be sent 11 */ 12 public $submitUrl = 'http://update.dokuwiki.org/popularity.php'; 13 14 /** 15 * Name of the file which determine if the the autosubmit is enabled, 16 * and when it was submited for the last time 17 */ 18 public $autosubmitFile; 19 20 /** 21 * File where the last error which happened when we tried to autosubmit, will be log 22 */ 23 public $autosubmitErrorFile; 24 25 /** 26 * Name of the file which determine when the popularity data was manually 27 * submitted for the last time 28 * (If this file doesn't exist, the data has never been sent) 29 */ 30 public $popularityLastSubmitFile; 31 32 33 public function __construct(){ 34 global $conf; 35 $this->autosubmitFile = $conf['cachedir'].'/autosubmit.txt'; 36 $this->autosubmitErrorFile = $conf['cachedir'].'/autosubmitError.txt'; 37 $this->popularityLastSubmitFile = $conf['cachedir'].'/lastSubmitTime.txt'; 38 } 39 40 /** 41 * Check if autosubmit is enabled 42 * 43 * @return boolean TRUE if we should send data once a month, FALSE otherwise 44 */ 45 public function isAutoSubmitEnabled(){ 46 return file_exists($this->autosubmitFile); 47 } 48 49 /** 50 * Send the data, to the submit url 51 * 52 * @param string $data The popularity data 53 * @return string An empty string if everything worked fine, a string describing the error otherwise 54 */ 55 public function sendData($data){ 56 $error = ''; 57 $httpClient = new DokuHTTPClient(); 58 $status = $httpClient->sendRequest($this->submitUrl, array('data' => $data), 'POST'); 59 if ( ! $status ){ 60 $error = $httpClient->error; 61 } 62 return $error; 63 } 64 65 /** 66 * Compute the last time the data was sent. If it has never been sent, we return 0. 67 * 68 * @return int 69 */ 70 public function lastSentTime(){ 71 $manualSubmission = @filemtime($this->popularityLastSubmitFile); 72 $autoSubmission = @filemtime($this->autosubmitFile); 73 74 return max((int) $manualSubmission, (int) $autoSubmission); 75 } 76 77 /** 78 * Gather all information 79 * 80 * @return string The popularity data as a string 81 */ 82 public function gatherAsString(){ 83 $data = $this->_gather(); 84 $string = ''; 85 foreach($data as $key => $val){ 86 if(is_array($val)) foreach($val as $v){ 87 $string .= hsc($key)."\t".hsc($v)."\n"; 88 }else{ 89 $string .= hsc($key)."\t".hsc($val)."\n"; 90 } 91 } 92 return $string; 93 } 94 95 /** 96 * Gather all information 97 * 98 * @return array The popularity data as an array 99 */ 100 protected function _gather(){ 101 global $conf; 102 /** @var $auth DokuWiki_Auth_Plugin */ 103 global $auth; 104 $data = array(); 105 $phptime = ini_get('max_execution_time'); 106 @set_time_limit(0); 107 $pluginInfo = $this->getInfo(); 108 109 // version 110 $data['anon_id'] = md5(auth_cookiesalt()); 111 $data['version'] = getVersion(); 112 $data['popversion'] = $pluginInfo['date']; 113 $data['language'] = $conf['lang']; 114 $data['now'] = time(); 115 $data['popauto'] = (int) $this->isAutoSubmitEnabled(); 116 117 // some config values 118 $data['conf_useacl'] = $conf['useacl']; 119 $data['conf_authtype'] = $conf['authtype']; 120 $data['conf_template'] = $conf['template']; 121 122 // number and size of pages 123 $list = array(); 124 search($list,$conf['datadir'],array($this,'_search_count'),array('all'=>false),''); 125 $data['page_count'] = $list['file_count']; 126 $data['page_size'] = $list['file_size']; 127 $data['page_biggest'] = $list['file_max']; 128 $data['page_smallest'] = $list['file_min']; 129 $data['page_nscount'] = $list['dir_count']; 130 $data['page_nsnest'] = $list['dir_nest']; 131 if($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count']; 132 $data['page_oldest'] = $list['file_oldest']; 133 unset($list); 134 135 // number and size of media 136 $list = array(); 137 search($list,$conf['mediadir'],array($this,'_search_count'),array('all'=>true)); 138 $data['media_count'] = $list['file_count']; 139 $data['media_size'] = $list['file_size']; 140 $data['media_biggest'] = $list['file_max']; 141 $data['media_smallest'] = $list['file_min']; 142 $data['media_nscount'] = $list['dir_count']; 143 $data['media_nsnest'] = $list['dir_nest']; 144 if($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count']; 145 unset($list); 146 147 // number and size of cache 148 $list = array(); 149 search($list,$conf['cachedir'],array($this,'_search_count'),array('all'=>true)); 150 $data['cache_count'] = $list['file_count']; 151 $data['cache_size'] = $list['file_size']; 152 $data['cache_biggest'] = $list['file_max']; 153 $data['cache_smallest'] = $list['file_min']; 154 if($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count']; 155 unset($list); 156 157 // number and size of index 158 $list = array(); 159 search($list,$conf['indexdir'],array($this,'_search_count'),array('all'=>true)); 160 $data['index_count'] = $list['file_count']; 161 $data['index_size'] = $list['file_size']; 162 $data['index_biggest'] = $list['file_max']; 163 $data['index_smallest'] = $list['file_min']; 164 if($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count']; 165 unset($list); 166 167 // number and size of meta 168 $list = array(); 169 search($list,$conf['metadir'],array($this,'_search_count'),array('all'=>true)); 170 $data['meta_count'] = $list['file_count']; 171 $data['meta_size'] = $list['file_size']; 172 $data['meta_biggest'] = $list['file_max']; 173 $data['meta_smallest'] = $list['file_min']; 174 if($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count']; 175 unset($list); 176 177 // number and size of attic 178 $list = array(); 179 search($list,$conf['olddir'],array($this,'_search_count'),array('all'=>true)); 180 $data['attic_count'] = $list['file_count']; 181 $data['attic_size'] = $list['file_size']; 182 $data['attic_biggest'] = $list['file_max']; 183 $data['attic_smallest'] = $list['file_min']; 184 if($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count']; 185 $data['attic_oldest'] = $list['file_oldest']; 186 unset($list); 187 188 // user count 189 if($auth && $auth->canDo('getUserCount')){ 190 $data['user_count'] = $auth->getUserCount(); 191 } 192 193 // calculate edits per day 194 $list = @file($conf['metadir'].'/_dokuwiki.changes'); 195 $count = count($list); 196 if($count > 2){ 197 $first = (int) substr(array_shift($list),0,10); 198 $last = (int) substr(array_pop($list),0,10); 199 $dur = ($last - $first)/(60*60*24); // number of days in the changelog 200 $data['edits_per_day'] = $count/$dur; 201 } 202 unset($list); 203 204 // plugins 205 $data['plugin'] = plugin_list(); 206 207 // pcre info 208 if(defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION; 209 $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit'); 210 $data['pcre_recursion'] = ini_get('pcre.recursion_limit'); 211 212 // php info 213 $data['os'] = PHP_OS; 214 $data['webserver'] = $_SERVER['SERVER_SOFTWARE']; 215 $data['php_version'] = phpversion(); 216 $data['php_sapi'] = php_sapi_name(); 217 $data['php_memory'] = $this->_to_byte(ini_get('memory_limit')); 218 $data['php_exectime'] = $phptime; 219 $data['php_extension'] = get_loaded_extensions(); 220 221 // plugin usage data 222 $this->_add_plugin_usage_data($data); 223 224 return $data; 225 } 226 227 /** 228 * Triggers event to let plugins add their own data 229 * 230 * @param $data 231 */ 232 protected function _add_plugin_usage_data(&$data){ 233 $pluginsData = array(); 234 trigger_event('PLUGIN_POPULARITY_DATA_SETUP', $pluginsData); 235 foreach($pluginsData as $plugin => $d){ 236 if ( is_array($d) ) { 237 foreach($d as $key => $value){ 238 $data['plugin_' . $plugin . '_' . $key] = $value; 239 } 240 } else { 241 $data['plugin_' . $plugin] = $d; 242 } 243 } 244 } 245 246 /** 247 * Callback to search and count the content of directories in DokuWiki 248 * 249 * @param array &$data Reference to the result data structure 250 * @param string $base Base usually $conf['datadir'] 251 * @param string $file current file or directory relative to $base 252 * @param string $type Type either 'd' for directory or 'f' for file 253 * @param int $lvl Current recursion depht 254 * @param array $opts option array as given to search() 255 * @return bool 256 */ 257 protected function _search_count(&$data,$base,$file,$type,$lvl,$opts){ 258 // traverse 259 if($type == 'd'){ 260 if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl; 261 $data['dir_count']++; 262 return true; 263 } 264 265 //only search txt files if 'all' option not set 266 if($opts['all'] || substr($file,-4) == '.txt'){ 267 $size = filesize($base.'/'.$file); 268 $date = filemtime($base.'/'.$file); 269 $data['file_count']++; 270 $data['file_size'] += $size; 271 if(!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size; 272 if($data['file_max'] < $size) $data['file_max'] = $size; 273 if(!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date; 274 } 275 276 return false; 277 } 278 279 /** 280 * Convert php.ini shorthands to byte 281 * 282 * @author <gilthans dot NO dot SPAM at gmail dot com> 283 * @link http://php.net/manual/en/ini.core.php#79564 284 * 285 * @param string $v 286 * @return int|string 287 */ 288 protected function _to_byte($v){ 289 $l = substr($v, -1); 290 $ret = substr($v, 0, -1); 291 switch(strtoupper($l)){ 292 /** @noinspection PhpMissingBreakStatementInspection */ 293 case 'P': 294 $ret *= 1024; 295 /** @noinspection PhpMissingBreakStatementInspection */ 296 case 'T': 297 $ret *= 1024; 298 /** @noinspection PhpMissingBreakStatementInspection */ 299 case 'G': 300 $ret *= 1024; 301 /** @noinspection PhpMissingBreakStatementInspection */ 302 case 'M': 303 $ret *= 1024; 304 case 'K': 305 $ret *= 1024; 306 break; 307 } 308 return $ret; 309 } 310} 311