John Vandenberg has uploaded a new change for review.
https://gerrit.wikimedia.org/r/189110
Change subject: Add Page.contributors() and .edit_count()
......................................................................
Add Page.contributors() and .edit_count()
Change-Id: Iee0fbd7337b70a4091292938e6d81764a5b4ff43
---
M pywikibot/page.py
M scripts/checkimages.py
M tests/page_tests.py
3 files changed, 81 insertions(+), 18 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/10/189110/1
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 6fa838d..0d7310b 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -1434,6 +1434,20 @@
reverse=not reverseOrder)
][:total]
+ def contributors(self, step=None, total=None):
+ """
+ Compile contributors of this page with edit counts.
+
+ @param step: limit each API call to this number of revisions
+ @param total: iterate no more than this number of revisions in total
+
+ @return: number of edits for each username
+ @rtype: L{collections.Counter}
+ """
+ return collections.Counter(
+ rev.user for rev in self.revisions(step=step, total=total))
+
+ @deprecated('contributors()')
def contributingUsers(self, step=None, total=None):
"""Return a set of usernames (or IPs) of users who edited this page.
@@ -1441,9 +1455,23 @@
@param total: iterate no more than this number of revisions in total
"""
- history = self.getVersionHistory(step=step, total=total)
- users = set(entry.user for entry in history)
- return users
+ return self.contributors.keys()
+
+ def edit_count(self, contributors=None):
+ """
+ Determine number of edits from a set of contributors.
+
+ @param contributors: contributor usernames
+ @type contributors: iterable of str
+
+ @return: number of edits for all provided usernames
+ @rtype: int
+ """
+ if not contributors:
+ return len(self.revisions())
+
+ cnt = self.contributors()
+ return sum(cnt[username] for username in contributors)
@deprecated('oldest_revision')
def getCreator(self):
@@ -1456,7 +1484,7 @@
result = self.oldest_revision
return result.user, result.timestamp
- @deprecated('revisions')
+ @deprecated('contributors() or revisions()')
@deprecated_args(limit="total")
def getLatestEditors(self, total=1):
"""Get a list of revision informations of the last total edits.
diff --git a/scripts/checkimages.py b/scripts/checkimages.py
index 19f07e6..4630fb5 100644
--- a/scripts/checkimages.py
+++ b/scripts/checkimages.py
@@ -873,22 +873,14 @@
"""Return the page title suitable as for an URL."""
return page.title(asUrl=True)
+ @deprecated('Page.edit_count()')
def countEdits(self, pagename, userlist):
"""Function to count the edit of a user or a list of users in a
page."""
# self.botolist
if isinstance(userlist, basestring):
userlist = [userlist]
page = pywikibot.Page(self.site, pagename)
- history = page.getVersionHistory()
- user_list = list()
-
- for data in history:
- user_list.append(data.user)
- number_edits = 0
-
- for username in userlist:
- number_edits += user_list.count(username)
- return number_edits
+ return page.edit_count(userlist)
def checkImageOnCommons(self):
"""Checking if the file is on commons."""
@@ -1039,8 +1031,9 @@
# (the last)
if len(images_to_tag_list) > 1:
for image_to_tag in images_to_tag_list[:-1]:
- already_reported_in_past = self.countEdits(
- u'File:%s' % image_to_tag, self.botolist)
+ fp = pywikibot.Page(self.site, u'File:%s' %
image_to_tag)
+ already_reported_in_past = fp.edit_count(
+ set(self.botolist))
# if you want only one edit, the edit found should be
# more than 0 -> num - 1
if already_reported_in_past > duplicates_rollback - 1:
@@ -1055,8 +1048,10 @@
commImage=dupComment_image, unver=True)
if len(images_to_tag_list) != 0 and not only_report:
- already_reported_in_past = self.countEdits(
- u'File:%s' % images_to_tag_list[-1], self.botolist)
+ fp = pywikibot.Page(self.site,
+ u'File:%s' % images_to_tag_list[-1])
+ already_reported_in_past = fp.edit_count(
+ set(self.botolist))
from_regex = r'\n\*\[\[:File:%s\]\]' \
% re.escape(self.convert_to_url(
self.imageName))
diff --git a/tests/page_tests.py b/tests/page_tests.py
index f95bd6d..e650599 100644
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -522,6 +522,46 @@
self.assertTrue(page_copy.isRedirectPage())
+class TestPageHistory(DefaultSiteTestCase):
+
+ """Test history related functionality."""
+
+ cached = True
+
+ def test_revisions(self):
+ """Test Page.revisions()."""
+ mp = self.get_mainpage()
+ revs = mp.revisions()
+ revs = iter(revs) # implicit assertion
+ revs = list(revs)
+ self.assertGreater(len(revs), 1)
+
+ def test_contributors(self):
+ """Test Page.contributors()."""
+ mp = self.get_mainpage()
+ cnt = mp.contributors()
+ self.assertIsInstance(cnt, dict)
+ self.assertGreater(len(cnt), 1)
+
+ def test_edit_count(self):
+ """Test Page.edit_count()."""
+ mp = self.get_mainpage()
+ rev_count = len(list(mp.revisions()))
+ cnt = mp.contributors()
+ self.assertEqual(rev_count, sum(cnt.values()))
+
+ top_two = cnt.most_common(2)
+ self.assertIsInstance(top_two, list)
+ self.assertIsInstance(top_two[0], tuple)
+ self.assertIsInstance(top_two[0][0], basestring)
+ self.assertIsInstance(top_two[0][1], int)
+ top_two_usernames = set([top_two[0][0], top_two[1][0]])
+ top_two_counts = set([top_two[0][1], top_two[1][1]])
+ top_two_edit_count = mp.edit_count(top_two_usernames)
+ self.assertIsInstance(top_two_edit_count, int)
+ self.assertEqual(top_two_edit_count, sum(top_two_counts))
+
+
class TestPageRedirects(TestCase):
"""Test redirects."""
--
To view, visit https://gerrit.wikimedia.org/r/189110
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iee0fbd7337b70a4091292938e6d81764a5b4ff43
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits