helper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. use dokuwiki\HTTP\DokuHTTPClient;
  3. use dokuwiki\Extension\Event;
  4. /**
  5. * Popularity Feedback Plugin
  6. *
  7. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  8. */
  9. class helper_plugin_popularity extends Dokuwiki_Plugin
  10. {
  11. /**
  12. * The url where the data should be sent
  13. */
  14. public $submitUrl = 'https://update.dokuwiki.org/popularity.php';
  15. /**
  16. * Name of the file which determine if the the autosubmit is enabled,
  17. * and when it was submited for the last time
  18. */
  19. public $autosubmitFile;
  20. /**
  21. * File where the last error which happened when we tried to autosubmit, will be log
  22. */
  23. public $autosubmitErrorFile;
  24. /**
  25. * Name of the file which determine when the popularity data was manually
  26. * submitted for the last time
  27. * (If this file doesn't exist, the data has never been sent)
  28. */
  29. public $popularityLastSubmitFile;
  30. /**
  31. * helper_plugin_popularity constructor.
  32. */
  33. public function __construct()
  34. {
  35. global $conf;
  36. $this->autosubmitFile = $conf['cachedir'].'/autosubmit.txt';
  37. $this->autosubmitErrorFile = $conf['cachedir'].'/autosubmitError.txt';
  38. $this->popularityLastSubmitFile = $conf['cachedir'].'/lastSubmitTime.txt';
  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. {
  47. return file_exists($this->autosubmitFile);
  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. {
  57. $error = '';
  58. $httpClient = new DokuHTTPClient();
  59. $status = $httpClient->sendRequest($this->submitUrl, array('data' => $data), 'POST');
  60. if (! $status) {
  61. $error = $httpClient->error;
  62. }
  63. return $error;
  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. {
  72. $manualSubmission = @filemtime($this->popularityLastSubmitFile);
  73. $autoSubmission = @filemtime($this->autosubmitFile);
  74. return max((int) $manualSubmission, (int) $autoSubmission);
  75. }
  76. /**
  77. * Gather all information
  78. *
  79. * @return string The popularity data as a string
  80. */
  81. public function gatherAsString()
  82. {
  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. * Initialize an empty list to be used in file traversing
  96. *
  97. * @return array
  98. * @see searchCountCallback
  99. */
  100. protected function initEmptySearchList()
  101. {
  102. return $list = array_fill_keys([
  103. 'file_count',
  104. 'file_size',
  105. 'file_max',
  106. 'file_min',
  107. 'dir_count',
  108. 'dir_nest',
  109. 'file_oldest'
  110. ], 0);
  111. }
  112. /**
  113. * Gather all information
  114. *
  115. * @return array The popularity data as an array
  116. */
  117. protected function gather()
  118. {
  119. global $conf;
  120. /** @var $auth DokuWiki_Auth_Plugin */
  121. global $auth;
  122. $data = array();
  123. $phptime = ini_get('max_execution_time');
  124. @set_time_limit(0);
  125. $pluginInfo = $this->getInfo();
  126. // version
  127. $data['anon_id'] = md5(auth_cookiesalt());
  128. $data['version'] = getVersion();
  129. $data['popversion'] = $pluginInfo['date'];
  130. $data['language'] = $conf['lang'];
  131. $data['now'] = time();
  132. $data['popauto'] = (int) $this->isAutoSubmitEnabled();
  133. // some config values
  134. $data['conf_useacl'] = $conf['useacl'];
  135. $data['conf_authtype'] = $conf['authtype'];
  136. $data['conf_template'] = $conf['template'];
  137. // number and size of pages
  138. $list = $this->initEmptySearchList();
  139. search($list, $conf['datadir'], array($this, 'searchCountCallback'), array('all'=>false), '');
  140. $data['page_count'] = $list['file_count'];
  141. $data['page_size'] = $list['file_size'];
  142. $data['page_biggest'] = $list['file_max'];
  143. $data['page_smallest'] = $list['file_min'];
  144. $data['page_nscount'] = $list['dir_count'];
  145. $data['page_nsnest'] = $list['dir_nest'];
  146. if ($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
  147. $data['page_oldest'] = $list['file_oldest'];
  148. unset($list);
  149. // number and size of media
  150. $list = $this->initEmptySearchList();
  151. search($list, $conf['mediadir'], array($this, 'searchCountCallback'), array('all'=>true));
  152. $data['media_count'] = $list['file_count'];
  153. $data['media_size'] = $list['file_size'];
  154. $data['media_biggest'] = $list['file_max'];
  155. $data['media_smallest'] = $list['file_min'];
  156. $data['media_nscount'] = $list['dir_count'];
  157. $data['media_nsnest'] = $list['dir_nest'];
  158. if ($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
  159. unset($list);
  160. // number and size of cache
  161. $list = $this->initEmptySearchList();
  162. search($list, $conf['cachedir'], array($this, 'searchCountCallback'), array('all'=>true));
  163. $data['cache_count'] = $list['file_count'];
  164. $data['cache_size'] = $list['file_size'];
  165. $data['cache_biggest'] = $list['file_max'];
  166. $data['cache_smallest'] = $list['file_min'];
  167. if ($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
  168. unset($list);
  169. // number and size of index
  170. $list = $this->initEmptySearchList();
  171. search($list, $conf['indexdir'], array($this, 'searchCountCallback'), array('all'=>true));
  172. $data['index_count'] = $list['file_count'];
  173. $data['index_size'] = $list['file_size'];
  174. $data['index_biggest'] = $list['file_max'];
  175. $data['index_smallest'] = $list['file_min'];
  176. if ($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
  177. unset($list);
  178. // number and size of meta
  179. $list = $this->initEmptySearchList();
  180. search($list, $conf['metadir'], array($this, 'searchCountCallback'), array('all'=>true));
  181. $data['meta_count'] = $list['file_count'];
  182. $data['meta_size'] = $list['file_size'];
  183. $data['meta_biggest'] = $list['file_max'];
  184. $data['meta_smallest'] = $list['file_min'];
  185. if ($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
  186. unset($list);
  187. // number and size of attic
  188. $list = $this->initEmptySearchList();
  189. search($list, $conf['olddir'], array($this, 'searchCountCallback'), array('all'=>true));
  190. $data['attic_count'] = $list['file_count'];
  191. $data['attic_size'] = $list['file_size'];
  192. $data['attic_biggest'] = $list['file_max'];
  193. $data['attic_smallest'] = $list['file_min'];
  194. if ($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
  195. $data['attic_oldest'] = $list['file_oldest'];
  196. unset($list);
  197. // user count
  198. if ($auth && $auth->canDo('getUserCount')) {
  199. $data['user_count'] = $auth->getUserCount();
  200. }
  201. // calculate edits per day
  202. $list = (array) @file($conf['metadir'].'/_dokuwiki.changes');
  203. $count = count($list);
  204. if ($count > 2) {
  205. $first = (int) substr(array_shift($list), 0, 10);
  206. $last = (int) substr(array_pop($list), 0, 10);
  207. $dur = ($last - $first)/(60*60*24); // number of days in the changelog
  208. $data['edits_per_day'] = $count/$dur;
  209. }
  210. unset($list);
  211. // plugins
  212. $data['plugin'] = plugin_list();
  213. // pcre info
  214. if (defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
  215. $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
  216. $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
  217. // php info
  218. $data['os'] = PHP_OS;
  219. $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
  220. $data['php_version'] = phpversion();
  221. $data['php_sapi'] = php_sapi_name();
  222. $data['php_memory'] = php_to_byte(ini_get('memory_limit'));
  223. $data['php_exectime'] = $phptime;
  224. $data['php_extension'] = get_loaded_extensions();
  225. // plugin usage data
  226. $this->addPluginUsageData($data);
  227. return $data;
  228. }
  229. /**
  230. * Triggers event to let plugins add their own data
  231. *
  232. * @param $data
  233. */
  234. protected function addPluginUsageData(&$data)
  235. {
  236. $pluginsData = array();
  237. Event::createAndTrigger('PLUGIN_POPULARITY_DATA_SETUP', $pluginsData);
  238. foreach ($pluginsData as $plugin => $d) {
  239. if (is_array($d)) {
  240. foreach ($d as $key => $value) {
  241. $data['plugin_' . $plugin . '_' . $key] = $value;
  242. }
  243. } else {
  244. $data['plugin_' . $plugin] = $d;
  245. }
  246. }
  247. }
  248. /**
  249. * Callback to search and count the content of directories in DokuWiki
  250. *
  251. * @param array &$data Reference to the result data structure
  252. * @param string $base Base usually $conf['datadir']
  253. * @param string $file current file or directory relative to $base
  254. * @param string $type Type either 'd' for directory or 'f' for file
  255. * @param int $lvl Current recursion depht
  256. * @param array $opts option array as given to search()
  257. * @return bool
  258. */
  259. public function searchCountCallback(&$data, $base, $file, $type, $lvl, $opts)
  260. {
  261. // traverse
  262. if ($type == 'd') {
  263. if ($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
  264. $data['dir_count']++;
  265. return true;
  266. }
  267. //only search txt files if 'all' option not set
  268. if ($opts['all'] || substr($file, -4) == '.txt') {
  269. $size = filesize($base.'/'.$file);
  270. $date = filemtime($base.'/'.$file);
  271. $data['file_count']++;
  272. $data['file_size'] += $size;
  273. if (!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
  274. if ($data['file_max'] < $size) $data['file_max'] = $size;
  275. if (!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
  276. }
  277. return false;
  278. }
  279. }