Source for file delete.inc.php

Documentation is available at delete.inc.php

  1. <?php
  2.  
  3. /*
  4. Program E related AIML knowledgebase tools
  5. Copyright 2004, Anne Kootstra [anne@aiml.info]
  6. http://www.AIML.info
  7. Program E is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. Program E is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Program E; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21.  
  22.  
  23.  
  24. /**
  25. * Delete a category using template ID
  26. *
  27. * This is the container function for a set of other functions for the
  28. * deletion of a category. It will travel the AIML tree backwards checking
  29. * if a word-node is used by other categories, if not then deletes it and
  30. * travels onward to the next word-node. If it it is used, then breaks of
  31. * the routine, it has then accomplised what it needed to do.
  32. *
  33. * @uses findmainnode()
  34. * @uses debugger()
  35. * @uses templateCheck()
  36. * @uses findprevioustemplate()
  37. * @uses deleteTemplate()
  38. * @uses updateGmcache()
  39. * @uses deleteCheck()
  40. * @uses findParent()
  41. * @uses deletePatternElement()
  42. *
  43. * @param integer $templateID The template's ID
  44. * @param integer $botid The bot's ID.
  45. *
  46. * @return void
  47. */
  48. function delete_category($templateID, $botid){
  49. // retrieve the pattern ID of the main node
  50. $mainNode= findmainnode($botid);
  51. debugger("(delete_category) mainNode ".$mainNode,2);
  52. // check if the template exists. If not, then no use continuing.
  53. $temp_check = templateCheck($templateID);
  54. if($temp_check) {
  55.  
  56. /*
  57. categories are put in the database in sequence with the last
  58. record being the reference to the template table. Thus a
  59. category (roughly) would exist from template reference to
  60. template reference.
  61. */
  62. $previous_templateID = findprevioustemplate($templateID);
  63. } else {
  64. break;
  65. }
  66. // if there is no previous template, for example often CONNECT category
  67. // or many other top level categories, the main node will need to serve
  68. // this purpose.
  69. if($previous_templateID == '') {
  70. $previous_templateID = $mainNode;
  71. }
  72. // delete the template from the templates table
  73. deleteTemplate($templateID);
  74. // edit (perhaps delete be better?) the GMcache to prevent cache responses
  75. // from bot using non-existent categories.
  76. updateGmcache($templateID);
  77. // Only if the node is not used by other categories and the template ID is
  78. // lower than that of the next template reference (or main node) are we allowed
  79. // to continue.
  80. while((deleteCheck($templateID) == 'TRUE')&&($templateID>$previous_templateID)) {
  81. // find the patternID of next word (i.e parentID of current word)
  82. // of the next word.
  83. $parentID = findParent($templateID);
  84. debugger("(delete_category) (while) parentID ".$parentID,2);
  85.  
  86. // for the current word the patternID is the templateID
  87. $patternID = $templateID;
  88. debugger("(delete_category) (while) patternID ".$patternID,2);
  89. // delete the word (or _ or *)
  90. deletePatternElement($patternID);
  91. // prepare for the next round, setting focus on the next word
  92. $templateID = $parentID;
  93. debugger("(delete_category) (while) templateID ".$templateID,2);
  94. }
  95. }
  96.  
  97.  
  98. /**
  99. * Delete the word from the Pattern Table.
  100. *
  101. * Remove the record from the pattern table using it's unique ID.
  102. *
  103. * @uses debugger()
  104. *
  105. * @param integer $patternID The word's pattern ID
  106. *
  107. * @return void
  108. *
  109. */
  110. function deletePatternElement($patternID){
  111.  
  112. $query = "delete from patterns where id='".$patternID."'";
  113. debugger($query,2);
  114. $selectcode = mysql_query($query);
  115. }
  116.  
  117.  
  118. /**
  119. * Delete the template from the Template Table.
  120. *
  121. * Delete the template record with a particular ID.
  122. *
  123. * @uses debugger()
  124. *
  125. * @param integer $templateID The category's ID
  126. *
  127. * @return void
  128. *
  129. */
  130. function deleteTemplate($templateID){
  131.  
  132. $query = "delete from templates where id='".$templateID."'";
  133. debugger($query,2);
  134. $selectcode = mysql_query($query);
  135. }
  136.  
  137.  
  138. /**
  139. * Disable record in GMcache table.
  140. *
  141. * The GMcache is used to speed up the bot's response time, by checking the
  142. * cache first before waking the AIML tree. But also means that if a category
  143. * is deleted, all Cached references need to be deleted or edited too.
  144. * Currently the records are edited, the Combined contents is replaced by
  145. * REMOVED and the template ID is replaced by 0
  146. *
  147. * @uses debugger()
  148. *
  149. * @param integer $templateID The category's ID
  150. *
  151. * @return void
  152. *
  153. */
  154. function updateGmcache($templateID){
  155.  
  156. $query = "UPDATE gmcache SET combined = 'REMOVED', template = '000000' WHERE template = '".$templateID."'";
  157. debugger($query,2);
  158. $selectcode = mysql_query($query);
  159. }
  160.  
  161.  
  162. /**
  163. * Check if it is safe to delete node.
  164. *
  165. * In order to be able to delete a node from a tree, there shouldn't be any
  166. * other records referencing to it. This function checks this for this
  167. * these types of references. So it checks to see if it's safe to delete this
  168. * node. TRUE -> safe, FALSE -> not safe.
  169. *
  170. * @param integer $patternID The word's pattern ID
  171. *
  172. * @return boolean TRUE/FALSE.
  173. *
  174. */
  175. function deleteCheck($parentID){
  176.  
  177. $query = "select if(count(id) = 0, 'TRUE', 'FALSE') as delete_safe from patterns where parent = '".$parentID."'";
  178. //debugger($query,2);
  179. $selectcode = mysql_query($query);
  180. if ($selectcode){
  181. if(!mysql_numrows($selectcode)){
  182. return "";
  183. }
  184. else{
  185. while ($q = mysql_fetch_array($selectcode)){
  186. return $q[0];
  187. }
  188. }
  189. }
  190.  
  191. return "";
  192. }
  193.  
  194.  
  195. /**
  196. * Retrieve the previous category's reference to the template Table.
  197. *
  198. * Categories are put in the database in sequence with the last record being
  199. * the reference to the template table. Thus a category (roughly) would exist
  200. * from template reference to template reference.
  201. *
  202. * @param integer $templateID The category's ID
  203. *
  204. * @return integer pattern/template ID.
  205. *
  206. */
  207. function findprevioustemplate($templateID){
  208.  
  209. $query = "select p.id from patterns as p, templates as t where p.id = t.id and p.id<=".$templateID." order by id desc limit 1,1";
  210. //debugger($query,2);
  211. $selectcode = mysql_query($query);
  212. if ($selectcode){
  213. if(!mysql_numrows($selectcode)){
  214. return "";
  215. }
  216. else{
  217. while ($q = mysql_fetch_array($selectcode)){
  218. return $q[0];
  219. }
  220. }
  221. }
  222.  
  223. return "";
  224. }
  225.  
  226. ?>

Documentation generated on Tue, 11 Jan 2005 18:41:04 +0100 by phpDocumentor 1.3.0RC3