PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/srai_lookup.php

https://gitlab.com/tutaalexandr/bot_local
PHP | 160 lines | 127 code | 13 blank | 20 comment | 6 complexity | ca88ae117cc6307be23c118b34973d55 MD5 | raw file
  1. <?php
  2. /***************************************
  3. * http://www.program-o.com
  4. * PROGRAM O
  5. * Version: 2.4.8
  6. * FILE: srai_lookup.php
  7. * AUTHOR: Elizabeth Perreau and Dave Morton
  8. * DATE: 05-26-2014
  9. * DETAILS: Manage entries in the srai_lookup table
  10. ***************************************/
  11. $post_vars = filter_input_array(INPUT_POST);
  12. $get_vars = filter_input_array(INPUT_GET);
  13. $form_vars = array_merge((array)$post_vars, (array)$get_vars);
  14. //exit('Form vars:<pre>' . PHP_EOL . print_r($form_vars, true));
  15. $action = (isset($form_vars['action'])) ? $form_vars['action'] : '';
  16. $group = (isset($get_vars['group'])) ? $get_vars['group'] : 1;
  17. $mainContent = $template->getSection('SRAI_lookup');
  18. $msg = (empty($action)) ? '' : $action();
  19. $mainContent = str_replace('[group]', $group, $mainContent);
  20. $upperScripts = '<script type="text/javascript" src="scripts/tablesorter.min.js"></script>'."\n";
  21. $topNav = $template->getSection('TopNav');
  22. $leftNav = $template->getSection('LeftNav');
  23. $rightNav = $template->getSection('RightNav');
  24. $main = $template->getSection('Main');
  25. $navHeader = $template->getSection('NavHeader');
  26. $FooterInfo = getFooter();
  27. $errMsgClass = (!empty($msg)) ? "ShowError" : "HideError";
  28. $errMsgStyle = $template->getSection($errMsgClass);
  29. $noLeftNav = '';
  30. $noTopNav = '';
  31. $noRightNav = $template->getSection('NoRightNav');
  32. $headerTitle = 'Actions:';
  33. $pageTitle = 'My-Program O - SRAI Lookup';
  34. $mainTitle = 'SRAI Lookup';
  35. $countSQL = 'select count(id) from srai_lookup where bot_id = :bot_id;';
  36. $countSTH = $dbConn->prepare($countSQL);
  37. /** @noinspection PhpUndefinedVariableInspection */
  38. $countSTH->bindValue(':bot_id', $bot_id, PDO::PARAM_INT);
  39. $countSTH->execute();
  40. $countRow = $countSTH->fetch();
  41. $countSTH->closeCursor();
  42. $row_count = number_format($countRow['count(id)']);
  43. $mainContent = str_replace('[row_count]', $row_count, $mainContent);
  44. $mainContent = str_replace('[bot_name]', $bot_name, $mainContent);
  45. /**
  46. * Function fillLookup
  47. *
  48. *
  49. * @return string
  50. */
  51. function fillLookup()
  52. {
  53. global $dbConn;
  54. $msg = '';
  55. $timeStart = microtime(true);
  56. // Drop the index on the table srai_lookup to speed things up
  57. try
  58. {
  59. $dropSQL = '
  60. ALTER TABLE srai_lookup DROP INDEX pattern;
  61. TRUNCATE TABLE `srai_lookup`;
  62. ALTER TABLE `aiml` ADD INDEX `srai_search` (`bot_id`, `pattern`(64));';
  63. $dropSTH = $dbConn->prepare($dropSQL);
  64. $dropSTH->execute();
  65. $dropSTH->closeCursor();
  66. }
  67. catch(Exception $e){}
  68. $searchSQL = "select id, bot_id, template from aiml where template like '%<srai>%' order by id asc;";
  69. $es = microtime(true);
  70. $searchSTH = $dbConn->prepare($searchSQL);
  71. $searchSTH->execute();
  72. $searchResult = $searchSTH->fetchAll();
  73. $searchSTH->closeCursor();
  74. $rowCount = count($searchResult);
  75. $totalRows = number_format($rowCount);
  76. $msg .= ("Found $totalRows rows that contain SRAI calls.<br>\n");
  77. //exit();
  78. $patterns = array(); // array to contain valid patterns, to prevent duplicates
  79. foreach ($searchResult as $row)
  80. {
  81. $tid = $row['id'];
  82. $bot_id = $row['bot_id'];
  83. if (!isset($patterns[$bot_id])) $patterns[$bot_id] = array();
  84. $AIMLtemplate = trim($row['template']);
  85. while (stripos($AIMLtemplate, '<srai>', 0) !== false)
  86. {
  87. $start = stripos($AIMLtemplate, '<srai>', 0);
  88. $end = stripos($AIMLtemplate, '</srai>', $start);
  89. $len = $end - $start;
  90. $srai = substr($AIMLtemplate, $start, $len);
  91. $srai = strtoupper($srai);
  92. $srai = trim(str_replace('<SRAI>', '', $srai));
  93. if (strstr($srai,'<') == false)
  94. {
  95. if (!in_array($srai, $patterns[$bot_id]))
  96. {
  97. $patterns[$bot_id][] = $srai;
  98. }
  99. }
  100. $AIMLtemplate = substr($AIMLtemplate, $end);
  101. }
  102. }
  103. $patternSQL = 'select id from aiml where pattern = :pattern and bot_id = :bot_id order by id limit 1;';
  104. $patternSTH = $dbConn->prepare($patternSQL);
  105. $lookups = array();
  106. foreach ($patterns as $id => $row)
  107. {
  108. foreach ($row as $pattern)
  109. {
  110. $patternSTH->bindValue(':pattern', $pattern);
  111. $patternSTH->bindValue(':bot_id', $id);
  112. $patternSTH->execute();
  113. $patternResult = $patternSTH->fetch();
  114. $patternSTH->closeCursor();
  115. $template_id = $patternResult['id'];
  116. $lookups[] = array(
  117. 'bot_id' => $id,
  118. 'pattern' => $pattern,
  119. 'template_id' => $template_id
  120. );
  121. }
  122. }
  123. $insertSQL = "insert into srai_lookup (id, bot_id, pattern, template_id) values (null, :bot_id, :pattern, :template_id);";
  124. $insertSTH = $dbConn->prepare($insertSQL);
  125. $insertCount = 0;
  126. foreach ($lookups as $row)
  127. {
  128. extract($row);
  129. if (empty($template_id)) continue;
  130. $insertSTH->bindValue(':bot_id', $bot_id);
  131. $insertSTH->bindValue(':pattern', $pattern);
  132. $insertSTH->bindValue(':template_id', $template_id);
  133. $insertSTH->execute();
  134. $insertCount++;
  135. }
  136. // Now put the index back
  137. $indexSQL = 'ALTER TABLE `srai_lookup` ADD INDEX `pattern` (`bot_id`, `pattern`(64)); ALTER TABLE aiml DROP INDEX srai_search;';
  138. $indexSTH = $dbConn->prepare($indexSQL);
  139. $indexSTH->execute();
  140. $insertCount = number_format($insertCount);
  141. $msg .= "Inserted $insertCount new entries into the SRAI lookup table!<br>\n";
  142. $timeEnd = microtime(true);
  143. $elapsed = $timeEnd - $timeStart;
  144. $elapsed = round($elapsed, 3);
  145. $msg .= "Elapsed time: $elapsed seconds.<br>\n";
  146. return $msg;
  147. }