jenkins-bot has submitted this change and it was merged.
Change subject: Bug 64188 - getVersionHistory does not follow total parameter ......................................................................
Bug 64188 - getVersionHistory does not follow total parameter
When a version history is already loaded, decreasing the total parameter getVersionHistory() now returns the decreased amount of values.
Fixed also for getVersionHistoryTable() and fullVersionHistory() methods.
Created namedtuple classes: - HistEntry - FullHistEntry to improve handling of data returned by getVersionHistory().
Adapted scripts to make use of new classes.
Fixed also a bug in page.previousRevision(), when page is new page.
Bug: 64188 Change-Id: I9aa1372e7e6366e3dee454d069b742abbc372b64 --- M pywikibot/page.py M scripts/checkimages.py M scripts/newitem.py 3 files changed, 82 insertions(+), 75 deletions(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 2c3db36..08a27ba 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -518,11 +518,9 @@ return self._lastNonBotUser
self._lastNonBotUser = None - for vh in self.getVersionHistory(): - (revid, timestmp, username, comment) = vh[:4] - - if username and (not self.site.isBot(username)): - self._lastNonBotUser = username + for entry in self.getVersionHistory(): + if entry.user and (not self.site.isBot(entry.user)): + self._lastNonBotUser = entry.user break
return self._lastNonBotUser @@ -540,11 +538,16 @@ def previousRevision(self): """Return the revision id for the previous revision of this Page.
+ If the page has only one revision, it shall return -1. + @return: long """ - self.getVersionHistory(total=2) - revkey = sorted(self._revisions, reverse=True)[1] - return revkey + history = self.getVersionHistory(total=2) + + if len(history) == 1: + return -1 + else: + return min(x.revid for x in history)
def exists(self): """Return True if page exists on the wiki, even if it's a redirect. @@ -1360,8 +1363,7 @@ # returned no more than 500 revisions; now, it iterates # all revisions unless 'total' argument is used @deprecated_args(forceReload=None, revCount="total", getAll=None) - def getVersionHistory(self, reverseOrder=False, step=None, - total=None): + def getVersionHistory(self, reverseOrder=False, step=None, total=None): """Load the version history page and return history information.
Return value is a list of tuples, where each tuple represents one @@ -1375,25 +1377,21 @@ """ self.site.loadrevisions(self, getText=False, rvdir=reverseOrder, step=step, total=total) - return [(self._revisions[rev].revid, - self._revisions[rev].timestamp, - self._revisions[rev].user, - self._revisions[rev].comment - ) for rev in sorted(self._revisions, - reverse=not reverseOrder) - ] + + return [rev.hist_entry() + for revid, rev in sorted(self._revisions.items(), + reverse=not reverseOrder) + ][:total]
@deprecated_args(forceReload=None) def getVersionHistoryTable(self, reverseOrder=False, step=None, total=None): """Return the version history as a wiki table.""" result = '{| class="wikitable"\n' result += '! oldid || date/time || username || edit summary\n' - for oldid, time, username, summary \ - in self.getVersionHistory(reverseOrder=reverseOrder, - step=step, total=total): + for entry in self.getVersionHistory(reverseOrder=reverseOrder, + step=step, total=total): result += '|----\n' - result += '| %s || %s || %s || <nowiki>%s</nowiki>\n'\ - % (oldid, time, username, summary) + result += '| %s || %s || %s || <nowiki>%s</nowiki>\n' % entry result += '|}\n' return result
@@ -1408,17 +1406,13 @@ edit date/time, user name and content
""" - self.site.loadrevisions(self, getText=True, - rvdir=reverseOrder, + self.site.loadrevisions(self, getText=True, rvdir=reverseOrder, step=step, total=total, rollback=rollback) - return [(self._revisions[rev].revid, - self._revisions[rev].timestamp, - self._revisions[rev].user, - self._revisions[rev].text, - self._revisions[rev].rollbacktoken - ) for rev in sorted(self._revisions, - reverse=not reverseOrder) - ] + + return [rev.hist_entry() + for revid, rev in sorted(self._revisions.items(), + reverse=not reverseOrder) + ][:total]
def contributingUsers(self, step=None, total=None): """Return a set of usernames (or IPs) of users who edited this page. @@ -1427,8 +1421,8 @@ @param total: iterate no more than this number of revisions in total
""" - edits = self.getVersionHistory(step=step, total=total) - users = set([edit[2] for edit in edits]) + history = self.getVersionHistory(step=step, total=total) + users = set(entry.user for entry in history) return users
@deprecate_arg("throttle", None) @@ -3846,6 +3840,19 @@
"""A structure holding information about a single revision of a Page."""
+ HistEntry = collections.namedtuple('HistEntry', + ['revid', + 'timestamp', + 'user', + 'comment']) + + FullHistEntry = collections.namedtuple('FullHistEntry', + ['revid', + 'timestamp', + 'user', + 'text', + 'rollbacktoken']) + def __init__(self, revid, timestamp, user, anon=False, comment=u"", text=None, minor=False, rollbacktoken=None): """ @@ -3879,6 +3886,16 @@ self.minor = minor self.rollbacktoken = rollbacktoken
+ def hist_entry(self): + """Return a namedtuple with a Page history record.""" + return Revision.HistEntry(self.revid, self.timestamp, self.user, + self.comment) + + def full_hist_entry(self): + """Return a namedtuple with a Page full history record.""" + return Revision.FullHistEntry(self.revid, self.timestamp, self.user, + self.text, self.rollbacktoken) +
class Link(ComparableMixin):
diff --git a/scripts/checkimages.py b/scripts/checkimages.py index ff56c81..8179fff 100644 --- a/scripts/checkimages.py +++ b/scripts/checkimages.py @@ -1,9 +1,10 @@ #!/usr/bin/python # -*- coding: utf-8 -*- """ -Script to check recently uploaded files. This script checks if a file -description is present and if there are other problems in the image's -description. +Script to check recently uploaded files. + +This script checks if a file description is present and if there are other +problems in the image's description.
This script will have to be configured for each language. Please submit translations as addition to the Pywikibot framework. @@ -565,24 +566,16 @@
class LogIsFull(pywikibot.Error):
- """An exception indicating that the log is full and the Bot cannot add - other data to prevent Errors. - - """ + """Log is full and the Bot cannot add other data to prevent Errors."""
class NothingFound(pywikibot.Error):
- """ An exception indicating that a regex has return [] instead of results. - - """ + """Regex returned [] instead of results."""
def printWithTimeZone(message): - """ Function to print the messages followed by the TimeZone encoded - correctly. - - """ + """Print the messages followed by the TimeZone encoded correctly.""" if message[-1] != ' ': message = '%s ' % unicode(message) if locale.getlocale()[1]: @@ -596,6 +589,8 @@
class checkImagesBot(object): + + """A robot to check recently uploaded files."""
def __init__(self, site, logFulNumber=25000, sendemailActive=False, duplicatesReport=False, logFullError=True): @@ -639,9 +634,10 @@ self.list_licenses = self.load_licenses()
def setParameters(self, imageName): - """ Function to set parameters, now only image but maybe it can be used - for others in "future" + """ + Set parameters.
+ Now only image but maybe it can be used for others in "future". """ self.imageName = imageName self.image = pywikibot.FilePage(self.site, self.imageName) @@ -707,10 +703,7 @@ return upBotArray[0]
def tag_image(self, put=True): - """ Function to add the template in the image and to find out - who's the user that has uploaded the file. - - """ + """Add template to the Image page and find out the uploader.""" # Get the image's description reportPageObject = pywikibot.FilePage(self.site, self.image_to_report)
@@ -829,9 +822,10 @@ return
def untaggedGenerator(self, untaggedProject, limit): - """ Generator that yield the files without license. It's based on a - tool of the toolserver. + """ + Generator that yield the files without license.
+ It's based on a tool of the toolserver. """ lang = untaggedProject.split('.', 1)[0] project = '.%s' % untaggedProject.split('.', 1)[1] @@ -863,10 +857,7 @@ u'that it works!')
def regexGenerator(self, regexp, textrun): - """ Generator used when an user use a regex parsing a page to yield the - results - - """ + """Find page to yield using regex to parse text.""" regex = re.compile(r'%s' % regexp, re.UNICODE | re.DOTALL) results = regex.findall(textrun) for image in results: @@ -935,7 +926,7 @@ user_list = list()
for data in history: - user_list.append(data[2]) + user_list.append(data.user) number_edits = 0
for username in userlist: @@ -1319,11 +1310,7 @@ return list_licenses
def miniTemplateCheck(self, template): - """ - Check whether the given template given in the licenses allowed or in the - licenses to skip. - - """ + """Check if template is in allowed licenses or in licenses to skip.""" # the list_licenses are loaded in the __init__ # (not to load them multimple times) if template in self.list_licenses: @@ -1345,11 +1332,12 @@
def templateInList(self): """ + Check if template is in list. + The problem is the calls to the Mediawiki system because they can be pretty slow. While searching in a list of objects is really fast, so first of all let's see if we can find something in the info that we already have, then make a deeper check. - """ for template in self.licenses_found: result = self.miniTemplateCheck(template) @@ -1368,10 +1356,12 @@ continue
def smartDetection(self): - """The bot instead of checking if there's a simple template in the + """ + Detect templates. + + The bot instead of checking if there's a simple template in the image's description, checks also if that template is a license or something else. In this sense this type of check is smart. - """ self.seems_ok = False self.license_found = None @@ -1522,9 +1512,10 @@ pywikibot.output('')
def wait(self, waitTime, generator, normal, limit): - """ Skip the images uploaded before x seconds to let - the users to fix the image's problem alone in the - first x seconds. + """ + Skip the images uploaded before x seconds. + + Let the users to fix the image's problem alone in the first x seconds. """ imagesToSkip = 0 # if normal, we can take as many images as "limit" has told us, diff --git a/scripts/newitem.py b/scripts/newitem.py index f320e0e..acba3da 100644 --- a/scripts/newitem.py +++ b/scripts/newitem.py @@ -78,9 +78,8 @@ % (page, page.editTime().isoformat())) return
- (revId, revTimestamp, revUser, - revComment) = page.getVersionHistory(reverseOrder=True, total=1)[0] - if revTimestamp > self.pageAgeBefore: + rev = page.getVersionHistory(reverseOrder=True, total=1)[0] + if rev.timestamp > self.pageAgeBefore: pywikibot.output( u'Page creation of %s on %s is too recent. Skipping.' % (page, page.editTime().isoformat()))
pywikibot-commits@lists.wikimedia.org