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 14 forks source link

import entity from wikidata errors: wikibaseintegrator.wbi_exceptions.MWApiError: 'The supplied language code was not recognized.' #752

Closed traopia closed 2 weeks ago

traopia commented 2 months ago

Hi! I have this error for several entities that I am trying to import from wikidata:

wikibaseintegrator.wbi_exceptions.MWApiError: 'The supplied language code was not recognized.'

Or the following error:

wikibaseintegrator.wbi_exceptions.ModificationFailed: 'Label and and description for language code ta can not have the same value.'

How can I deal with this? They are quite a common errors (at least in my experience so far) so I guess there must be a way around. Thank you!

LeMyst commented 2 months ago

Hello @traopia , do you have a code example and/or the wikidata entity you are trying to import?

traopia commented 2 months ago

yes!

  1. For the first example, I am trying to import a wikidata entity into my wiki base and add a statement (based on my wiki base to it). It works for many entities, but for some it doesn't and give the error: "The supplied language code was not recognized."

CODE:

def copy_entity_wikibase_with_property(entity_id, property_id, value_id):
    """entity_id = wikidata entity id
    property_id = wikibase property id
    value_id = wikibase value id"""
    failed_entities = []
    try:

        # Fetch the entity from Wikidata
        entity = wbi.item.get(entity_id, mediawiki_api_url='https://www.wikidata.org/w/api.php', login=login_wikidata)
        # Prepare the entity for writing to the new Wikibase instance
        entity.claims = Claims()  # Ensure claims are correctly processed if needed
        statement = Item(value=value_id, prop_nr=property_id)
        # Add the new statement to the entity
        entity.claims.add(statement)

        # Attempt to write the entity to the new Wikibase instance
        entity.write(mediawiki_api_url='https://fashion.wikibase.cloud/w/api.php', login=login_testwikidata, as_new=True)
        print(entity_id)

    except Exception as e:
        # Log the error and add the entity ID to the list of failed entities

        failed_entities.append(entity_id)
        pass
    return failed_entities

EXAMPLE USAGE WHERE IT WORKS copy_entity_wikibase_with_property('Q1891','P6','Q930') EXAMPLE WHERE IT DOESNT WORK copy_entity_wikibase_with_property('Q159','P6','Q930')

  1. For the second error I think I found a fix: which is simply adding a space to the description imported - so that it doesn't match with the label.

Thank you for your help!

LeMyst commented 2 months ago

I tried to reproduce your issue, without success. I've a slightly more precise error message because the "ak" language doesn't exist on my Wikibase instance.

My issue is located, on the two entity, with the "ak" label, which doesn't exist anymore so you can't import it in new wikibase instance.

Which version of Mediawiki and Wikibase do you use?

traopia commented 2 weeks ago

Only keep the English labels, and explicitly removing all the other languages worked!

ex:

` entity = wbi.item.get(wikidata_id, mediawiki_api_url=wikidata_api_url, login=login_wikidata)

    entity.sitelinks.sitelinks.clear()

    entity.aliases.aliases.clear()

    en_description = entity.descriptions.get('en').value

    en_label = entity.labels.get('en').value

    entity.labels = Labels()

    entity.descriptions = Descriptions()

    entity.aliases = Aliases()

    entity.labels.set('en',en_label)

    entity.descriptions.set('en',  en_description)`