jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/639893 )
Change subject: [bugfix] check for {{bots}}/{{nobots}} for original wiki text first ......................................................................
[bugfix] check for {{bots}}/{{nobots}} for original wiki text first
{{bots}}/{{nobots}} templates are checked for Page.text whenever the Page is saved but this does not work if the text was changed and these templates are removed. Therefore check for these templates with the first get() statement and save the result.
Also print the exception in category_redirect.py and decrease nested flow statements.
Tests added-
Bug: T262136 Change-Id: I6acaccaf654c32ec501a825b3fd2c7010e824583 --- M pywikibot/families/wikipedia_family.py M pywikibot/page/__init__.py M scripts/category_redirect.py M tests/page_tests.py 4 files changed, 49 insertions(+), 27 deletions(-)
Approvals: Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/families/wikipedia_family.py b/pywikibot/families/wikipedia_family.py index 3cc3f05..1217c68 100644 --- a/pywikibot/families/wikipedia_family.py +++ b/pywikibot/families/wikipedia_family.py @@ -202,6 +202,7 @@ 'he': ('בעבודה',), 'hr': ('Radovi',), 'sr': ('Радови у току', 'Рут',), + 'test': ('In use',), 'ur': ('زیر ترمیم',), 'zh': ('Inuse',), } diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py index 23ae11c..248ee77 100644 --- a/pywikibot/page/__init__.py +++ b/pywikibot/page/__init__.py @@ -615,12 +615,20 @@ """ if getattr(self, '_text', None) is not None: return self._text + + if hasattr(self, '_revid'): + return self.latest_revision.text + try: - return self.get(get_redirect=True) + self.get(get_redirect=True) except pywikibot.NoPage: # TODO: what other exceptions might be returned? return ''
+ # check botMayEdit on a very early state (T262136) + self._bot_may_edit = self.botMayEdit() + return self.latest_revision.text + @text.setter def text(self, value: str): """ @@ -1099,6 +1107,9 @@ to override this by setting ignore_bot_templates=True in user-config.py, or using page.put(force=True). """ + if hasattr(self, '_bot_may_edit'): + return self._bot_may_edit + if not hasattr(self, 'templatesWithParams'): return True
diff --git a/scripts/category_redirect.py b/scripts/category_redirect.py index 9b45398..2118a40 100755 --- a/scripts/category_redirect.py +++ b/scripts/category_redirect.py @@ -247,36 +247,36 @@ # race condition: someone else removed the redirect while we # were checking for it continue - if target.is_categorypage(): - # this is a hard-redirect to a category page - newtext = ('{{%(template)s|%(cat)s}}' - % {'cat': target.title(with_ns=False), - 'template': self.template_list[0]}) - try: - page.text = newtext - page.save(comment) - message = i18n.twtranslate( - self.site, 'category_redirect-log-added', { - 'ns': self.site.namespaces.TEMPLATE, - 'template': self.template_list[0], - 'oldcat': page.title(as_link=True, textlink=True) - }) - self.log_text.append(message) - except pywikibot.Error: - message = i18n.twtranslate( - self.site, 'category_redirect-log-add-failed', { - 'ns': self.site.namespaces.TEMPLATE, - 'template': self.template_list[0], - 'oldcat': page.title(as_link=True, textlink=True) - }) - self.log_text.append(message) - else: + + if not target.is_categorypage(): message = i18n.twtranslate( self.site, 'category_redirect-problem-hard', { 'oldcat': page.title(as_link=True, textlink=True), 'page': target.title(as_link=True, textlink=True) }) self.problems.append(message) + continue + + # this is a hard-redirect to a category page + newtext = ('{{%(template)s|%(cat)s}}' + % {'cat': target.title(with_ns=False), + 'template': self.template_list[0]}) + params = { + 'ns': self.site.namespaces.TEMPLATE, + 'template': self.template_list[0], + 'oldcat': page.title(as_link=True, textlink=True) + } + try: + page.text = newtext + page.save(comment) + message = i18n.twtranslate( + self.site, 'category_redirect-log-added', params) + self.log_text.append(message) + except pywikibot.Error: + pywikibot.exception() + message = i18n.twtranslate( + self.site, 'category_redirect-log-add-failed', params) + self.log_text.append(message)
def run(self): """Run the bot.""" diff --git a/tests/page_tests.py b/tests/page_tests.py index 7df0893..e4ef897 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -724,7 +724,7 @@ """Test Page.botMayEdit() method."""
family = 'wikipedia' - code = 'en' + code = 'test'
cached = True user = True @@ -869,13 +869,23 @@
@mock.patch.object(config, 'ignore_bot_templates', False) def test_bot_may_edit_inuse(self): - """Test with {{inuse}} that bot is allowed to edit.""" + """Test with {{in use}} that bot is allowed to edit.""" self.page._templates = [pywikibot.Page(self.site, 'Template:In use')]
# Ban all users including bots. self.page.text = '{{in use}}' self.assertFalse(self.page.botMayEdit())
+ def test_bot_may_edit_page(self): + """Test botMayEdit when changing content.""" + self.assertTrue(self.page.botMayEdit()) + self.page.text = '{{nobots}}' + self.assertTrue(self.page.botMayEdit()) + page = pywikibot.Page(self.site, 'Pywikibot nobots test') + self.assertFalse(page.botMayEdit()) + page.text = '' + self.assertFalse(page.botMayEdit()) +
class TestPageHistory(DefaultSiteTestCase):
pywikibot-commits@lists.wikimedia.org