SuLab / WikidataIntegrator

A Wikidata Python module integrating the MediaWiki API and the Wikidata SPARQL endpoint
MIT License
244 stars 46 forks source link

mediawiki_api_url value is ignored; edits made to Wikidata anyway #183

Closed harej closed 2 years ago

harej commented 2 years ago

Steps to reproduce:

  1. Create a WDLogin object, configured to point to a non-Wikidata wiki:
wiki = 'https://wikipediacitations.wiki.opencura.com'
mediawiki_api_url = wiki + '/w/api.php'
mediawiki_index_url = wiki + '/w/index.php'
sparql_endpoint_url = wiki + '/query/sparql'
login = wdi_login.WDLogin(
    user='Harej',
    mediawiki_api_url=mediawiki_api_url,
    mediawiki_index_url=mediawiki_index_url,
    consumer_key='key',
    consumer_secret='secret')
  1. Create an item using the Item Engine:
item = wdi_core.WDItemEngine(
    wd_item_id=wcd_qid,
    new_item=approve_new_item(pageid),
    data=itemdata)
  1. Write the new item: item.write(login)

What should happen: WikidataIntegrator edits the wiki defined at mediawiki_api_url using the supplied OAuth credentials

What happens instead: WikidataIntegrator edits Wikidata while logged out.

When a different Wikibase is defined, edits should NOT be made to Wikidata. I expect operating environment to be defined in the WDLogin object and it is baffling when a different operating environment would be used anyway.

andrawaag commented 2 years ago

Could it be your login object was still valid in memory? This sometimes happens when working with for example jupyter notebooks. I tried to reproduce but can't. When I don't provide the wikibase credential (API and sparql) I get the following error message: image

One code pattern I can recommend is the following


wikibase = "https://{}.wiki.opencura.com/".format(wbstack)
api = "https://{}.wiki.opencura.com/w/api.php".format(wbstack)
sparql = "https://{}.wiki.opencura.com/query/sparql".format(wbstack)
entityUri = wikibase.replace("https:", "http:")+"entity/"

WBUSER = "<Username>"
WBPASS = "<Password>"
login = wdi_login.WDLogin(WBUSER, WBPASS, mediawiki_api_url=api)

localEntityEngine = wdi_core.WDItemEngine.wikibase_item_engine_factory(api,sparql)
item = localEntityEngine(...)```
LeMyst commented 2 years ago

Hello @harej ,

There is a difference between the URL passed to WDLogin and WDItemEngine. They are not shared.

You need to change your code like this:

item = wdi_core.WDItemEngine(
    wd_item_id=wcd_qid,
    mediawiki_api_url=mediawiki_api_url,
    sparql_endpoint_url=sparql_endpoint_url,
    wikibase_url=wiki,
    new_item=approve_new_item(pageid),
    data=itemdata)

or overloading the wbi_config to change the default URLs:

from wikidataintegrator.wdi_config import config as wdi_config
wdi_config['WIKIBASE_URL'] = wiki
wdi_config['MEDIAWIKI_API_URL'] = mediawiki_api_url
wdi_config['MEDIAWIKI_INDEX_URL'] = mediawiki_index_url
wdi_config['SPARQL_ENDPOINT_URL'] = sparql_endpoint_url