"Encode special chars in string for XML-compliancy."
s = string.replace(s, '&', '&')
s = string.replace(s, '<', '<')
return s
def encode_for_url(s):
"Encode special chars in string for URL-compliancy."
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, '%', '%25')
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 bib02x AS bx, bibrec_bib02x AS bb WHERE bx.tag='%s' AND bb.id_bibxxx=bx.id AND bx.value='%s'" % (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 bib02x AS bx LEFT JOIN bibrec_bib02x 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'" % (oaisetfield,set)
else:
query = "SELECT DISTINCT bibx.id_bibrec FROM bib02x AS bx LEFT JOIN bibrec_bib02x AS bibx ON bx.id=bibx.id_bibxxx LEFT JOIN bibrec AS b ON b.id=bibx.id_bibrec WHERE bx.tag='%s'" % (oaiidfield)
query = "SELECT setSpec,setName,setDescription FROM oaiset"
res = run_sql (query)
for row in res:
row_bis = [row[0],row[1],row[2]]
out.append(row_bis)
return out
def parse_args(args=""):
"Parse input args"
out_args = {
"verb" : "",
"metadataPrefix" : "",
"from" : "",
"until" : "",
"set" : "",
"identifier" : "",
"resumptionToken" : ""
}
if args == "" or args == None:
pass
else:
list_of_arguments = args.split('&')
for item in list_of_arguments:
keyvalue = item.split('=')
if len(keyvalue) == 2:
out_args[keyvalue[0]] = keyvalue[1]
else:
out_args['verb'] = ""
return out_args
def check_args(arguments):
"Check OAI arguments"
out = ""
## principal argument required
#
#
if verbs.has_key(arguments['verb']):
pass
else:
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")