Page MenuHomePhabricator

Cannot login into private wikibase using pywikibot
Closed, ResolvedPublicBUG REPORT

Description

Hi,

I'm using an old version of pywikibot to edit our private wiki. I would like to upgrade, but I'm not able to configure the bot properly. (Note: at the end I say that with an older version it was working, not that you loose time in reading detailed configuration files)

My minimal example looks like

pywikibot==5.3.0

./config/my_family.py

from __future__ import absolute_import, division, unicode_literals

from pywikibot import config
from pywikibot import family


# The Wikidata family
class Family(family.WikimediaFamily):

    """Family class for Wikidata."""

    name = 'my'

    langs = {
         'my': 'linkedopendata.eu',
    }

    interwiki_forward = 'wikipedia'

    category_redirect_templates = {
        'my': (
            'Category redirect',
        ),
    }


    # Subpages for documentation.
    doc_subpages = {
        '_default': (('/doc', ), ['wikidata']),
    }

    # Disable cosmetic changes
    config.cosmetic_changes_disable.update({
        'my': ('wikidata2', 'test', 'beta')
    })

    def interface(self, code):
        """Return 'DataSite'."""
        return 'DataSite'

    def calendarmodel(self, code):
        """Default calendar model for WbTime datatype."""
        return 'http://www.wikidata.org/entity/Q1985727'

    def shared_geo_shape_repository(self, code):
        """Return Wikimedia Commons as the repository for geo-shapes."""
        # Per geoShapeStorageFrontendUrl settings in Wikibase
        return ('commons', 'commons')

    def shared_tabular_data_repository(self, code):
        """Return Wikimedia Commons as the repository for tabular-datas."""
        # Per tabularDataStorageFrontendUrl settings in Wikibase
        return ('commons', 'commons')

    def default_globe(self, code):
        """Default globe for Coordinate datatype."""
        return 'earth'

    def protocol(self, code):
        return {
            'my': 'https',
        }[code]

    def globes(self, code):
        """Supported globes for Coordinate datatype."""
        return {
            'ariel': 'http://www.wikidata.org/entity/Q3343',
            'callisto': 'http://www.wikidata.org/entity/Q3134',
            'ceres': 'http://www.wikidata.org/entity/Q596',
            'deimos': 'http://www.wikidata.org/entity/Q7548',
            'dione': 'http://www.wikidata.org/entity/Q15040',
            'earth': 'http://www.wikidata.org/entity/Q2',
            'enceladus': 'http://www.wikidata.org/entity/Q3303',
            'eros': 'http://www.wikidata.org/entity/Q16711',
            'europa': 'http://www.wikidata.org/entity/Q3143',
            'ganymede': 'http://www.wikidata.org/entity/Q3169',
            'gaspra': 'http://www.wikidata.org/entity/Q158244',
            'hyperion': 'http://www.wikidata.org/entity/Q15037',
            'iapetus': 'http://www.wikidata.org/entity/Q17958',
            'io': 'http://www.wikidata.org/entity/Q3123',
            'jupiter': 'http://www.wikidata.org/entity/Q319',
            'lutetia': 'http://www.wikidata.org/entity/Q107556',
            'mars': 'http://www.wikidata.org/entity/Q111',
            'mercury': 'http://www.wikidata.org/entity/Q308',
            'mimas': 'http://www.wikidata.org/entity/Q15034',
            'miranda': 'http://www.wikidata.org/entity/Q3352',
            'moon': 'http://www.wikidata.org/entity/Q405',
            'oberon': 'http://www.wikidata.org/entity/Q3332',
            'phobos': 'http://www.wikidata.org/entity/Q7547',
            'phoebe': 'http://www.wikidata.org/entity/Q17975',
            'pluto': 'http://www.wikidata.org/entity/Q339',
            'rhea': 'http://www.wikidata.org/entity/Q15050',
            'steins': 'http://www.wikidata.org/entity/Q150249',
            'tethys': 'http://www.wikidata.org/entity/Q15047',
            'titan': 'http://www.wikidata.org/entity/Q2565',
            'titania': 'http://www.wikidata.org/entity/Q3322',
            'triton': 'http://www.wikidata.org/entity/Q3359',
            'umbriel': 'http://www.wikidata.org/entity/Q3338',
            'venus': 'http://www.wikidata.org/entity/Q313',
            'vesta': 'http://www.wikidata.org/entity/Q3030',
        }

user_config.py

from pywikibot.config2 import usernames, register_families_folder

register_families_folder('./config')
mylang = "my"
family = "my"
usernames['my']['my'] = u'WikidataUpdater'
password_file = "user-password.py"

simpleTest.py

import pywikibot

wikibase = pywikibot.Site("my", "my")
wikibase_repo = wikibase.data_repository()

wikibase_item = pywikibot.PropertyPage(wikibase_repo, datatype='external-id')
mylabels = {"en": "Wikidata QID"}
wikibase_item.editLabels(mylabels, summary=u'Insert a property')

Running python simpleTest.py gives the error:

  File "/Users/Dennis/PycharmProjects/wikibaseEditor/venv/lib/python3.7/site-packages/pywikibot/family.py", line 709, in load
    raise UnknownFamily('Family %s does not exist' % fam)
pywikibot.exceptions.UnknownFamily: Family my does not exist
CRITICAL: Exiting due to uncaught exception <class 'pywikibot.exceptions.UnknownFamily'>

which I already find strange. I feel I followed the instructions at "https://www.mediawiki.org/wiki/Manual:Pywikibot/Use_on_third-party_wikis".

I can avoid this problem by setting:

simpleTest.py

import os

import pywikibot
from pywikibot import config2

