has_bibdocs=run_sql("SELECT count(id_bibdoc) FROM bibrec_bibdoc JOIN bibdoc ON id_bibdoc=id WHERE id_bibrec=%s AND status<>'DELETED'",(rec_id,))[0][0]>0
write_message('Found recid %s for extoaiid="%s"'%(id_bibrec,extoaiid),verbose=9)
ret.add(id_bibrec)
break
else:
raiseError('Found recid %s for extoaiid="%s" that doesn\'t specify any provenance, while input record does.'%(id_bibrec,extoaiid))
else:
ifextoaiidisNone:
raiseError('Found recid %s for extoaiid="%s" that specify as provenance "%s", while input record does not specify any provenance.'%(id_bibrec,extoaiid,provenance))
elifprovenance==extoaisrc:
write_message('Found recid %s for extoaiid="%s" with provenance="%s"'%(id_bibrec,extoaiid,extoaisrc),verbose=9)
ret.add(id_bibrec)
break
returnret
exceptError,error:
write_message(" Error during find_records_from_extoaiid(): %s "
%error,verbose=1,stream=sys.stderr)
raise
deffind_record_from_oaiid(oaiid):
"""
Try to find record in the database from the OAI ID number and OAI SRC.
Return record ID if found, None otherwise.
"""
bibxxx='bib'+CFG_OAI_ID_FIELD[0:2]+'x'
bibrec_bibxxx='bibrec_'+bibxxx
try:
res=run_sql("""SELECT bb.id_bibrec FROM %(bibrec_bibxxx)s AS bb,
%(bibxxx)s AS b WHERE b.tag=%%s AND b.value=%%s
AND bb.id_bibxxx=b.id"""% \
{'bibxxx':bibxxx,
'bibrec_bibxxx':bibrec_bibxxx},
(CFG_OAI_ID_FIELD,oaiid,))
exceptError,error:
write_message(" Error during find_record_from_oaiid(): %s "%error,
verbose=1,stream=sys.stderr)
ifres:
returnres[0][0]
else:
returnNone
defextract_tag_from_record(record,tag_number):
""" Extract the tag_number for record."""
# first step verify if the record is not already in the database
ifrecord:
returnrecord.get(tag_number,None)
returnNone
defretrieve_rec_id(record,opt_mode):
"""Retrieve the record Id from a record by using tag 001 or SYSNO or OAI ID
tag. opt_mod is the desired mode."""
rec_id=None
# 1st step: we look for the tag 001
tag_001=extract_tag_from_record(record,'001')
iftag_001isnotNone:
# We extract the record ID from the tag
rec_id=tag_001[0][3]
# if we are in insert mode => error
ifopt_mode=='insert':
write_message(" Failed : Error tag 001 found in the xml" \
" submitted, you should use the option replace," \
" correct or append to replace an existing" \
" record. (-h for help)",
verbose=1,stream=sys.stderr)
return-1
else:
# we found the rec id and we are not in insert mode => continue
# we try to match rec_id against the database:
iffind_record_from_recid(rec_id)isnotNone:
# okay, 001 corresponds to some known record
returnrec_id
else:
# The record doesn't exist yet. We shall have try to check
# the SYSNO or OAI id later.
write_message(" -Tag 001 value not found in database.",
verbose=9)
rec_id=None
else:
write_message(" -Tag 001 not found in the xml marc file.",verbose=9)
ifrec_idisNone:
# 2nd step we look for the SYSNO
sysnos=record_get_field_values(record,
CFG_BIBUPLOAD_EXTERNAL_SYSNO_TAG[0:3],
CFG_BIBUPLOAD_EXTERNAL_SYSNO_TAG[3:4]!="_"and \
CFG_BIBUPLOAD_EXTERNAL_SYSNO_TAG[3:4]or"",
CFG_BIBUPLOAD_EXTERNAL_SYSNO_TAG[4:5]!="_"and \
CFG_BIBUPLOAD_EXTERNAL_SYSNO_TAG[4:5]or"",
CFG_BIBUPLOAD_EXTERNAL_SYSNO_TAG[5:6])
ifsysnos:
sysno=sysnos[0]# there should be only one external SYSNO
write_message(" -Checking if SYSNO "+sysno+ \
" exists in the database",verbose=9)
# try to find the corresponding rec id from the database
rec_id=find_record_from_sysno(sysno)
ifrec_idisnotNone:
# rec_id found
pass
else:
# The record doesn't exist yet. We will try to check
# external and internal OAI ids later.
write_message(" -Tag SYSNO value not found in database.",
verbose=9)
rec_id=None
else:
write_message(" -Tag SYSNO not found in the xml marc file.",
write_message("('%s', '%s', '%s', '%s', '%s', '%s', '%s') not inserted because format already exists (%s)."%(url,format,docname,doctype,newname,description,comment,e),stream=sys.stderr)
raise
finally:
os.remove(tmpurl)
exceptException,e:
write_message("Error in downloading '%s' because of: %s"%(url,e),stream=sys.stderr)
write_message('Just updating description and comment for %s with format %s with description %s and comment %s'%(docname,format,description,comment),verbose=9)
try:
bibdoc.set_description(description,format)
bibdoc.set_comment(comment,format)
exceptStandardError,e:
write_message("('%s', '%s', '%s', '%s') description and comment not updated because '%s'."%(docname,format,description,comment,e))
raise
returnTrue
def_add_new_icon(bibdoc,url,restriction):
"""Adds a new icon to an existing bibdoc, replacing the previous one if it exists. If url is empty, just remove the current icon."""
query="""UPDATE oaiHARVESTLOG SET date_inserted=NOW(), inserted_to_db=%s, id_bibrec=%s WHERE oai_id = %s AND bibupload_task_id = %s ORDER BY date_harvested LIMIT 1"""