"Encode special chars in string for XML-compliancy."
if s == None:
return s
else:
s = string.replace(s, '&', '&')
s = string.replace(s, '<', '<')
return s
def escape_space(s):
"Encode special chars in string for URL-compliancy."
s = string.replace(s, ' ', '%20')
return s
def encode_for_url(s):
"Encode special chars in string for URL-compliancy."
s = string.replace(s, '%', '%25')
s = string.replace(s, ' ', '%20')
s = string.replace(s, '?', '%3F')
s = string.replace(s, '#', '%23')
s = string.replace(s, '=', '%3D')
s = string.replace(s, '&', '%26')
s = string.replace(s, '/', '%2F')
s = string.replace(s, ':', '%3A')
s = string.replace(s, ';', '%3B')
s = string.replace(s, '+', '%2B')
return s
def oai_header(args, verb):
"Print OAI header"
out = ""
out = out + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n"
out = out + "<OAI-PMH xmlns=\"http://www.openarchives.org/OAI/2.0/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd\">\n"
out = out + " <responseDate>" + OAIGetResponseDate() + "</responseDate>\n"
if verb:
out = out + " <request verb=\"%s\">%s</request>\n" % (verb,OAIGetRequestURL(args))
out = out + " <%s>\n" % verb
else:
out = out + " <request>%s</request>\n" % (OAIGetRequestURL(args))
return out
def oai_footer(verb):
"Print OAI footer"
out = ""
if verb:
out = "%s </%s>\n" % (out, verb)
out = out + "</OAI-PMH>\n"
return out
def oai_error_header(args, verb):
"Print OAI header"
out = ""
### out = "Content-Type: text/xml\n\n"
out = out + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n"
out = out + "<OAI-PMH xmlns=\"http://www.openarchives.org/OAI/2.0/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd\">\n"
out = out + " <responseDate>" + OAIGetResponseDate() + "</responseDate>\n"
out = out + " <request verb=\"%s\">%s</request>\n" % (verb,OAIGetRequestURL(args))
return out
def oai_error_footer(verb):
"Print OAI footer"
out = ""
out = out + "</OAI-PMH>\n"
return out
def get_field(sysno, field):
"Gets list of field 'field' for the record with 'sysno' system number."
out = []
digit = field[0:2]
bx = "bib%sx" % digit
bibx = "bibrec_bib%sx" % digit
query = "SELECT bx.value FROM %s AS bx, %s AS bibx WHERE bibx.id_bibrec='%s' AND bx.id=bibx.id_bibxxx AND bx.tag='%s'" % (bx, bibx, sysno, field)
"Returns the first MySQL BIB ID for the OAI identifier 'identifier', if it exists."
sysno = None
if identifier:
query = "SELECT DISTINCT(bb.id_bibrec) FROM bib%sx AS bx, bibrec_bib%sx AS bb WHERE bx.tag='%s' AND bb.id_bibxxx=bx.id AND bx.value='%s'" % (oaiidfield[0:2], oaiidfield[0:2], oaiidfield,identifier)
res = run_sql(query)
for row in res:
sysno = row[0]
return sysno
def OAIGetSysnoList(set, fromDate, untilDate):
"Returns list of system numbers for the OAI set 'set', modified from 'date_from' until 'date_until'."
out_dict = {} # dict to hold list of out sysnos as its keys
if set:
query = "SELECT DISTINCT bibx.id_bibrec FROM bib%sx AS bx LEFT JOIN bibrec_bib%sx AS bibx ON bx.id=bibx.id_bibxxx LEFT JOIN bibrec AS b ON b.id=bibx.id_bibrec WHERE bx.tag='%s' AND bx.value='%s'" % (oaiidfield[0:2],oaiidfield[0:2],oaisetfield,set)
else:
query = "SELECT DISTINCT bibx.id_bibrec FROM bib%sx AS bx LEFT JOIN bibrec_bib%sx AS bibx ON bx.id=bibx.id_bibxxx LEFT JOIN bibrec AS b ON b.id=bibx.id_bibrec WHERE bx.tag='%s'" % (oaiidfield[0:2],oaiidfield[0:2],oaiidfield)
"Check if record with recId has been deleted. Return 1 if deleted."
query = "select a.id from bibrec as a left join bibrec_bib98x as b on a.id=b.id_bibrec left join bib98x as c on b.id_bibxxx=c.id where c.value='DELETED' and a.id=%s" % recId
res = run_sql(query)
for item in res:
if item == None:
return 0
else:
return 1
def OAIGenResumptionToken():
"Generates unique ID for resumption token management."
return md5.new(str(time.time())).hexdigest()
def OAICacheIn(resumptionToken, sysnos):
"Stores or adds sysnos in cache. Input is a string of sysnos separated by commas."
out = out + oai_error("badArgument","Malformed request")
## resumptionToken exclusive
#
#
if ((arguments['from']!="" or arguments['until']!="" or arguments['metadataPrefix']!="" or arguments['identifier']!="" or arguments['set']!="") and arguments['resumptionToken']!=""):
out = out + oai_error("badArgument","The request includes illegal arguments")
## datestamp formats
#
#
if arguments['from']!="" and arguments['from']!="":
from_length = len(arguments['from'])
if check_date(arguments['from'],"T00:00:00Z") == "":
out = out + oai_error("badArgument","Bad datestamp format in from")
else:
from_length = 0
if arguments['until']!="" and arguments['until']!="":
until_length = len(arguments['until'])
if check_date(arguments['until'],"T23:59:59Z") == "":
out = out + oai_error("badArgument","Bad datestamp format in until")
else:
until_length = 0
if from_length <> 0:
if until_length <> 0:
if from_length <> until_length:
out = out + oai_error("badArgument","Bad datestamp format")
if arguments['from'] > arguments['until']:
out = out + oai_error("badArgument", "Wrong date")
## Identify exclusive
#
#
if (arguments['verb']=="Identify" and (arguments['metadataPrefix']!="" or arguments['identifier']!="" or arguments['set']!="" or arguments['from']!="" or arguments['until']!="" or arguments['resumptionToken']!="")):
out = out + oai_error("badArgument","The request includes illegal arguments")
## parameters for GetRecord
#
#
if arguments['verb']=="GetRecord" and arguments['identifier'] == "":
out = out + oai_error("badArgument","Record identifier missing")
if arguments['verb']=="GetRecord" and arguments['metadataPrefix'] == "":
out = out + oai_error("badArgument","Missing metadataPrefix")
## parameters for ListRecords and ListIdentifiers
#
#
if (arguments['verb']=="ListRecords" or arguments['verb']=="ListIdentifiers") and (arguments['metadataPrefix'] == "" and arguments['resumptionToken'] == ""):
out = out + oai_error("badArgument","Missing metadataPrefix")