Factoids: Add --author filtering to @search.

This commit is contained in:
Valentin Lorentz 2020-07-14 18:15:47 +02:00
parent 6e47278b9f
commit a84eabf146
2 changed files with 25 additions and 6 deletions

View File

@ -801,7 +801,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
_sqlTrans = utils.str.MultipleReplacer({'*': '%', '?': '_'}) _sqlTrans = utils.str.MultipleReplacer({'*': '%', '?': '_'})
@internationalizeDocstring @internationalizeDocstring
def search(self, irc, msg, args, channel, optlist, globs): def search(self, irc, msg, args, channel, optlist, globs):
"""[<channel>] [--values] [--{regexp} <value>] [<glob> ...] """[<channel>] [--values] [--regexp <value>] [--author <username>] [<glob> ...]
Searches the keyspace for keys matching <glob>. If --regexp is given, Searches the keyspace for keys matching <glob>. If --regexp is given,
its associated value is taken as a regexp and matched against the keys. its associated value is taken as a regexp and matched against the keys.
@ -810,6 +810,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
if not optlist and not globs: if not optlist and not globs:
raise callbacks.ArgumentError raise callbacks.ArgumentError
tables = ['keys'] tables = ['keys']
join_factoids = False
formats = [] formats = []
criteria = [] criteria = []
target = 'keys.key' target = 'keys.key'
@ -818,19 +819,29 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
for (option, arg) in optlist: for (option, arg) in optlist:
if option == 'values': if option == 'values':
target = 'factoids.fact' target = 'factoids.fact'
if 'factoids' not in tables: join_factoids = True
tables.append('factoids')
tables.append('relations')
criteria.append('factoids.id=relations.fact_id AND keys.id=relations.key_id')
elif option == 'regexp': elif option == 'regexp':
criteria.append('%s(TARGET)' % predicateName) criteria.append('%s(TARGET)' % predicateName)
def p(s, r=arg): def p(s, r=arg):
return int(bool(r.search(s))) return int(bool(r.search(s)))
db.create_function(predicateName, 1, p) db.create_function(predicateName, 1, p)
predicateName += 'p' predicateName += 'p'
elif option == 'author':
join_factoids = True
criteria.append('factoids.added_by=?')
formats.append(arg)
for glob in globs: for glob in globs:
criteria.append('TARGET LIKE ?') criteria.append('TARGET LIKE ?')
formats.append(self._sqlTrans(glob)) formats.append(self._sqlTrans(glob))
if join_factoids:
if 'factoids' not in tables:
tables.append('factoids')
tables.append('relations')
criteria.append(
'factoids.id=relations.fact_id AND keys.id=relations.key_id'
)
cursor = db.cursor() cursor = db.cursor()
sql = """SELECT keys.key FROM %s WHERE %s""" % \ sql = """SELECT keys.key FROM %s WHERE %s""" % \
(', '.join(tables), ' AND '.join(criteria)) (', '.join(tables), ' AND '.join(criteria))
@ -861,7 +872,11 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
s = format('%L', keys) s = format('%L', keys)
irc.reply(s) irc.reply(s)
search = wrap(search, ['channel', search = wrap(search, ['channel',
getopts({'values': '', 'regexp': 'regexpMatcher'}), getopts({
'values': '',
'regexp': 'regexpMatcher',
'author': 'somethingWithoutSpaces',
}),
any('glob')]) any('glob')])

View File

@ -112,6 +112,10 @@ class FactoidsTestCase(ChannelPluginTestCase):
'my primary author') 'my primary author')
self.assertRegexp('factoids search --values primary author', self.assertRegexp('factoids search --values primary author',
'my primary author') 'my primary author')
self.assertRegexp('factoids search --author test j*',
'jamessan.*jemfinch')
self.assertRegexp('factoids search --author blahblah j*',
'No keys matched that query.')
def testWhatisOnNumbers(self): def testWhatisOnNumbers(self):
self.assertNotError('learn 911 is emergency number') self.assertNotError('learn 911 is emergency number')