Page MenuHomePhabricator

Implement pywikibot support for adding thanks to normal revisions
Closed, ResolvedPublic

Description

I will rewrite the following description as needed.

Under the History tab of every page, there is a list of revisions of the page containing details of the timestamp when the edit was made and the user who made the edits. Next to each revision (if the revision is not by the current logged in user), there is a 'thank' link. Clicking on this link will send an notification to the corresponding user using the Echo extension, unless the user has opted out of receiving thanks notifications. (Anons can't currently receive thanks, so a simple version of User.thanks_enabled property, as mentioned in Subtask 4, is needed here) .The Thanks API is called using action=thank, with parameters rev, source and token. This concept of thanking revisions, as seen in the class ApiRevThank (which extends ApiThank) in Thanks needs to be implemented in Pywikibot, and then tested using the test suite.

Event Timeline

As per T135933, my method in thanks.py for this should take a revision object as argument, and then use the thank API to thank the rev as a bot. After looking at flow.py, flow_tests.py, page.py and site.py to see how Alex approached the workflow, I'll start with writing the basic script which thanks a rev (hard coded rev to begin with?).

I am behind schedule on this one.

As discussed in T136892, I have written a proof of concept which can thank revisions on test.wikipedia. However, I am not being successful in making a POST request to the API to thank the edit.

See the code for scripts/thanks.py here.

The error being thrown up on this one is:

{"servedby":"mw1136","error":{"code":"badtoken","info":"Invalid token","*":"See https://test.wikipedia.org/w/api.php for API usage"}}

I have tried moving from urllib2 to request, I have change the values of the token by trying different combinations of escape sequences for the +\ at the end. It still shows the same error.

Can I have some help as to where I am going wrong?

What username are you loggjng in as?
Please paste your script and a debug log into phabricator. Take care to remove passwords.

@jayvdb, the entire script has been linked to in T135409#2373533. Here's the method involved:

def thank_rev(rev_id):
    """
    Thank the given revision ID on the wiki on behalf of the user logged in.
    """
    #Login into the wiki
    site = pywikibot.Site()
    site.login()
    #Get csrf token from site.
    token = site.tokens['csrf']
     
    # print rev_id
    # print token

    values = {'rev' : rev_id,
              'source' : 'test_thank',
              'token' : token }
    r = requests.post("https://test.wikipedia.org/w/api.php?action=thank&format=json", data=values)
    print (r.text)

The user name used is 'darthbhyrava'. If I print the token, and use in the API sandbox, it works. But the script returns the value:

{"servedby":"mw1197","error":{"code":"badtoken","info":"Invalid token","*":"See https://test.wikipedia.org/w/api.php for API usage"}}

Change 294901 had a related patch set uploaded (by Darthbhyrava):
Implement pywikibot support for adding thanks to normal revisions

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

@jayvdb - an updated patch has been put up for this. It is throwing an error because I cannot seem to execute either isinstance(var, basestring) or isinstance(var, unicode) without pyflakes throwing up errors. I need to get pyflakes throwing the same error locally, I'll fix it.

Could you review the rest of the code, please?

One reason this patch didn't go through was due to lack of test coverage. Could I have some assistance with that, please? What metrics do I use, and which tools? Should I be integrating something like Coveralls for my GitHub repository to ensure this?

Change 357957 had a related patch set uploaded (by Happy5214; owner: Happy5214):
[pywikibot/core@master] [WIP] Add on to pywikibot support for thanking normal revisions

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

Change 294901 merged by jenkins-bot:
[pywikibot/core@master] Implement pywikibot support for adding thanks to normal revisions

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

Change 357957 merged by jenkins-bot:
[pywikibot/core@master] Add on to pywikibot support for thanking normal revisions

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