http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11488
Revision: 11488 Author: amir Date: 2013-05-03 08:14:51 +0000 (Fri, 03 May 2013) Log Message: ----------- using API for unblock. follow up r11388
Modified Paths: -------------- trunk/pywikipedia/userlib.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/userlib.py =================================================================== --- trunk/pywikipedia/userlib.py 2013-05-02 07:59:55 UTC (rev 11487) +++ trunk/pywikipedia/userlib.py 2013-05-03 08:14:51 UTC (rev 11488) @@ -505,7 +505,6 @@
raise BlockError return True - def unblock(self, reason=None): """ Unblock the user. @@ -518,6 +517,8 @@ blockID = self.name()[1:] else: blockID = self._getBlockID() + if not self.site().has_api() or self.site().versionnumber() < 12: + return self._unblockOld(blockID, reason) self._unblock(blockID,reason)
def _getBlockID(self): @@ -526,16 +527,29 @@ usertype="ip" else: usertype="users" + if not self.site().has_api() or self.site().versionnumber() < 12: + return getBlockIDOld() data = self.site().blocksearch_address(self.name(),usertype) try: bIDre = data[1]["query"]["blocks"][0]["id"] - except KeyError: + except IndexError: pywikibot.output(data) raise BlockIDError return bIDre - def _unblock(self, blockID, reason): pywikibot.output(u"Unblocking [[User:%s]]..." % self.name()) + params = { + 'action':'unblock', + 'reason':reason, + 'id': blockID, + 'token': self.site().getToken(self, sysop = True), + } + data = query.GetData(params, self.site(), back_response=True,sysop=True) + if 'error' in data: + pywikibot.output("Error happend: %s" % str(data['error'])) + return True + def _unblockOld(self, blockID, reason): + pywikibot.output(u"Unblocking [[User:%s]]..." % self.name()) token = self.site().getToken(self, sysop = True) predata = { 'id': blockID, @@ -550,8 +564,15 @@ raise AlreadyUnblockedError raise UnblockError, data return True - - + def getBlockIDOld(self): + self.family.blocksearch_address(self.lang, s) + address = self.site().blocksearch_address(self.name()) + data = self.site().getUrl(address) + bIDre = re.search(r'action=unblock&id=(\d+)', data) + if not bIDre: + pywikibot.output(data) + raise BlockIDError + return bIDre.group(1) def getall(site, users, throttle=True, force=False): """Bulk-retrieve users data from site
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2013-05-02 07:59:55 UTC (rev 11487) +++ trunk/pywikipedia/wikipedia.py 2013-05-03 08:14:51 UTC (rev 11488) @@ -8309,6 +8309,8 @@
def blocksearch_address(self, s, usertype): """Return path to search for blocks on IP address 's'.""" + if not self.has_api() or self.versionnumber() <12: + return self.family.blocksearch_address(self.lang, s) params = { 'bk%s' % usertype : s, 'action' : 'query',