LeMyst / WikibaseIntegrator

A Python module to manipulate data on a Wikibase instance (like Wikidata) through the MediaWiki Wikibase API and the Wikibase SPARQL endpoint.
MIT License
67 stars 17 forks source link

Is it possible to update an entity adding a statement with multiple values? #754

Closed traopia closed 3 months ago

traopia commented 3 months ago

I would like to update a set of entities with a statement that needs to have multiple values: Ex: entityA - has children - entityB, entityC, entityD.

I am trying to do it like this:

def update_entity(entity_id, property_id, list_values):
    try:
        #statements = []
        for v in list_values'
            statement = Item(value=v, prop_nr=property_id)
            #statements.append(statement)
            print(statement)

            # Fetch the current entity data
            entity = wbi.item.get(entity_id)

            # Add the new statement to the entity
            #entity.claims.add(statement)
            entity.claims.add(statement)

        # Write the updated entity back to the Wikibase
        entity.write()
        print(entity_id)

    except Exception as e:
        # Log the error
        #print(e)
        pass

Where list_values contains all the entities object to be added to the given statement (entityB, entityC, entityD), entity Id is the subject - thus the entity to be updated (entityA) and property ID the property of the statement. Following this code I end up with entityA - has child - entityD, because the object in the statement is overwritten instead of being added. Any lead on how I could fix this? Thanks!

traopia commented 3 months ago

ok apparently just changing the function like this worked :) sorry for bothering!

def update_entity(entity_id, property_id, value_id):
    try:
        statements = []
        for v in value_id:
            statement = Item(value=v, prop_nr=property_id)
            statements.append(statement)

            # Fetch the current entity data
            entity = wbi.item.get(entity_id)

            # Add the new statement to the entity
            #entity.claims.add(statement)
            entity.claims.add(statements)

        # Write the updated entity back to the Wikibase
        entity.write()
        print(entity_id)

    except Exception as e:
        # Log the error
        #print(e)
        pass