def write_to_methoddata_table(id_method, data_dict, data_dict_ordered, data_list_sorted, update_timestamp=True): """Serialize the date and write it to the bsrMETHODDATA""" write_message('Starting serializing the data..', verbose=5) serialized_data_dict = serialize_via_marshal(data_dict) serialized_data_dict_ordered = serialize_via_marshal(data_dict_ordered) serialized_data_list_sorted = serialize_via_marshal(data_list_sorted) write_message('Serialization completed.', verbose=5) date = strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if not update_timestamp: try: date = run_sql( 'SELECT last_update from bsrMETHODDATA WHERE id_bsrMETHOD = %s', (id_method, ))[0][0] except IndexError: pass # keep the generated date write_message("Starting writing the data for method_id=%s " \ "to the database (table bsrMETHODDATA)" %id_method, verbose=5) try: write_message('Deleting old data..', verbose=5) run_sql("DELETE FROM bsrMETHODDATA WHERE id_bsrMETHOD = %s", (id_method, )) write_message('Inserting new data..', verbose=5) run_sql("INSERT into bsrMETHODDATA \ (id_bsrMETHOD, data_dict, data_dict_ordered, data_list_sorted, last_updated) \ VALUES (%s, %s, %s, %s, %s)" , \ (id_method, serialized_data_dict, serialized_data_dict_ordered, \ serialized_data_list_sorted, date, )) except Error, err: write_message("The error [%s] occured when inserting new bibsort data "\ "into bsrMETHODATA table" %err, sys.stderr) return False
def write_to_methoddata_table(id_method, data_dict, data_dict_ordered, data_list_sorted, update_timestamp=True): """Serialize the date and write it to the bsrMETHODDATA""" write_message('Starting serializing the data..', verbose=5) serialized_data_dict = serialize_via_marshal(data_dict) serialized_data_dict_ordered = serialize_via_marshal(data_dict_ordered) serialized_data_list_sorted = serialize_via_marshal(data_list_sorted) write_message('Serialization completed.', verbose=5) date = strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if not update_timestamp: try: date = run_sql('SELECT last_update from bsrMETHODDATA WHERE id_bsrMETHOD = %s', (id_method, ))[0][0] except IndexError: pass # keep the generated date write_message("Starting writing the data for method_id=%s " \ "to the database (table bsrMETHODDATA)" %id_method, verbose=5) try: write_message('Deleting old data..', verbose=5) run_sql("DELETE FROM bsrMETHODDATA WHERE id_bsrMETHOD = %s", (id_method, )) write_message('Inserting new data..', verbose=5) run_sql("INSERT into bsrMETHODDATA \ (id_bsrMETHOD, data_dict, data_dict_ordered, data_list_sorted, last_updated) \ VALUES (%s, %s, %s, %s, %s)", \ (id_method, serialized_data_dict, serialized_data_dict_ordered, \ serialized_data_list_sorted, date, )) except Error, err: write_message("The error [%s] occured when inserting new bibsort data "\ "into bsrMETHODATA table" %err, sys.stderr) return False
def modify_oai_src(oai_src_id, oai_src_name, oai_src_baseurl, oai_src_prefix, oai_src_frequency, oai_src_post, oai_src_comment, oai_src_sets=None, oai_src_args=None): """Modifies a row's parameters""" if oai_src_sets is None: oai_src_sets = [] if oai_src_post is None: oai_src_post = [] if oai_src_args is None: oai_src_args = {} sql = """UPDATE oaiHARVEST SET baseurl=%s, metadataprefix=%s, arguments=%s, comment=%s, name=%s, frequency=%s, postprocess=%s, setspecs=%s WHERE id=%s""" try: run_sql(sql, (oai_src_baseurl, oai_src_prefix, serialize_via_marshal(oai_src_args), oai_src_comment, oai_src_name, oai_src_frequency, '-'.join(oai_src_post), ' '.join(oai_src_sets), oai_src_id)) return (1, "") except StandardError, e: return (0, e)
def add_oai_src(oai_src_name, oai_src_baseurl, oai_src_prefix, oai_src_frequency, oai_src_lastrun, oai_src_post, oai_src_comment, oai_src_sets=None, oai_src_args=None): """Adds a new row to the database with the given parameters""" if oai_src_sets is None: oai_src_sets = [] if oai_src_args is None: oai_src_args = {} #return (0, str(serialize_via_marshal(oai_src_args))) try: if oai_src_lastrun in [0, "0"]: lastrun_mode = 'NULL' else: lastrun_mode = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # lastrun_mode = "'"+lastrun_mode+"'" run_sql("INSERT INTO oaiHARVEST " "(baseurl, metadataprefix, arguments, comment, name, lastrun, " "frequency, postprocess, setspecs) VALUES " "(%s, %s, %s, %s, %s, %s, %s, %s, %s)", (oai_src_baseurl, oai_src_prefix, serialize_via_marshal(oai_src_args), \ oai_src_comment, oai_src_name, lastrun_mode, oai_src_frequency, \ "-".join(oai_src_post), " ".join(oai_src_sets))) return (1, "") except StandardError, e: return (0, e)
def intoDB(dict, date, rank_method_code): """Insert the rank method data into the database""" mid = run_sql("SELECT id from rnkMETHOD where name=%s", (rank_method_code, )) del_rank_method_codeDATA(rank_method_code) serdata = serialize_via_marshal(dict) midstr = str(mid[0][0]) run_sql( "INSERT INTO rnkMETHODDATA(id_rnkMETHOD, relevance_data) VALUES (%s,%s)", ( midstr, serdata, )) run_sql("UPDATE rnkMETHOD SET last_updated=%s WHERE name=%s", (date, rank_method_code)) # FIXME: the following is a workaround for the citation indexer # memory troubles, when Apache WSGI daemon processes may end up # doubling the memory after citation dictionary is updated; # therefore let us restart the WSGI daemon application after the # citation indexer finished, which relieves this problem. The # restart is done via touching invenio.wsgi file. The proper fix # for this problem would be strict separation between citation # indexer updating dicts and citation searcher loading dicts. if rank_method_code == 'citation': os.system('touch ' + os.path.join(CFG_PREFIX, 'var', 'www-wsgi', 'invenio.wsgi'))
def insert_cit_ref_list_intodb(citation_dic, reference_dic, selfcbdic, selfdic, authorcitdic): """Insert the reference and citation list into the database""" insert_into_cit_db(reference_dic, "reversedict") insert_into_cit_db(citation_dic, "citationdict") insert_into_cit_db(selfcbdic, "selfcitedbydict") insert_into_cit_db(selfdic, "selfcitdict") for a in authorcitdic.keys(): lserarr = (serialize_via_marshal(authorcitdic[a])) #author name: replace " with something else a.replace('"', '\'') a = unicode(a, 'utf-8') try: ablob = run_sql( "select hitlist from rnkAUTHORDATA where aterm = %s", (a, )) if not (ablob): #print "insert into rnkAUTHORDATA(aterm,hitlist) values (%s,%s)" , (a,lserarr) run_sql( "insert into rnkAUTHORDATA(aterm,hitlist) values (%s,%s)", (a, lserarr)) else: #print "UPDATE rnkAUTHORDATA SET hitlist = %s where aterm=%s""" , (lserarr,a) run_sql( "UPDATE rnkAUTHORDATA SET hitlist = %s where aterm=%s", (lserarr, a)) except: register_exception( prefix="could not read/write rnkAUTHORDATA aterm=" + a + " hitlist=" + str(lserarr), alert_admin=True)
def insert_cit_ref_list_intodb(citation_dic, reference_dic, selfcbdic, selfdic, authorcitdic): """Insert the reference and citation list into the database""" insert_into_cit_db(reference_dic,"reversedict") insert_into_cit_db(citation_dic,"citationdict") insert_into_cit_db(selfcbdic,"selfcitedbydict") insert_into_cit_db(selfdic,"selfcitdict") for a in authorcitdic.keys(): lserarr = (serialize_via_marshal(authorcitdic[a])) #author name: replace " with something else a.replace('"', '\'') a = unicode(a, 'utf-8') try: ablob = run_sql("select hitlist from rnkAUTHORDATA where aterm = %s", (a,)) if not (ablob): #print "insert into rnkAUTHORDATA(aterm,hitlist) values (%s,%s)" , (a,lserarr) run_sql("insert into rnkAUTHORDATA(aterm,hitlist) values (%s,%s)", (a,lserarr)) else: #print "UPDATE rnkAUTHORDATA SET hitlist = %s where aterm=%s""" , (lserarr,a) run_sql("UPDATE rnkAUTHORDATA SET hitlist = %s where aterm=%s", (lserarr,a)) except: register_exception(prefix="could not read/write rnkAUTHORDATA aterm="+a+" hitlist="+str(lserarr), alert_admin=True)
def insert_into_cit_db(dic, name): """an aux thing to avoid repeating code""" ndate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) try: s = serialize_via_marshal(dic) write_message("size of " + name + " " + str(len(s))) #check that this column really exists testres = run_sql( "select object_name from rnkCITATIONDATA where object_name = %s", (name, )) if testres: run_sql( "UPDATE rnkCITATIONDATA SET object_value = %s where object_name = %s", (s, name)) else: #there was no entry for name, let's force.. run_sql( "INSERT INTO rnkCITATIONDATA(object_name,object_value) values (%s,%s)", (name, s)) run_sql( "UPDATE rnkCITATIONDATA SET last_updated = %s where object_name = %s", (ndate, name)) except: register_exception(prefix="could not write " + name + " into db", alert_admin=True)
def insert_into_cit_db(dic, name): """Stores citation dictionary in the database""" ndate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) s = serialize_via_marshal(dic) write_message("size of %s %s" % (name, len(s))) # check that this column really exists run_sql("""REPLACE INTO rnkCITATIONDATA(object_name, object_value, last_updated) VALUES (%s, %s, %s)""", (name, s, ndate))
def intoDB(dict, date, rank_method_code): """Insert the rank method data into the database""" mid = run_sql("SELECT id from rnkMETHOD where name=%s", (rank_method_code, )) del_rank_method_codeDATA(rank_method_code) serdata = serialize_via_marshal(dict); midstr = str(mid[0][0]); run_sql("INSERT INTO rnkMETHODDATA(id_rnkMETHOD, relevance_data) VALUES (%s,%s)", (midstr, serdata,)) run_sql("UPDATE rnkMETHOD SET last_updated=%s WHERE name=%s", (date, rank_method_code))
def get_recstruct_record(recid): value = serialize_via_marshal(get_record(recid)) b = Bibfmt(id_bibrec=recid, format='recstruct', last_updated=db.func.now(), value=value) db.session.add(b) db.session.commit()
def intoDB(dict, date, rank_method_code): """Insert the rank method data into the database""" mid = run_sql("SELECT id from rnkMETHOD where name=%s", (rank_method_code, )) del_rank_method_codeDATA(rank_method_code) serdata = serialize_via_marshal(dict); midstr = str(mid[0][0]); run_sql("INSERT INTO rnkMETHODDATA(id_rnkMETHOD, relevance_data) VALUES (%s,%s)", (midstr, serdata,)) if date: run_sql("UPDATE rnkMETHOD SET last_updated=%s WHERE name=%s", (date, rank_method_code))
def do_upgrade(): rows_to_change = run_sql("SELECT id, arguments FROM oaiHARVEST", with_dict=True) # Move away from old columns for row in rows_to_change: if row['arguments']: arguments = deserialize_via_marshal(row['arguments']) if "c_cfg-file" in arguments: arguments['c_stylesheet'] = arguments['c_cfg-file'] del arguments['c_cfg-file'] run_sql("UPDATE oaiHARVEST set arguments=%s WHERE id=%s", (serialize_via_marshal(arguments), row['id']))
def into_db(dict_of_ranks, rank_method_code): """Writes into the rnkMETHODDATA table the ranking results""" method_id = run_sql("SELECT id from rnkMETHOD where name=%s", \ (rank_method_code, )) del_rank_method_data(rank_method_code) serialized_data = serialize_via_marshal(dict_of_ranks) method_id_str = str(method_id[0][0]) run_sql("INSERT INTO rnkMETHODDATA(id_rnkMETHOD, relevance_data) \ VALUES(%s, %s) ", (method_id_str, serialized_data, )) date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) run_sql("UPDATE rnkMETHOD SET last_updated=%s WHERE name=%s", \ (date, rank_method_code)) write_message("Finished writing the ranks into rnkMETHOD table", verbose=5)
def do_upgrade(): create_statement = run_sql('SHOW CREATE TABLE oaiHARVEST')[0][1] if '`arguments` text' in create_statement: run_sql("ALTER TABLE oaiHARVEST CHANGE arguments arguments blob") # translate old values if '`bibconvertcfgfile`' in create_statement: rows_to_change = run_sql("SELECT id, bibconvertcfgfile, bibfilterprogram, arguments FROM oaiHARVEST", with_dict=True) # Move away from old columns for row in rows_to_change: if row['arguments']: arguments = deserialize_via_marshal(row['arguments']) else: arguments = {} arguments['c_cfg-file'] = row['bibconvertcfgfile'] arguments['f_filter-file'] = row['bibfilterprogram'] run_sql("UPDATE oaiHARVEST set arguments=%s WHERE id=%s", (serialize_via_marshal(arguments), row['id'])) run_sql("ALTER TABLE oaiHARVEST DROP COLUMN bibconvertcfgfile") run_sql("ALTER TABLE oaiHARVEST DROP COLUMN bibfilterprogram")
def insert_into_cit_db(dic, name): """an aux thing to avoid repeating code""" ndate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) try: s = serialize_via_marshal(dic) write_message("size of "+name+" "+str(len(s))) #check that this column really exists testres = run_sql("select object_name from rnkCITATIONDATA where object_name = %s", (name,)) if testres: run_sql("UPDATE rnkCITATIONDATA SET object_value = %s where object_name = %s", (s, name)) else: #there was no entry for name, let's force.. run_sql("INSERT INTO rnkCITATIONDATA(object_name,object_value) values (%s,%s)", (name,s)) run_sql("UPDATE rnkCITATIONDATA SET last_updated = %s where object_name = %s", (ndate,name)) except: register_exception(prefix="could not write "+name+" into db", alert_admin=True)
def intoDB(dict, date, rank_method_code): """Insert the rank method data into the database""" mid = run_sql("SELECT id from rnkMETHOD where name=%s", (rank_method_code, )) del_rank_method_codeDATA(rank_method_code) serdata = serialize_via_marshal(dict); midstr = str(mid[0][0]); run_sql("INSERT INTO rnkMETHODDATA(id_rnkMETHOD, relevance_data) VALUES (%s,%s)", (midstr, serdata,)) run_sql("UPDATE rnkMETHOD SET last_updated=%s WHERE name=%s", (date, rank_method_code)) # FIXME: the following is a workaround for the citation indexer # memory troubles, when Apache WSGI daemon processes may end up # doubling the memory after citation dictionary is updated; # therefore let us restart the WSGI daemon application after the # citation indexer finished, which relieves this problem. The # restart is done via touching invenio.wsgi file. The proper fix # for this problem would be strict separation between citation # indexer updating dicts and citation searcher loading dicts. if rank_method_code == 'citation': os.system('touch ' + os.path.join(CFG_PREFIX, 'var', 'www-wsgi', 'invenio.wsgi'))
def do_upgrade(): create_statement = run_sql('SHOW CREATE TABLE oaiHARVEST')[0][1] if '`arguments` text' in create_statement: run_sql("ALTER TABLE oaiHARVEST CHANGE arguments arguments blob") # translate old values if '`bibconvertcfgfile`' in create_statement: rows_to_change = run_sql( "SELECT id, bibconvertcfgfile, bibfilterprogram, arguments FROM oaiHARVEST", with_dict=True) # Move away from old columns for row in rows_to_change: if row['arguments']: arguments = deserialize_via_marshal(row['arguments']) else: arguments = {} arguments['c_cfg-file'] = row['bibconvertcfgfile'] arguments['f_filter-file'] = row['bibfilterprogram'] run_sql("UPDATE oaiHARVEST set arguments=%s WHERE id=%s", (serialize_via_marshal(arguments), row['id'])) run_sql("ALTER TABLE oaiHARVEST DROP COLUMN bibconvertcfgfile") run_sql("ALTER TABLE oaiHARVEST DROP COLUMN bibfilterprogram")
def set_user_preferences(uid, pref): assert(type(pref) == type({})) run_sql("UPDATE user SET settings=%s WHERE id=%s", (serialize_via_marshal(pref), uid))
def registerUser(req, email, passw, nickname, register_without_nickname=False, login_method=None, ln=CFG_SITE_LANG): """Register user with the desired values of NICKNAME, EMAIL and PASSW. If REGISTER_WITHOUT_NICKNAME is set to True, then ignore desired NICKNAME and do not set any. This is suitable for external authentications so that people can login without having to register an internal account first. Return 0 if the registration is successful, 1 if email is not valid, 2 if nickname is not valid, 3 if email is already in the database, 4 if nickname is already in the database, 5 when users cannot register themselves because of the site policy, 6 when the site is having problem contacting the user. If login_method is None or is equal to the key corresponding to local authentication, then CFG_ACCESS_CONTROL_LEVEL_ACCOUNTS is taken in account for deciding the behaviour about registering. """ # is email valid? email = email.lower() if not email_valid_p(email): return 1 _ = gettext_set_language(ln) # is email already taken? res = run_sql("SELECT email FROM user WHERE email=%s", (email,)) if len(res) > 0: return 3 if register_without_nickname: # ignore desired nick and use default empty string one: nickname = "" else: # is nickname valid? if not nickname_valid_p(nickname): return 2 # is nickname already taken? res = run_sql("SELECT nickname FROM user WHERE nickname=%s", (nickname,)) if len(res) > 0: return 4 activated = 1 # By default activated if not login_method or not CFG_EXTERNAL_AUTHENTICATION[login_method]: # local login if CFG_ACCESS_CONTROL_LEVEL_ACCOUNTS >= 2: return 5 elif CFG_ACCESS_CONTROL_NOTIFY_USER_ABOUT_NEW_ACCOUNT: activated = 2 # Email confirmation required elif CFG_ACCESS_CONTROL_LEVEL_ACCOUNTS >= 1: activated = 0 # Administrator confirmation required if CFG_ACCESS_CONTROL_NOTIFY_USER_ABOUT_NEW_ACCOUNT: address_activation_key = mail_cookie_create_mail_activation(email) ip_address = req.remote_host or req.remote_ip try: if not send_email(CFG_SITE_SUPPORT_EMAIL, email, _("Account registration at %s") % CFG_SITE_NAME_INTL.get(ln, CFG_SITE_NAME), tmpl.tmpl_account_address_activation_email_body(email, address_activation_key, ip_address, ln)): return 1 except (smtplib.SMTPException, socket.error): return 6 # okay, go on and register the user: user_preference = get_default_user_preferences() uid = run_sql("INSERT INTO user (nickname, email, password, note, settings, last_login) " "VALUES (%s,%s,AES_ENCRYPT(email,%s),%s,%s, NOW())", (nickname, email, passw, activated, serialize_via_marshal(user_preference))) if activated == 1: # Ok we consider the user as logged in :-) setUid(req, uid) return 0
def set_user_preferences(uid, pref): assert (type(pref) == type({})) run_sql("UPDATE user SET settings=%s WHERE id=%s", (serialize_via_marshal(pref), uid))
def registerUser(req, email, passw, nickname, register_without_nickname=False, login_method=None, ln=CFG_SITE_LANG): """Register user with the desired values of NICKNAME, EMAIL and PASSW. If REGISTER_WITHOUT_NICKNAME is set to True, then ignore desired NICKNAME and do not set any. This is suitable for external authentications so that people can login without having to register an internal account first. Return 0 if the registration is successful, 1 if email is not valid, 2 if nickname is not valid, 3 if email is already in the database, 4 if nickname is already in the database, 5 when users cannot register themselves because of the site policy, 6 when the site is having problem contacting the user. If login_method is None or is equal to the key corresponding to local authentication, then CFG_ACCESS_CONTROL_LEVEL_ACCOUNTS is taken in account for deciding the behaviour about registering. """ # is email valid? email = email.lower() if not email_valid_p(email): return 1 _ = gettext_set_language(ln) # is email already taken? res = run_sql("SELECT email FROM user WHERE email=%s", (email, )) if len(res) > 0: return 3 if register_without_nickname: # ignore desired nick and use default empty string one: nickname = "" else: # is nickname valid? if not nickname_valid_p(nickname): return 2 # is nickname already taken? res = run_sql("SELECT nickname FROM user WHERE nickname=%s", (nickname, )) if len(res) > 0: return 4 activated = 1 # By default activated if not login_method or not CFG_EXTERNAL_AUTHENTICATION[ login_method]: # local login if CFG_ACCESS_CONTROL_LEVEL_ACCOUNTS >= 2: return 5 elif CFG_ACCESS_CONTROL_NOTIFY_USER_ABOUT_NEW_ACCOUNT: activated = 2 # Email confirmation required elif CFG_ACCESS_CONTROL_LEVEL_ACCOUNTS >= 1: activated = 0 # Administrator confirmation required if CFG_ACCESS_CONTROL_NOTIFY_USER_ABOUT_NEW_ACCOUNT: address_activation_key = mail_cookie_create_mail_activation(email) ip_address = req.remote_host or req.remote_ip try: if not send_email( CFG_SITE_SUPPORT_EMAIL, email, _("Account registration at %s") % CFG_SITE_NAME_INTL.get(ln, CFG_SITE_NAME), tmpl.tmpl_account_address_activation_email_body( email, address_activation_key, ip_address, ln)): return 1 except (smtplib.SMTPException, socket.error): return 6 # okay, go on and register the user: user_preference = get_default_user_preferences() uid = run_sql( "INSERT INTO user (nickname, email, password, note, settings, last_login) " "VALUES (%s,%s,AES_ENCRYPT(email,%s),%s,%s, NOW())", (nickname, email, passw, activated, serialize_via_marshal(user_preference))) if activated == 1: # Ok we consider the user as logged in :-) setUid(req, uid) return 0
from invenio.dbquery import run_sql, deserialize_via_marshal, serialize_via_marshal ### ON PROD: #pids_to_regard = [9835, 10860, 85442, 144021, 203699, 178081, 216415, 238188, 233816, 114133, 143592, 62586, 179240, 102047, 23459, 186671, 21183, 85338, 40075, 198998] pids_to_regard = [204567, 197161, 51316] ptrs = set(pids_to_regard) all_tickets = run_sql("select * from aidPERSONID where tag like 'rt%' and personid in " + str(tuple(ptrs))) pdata = {} for p in ptrs: pdata[p] = {} pdata[p]['tickets'] = run_sql("select * from aidPERSONID where tag like 'rt%%' and personid = %s", (p,)) pdata[p]['cid'] = run_sql("select data from aidPERSONID where tag = 'canonical_name' and personid = %s", (p,)) sm = serialize_via_marshal(pdata) fp = open('pdata2.dat', 'w') fp.write(sm) fp.close() ### THEN ON TEST from invenio.dbquery import run_sql, deserialize_via_marshal, serialize_via_marshal fp = open('pdata.dat', 'r') dm = deserialize_via_marshal(fp.read()) fp.close() for d in dm: dm[d]['tickets'] = list(dm[d]['tickets']) for i, t in enumerate(dm[d]['tickets']): dm[d]['tickets'][i] = list(t) for pp in dm:
def store_weights_cache(weights): """Store into key/value store""" redis = get_redis() redis.set('selfcites_weights', serialize_via_marshal(weights))
def process_bind_param(self, value, dialect): if value is not None: value = serialize_via_marshal(self.force_type(value)) return value return value