def delete_object_relations(object_id, cursor=None): """ Purge all non-DC relations on an object. """ cursor = check_cursor(cursor) for relation_db_info in OBJECT_RELATION_MAP.values(): # Delete from specific tables. cursor.execute(''' DELETE FROM {} WHERE rdf_subject = %s '''.format(relation_db_info['table']), (object_id,)) logger.debug('Deleted any RDF relations in %s about object %s.', relation_db_info['table'], object_id) # Delete from general table. cursor.execute(''' DELETE FROM object_relationships WHERE rdf_subject = %s ''', (object_id,)) logger.debug(('Deleted any RDF relation about object: %s from the general' ' table.'), object_id) # Delete from sequence number of table. cursor.execute(''' DELETE FROM is_sequence_number_of WHERE rdf_subject = %s ''', (object_id,)) logger.debug(('Deleted any RDF relation about object: %s from the sequence' ' number of table.'), object_id) return cursor
def read_sequence_number(subject=None, paged_object=None, cursor=None): """ Read an is sequence number of object relation from the repository. """ cursor = check_cursor(cursor) if (subject and paged_object): cursor.execute(''' SELECT * FROM is_sequence_number_of WHERE rdf_subject = %s and rdf_object = %s ''', (subject, paged_object)) elif (subject): cursor.execute(''' SELECT * FROM is_sequence_number_of WHERE rdf_subject = %s ''', (subject,)) elif (paged_object): cursor.execute(''' SELECT * FROM is_sequence_number_of WHERE rdf_object = %s ''', (paged_object,)) else: raise ValueError('Specify either subject, object or both.') return cursor
def internalize_rels_dc(relations_file, object_id, purge=True, cursor=None): """ Update the DC relation information in the DB. """ cursor = check_cursor(cursor, ISOLATION_LEVEL_READ_COMMITTED) if purge: # Purge existing relations. object_relations_purger.delete_dc_relations(object_id, cursor=cursor) if relations_file is None: return cursor # Ingest new relations. for relation in etree.parse(relations_file).getroot(): if relation.text is None: rdf_object = '' else: rdf_object = relation.text object_relations_writer.write_relationship( relations.DC_NAMESPACE, etree.QName(relation).localname, object_id, rdf_object, LITERAL_RDF_OBJECT, cursor=cursor) cursor.fetchall() return cursor
def write_to_general_rdf_table(predicate_id, subject, rdf_object, rdf_type, cursor=None): """ Write to the main datastream RDF table. """ if rdf_type in LINKED_RDF_OBJECT_TYPES: raise TypeError(('Trying to place {} of type {} into the datastream ' 'general table.').format(rdf_object, rdf_type)) cursor = check_cursor(cursor) cursor.execute( ''' INSERT INTO datastream_relationships ( predicate, rdf_subject, rdf_object ) VALUES (%s, %s, %s) RETURNING id ''', (predicate_id, subject, rdf_object)) logger.debug('Upserted a datastream relation "%s" on %s as %s.', predicate_id, subject, rdf_object) return cursor
def write_relationship(namespace, predicate, subject, rdf_object, rdf_type, cursor=None): """ Write a datastream relation to the repository. """ cursor = check_cursor(cursor) try: relation_db_info = DATASTREAM_RELATION_MAP[(namespace, predicate)] write_to_standard_relation_table(relation_db_info['table'], relation_db_info['upsert message'], subject, rdf_object, cursor) except KeyError: predicate_id = cache.predicate_id_from_raw(namespace, predicate, cursor=cursor) write_to_general_rdf_table(predicate_id, subject, rdf_object, rdf_type, cursor=cursor) return cursor
def create_default_dc_ds(object_id, pid, cursor=None): """ Populate a minimal DC DS as Fedora does. """ cursor = check_cursor(cursor, ISOLATION_LEVEL_READ_COMMITTED) dc_tree = etree.fromstring(''' <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd"> <dc:identifier></dc:identifier> </oai_dc:dc> ''') dc_tree[0].text = pid log = upsert_log('Automatically generated DC.').fetchone()[0] filestore.create_datastream_from_data( { 'object': object_id, 'dsid': 'DC', 'label': 'DC Record', 'log': log, 'control_group': 'X' }, etree.tostring(dc_tree), 'application/xml', cursor=cursor)
def is_datastream_manageable(ds_db_id, user_id=None, roles=None, cursor=None): """ Check if the datastream is manageable by the given user or roles. """ cursor = check_cursor(cursor) if user_id is not None: datastream_relations.read_relationship( relations.ISLANDORA_RELS_EXT_NAMESPACE, relations.IS_MANAGEABLE_BY_USER_PREDICATE, ds_db_id, user_id, cursor=cursor ) if cursor.fetchone(): return True if roles: datastream_relations.read_relationship( relations.ISLANDORA_RELS_EXT_NAMESPACE, relations.IS_MANAGEABLE_BY_ROLE_PREDICATE, ds_db_id, cursor=cursor ) db_role_info = cursor.fetchall() db_roles = set() for db_role in db_role_info: db_roles.add(db_role['rdf_object']) if db_roles.intersection(roles): return True return False
def internalize_rels_ext(relations_file, object_id, source, purge=True, cursor=None): """ Update the RELS_EXT information in the DB. """ cursor = check_cursor(cursor, ISOLATION_LEVEL_READ_COMMITTED) if purge: # Purge existing relations. object_relations_purger.delete_object_relations(object_id, cursor=cursor) if relations_file is None: return cursor # Ingest new relations. for relation in etree.parse(relations_file).getroot()[0]: rdf_object, rdf_type = repo_object_rdf_object_from_element( relation, source, cursor) relation_qname = etree.QName(relation) object_relations_writer.write_relationship(relation_qname.namespace, relation_qname.localname, object_id, rdf_object, rdf_type=rdf_type, cursor=cursor) cursor.fetchone() return cursor
def delete_datastream_relations(ds_db_id, cursor=None): """ Purge all relations on a datastream. """ cursor = check_cursor(cursor) for relation_db_info in DATASTREAM_RELATION_MAP.values(): # Delete from specific tables. cursor.execute(''' DELETE FROM {} WHERE rdf_subject = %s '''.format(relation_db_info['table']), (ds_db_id,)) logger.debug('Deleted any RDF relations in %s about datastream %s.', relation_db_info['table'], ds_db_id) # Delete from general table. cursor.execute(''' DELETE FROM datastream_relationships WHERE rdf_subject = %s ''', (ds_db_id,)) logger.debug(('Deleted any RDF relation about datastream: %s from the ' 'general table.'), ds_db_id) return cursor
def read_relationship(namespace, predicate, subject=None, rdf_object=None, cursor=None): """ Read an object relation from the repository. """ cursor = check_cursor(cursor) try: relations_reader.read_from_standard_relation_table( OBJECT_RELATION_MAP[(namespace, predicate)]['table'], subject, rdf_object, cursor ) except KeyError: predicate_id = cache.predicate_id_from_raw( namespace, predicate, cursor ) read_from_general_rdf_table( predicate_id, subject, rdf_object, cursor ) return cursor
def predicate_id_from_raw(namespace, predicate, cursor=None): """ Get a RDF predicate ID from string values, creating it if necessary. """ cursor = check_cursor(cursor) namespace_id = rdf_namespace_id(namespace, cursor=cursor) return predicate_id(namespace_id, predicate, cursor=cursor)
def delete_datastream_versions(pid, dsid, start=None, end=None, cursor=None): """ Delete versions of a datastream """ cursor = check_cursor(cursor) datastream_reader.datastream_from_raw(pid, dsid, cursor=cursor) ds_info = cursor.fetchone() if ds_info is None: return cursor # Handle base datastream. if end is None and start is None: return delete_datastream(ds_info['id'], cursor=cursor) elif end is None or end >= ds_info['modified']: # Find youngest surviving version and make it current. ds_replacement = datastream_reader.datastream_as_of_time( ds_info['id'], start, cursor, False) if ds_replacement is not None: datastream_writer.upsert_datastream(dict(ds_replacement), cursor=cursor) cursor.execute( ''' DELETE FROM old_datastreams WHERE datastream = %s AND committed = %s ''', (ds_info['id'], ds_replacement['modified'])) # Handle old datastreams. if start is None: # Remove from dawn of time to specified end. cursor.execute( ''' DELETE FROM old_datastreams WHERE datastream = %s AND committed <= %s ''', (ds_info['id'], end)) elif end is None: # Remove from specified start to end of time. cursor.execute( ''' DELETE FROM old_datastreams WHERE datastream = %s AND committed >= %s ''', (ds_info['id'], start)) else: # Remove items between specified start and end times. cursor.execute( ''' DELETE FROM old_datastreams WHERE datastream = %s AND committed <= %s AND committed >= %s ''', (ds_info['id'], end, start)) logger.debug('Deleted datastream versions for %s on %s between %s and %s.', dsid, pid, start, end) return cursor
def __init__(self, source, pid=None, cursor=None): """ Prep for use. """ self.cursor = check_cursor(cursor, ISOLATION_LEVEL_READ_COMMITTED) self.source = source self.object_info = {'PID': pid} self.ds_info = {} self.object_id = None self.rels_int = None self.ds_file = None self.tree_builder = None self.dsid = None
def user(user_id, cursor=None): """ Query for user information from the repository. """ cursor = check_cursor(cursor) cursor.execute(''' SELECT * FROM users WHERE id = %s ''', (user_id,)) return cursor
def user_id(data, cursor=None): """ Query for a user ID from the repository. """ cursor = check_cursor(cursor) cursor.execute(''' SELECT id FROM users WHERE name = %(name)s AND source = %(source)s ''', data) return cursor
def role_id(data, cursor=None): """ Query for a role ID from the repository. """ cursor = check_cursor(cursor) cursor.execute(''' SELECT id FROM user_roles WHERE role = %(role)s AND source = %(source)s ''', data) return cursor
def source_id(source, cursor=None): """ Query for a source ID from the repository. """ cursor = check_cursor(cursor) cursor.execute(''' SELECT id FROM sources WHERE source = %s ''', (source,)) return cursor
def log_id(log, cursor=None): """ Query for a log ID from the repository. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT id FROM log WHERE log = %s ''', (log, )) return cursor
def object_info(db_id, cursor=None): """ Query for an object's information from the repository. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT * FROM objects WHERE id = %s ''', (db_id, )) return cursor
def old_object_id(data, cursor=None): """ Query for an old object ID from the repository. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT id FROM old_objects WHERE object = %(object)s AND committed = %(committed)s ''', data) return cursor
def namespace_id(namespace, cursor=None): """ Query for a namespace ID from the repository. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT id FROM pid_namespaces WHERE namespace = %s ''', (namespace, )) return cursor
def object_id(data, cursor=None): """ Query for an object ID from the repository. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT id FROM objects WHERE objects.pid_id = %(pid_id)s AND namespace = %(namespace)s ''', data) return cursor
def namespace_info(namespace_id, cursor=None): """ Query for namespace info from the repository. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT * FROM pid_namespaces WHERE id = %s ''', (namespace_id, )) return cursor
def datastreams(object_id, cursor=None): """ Query for all datastreams on an object. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT * FROM datastreams WHERE object = %s ''', (object_id, )) return cursor
def delete_user(user_id, cursor=None): """ Delete a user in the repository. """ cursor = check_cursor(cursor) cursor.execute(''' DELETE FROM users WHERE id = %s ''', (user_id, )) logger.debug('Deleted user with ID: %s', user_id) return cursor
def delete_mime(mime_id, cursor=None): """ Delete a mime from the repository. """ cursor = check_cursor(cursor) cursor.execute(''' DELETE FROM mimes WHERE id = %s ''', (mime_id, )) logger.debug('Deleted mime with ID: %s', mime_id) return cursor
def datastream_id(data, cursor=None): """ Query for a datastream database ID from the repository. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT id FROM datastreams WHERE object = %(object)s AND dsid = %(dsid)s ''', data) return cursor
def resource_id(uri, cursor=None): """ Query for a resource ID from the repository. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT id FROM resources WHERE uri = %s ''', (uri, )) return cursor
def delete_log(log_id, cursor=None): """ Delete a log from the repository. """ cursor = check_cursor(cursor) cursor.execute(''' DELETE FROM log WHERE id = %s ''', (log_id,)) logger.debug('Deleted log with ID: %s', log_id) return cursor
def old_datastream_id(data, cursor=None): """ Query for an old datastream ID from the repository. """ cursor = check_cursor(cursor) cursor.execute( ''' SELECT id FROM old_datastreams WHERE datastream = %(datastream)s AND committed = %(committed)s ''', data) return cursor