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