jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[FEAT] Make Revision support slots

In new MediaWiki versions, one revision can hold more
than just one content. This feature is called "slot".

Bug: T226544
Change-Id: Ic926b34980f7b61b8447acc0631b7649c3130ed7
---
M pywikibot/data/api.py
M pywikibot/page.py
2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 0702d38..eae947f 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -3249,8 +3249,6 @@
"""Update page revisions."""
# TODO: T102735: Use the page content model for <1.21
for rev in revisions:
- if 'slots' in rev and 'main' in rev['slots']: # MW 1.32+
- rev.update(rev['slots']['main'])
revision = pywikibot.page.Revision(
revid=rev['revid'],
timestamp=pywikibot.Timestamp.fromISOformat(rev['timestamp']),
@@ -3258,10 +3256,11 @@
anon='anon' in rev,
comment=rev.get('comment', ''),
minor='minor' in rev,
- text=rev.get('*'),
+ slots=rev.get('slots'), # MW 1.32+
+ text=rev.get('*'), # b/c
rollbacktoken=rev.get('rollbacktoken'),
parentid=rev.get('parentid'),
- contentmodel=rev.get('contentmodel'),
+ contentmodel=rev.get('contentmodel'), # b/c
sha1=rev.get('sha1')
)
page._revisions[revision.revid] = revision
diff --git a/pywikibot/page.py b/pywikibot/page.py
index b9f4f35..8172689 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -5606,7 +5606,7 @@

def __init__(self, revid, timestamp, user, anon=False, comment='',
text=None, minor=False, rollbacktoken=None, parentid=None,
- contentmodel=None, sha1=None):
+ contentmodel=None, sha1=None, slots=None):
"""
Initializer.

@@ -5635,9 +5635,11 @@
@type contentmodel: str
@param sha1: sha1 of revision text (v1.19+)
@type sha1: str
+ @param slots: revision slots (v1.32+)
+ @type slots: dict
"""
self.revid = revid
- self.text = text
+ self._text = text
self.timestamp = timestamp
self.user = user
self.anon = anon
@@ -5647,6 +5649,7 @@
self._parent_id = parentid
self._content_model = contentmodel
self._sha1 = sha1
+ self.slots = slots

@property
def parent_id(self):
@@ -5666,15 +5669,38 @@
return self._parent_id

@property
+ def text(self):
+ """
+ Return text of this revision.
+
+ This is meant for compatibility with older MW version which
+ didn't support revisions with slots. For newer MW versions,
+ this returns the contents of the main slot.
+
+ @return: text of the revision
+ @rtype: str or None if text not yet retrieved
+ """
+ if self.slots is not None:
+ return self.slots.get('main', {}).get('*')
+ return self._text
+
+ @property
def content_model(self):
"""
Return content model of the revision.

+ This is meant for compatibility with older MW version which
+ didn't support revisions with slots. For newer MW versions,
+ this returns the content model of the main slot.
+
@return: content model
@rtype: str
@raises AssertionError: content model not supplied to the constructor
which always occurs for MediaWiki versions lower than 1.21.
"""
+ if self._content_model is None and self.slots is not None:
+ self._content_model = self.slots.get('main', {}).get(
+ 'contentmodel')
# TODO: T102735: Add a sane default of 'wikitext' and others for <1.21
assert self._content_model is not None, (
'Revision {0} was instantiated without a content model'

To view, visit change 573259. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ic926b34980f7b61b8447acc0631b7649c3130ed7
Gerrit-Change-Number: 573259
Gerrit-PatchSet: 2
Gerrit-Owner: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: Dvorapa <dvorapa@seznam.cz>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999@gmail.com>
Gerrit-Reviewer: jenkins-bot (75)