Documentation is available at delete.inc.php
- <?php
- /*
- Program E related AIML knowledgebase tools
- Copyright 2004, Anne Kootstra [anne@aiml.info]
- http://www.AIML.info
- Program E is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- Program E is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Program E; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- /**
- * Delete a category using template ID
- *
- * This is the container function for a set of other functions for the
- * deletion of a category. It will travel the AIML tree backwards checking
- * if a word-node is used by other categories, if not then deletes it and
- * travels onward to the next word-node. If it it is used, then breaks of
- * the routine, it has then accomplised what it needed to do.
- *
- * @uses findmainnode()
- * @uses debugger()
- * @uses templateCheck()
- * @uses findprevioustemplate()
- * @uses deleteTemplate()
- * @uses updateGmcache()
- * @uses deleteCheck()
- * @uses findParent()
- * @uses deletePatternElement()
- *
- * @param integer $templateID The template's ID
- * @param integer $botid The bot's ID.
- *
- * @return void
- */
- function delete_category($templateID, $botid){
- // retrieve the pattern ID of the main node
- $mainNode= findmainnode($botid);
- debugger("(delete_category) mainNode ".$mainNode,2);
- // check if the template exists. If not, then no use continuing.
- $temp_check = templateCheck($templateID);
- if($temp_check) {
- /*
- categories are put in the database in sequence with the last
- record being the reference to the template table. Thus a
- category (roughly) would exist from template reference to
- template reference.
- */
- $previous_templateID = findprevioustemplate($templateID);
- } else {
- break;
- }
- // if there is no previous template, for example often CONNECT category
- // or many other top level categories, the main node will need to serve
- // this purpose.
- if($previous_templateID == '') {
- $previous_templateID = $mainNode;
- }
- // delete the template from the templates table
- deleteTemplate($templateID);
- // edit (perhaps delete be better?) the GMcache to prevent cache responses
- // from bot using non-existent categories.
- updateGmcache($templateID);
- // Only if the node is not used by other categories and the template ID is
- // lower than that of the next template reference (or main node) are we allowed
- // to continue.
- while((deleteCheck($templateID) == 'TRUE')&&($templateID>$previous_templateID)) {
- // find the patternID of next word (i.e parentID of current word)
- // of the next word.
- $parentID = findParent($templateID);
- debugger("(delete_category) (while) parentID ".$parentID,2);
- // for the current word the patternID is the templateID
- $patternID = $templateID;
- debugger("(delete_category) (while) patternID ".$patternID,2);
- // delete the word (or _ or *)
- deletePatternElement($patternID);
- // prepare for the next round, setting focus on the next word
- $templateID = $parentID;
- debugger("(delete_category) (while) templateID ".$templateID,2);
- }
- }
- /**
- * Delete the word from the Pattern Table.
- *
- * Remove the record from the pattern table using it's unique ID.
- *
- * @uses debugger()
- *
- * @param integer $patternID The word's pattern ID
- *
- * @return void
- *
- */
- function deletePatternElement($patternID){
- $query = "delete from patterns where id='".$patternID."'";
- debugger($query,2);
- $selectcode = mysql_query($query);
- }
- /**
- * Delete the template from the Template Table.
- *
- * Delete the template record with a particular ID.
- *
- * @uses debugger()
- *
- * @param integer $templateID The category's ID
- *
- * @return void
- *
- */
- function deleteTemplate($templateID){
- $query = "delete from templates where id='".$templateID."'";
- debugger($query,2);
- $selectcode = mysql_query($query);
- }
- /**
- * Disable record in GMcache table.
- *
- * The GMcache is used to speed up the bot's response time, by checking the
- * cache first before waking the AIML tree. But also means that if a category
- * is deleted, all Cached references need to be deleted or edited too.
- * Currently the records are edited, the Combined contents is replaced by
- * REMOVED and the template ID is replaced by 0
- *
- * @uses debugger()
- *
- * @param integer $templateID The category's ID
- *
- * @return void
- *
- */
- function updateGmcache($templateID){
- $query = "UPDATE gmcache SET combined = 'REMOVED', template = '000000' WHERE template = '".$templateID."'";
- debugger($query,2);
- $selectcode = mysql_query($query);
- }
- /**
- * Check if it is safe to delete node.
- *
- * In order to be able to delete a node from a tree, there shouldn't be any
- * other records referencing to it. This function checks this for this
- * these types of references. So it checks to see if it's safe to delete this
- * node. TRUE -> safe, FALSE -> not safe.
- *
- * @param integer $patternID The word's pattern ID
- *
- * @return boolean TRUE/FALSE.
- *
- */
- function deleteCheck($parentID){
- $query = "select if(count(id) = 0, 'TRUE', 'FALSE') as delete_safe from patterns where parent = '".$parentID."'";
- //debugger($query,2);
- $selectcode = mysql_query($query);
- if ($selectcode){
- if(!mysql_numrows($selectcode)){
- return "";
- }
- else{
- while ($q = mysql_fetch_array($selectcode)){
- return $q[0];
- }
- }
- }
- return "";
- }
- /**
- * Retrieve the previous category's reference to the template Table.
- *
- * Categories are put in the database in sequence with the last record being
- * the reference to the template table. Thus a category (roughly) would exist
- * from template reference to template reference.
- *
- * @param integer $templateID The category's ID
- *
- * @return integer pattern/template ID.
- *
- */
- function findprevioustemplate($templateID){
- $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";
- //debugger($query,2);
- $selectcode = mysql_query($query);
- if ($selectcode){
- if(!mysql_numrows($selectcode)){
- return "";
- }
- else{
- while ($q = mysql_fetch_array($selectcode)){
- return $q[0];
- }
- }
- }
- return "";
- }
- ?>
Documentation generated on Tue, 11 Jan 2005 18:41:04 +0100 by phpDocumentor 1.3.0RC3