Commons-query.wikipedia.org requires that user has been logged in to Wikimedia Commons and has authorized the use of Commons-query.wikipedia.org. Currently the SPARQL-query returns None if there is any error.
However, code should throw an exception and human readable error message if there is authorization error from "Commons-query.wikipedia.org". In other cases keep returing None for backwards compability.
Pywikibot developing
Documentation
- https://www.mediawiki.org/wiki/Manual:Pywikibot/Gerrit#For_developers
- Commit messages: https://www.mediawiki.org/wiki/Gerrit/Commit_message_guidelines
- Zache's notes: https://docs.google.com/document/d/1DU8Y4GhsGeusrqh1xqxDTfqtGc6YG4AMaOrvDD36MIs/edit
Howto Install development enviroment
python -m venv venv source venv/bin/activate git clone --recursive ssh://WIKITECH_USERNAME@gerrit.wikimedia.org:29418/pywikibot/core.git pywikibot-git cd pywikibot-git pip install --upgrade pip pip install -r requirements.txt pip install flake8 pip install git-review cp user-config.py.sample user-config.py
And update user-config.py with correct Wikimedia Commons user name
- Config Git-review **
Setup emails and settings in gerrit first: https://gerrit.wikimedia.org/r/settings/
then setup git
cd pywikibot-git git config user.email "EMAIL" git config user.name "USERNAME" git review -s --verbose
Howto toggle OAUTH permissions
- Give permission: Login to Wikimedia Commons and go to https://commons-query.wikimedia.org using web browser
- Revoke permission: Login to Wikimedia Commons and remove permission using https://commons.wikimedia.org/wiki/Special:OAuthManageMyGrants page
Example code for sparql query
import pywikibot from pywikibot.data import sparql site = pywikibot.Site('commons', 'commons') ## Login or logout when needed #site.logout() #site.login() # Define the SPARQL query query = "SELECT ?item ?finna_id WHERE { ?item wdt:P9478 ?finna_id } LIMIT 4" # Set up the SPARQL endpoint and entity URL # Note: https://commons-query.wikimedia.org requires user to be logged in entity_url = 'https://commons.wikimedia.org/entity/' endpoint = 'https://commons-query.wikimedia.org/sparql' # Create a SparqlQuery object query_object = sparql.SparqlQuery(endpoint= endpoint, entity_url= entity_url) # Execute the SPARQL query and retrieve the data data = query_object.select(query, full_data=False) print(data)
Sparql queres are executed in SparqlQuery class function query() in pywikibot/data/sparql.py:
Error messages
Note. Server returns valid HTTP 200 OK messages and not errors so login status need to be detected from self.last_response.content . (ie. if it contains a string "You need to log in" then user is not logged in. Similarly if it asks OAUTH authorization then oauth is missing.)
Suitable error message for user not logged in could be
message='You need to log in to Wikimedia Commons and give OAUTH permission. Open https://commons-query.wikimedia.org using web browser to login and give permission.' raise NoUsernameError('User not logged in. ' + message)
And for logged in user but with missing OAUTH permission
message='You need to log in to Wikimedia Commons and give OAUTH permission. Open https://commons-query.wikimedia.org using web browser to login and give permission.' raise UserRightsError('User OAUTH authorization is missing. ' + message)
For other sites than commons-query.wikimedia.org keep existing return None for backwards compability
Running code
Running code using the github version of pywikibot
python pwb.py myscript.py
Tests
Add test case for testing missing oauth permission to Pywikibot tests to tests/sparql_tests.py
python -m unittest -v tests.sparql_tests
Howto test code formatting
flake8 tests/sparql_tests.py
Howto push change to review
Basic idea:
- Only single commits are viewed
- All reviewed commits are reviewed against current master
- When new version of the commit is reviewed the user dev-branch is updated to latest master and changes are amended to single commit
- Reviews and phabricator tickets are linked using phabricator ticket in commit message
- Commit message is message which will be shown as commit message when change is merged to master
Example commit message
- First line is title without ticketnumber or prefix.
- Second line is empty line
- Next lines are description
- Then is one empty line and line which contains text "Bug: " and Phabricator ticket number.
Give visible error to user when there are no rights. Currently bot does not give user-visible output when user is not logged in or user does not have permissions. Instead of silently failing inform user of a problem. Bug: T345342
Submitting first version
git checkout -b bugfix/T123456-short-description DO CHANGES git stash git fetch —-all git pull --rebase origin master git stash pop git status git ADD CHANGED FILE git commit git review
Submit updated versions to re-review
DO CHANGES git stash git fetch —-all git pull --rebase origin master FIX MERGE CONFLICTS git add fixed file git rebase --continue git stash pop git status git ADD CHANGED FILE git commit --amend git review
Howto test something in gerrit
- 959755 is gerrit id number
git review -d 959755