family = 'my'
mylang = 'my'
familyfile=os.path.relpath("./config/my_family.py")
if not os.path.isfile(familyfile):
  print ("family file %s is missing" % (familyfile))
config2.register_family_file(family, familyfile)
config2.password_file = "user-password.py"
config2.usernames['my']['my'] = 'WikidataUpdater'


wikibase = pywikibot.Site("my", "my")
wikibase_repo = wikibase.data_repository()

wikibase_item = pywikibot.PropertyPage(wikibase_repo, datatype='external-id')
mylabels = {"en": "Wikidata QID"}
wikibase_item.editLabels(mylabels, summary=u'Insert a property')

which is far from ideal (I really would like to clean the code here) .... Running python simpleTest.py gives the error:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Dennis/PycharmProjects/wikibaseEditor/simpleTest.py", line 22, in <module>
    wikibase_item.editLabels(mylabels, summary=u'Insert a property')
  File "/Users/Dennis/PycharmProjects/wikibaseEditor/venv/lib/python3.7/site-packages/pywikibot/page/__init__.py", line 4215, in editLabels
    self.editEntity(data, **kwargs)
  File "/Users/Dennis/PycharmProjects/wikibaseEditor/venv/lib/python3.7/site-packages/pywikibot/page/__init__.py", line 138, in wrapper
    handle(func, self, *args, **kwargs)
  File "/Users/Dennis/PycharmProjects/wikibaseEditor/venv/lib/python3.7/site-packages/pywikibot/page/__init__.py", line 130, in handle
    raise pywikibot.OtherPageSaveError(self, err)
pywikibot.exceptions.OtherPageSaveError: Edit to page [[my:Property:-1]] failed:
User "None" does not have required user right "edit"
CRITICAL: Exiting due to uncaught exception <class 'pywikibot.exceptions.OtherPageSaveError'>

so the user is not passed correctly. Note that if I use as a pywikibot dependency

-e git+https://github.com/wikimedia/pywikibot.git@083ed9341881f562167e732974df35ee051ba994#egg=pywikibot

which is much older .... this worked. My guess is that there is either a bug or the documentation has to be changed.

Thank you for your help
D063520

Event Timeline

any news on this issue? I tried again and I'm really stuck. Basically I think that the current code/documentation for pywikibot on a third party wikibase are not wokring. Could you test?

Xqt removed Xqt as the assignee of this task.Mar 5 2021, 12:21 AM
Xqt triaged this task as Medium priority.
Xqt subscribed.

Sorry, haven't seen this task due to missing Pywikibot project tag. I'll investigate into this manner soon.

There are two issues:

  • register_families_folder does not convert relative paths to absolute paths
  • family.py seems to get a copy of config.family_files dict somehow; the registered families from user_config aren't included

Note that if I use as a pywikibot dependency
-e git+https://github.com/wikimedia/pywikibot.git@083ed9341881f562167e732974df35ee051ba994#egg=pywikibot

@DD063520: Can you give me a Version numer of this old Pywikibot release which is working? I walked back for many branches until the first implementation in 2014. I didn't found it ever working :(

Probably this is caused by rPWBC6f0cafd; using globals() instead of the deep copy works. Maybe a Python 3 problem. @Huji, @zhuyifei1999: any ideas?

Xqt raised the priority of this task from Medium to High.Mar 5 2021, 9:37 AM

Change 668661 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [bugfix] Re-enable setting private family files

https://gerrit.wikimedia.org/r/668661

@DD063520 : I made a new implementation for registering family files. Are you able to test it? If appropriate I will update the documentation accordingly.

If you explain me how to test it, I will do. How to I download the code with the latest commits?

If you explain me how to test it, I will do. How to I download the code with the latest commits?

checkout the patch from https://gerrit.wikimedia.org/r/c/pywikibot/core/+/668661 or download the patch or the right content from there and replace your current config2.py file with it.
Then add the following line in your user-config.py to register private families:

user_families_paths = ['./config']

I also checked this release. It does not work for me with Python 3.

@Xqt: so one bug is gone, i.e. the configuration file is taken into account:

user-config.py looks like

from pywikibot.config2 import usernames, register_families_folder

user_families_paths = ['./config']
mylang = "my"
family = "my"
usernames['my']['my'] = u'WikidataUpdater'
password_file = "user-password.py"

and

import pywikibot

wikibase = pywikibot.Site("my", "my")
wikibase_repo = wikibase.data_repository()

print("start")
item = pywikibot.ItemPage(wikibase_repo, 'Q235272')  # a repository item
data = item.get()  # get all item data from repository for this item
print(data)

data={}
data['labels'] = {'en': 'Test'}
item.editEntity(data)

Nice and clean! : ) Now there is still a bug. He can read, but he cannot edit:

pywikibot.exceptions.OtherPageSaveError: Edit to page [[Item:Q235272]] failed:
User "None" does not have required user right "edit"
CRITICAL: Exiting due to uncaught exception <class 'pywikibot.exceptions.OtherPageSaveError'>

Now there is still a bug. He can read, but he cannot edit:

Thanks for review. Could you please test my newest patch; it should solve it.

By the way: you shouldn't import anything in user-config.py. The global variables are already passed during exec function call in config2.py

II used this patch:

https://gerrit.wikimedia.org/r/c/pywikibot/core/+/668661

and I think it is rather not working ....

WARNING: No user is logged in on site my:my

could be an error on my side but I would not know which one ...

WARNING: No user is logged in on site my:my

You have to login first. Either use the login.py script:

python.exe pwb.py login -site:my:my

or force it within your script:

import pywikibot
repo = pywikibot.Site('my:my')
repo.login()

Change 668661 merged by jenkins-bot:
[pywikibot/core@master] [bugfix] Re-enable setting private family files

https://gerrit.wikimedia.org/r/668661