Encyclopedia: Various improvements on aliases.

* Make 'delete' work on aliases without prior 'unforget' of target.
* Also show forgotten aliases in factoid info.
* Also hide forgotten aliases from web page unless searched.
* Improve handling of alias edits.
* Check for aliases on 'forget' and 'delete'
This commit is contained in:
Krytarik Raido
2021-06-14 01:56:04 +02:00
parent 987b3c91fa
commit 902534d10b
3 changed files with 42 additions and 23 deletions

View File

@ -24,7 +24,7 @@ import supybot
import supybot.world as world
from importlib import reload
__version__ = "3.5.2"
__version__ = "3.6.0"
__author__ = supybot.Author("Krytarik Raido", "krytarik", "krytarik@gmail.com")
__contributors__ = {
supybot.Author("Dennis Kaarsemaker", "Seveas", "dennis@kaarsemaker.net"): ['Original Author'],

View File

@ -80,10 +80,10 @@ if search:
cur.execute("SELECT COUNT(*) FROM facts WHERE name NOT LIKE '%%-also' AND %s" % qterms, values)
total = cur.fetchall()[0][0]
else:
cur.execute("SELECT name, value, author, added, editor, edited, popularity FROM facts WHERE value NOT LIKE '<alias>%%' AND name NOT LIKE '%%-also' ORDER BY %s LIMIT %d, %d" %
cur.execute("SELECT name, value, author, added, editor, edited, popularity FROM facts WHERE value NOT LIKE '%%<alias>%%' AND name NOT LIKE '%%-also' ORDER BY %s LIMIT %d, %d" %
(order_by, NUM_PER_PAGE * (page - 1), NUM_PER_PAGE))
factoids = [Factoid(*x) for x in cur.fetchall()]
cur.execute("SELECT COUNT(*) FROM facts WHERE value NOT LIKE '<alias>%%' AND name NOT LIKE '%%-also'")
cur.execute("SELECT COUNT(*) FROM facts WHERE value NOT LIKE '%%<alias>%%' AND name NOT LIKE '%%-also'")
total = cur.fetchall()[0][0]
# Pagination links

View File

@ -132,9 +132,14 @@ def capab(prefix, capability):
return False
def get_factoid_label(n, v):
if v.startswith('<alias>'):
n += '@' + v[7:].strip()
elif v.startswith('<deleted>'):
if '<alias>' in v:
n += '@' + v[v.find('<alias>')+7:].strip()
if v.startswith('<deleted>'):
n += '*'
return n
def get_alias_label(n, v):
if v.startswith('<deleted>'):
n += '*'
return n
@ -347,13 +352,9 @@ class Encyclopedia(callbacks.Plugin):
if not factoid:
return
ret = [get_factoid_label(factoid.name, factoid.value)]
db = self.get_db(channel)
cur = db.cursor()
# Try and find aliases
cur.execute("SELECT name FROM facts WHERE value LIKE ?", ('<alias>_%s' % factoid.name,))
data = cur.fetchall()
if data:
ret.append("aliases: %s" % ', '.join([x[0] for x in data]))
aliases = self.get_aliases(channel, factoid)
if aliases:
ret.append("aliases: %s" % ', '.join([get_alias_label(x[0], x[1]) for x in aliases]))
else:
ret.append("no aliases")
# Author info
@ -364,21 +365,26 @@ class Encyclopedia(callbacks.Plugin):
ret.append("requested %d times" % factoid.popularity)
return ' - '.join(ret)
def check_aliases(self, channel, factoid):
def get_aliases(self, channel, factoid):
db = self.get_db(channel)
cur = db.cursor()
cur.execute("SELECT name, value FROM facts WHERE value LIKE ?", ('%%<alias>_%s' % factoid.name,))
data = cur.fetchall()
return data
def check_aliases(self, channel, factoid, oldvalue, etype):
now = time.time()
for e in list(self.edits.keys()):
if self.edits[e] < now - 10:
self.edits.pop(e)
if not factoid.value.startswith('<alias>'):
return
# Was the old value an alias?
oldf = self.get_single_factoid(channel, factoid.name)
if oldf and oldf.value.startswith('<alias>'):
if etype == 'edit' and oldvalue.startswith('<alias>') \
and not factoid.value.startswith('<alias>'):
if factoid.name not in self.edits:
self.edits[factoid.name] = now
return "You are editing an alias. Please repeat the edit command within the next 10 seconds to confirm"
# Do some alias resolving
if factoid.value.startswith('<alias>'):
if factoid.value.startswith('<alias>') and etype != 'delete':
alias_name = factoid.value[7:].strip()
alias = self.get_single_factoid(channel, alias_name)
if not alias:
@ -599,8 +605,11 @@ class Encyclopedia(callbacks.Plugin):
factoid = self.get_single_factoid(channel, name, deleted=True)
if not factoid:
return "I know nothing about '%s' yet, %s" % (name, editor)
aliases = self.get_aliases(channel, factoid)
if aliases:
return "Factoid '%s' has aliases still, %s" % (name, editor)
retmsg = "I'll delete that, %s" % editor
ret = self.check_aliases(channel, factoid)
ret = self.check_aliases(channel, factoid, None, 'delete')
if ret:
return ret
return 'delete', factoid, retmsg
@ -615,7 +624,8 @@ class Encyclopedia(callbacks.Plugin):
db.commit()
def factoid_edit_check(self, text, channel, editor):
factoid = value = retmsg = None
factoid = value = oldvalue = retmsg = None
etype = 'edit'
if text.lower()[:3] in ('no ', 'no,'):
match = re.match(r'^no[\s,]+(?P<name>\S+.*?)\s+is(?:\s+|\b)(?P<value>.*?\S+)$', text, re.I)
if not match:
@ -627,24 +637,32 @@ class Encyclopedia(callbacks.Plugin):
return "I know nothing about '%s' yet, %s" % (name, editor)
if value == factoid.value:
return "Nothing changed there"
oldvalue = factoid.value
factoid.value = value
retmsg = "I'll remember that, %s" % editor
elif text.lower().startswith('forget '):
etype = 'forget'
name = text[7:].strip()
factoid = self.get_single_factoid(channel, name)
if not factoid:
return "I know nothing about '%s' yet, %s" % (name, editor)
aliases = self.get_aliases(channel, factoid)
if aliases:
return "Factoid '%s' has aliases still, %s" % (name, editor)
oldvalue = factoid.value
factoid.value = '<deleted>%s' % factoid.value
retmsg = "I'll forget that, %s" % editor
elif text.lower().startswith('unforget '):
etype = 'unforget'
name = text[9:].strip()
factoid = self.get_single_factoid(channel, name, deleted=True)
if not factoid:
return "I know nothing about '%s' at all, %s" % (name, editor)
if not factoid.value.startswith('<deleted>'):
return "Factoid '%s' is not deleted yet, %s" % (factoid.name, editor)
oldvalue = factoid.value
factoid.value = factoid.value[9:]
retmsg = "I suddenly remember '%s' again, %s" % (factoid.name, editor)
@ -676,10 +694,11 @@ class Encyclopedia(callbacks.Plugin):
value = regex.sub(replace, factoid.value, 1)
if value == factoid.value:
return "Nothing changed there"
oldvalue = factoid.value
factoid.value = value
retmsg = "I'll remember that, %s" % editor
ret = self.check_aliases(channel, factoid)
ret = self.check_aliases(channel, factoid, oldvalue, etype)
if ret:
return ret
return 'edit', factoid, retmsg
@ -710,7 +729,7 @@ class Encyclopedia(callbacks.Plugin):
return "But '%s' already means something else!" % name
factoid = Factoid(name, value)
retmsg = "I'll remember that, %s" % editor
ret = self.check_aliases(channel, factoid)
ret = self.check_aliases(channel, factoid, None, 'add')
if ret:
return ret
return 'add', factoid, retmsg