jenkins-bot has submitted this change and it was merged.

Change subject: New site method expand_text() which uses api to parse text.
......................................................................


New site method expand_text() which uses api to parse text.

Change Page.expand_text() by calling this site method.
Change Site.getcurrenttimestamp() by calling this site method.

Change-Id: I88ad007a015787d93d2c00fe4b4acc1956ff2d0c
---
M pywikibot/page.py
M pywikibot/site.py
2 files changed, 40 insertions(+), 14 deletions(-)

Approvals:
  Ricordisamoa: Looks good to me, but someone else must approve
  Merlijn van Deen: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/page.py b/pywikibot/page.py
index 92607f7..754aba9 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -419,14 +419,22 @@
         """
         return self.properties(force=force).get('defaultsort')
 
-    def expand_text(self, refresh=False):
-        """Return the page text with all templates expanded."""
-        if not hasattr(self, "_expanded_text") or (
-                self._expanded_text is None) or refresh:
-            req = pywikibot.data.api.Request(
-                action="expandtemplates", text=self.text,
-                title=self.title(withSection=False), site=self.site)
-            self._expanded_text = req.submit()["expandtemplates"]["*"]
+    @deprecate_arg('refresh', 'force')
+    def expand_text(self, force=False, includecomments=False):
+        """Return the page text with all templates and parser words expanded.
+
+        @param force: force updating from the live site
+        @param includecomments: Also strip comments if includecomments
+            parameter is not True.
+        @return: unicode or None
+
+        """
+        if not hasattr(self, '_expanded_text') or (
+                self._expanded_text is None) or force:
+            self._expanded_text = self.site.expand_text(
+                self.text,
+                title=self.title(withSection=False),
+                includecomments=includecomments)
         return self._expanded_text
 
     def userName(self):
diff --git a/pywikibot/site.py b/pywikibot/site.py
index fccf2f0..0132716 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -927,18 +927,36 @@
 
         return self._months_names
 
+    def expand_text(self, text, title=None, includecomments=None):
+        """ Parses the given text for preprocessing and rendering
+        e.g expand templates and strip comments if includecomments
+        parameter is not True. Keeps text inside
+        <nowiki></nowiki> tags unchanges etc. Can be used to parse
+        magic parser words like {{CURRENTTIMESTAMP}}.
+
+        @param text: text to be expanded
+        @type text: unicode
+        @param title: page title without section
+        @type title: unicode
+        @param includecomments: if True do not strip comments
+        @type includecomments: bool
+        @return: unicode
+
+        """
+        req = api.Request(site=self, action='expandtemplates', text=text)
+        if title is not None:
+            req['title'] = title
+        if includecomments is True:
+            req['includecomments'] = u''
+        return req.submit()['expandtemplates']['*']
+
     def getcurrenttimestamp(self):
         """Return server time, {{CURRENTTIMESTAMP}}, as a string.
 
         Format is 'yyyymmddhhmmss'
 
         """
-        r = api.Request(site=self,
-                        action="parse",
-                        text="{{CURRENTTIMESTAMP}}",
-                        contentmodel="wikitext")
-        result = r.submit()
-        return re.search('\d+', result['parse']['text']['*']).group()
+        return self.expand_text("{{CURRENTTIMESTAMP}}")
 
     def getcurrenttime(self):
         """Return a Timestamp object representing the current server time."""

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I88ad007a015787d93d2c00fe4b4acc1956ff2d0c
Gerrit-PatchSet: 14
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl>
Gerrit-Reviewer: Pyfisch <pyfi...@gmail.com>
Gerrit-Reviewer: Ricordisamoa <ricordisa...@live.it>
Gerrit-Reviewer: Russell Blau <russb...@imapmail.org>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to