From 203308647b74c9a4ca87b2de2a7d5ec6cb4c1f35 Mon Sep 17 00:00:00 2001 From: Daniel Folkinshteyn Date: Fri, 2 Apr 2010 00:49:43 -0400 Subject: [PATCH] add factoids alias function, to link more keys to existing factoids. --- plugins/Factoids/plugin.py | 71 +++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/plugins/Factoids/plugin.py b/plugins/Factoids/plugin.py index 28dc665cd..e976b7eda 100644 --- a/plugins/Factoids/plugin.py +++ b/plugins/Factoids/plugin.py @@ -304,7 +304,76 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler): factoids = self._lookupFactoid(channel, key) self._replyFactoids(irc, msg, key, channel, factoids, number) whatis = wrap(whatis, ['channel', many('something')]) - + + def alias(self, irc, msg, args, channel, oldkey, newkey, number): + """[] [] + + Adds a new key for factoid associated with . + is only necessary if there's more than one factoid associated + with . + + The same action can be accomplished by using the 'learn' function with + a new key but an existing (verbatim) factoid content. + """ + def _getNewKey(channel, newkey, arelation): + db = self.getDb(channel) + cursor = db.cursor() + cursor.execute("""SELECT id FROM keys WHERE key=?""", (newkey,)) + newkey_info = cursor.fetchall() + if len(newkey_info) == 1: + # check if we already have the requested relation + cursor.execute("""SELECT id FROM relations WHERE + key_id=? and fact_id=?""", + (arelation[1], arelation[2])) + existentrelation = cursor.fetchall() + if len(existentrelation) != 0: + newkey_info = False + if len(newkey_info) == 0: + cursor.execute("""INSERT INTO keys VALUES (NULL, ?)""", + (newkey,)) + db.commit() + cursor.execute("""SELECT id FROM keys WHERE key=?""", (newkey,)) + newkey_info = cursor.fetchall() + return newkey_info + + db = self.getDb(channel) + cursor = db.cursor() + cursor.execute("""SELECT relations.id, relations.key_id, relations.fact_id + FROM keys, relations + WHERE keys.key=? AND + relations.key_id=keys.id""", (oldkey,)) + results = cursor.fetchall() + if len(results) == 0: + irc.error('No factoid matches that key.') + return + elif len(results) == 1: + newkey_info = _getNewKey(channel, newkey, results[0]) + if newkey_info is not False: + cursor.execute("""INSERT INTO relations VALUES(NULL, ?, ?, ?)""", + (newkey_info[0][0], results[0][2], 0,)) + irc.replySuccess() + else: + irc.error('This key-factoid relationship already exists.') + elif len(results) > 1: + try: + arelation = results[number-1] + except IndexError: + irc.error("That's not a valid number for that key.") + return + except TypeError: + irc.error("This key has more than one factoid associated with " + "it, but you have not provided a number.") + return + newkey_info = _getNewKey(channel, newkey, arelation) + if newkey_info is not False: + cursor.execute("""INSERT INTO relations VALUES(NULL, ?, ?, ?)""", + (newkey_info[0][0], arelation[2], 0,)) + irc.replySuccess() + else: + irc.error('This key-factoid relationship already exists.') + + alias = wrap(alias, ['channel', 'something', 'something', optional('int')]) + def rank(self, irc, msg, args, channel): """[]