jenkins-bot has submitted this change and it was merged.
Change subject: Add Revision.parent_id and Revision.content_model
......................................................................
Add Revision.parent_id and Revision.content_model
Deprecates recently added Page.previous_revision_id.
Also remove some uses of deprecated methods appearing
inside loops run by the tests, and use DeprecationTestCase
to continue testing the deprecated methods without warnings
appearing in the test log.
Change-Id: I0e1827d80d0f47784b1bd87280bd66a9721bdb78
---
M pywikibot/data/api.py
M pywikibot/page.py
M tests/basepage_tests.py
M tests/flow_tests.py
M tests/page_tests.py
5 files changed, 88 insertions(+), 18 deletions(-)
Approvals:
XZise: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index fbdb02d..b0e8d6d 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -2772,7 +2772,12 @@
for item in pagedict['protection']:
page._protection[item['type']] = item['level'], item['expiry']
if 'revisions' in pagedict:
+ # TODO: T102735: Use the page content model for <1.21
+ # TODO: Add rvprop 'contentmodel' to all revisions calls, but only
+ # on 1.21+ otherwise it causes API warnings
for rev in pagedict['revisions']:
+ assert 'parentid' in rev, 'parentid missing in revision %r' % rev
+
revision = pywikibot.page.Revision(
revid=rev['revid'],
timestamp=pywikibot.Timestamp.fromISOformat(rev['timestamp']),
@@ -2781,7 +2786,9 @@
comment=rev.get('comment', u''),
minor='minor' in rev,
text=rev.get('*', None),
- rollbacktoken=rev.get('rollbacktoken', None)
+ rollbacktoken=rev.get('rollbacktoken', None),
+ parentid=rev.get('parentid'),
+ contentmodel=rev.get('contentmodel', None),
)
page._revisions[revision.revid] = revision
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 2c49270..446bbe0 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -174,6 +174,7 @@
If it cannot be reliably determined via the API,
None is returned.
"""
+ # TODO: T102735: Add a sane default of 'wikitext' and others for <1.21
if not hasattr(self, '_contentmodel'):
self.site.loadpageinfo(self)
return self._contentmodel
@@ -593,7 +594,7 @@
return self._lastNonBotUser
self._lastNonBotUser = None
- for entry in self.getVersionHistory():
+ for entry in self.revisions():
if entry.user and (not self.site.isBot(entry.user)):
self._lastNonBotUser = entry.user
break
@@ -609,6 +610,7 @@
return self.latest_revision.timestamp
@property
+ @deprecated('latest_revision.parent_id (0 instead of -1 when no parent)')
def previous_revision_id(self):
"""Return the revision id for the previous revision of this Page.
@@ -616,23 +618,18 @@
@return: long
"""
- history = self.getVersionHistory(total=2)
+ return self.latest_revision.parent_id or -1
- if len(history) == 1:
- return -1
- else:
- return min(x.revid for x in history)
-
- @deprecated('previous_revision_id')
+ @deprecated('latest_revision.parent_id (0 instead of -1 when no parent)')
def previousRevision(self):
"""
Return the revision id for the previous revision.
- DEPRECATED: Use previous_revision_id instead.
+ DEPRECATED: Use latest_revision.parent_id instead.
@return: long
"""
- return self.previous_revision_id
+ return self.latest_revision.parent_id or -1
def exists(self):
"""Return True if page exists on the wiki, even if it's a redirect.
@@ -651,7 +648,7 @@
@rtype: L{Revision}
"""
- return next(self.revisions(reverseOrder=True, total=1))
+ return next(self.revisions(reverse=True, total=1))
def isRedirectPage(self):
"""Return True if this is a redirect, False if not or not existing."""
@@ -4419,7 +4416,8 @@
'rollbacktoken'])
def __init__(self, revid, timestamp, user, anon=False, comment=u"",
- text=None, minor=False, rollbacktoken=None):
+ text=None, minor=False, rollbacktoken=None, parentid=None,
+ contentmodel=None):
"""
Constructor.
@@ -4450,6 +4448,44 @@
self.comment = comment
self.minor = minor
self.rollbacktoken = rollbacktoken
+ self._parent_id = parentid
+ self._content_model = contentmodel
+
+ @property
+ def parent_id(self):
+ """
+ Return id of parent/previous revision.
+
+ Returns 0 if there is no previous revision
+
+ @return: id of parent/previous revision
+ @rtype: long
+ @raises AssertionError: parent id not supplied to the constructor
+ """
+ if self._parent_id is None:
+ raise AssertionError(
+ 'Revision %d was instantiated without a parent id'
+ % self.revid)
+
+ return self._parent_id
+
+ @property
+ def content_model(self):
+ """
+ Return content model of the revision.
+
+ @return: content model
+ @rtype: str
+ @raises AssertionError: content model not supplied to the constructor
+ which always occurs for MediaWiki versions lower than 1.21.
+ """
+ # TODO: T102735: Add a sane default of 'wikitext' and others for <1.21
+ if self._content_model is None:
+ raise AssertionError(
+ 'Revision %d was instantiated without a content model'
+ % self.revid)
+
+ return self._content_model
def hist_entry(self):
"""Return a namedtuple with a Page history record."""
diff --git a/tests/basepage_tests.py b/tests/basepage_tests.py
index c7cd907..a90542c 100644
--- a/tests/basepage_tests.py
+++ b/tests/basepage_tests.py
@@ -89,7 +89,14 @@
def _test_invoke(self):
"""Basic invocation of some base methods and properties."""
self.assertTrue(self._page.exists())
- self._page.previous_revision_id
+ self.assertIsNotNone(self._page.latest_revision)
+
+ self.assertIsInstance(self._page.latest_revision_id, int)
+ self.assertGreaterEqual(self._page.latest_revision_id, 1)
+
+ self.assertIsInstance(self._page.latest_revision.parent_id, int)
+ self.assertGreaterEqual(self._page.latest_revision.parent_id, 0)
+
self._page.botMayEdit()
def _test_return_datatypes(self):
diff --git a/tests/flow_tests.py b/tests/flow_tests.py
index 6500c49..2e95c48 100644
--- a/tests/flow_tests.py
+++ b/tests/flow_tests.py
@@ -39,6 +39,7 @@
self._test_invoke()
self._test_return_datatypes()
self.assertEqual(self._page.isRedirectPage(), False)
+ self.assertEqual(self._page.latest_revision.parent_id, 0)
def test_content_model(self):
"""Test Flow page content model."""
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 89ebac0..7460b2c 100644
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -17,6 +17,7 @@
from tests.aspects import (
unittest, TestCase, DefaultSiteTestCase, SiteAttributeTestCase,
+ DeprecationTestCase,
)
from tests.utils import expected_failure_if
@@ -249,10 +250,10 @@
u"[[:File:Jean-Léon Gérôme 003.jpg|Jean-Léon Gérôme
003.jpg]]")
def test_creation(self):
+ """Test Page.oldest_revision."""
mainpage = self.get_mainpage()
- creation = mainpage.getCreator()
- self.assertEqual(creation[0], 'TwoOneTwo')
- self.assertIsInstance(creation[1], pywikibot.Timestamp)
+ self.assertEqual(mainpage.oldest_revision.user, 'TwoOneTwo')
+ self.assertIsInstance(mainpage.oldest_revision.timestamp,
pywikibot.Timestamp)
class TestPageObject(DefaultSiteTestCase):
@@ -404,7 +405,6 @@
self.assertIsInstance(mainpage.canBeEdited(), bool)
self.assertIsInstance(mainpage.botMayEdit(), bool)
self.assertIsInstance(mainpage.editTime(), pywikibot.Timestamp)
- self.assertIsInstance(mainpage.previous_revision_id, int)
self.assertIsInstance(mainpage.permalink(), basestring)
def test_talk_page(self):
@@ -507,6 +507,25 @@
self.assertTrue(page_copy.isRedirectPage())
+class TestPageDeprecation(DefaultSiteTestCase, DeprecationTestCase):
+
+ """Test deprecation of Page attributes."""
+
+ def test_creator(self):
+ """Test getCreator."""
+ mainpage = self.get_mainpage()
+ self.assertEqual(mainpage.getCreator(),
+ (mainpage.oldest_revision.user,
+ mainpage.oldest_revision.timestamp))
+ self.assertDeprecation()
+
+ self._reset_messages()
+ self.assertIsInstance(mainpage.previous_revision_id, int)
+ self.assertEqual(mainpage.previous_revision_id,
+ mainpage.latest_revision.parent_id)
+ self.assertDeprecation()
+
+
class TestPageRepr(DefaultSiteTestCase):
"""Test Page representation."""
--
To view, visit https://gerrit.wikimedia.org/r/216931
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I0e1827d80d0f47784b1bd87280bd66a9721bdb78
Gerrit-PatchSet: 6
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: XZise <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits