Migrating from WikidataIntegrator#
WikibaseIntegrator started as a fork of WikidataIntegrator, whose last release is 0.9.30 (June 2023). The concepts are the same (entities, data types, references, qualifiers, the fast run mode), but the API was rewritten in v0.12: there is no compatibility layer, so a WikidataIntegrator script has to be adapted. This page maps the WikidataIntegrator 0.9.30 API to WikibaseIntegrator.
Installation#
python -m pip uninstall wikidataintegrator
python -m pip install wikibaseintegrator
WikibaseIntegrator requires Python 3.11 or newer.
A script, before and after#
A bot updating an existing item, with WikidataIntegrator:
from wikidataintegrator import wdi_core, wdi_login
login = wdi_login.WDLogin(user='MyUsername@MyBot', pwd='<bot password>')
references = [[
wdi_core.WDItemID(value='Q20641742', prop_nr='P248', is_reference=True),
wdi_core.WDTime('+2020-02-08T00:00:00Z', prop_nr='P813', is_reference=True),
]]
data = [
wdi_core.WDExternalID(value='50943', prop_nr='P351', references=references),
wdi_core.WDItemID(value='Q7187', prop_nr='P31', references=references),
]
# The values of P351 replace the existing ones, the P31 value is appended (append_value)
item = wdi_core.WDItemEngine(wd_item_id='Q14911732', data=data, append_value=['P31'])
item.set_label('FOXP3', lang='en')
qid = item.write(login, edit_summary='Update from NCBI Gene')
The same bot with WikibaseIntegrator:
from wikibaseintegrator import WikibaseIntegrator, wbi_login
from wikibaseintegrator.datatypes import ExternalID, Item, Time
from wikibaseintegrator.wbi_config import config
from wikibaseintegrator.wbi_enums import ActionIfExists
# Wikimedia asks every tool to identify itself: https://foundation.wikimedia.org/wiki/Policy:User-Agent_policy
config['USER_AGENT'] = 'MyBot/1.0 (https://www.wikidata.org/wiki/User:MyUsername)'
login = wbi_login.Login(user='MyUsername@MyBot', password='<bot password>')
wbi = WikibaseIntegrator(login=login)
# No is_reference: a reference is a list of claims, references a list of references
references = [[
Item(value='Q20641742', prop_nr='P248'),
Time(time='+2020-02-08T00:00:00Z', prop_nr='P813'),
]]
item = wbi.item.get('Q14911732')
item.claims.add(ExternalID(value='50943', prop_nr='P351', references=references), action_if_exists=ActionIfExists.REPLACE_ALL)
item.claims.add(Item(value='Q7187', prop_nr='P31', references=references)) # APPEND_OR_REPLACE by default
item.labels.set('en', 'FOXP3')
item = item.write(summary='Update from NCBI Gene')
qid = item.id
The main differences:
The entity is loaded first (
wbi.item.get(), orwbi.item.new()for a new item), then modified, then written. Nothing is sent to the instance beforewrite().write()returns the written entity, not its ID: the ID isitem.id.The login is given once to
WikibaseIntegrator(login=...)(or to eachwrite(login=...)).The arguments are named:
labels.set(language, value)takes the language first,set_label(label, lang)took it last.
Entities#
WikidataIntegrator |
WikibaseIntegrator |
|---|---|
|
|
|
|
|
No automatic search, see Finding an item from its data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WikibaseIntegrator also reads and writes properties (wbi.property), lexemes with their forms and senses
(wbi.lexeme) and MediaInfo entities (wbi.mediainfo). See Entities.
Data types#
The data types lose their WD prefix and are imported from wikibaseintegrator.datatypes. Their arguments are named:
WikidataIntegrator |
WikibaseIntegrator |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
— |
|
The other arguments:
WikidataIntegrator |
WikibaseIntegrator |
|---|---|
|
Removed: the same classes are used for claims, qualifiers and references |
|
|
|
|
|
|
|
|
|
No equivalent: two claims are equal when they have the same property, value and qualifiers |
|
|
The enumerations (WikibaseRank, WikibaseSnakType, WikibaseTimePrecision, ActionIfExists…) are in
wikibaseintegrator.wbi_enums. See Data types.
Writing statements#
WikidataIntegrator took all the statements in the data list of the constructor and merged them with the item.
WikibaseIntegrator adds them with item.claims.add(claims, action_if_exists=...), where action_if_exists says what
to do with the existing statements of the same property:
WikidataIntegrator |
WikibaseIntegrator |
|---|---|
A property in |
|
A property in |
|
A data type with an empty value ( |
|
|
|
|
More |
The reference handling is different. WikidataIntegrator merged the references of a statement already present
following global_ref_mode (KEEP_GOOD by default), and kept the statements with “good” references
(keep_good_ref_statements, good_refs). WikibaseIntegrator has no notion of good references, and its actions
handle the references of a statement already present (same property, value and qualifiers) as follows:
WikidataIntegrator |
WikibaseIntegrator |
|---|---|
|
|
|
|
|
|
|
No equivalent: get the existing statement with |
REPLACE_ALL keeps the statements already present as they are, references included. To remove the other values of a
property and add the new references to the statements already present, add the claims twice:
item.claims.add(data, action_if_exists=ActionIfExists.REPLACE_ALL) # remove the other values of the properties
item.claims.add(data, action_if_exists=ActionIfExists.MERGE_REFS_OR_APPEND) # add the new references
Finding an item from its data#
Without wd_item_id, WikidataIntegrator searched the item holding the values of data for the properties with a
distinct-values constraint (core_props, core_prop_match_thresh), and created a new item if none was found.
WikibaseIntegrator doesn’t search automatically: find the item yourself, for example with the fast run container,
which resolves the item from a unique value with a SPARQL query:
from wikibaseintegrator import wbi_fastrun
from wikibaseintegrator.datatypes import ExternalID, Item
base_filter = [ExternalID(prop_nr='P351'), Item(prop_nr='P703', value='Q15978631')]
frc = wbi_fastrun.get_fastrun_container(base_filter=base_filter)
qids = frc.get_entities(claims=[ExternalID(value='50943', prop_nr='P351')])
item = wbi.item.get(qids[0]) if qids else wbi.item.new()
or with the search of the MediaWiki API, which supports the haswbstatement keyword on Wikidata:
from wikibaseintegrator import wbi_helpers
results = wbi_helpers.fulltext_search('haswbstatement:P351=50943')
Fast run#
WikidataIntegrator |
WikibaseIntegrator |
|---|---|
|
|
|
The value returned by |
|
|
|
|
The base filter is a list of data type instances instead of a dictionary: a value is a required value, no value means any value. It also supports property paths. See Fast run.
Login#
WikidataIntegrator |
WikibaseIntegrator |
|---|---|
|
|
|
|
|
|
— |
|
— |
|
See Login.
Helpers#
WDFunctionsEngine and the static methods of WDItemEngine become functions of wikibaseintegrator.wbi_helpers:
WikidataIntegrator |
WikibaseIntegrator |
|---|---|
|
|
|
No equivalent: build the dataframe from |
|
|
|
|
|
|
|
|
|
No equivalent. |
|
|
|
The standard |
WikibaseIntegrator has more helpers (redirects, lexeme forms and senses, constraint checks, value parsing, EditGroups…): see Helpers.
Configuration#
wdi_config.config becomes wikibaseintegrator.wbi_config.config, with the same keys, except:
USER_AGENT_DEFAULTbecomesUSER_AGENT. Wikimedia sites require a User-Agent identifying your tool.CONCEPT_BASE_URIis gone: the concept URIs are built fromWIKIBASE_URL.MAXLAGis gone:maxlagis an argument of the API calls (mediawiki_api_call_helper(..., maxlag=5), 5 by default).The retries:
write()and the API calls retry 20 times by default (max_retries), instead of 1000.
Without an equivalent#
These parts of WikidataIntegrator have no equivalent in WikibaseIntegrator:
The search of an item from its data (
core_props), see Finding an item from its data.The good references (
good_refs,keep_good_ref_statements,KEEP_GOOD) and the custom reference handlers (ref_handler, theref_handlersmodule).rollback(),get_linked_by()(use a SPARQL query),count_references(),get_reference_properties(),get_qualifier_properties().The ShEx checks (
check_entity_schema(),check_shex_conformance(),extract_shex()).wdi_helpers(PubmedItem,PublicationHelper,Release,try_write,prop2qid,id_mapper…),wdi_rdfandMappingRelationHelper.