scrape_alerts_desc.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. require_once (__DIR__ . '/../inc/header.inc.php');
  3. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  4. $baseDir = __DIR__ . DIRECTORY_SEPARATOR . '..';
  5. $foldersRecursive = [
  6. 'inc',
  7. 'modules',
  8. 'periodic',
  9. 'studio',
  10. 'template'
  11. ];
  12. // skip any path containing /modules/*/*/updates
  13. function shouldSkip($path) {
  14. // normalize for safety
  15. $path = str_replace('\\', '/', $path);
  16. // matches: modules/anything/anything/updates
  17. if (preg_match('#/modules/[^/]+/[^/]+/updates(/|$)#', $path)) {
  18. return true;
  19. }
  20. return false;
  21. }
  22. $pattern = '/@hookdef\s+hook-[^\s]+\s+\'([^\']+)\'\s*,\s*\'([^\']+)\'\s*-\s*(.+)/';
  23. $result = [];
  24. function processFile($baseDir, $filePath, $pattern, &$result) {
  25. $handle = fopen($filePath, 'r');
  26. if (!$handle) return;
  27. $lineNumber = 0;
  28. while (($line = fgets($handle)) !== false) {
  29. $lineNumber++;
  30. if (strpos($line, '@hookdef') === false) continue;
  31. if (preg_match($pattern, $line, $m)) {
  32. $result[] = [
  33. 'file' => str_replace($baseDir . DIRECTORY_SEPARATOR, '', $filePath),
  34. 'line' => $lineNumber,
  35. 'unit' => $m[1],
  36. 'action' => $m[2],
  37. 'desc' => trim($m[3]),
  38. ];
  39. }
  40. }
  41. fclose($handle);
  42. }
  43. /**
  44. * Base dir (non-recursive)
  45. */
  46. foreach (scandir($baseDir) as $file) {
  47. $path = $baseDir . DIRECTORY_SEPARATOR . $file;
  48. if (is_file($path) && pathinfo($path, PATHINFO_EXTENSION) === 'php') {
  49. processFile($baseDir, $path, $pattern, $result);
  50. }
  51. }
  52. /**
  53. * Recursive folders with skip logic
  54. */
  55. foreach ($foldersRecursive as $folder) {
  56. $dir = $baseDir . DIRECTORY_SEPARATOR . $folder;
  57. if (!is_dir($dir)) continue;
  58. $directory = new RecursiveDirectoryIterator(
  59. $dir,
  60. FilesystemIterator::SKIP_DOTS
  61. );
  62. $filter = new RecursiveCallbackFilterIterator(
  63. $directory,
  64. function ($current, $key, $iterator) {
  65. $path = $current->getPathname();
  66. // skip unwanted subtree
  67. if (shouldSkip($path)) {
  68. return false;
  69. }
  70. return true;
  71. }
  72. );
  73. $iterator = new RecursiveIteratorIterator($filter);
  74. foreach ($iterator as $file) {
  75. if ($file->getExtension() !== 'php') continue;
  76. processFile($baseDir, $file->getPathname(), $pattern, $result);
  77. }
  78. }
  79. /**
  80. * Output
  81. */
  82. BxDolDb::getInstance()->query("TRUNCATE TABLE sys_alerts_desc");
  83. foreach ($result as $row) {
  84. echo "{$row['unit']} | {$row['action']} | {$row['desc']}\n";
  85. echo " -> {$row['file']}:{$row['line']}\n\n";
  86. BxDolDb::getInstance()->query("INSERT IGNORE INTO sys_alerts_desc SET `unit` = :unit, `action` = :action, `description` = :desc", [
  87. 'unit' => $row['unit'],
  88. 'action' => $row['action'],
  89. 'desc' => $row['desc']
  90. ]);
  91. }