You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

313 lines
11 KiB

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