John Vandenberg has submitted this change and it was merged.

Change subject: Allow all action=edit optional arguments
......................................................................


Allow all action=edit optional arguments

APISite.editpage prevented some valid uses of action=edit,
such as using appendtext, prependtext, and undo.
It also required an edit summary, which is not required by the API.

Bug: T57054
Change-Id: I460b2c0aa45b2d65effcb2420986ef4118973197
---
M pywikibot/site.py
M tests/edit_tests.py
2 files changed, 67 insertions(+), 18 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, but someone else must approve
  XZise: Looks good to me, but someone else must approve
  Ladsgroup: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/site.py b/pywikibot/site.py
index 572e489..4d3e34c 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -4231,16 +4231,18 @@
         "protectedtitle": LockedNoPage,
         "cascadeprotected": CascadeLockedPage,
     }
+    _ep_text_overrides = set(['appendtext', 'prependtext', 'undo'])
 
     @must_be(group='user')
-    def editpage(self, page, summary, minor=True, notminor=False,
+    def editpage(self, page, summary=None, minor=True, notminor=False,
                  bot=True, recreate=True, createonly=False, nocreate=False,
-                 watch=None):
-        """Submit an edited Page object to be saved to the wiki.
+                 watch=None, **kwargs):
+        """Submit an edit to be saved to the wiki.
 
-        @param page: The Page to be saved; its .text property will be used
+        @param page: The Page to be saved.
+            By default its .text property will be used
             as the new text to be saved to the wiki
-        @param summary: the edit summary (required!)
+        @param summary: the edit summary
         @param minor: if True (default), mark edit as minor
         @param notminor: if True, override account preferences to mark edit
             as non-minor
@@ -4257,18 +4259,53 @@
             * preferences: use the preference settings (default)
             * nochange: don't change the watchlist
         @param bot: if True, mark edit with bot flag
+        @kwarg text: Overrides Page.text
+        @type text: unicode
+        @kwarg section: Edit an existing numbered section or
+            a new section ('new')
+        @type section: int or str
+        @kwarg prependtext: Prepend text. Overrides Page.text
+        @type text: unicode
+        @kwarg appendtext: Append text. Overrides Page.text.
+        @type text: unicode
+        @kwarg undo: Revision id to undo. Overrides Page.text
+        @type undo: int
         @return: True if edit succeeded, False if it failed
-
+        @raises Error: No text to be saved
+        @raises NoPage: recreate is disabled and page does not exist
         """
-        text = page.text
-        if text is None:
-            raise Error("editpage: no text to be saved")
-        try:
-            lastrev = page.latest_revision
-        except NoPage:
-            lastrev = None
-            if not recreate:
-                raise
+        basetimestamp = True
+        text_overrides = self._ep_text_overrides.intersection(kwargs.keys())
+
+        if text_overrides:
+            if 'text' in kwargs:
+                raise ValueError('text can not be used with any of %s'
+                                 % ', '.join(text_overrides))
+            if len(text_overrides) > 1:
+                raise ValueError('Multiple text overrides used: %s'
+                                 % ', '.join(text_overrides))
+            text = None
+            basetimestamp = False
+        elif 'text' in kwargs:
+            text = kwargs.pop('text')
+            if 'section' in kwargs and kwargs['section'] == 'new':
+                basetimestamp = False
+        elif 'section' in kwargs:
+            raise ValueError('text must be used with section')
+        else:
+            text = page.text
+            if text is None:
+                raise Error("editpage: no text to be saved")
+
+        if basetimestamp or not recreate:
+            try:
+                lastrev = page.latest_revision
+                basetimestamp = lastrev.timestamp
+            except NoPage:
+                basetimestamp = False
+                if not recreate:
+                    raise
+
         token = self.tokens['edit']
         if bot is None:
             bot = ("bot" in self.userinfo["rights"])
@@ -4277,10 +4314,11 @@
                       text=text, token=token, summary=summary, bot=bot,
                       recreate=recreate, createonly=createonly,
                       nocreate=nocreate, minor=minor,
-                      notminor=not minor and notminor)
+                      notminor=not minor and notminor,
+                      **kwargs)
 
-        if lastrev is not None:
-            params['basetimestamp'] = lastrev.timestamp
+        if basetimestamp and 'basetimestamp' not in kwargs:
+            params['basetimestamp'] = basetimestamp
 
         watch_items = set(["watch", "unwatch", "preferences", "nochange"])
         if watch in watch_items:
diff --git a/tests/edit_tests.py b/tests/edit_tests.py
index 7a4cf5d..db5cfee 100644
--- a/tests/edit_tests.py
+++ b/tests/edit_tests.py
@@ -60,6 +60,17 @@
         self.assertEqual(p.text, ts)
         self.assertTrue(called_back)
 
+    def test_appendtext(self):
+        """Test writing to a page without preloading the .text."""
+        ts = str(time.time())
+        p = pywikibot.Page(self.site, 'User:John Vandenberg/appendtext test')
+        self.assertFalse(hasattr(p, '_text'))
+        p.site.editpage(p, appendtext=ts)
+        self.assertFalse(hasattr(p, '_text'))
+        p = pywikibot.Page(self.site, 'User:John Vandenberg/appendtext test')
+        self.assertTrue(p.text.endswith(ts))
+        self.assertTrue(p.text != ts)
+
 if __name__ == '__main__':
     try:
         unittest.main()

-- 
To view, visit https://gerrit.wikimedia.org/r/216529
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I460b2c0aa45b2d65effcb2420986ef4118973197
Gerrit-PatchSet: 7
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: Mpaa <[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

Reply via email to