PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/plugins/popularity/helper.php

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