jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/599292 )
Change subject: [IMPR] Move latest revision id handling to WikibaseEntity ......................................................................
[IMPR] Move latest revision id handling to WikibaseEntity
Nevertheless, users should avoid relying on this. The implementation seems hacky because unlike standard MediaWiki pages, Pywikibot doesn't strictly bind Wikibase entities with revisions.
Bug: T233406 Change-Id: I8353d31d744cdff5a980d9f179cf9f6c3e30095f --- M pywikibot/page/__init__.py 1 file changed, 38 insertions(+), 11 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py index e6dbf17..9ad8bb2 100644 --- a/pywikibot/page/__init__.py +++ b/pywikibot/page/__init__.py @@ -3830,6 +3830,29 @@ norm_data[key] = attr.normalizeData(data[key]) return norm_data
+ @property + def latest_revision_id(self) -> Optional[int]: + """ + Get the revision identifier for the most recent revision of the entity. + + @rtype: int or None if it cannot be determined + @raise NoWikibaseEntity: if the entity doesn't exist + """ + if not hasattr(self, '_revid'): + # fixme: unlike BasePage.latest_revision_id, this raises + # exception when entity is redirect, cannot use get_redirect + self.get() + return self._revid + + @latest_revision_id.setter + def latest_revision_id(self, value: Optional[int]) -> None: + self._revid = value + + @latest_revision_id.deleter + def latest_revision_id(self) -> None: + if hasattr(self, '_revid'): + del self._revid + def exists(self) -> bool: """Determine if an entity exists in the data repository.""" if not hasattr(self, '_content'): @@ -3865,6 +3888,8 @@ if 'missing' in self._content: raise pywikibot.NoWikibaseEntity(self)
+ self.latest_revision_id = self._content.get('lastrevid') + data = {} for key, cls in self.DATA_ATTRIBUTES.items(): value = cls.fromJSON(self._content.get(key, {}), self.repo) @@ -3884,7 +3909,10 @@ else: data = self._normalizeData(data)
- updates = self.repo.editEntity(self, data, **kwargs) + baserevid = getattr(self, '_revid', None) + + updates = self.repo.editEntity( + self, data, baserevid=baserevid, **kwargs)
# the attribute may have been unset in ItemPage if getattr(self, 'id', '-1') == '-1': @@ -4099,9 +4127,6 @@ # todo: raise a nicer exception here (T87345) raise pywikibot.NoPage(self)
- if 'lastrevid' in self._content: - self.latest_revision_id = self._content['lastrevid'] - if 'pageid' in self._content: self._pageid = self._content['pageid']
@@ -4113,7 +4138,12 @@
@property def latest_revision_id(self) -> int: - """Get revision identifier for the most recent revision of entity.""" + """ + Get the revision identifier for the most recent revision of the entity. + + @rtype: int + @raise pywikibot.exceptions.NoPage: if the entity doesn't exist + """ if not hasattr(self, '_revid'): self.get() return self._revid @@ -4124,6 +4154,7 @@
@latest_revision_id.deleter def latest_revision_id(self): + # fixme: this seems too destructive in comparison to the parent self.clear_cache()
@allow_asynchronous @@ -4149,12 +4180,8 @@ by bots that need to keep track of which saves were successful. @type callback: callable """ - if hasattr(self, '_revid'): - baserevid = self.latest_revision_id - else: - baserevid = None - - super().editEntity(data, baserevid=baserevid, **kwargs) + # kept for the decorator + super().editEntity(data, **kwargs)
def editLabels(self, labels, **kwargs): """
pywikibot-commits@lists.wikimedia.org