jenkins-bot has submitted this change and it was merged.
Change subject: Properly handle alternate formatting of CFD template comments ......................................................................
Properly handle alternate formatting of CFD template comments
Change-Id: I6164c92796e275fcce30a1d4b19530bec28fccc9 --- M scripts/category.py M tests/__init__.py A tests/category_bot_tests.py 3 files changed, 83 insertions(+), 9 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved Cyde: Looks good to me, but someone else must approve jenkins-bot: Verified
diff --git a/scripts/category.py b/scripts/category.py index f2ae4d0..9fb3f62 100755 --- a/scripts/category.py +++ b/scripts/category.py @@ -542,13 +542,18 @@ if self.newcat and self.move_oldcat: if self.can_move_cats: if can_move_page: - oldcattitle = self.oldcat.title() + old_cat_title = self.oldcat.title() + old_cat_text = self.oldcat.text self.newcat = self.oldcat.move(self.newcat.title(), reason=self.comment, movetalkpage=can_move_talk) + # Copy over the article text so it can be stripped of + # CFD templates and re-saved. This is faster than + # reloading the article in place. + self.newcat.text = old_cat_text self._strip_cfd_templates() self.oldcat = pywikibot.Category(self.oldcat.site, - oldcattitle) + old_cat_title) else: if can_move_page: self._movecat() @@ -657,11 +662,11 @@ comma = self.site.mediawiki_message('comma-separator') authors = comma.join(self.oldcat.contributingUsers()) template_vars = {'oldcat': self.oldcat.title(), 'authors': authors} - comment = i18n.twtranslate(self.site, 'category-renamed', template_vars) + summary = i18n.twtranslate(self.site, 'category-renamed', template_vars) self.newcat.text = self.oldcat.text - self._strip_cfd_templates(comment) + self._strip_cfd_templates(summary)
- def _strip_cfd_templates(self, comment=None): + def _strip_cfd_templates(self, summary=None, commit=True): """Private function to strip out CFD templates from the new category.
The new category is saved. @@ -669,7 +674,8 @@ Do not use this function from outside the class. """ # Remove all substed CFD templates - REGEX = r"<!--BEGIN CFD TEMPLATE-->.*?<!--END CFD TEMPLATE-->" + REGEX = (r'<!--\s*BEGIN CFD TEMPLATE\s*-->.*?' + r'<!--\s*END CFD TEMPLATE\s*-->\n?') match = re.compile(REGEX, re.IGNORECASE | re.MULTILINE | re.DOTALL) self.newcat.text = match.sub('', self.newcat.text) @@ -680,10 +686,11 @@ self.newcat.text = match.sub('', self.newcat.text) # Remove leading whitespace self.newcat.text = self.newcat.text.lstrip() - if not comment: - comment = i18n.twtranslate(self.site, + if not summary: + summary = i18n.twtranslate(self.site, 'category-strip-cfd-templates') - self.newcat.save(comment) + if commit: + self.newcat.save(summary=summary)
def _movetalk(self): """Private function to move the category talk page. diff --git a/tests/__init__.py b/tests/__init__.py index 4ed946b..8d768ba 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -129,6 +129,7 @@ 'script', 'l10n', 'archivebot', + 'category_bot', 'checkimages', 'data_ingestion', 'deletionbot', diff --git a/tests/category_bot_tests.py b/tests/category_bot_tests.py new file mode 100644 index 0000000..293679b --- /dev/null +++ b/tests/category_bot_tests.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +"""Tests for the category bot script.""" +# +# (C) Pywikibot team, 2015-2016 +# +# Distributed under the terms of the MIT license. +# +from __future__ import absolute_import, unicode_literals + +__version__ = '$Id$' + +from scripts.category import CategoryMoveRobot + +from tests.aspects import unittest, DefaultSiteTestCase + + +class CfdActions(DefaultSiteTestCase): + + """Test CFD (Categories for deletion) actions.""" + + def test_strip_cfd_templates_does_nothing_when_no_templates(self): + """Test that when there are no CFD templates, the page text is not changed.""" + bot = CategoryMoveRobot(oldcat='Old', newcat='New') + bot.newcat.text = "Nothing should change.\n\nAnother line." + bot._strip_cfd_templates(commit=False) + self.assertEqual(bot.newcat.text, + "Nothing should change.\n\nAnother line.") + + def test_strip_cfd_templates_with_spaces_in_comments(self): + """Test that CFD templates with spaces in the syntax are removed properly.""" + self._runtest_strip_cfd_templates('<!-- BEGIN CFD TEMPLATE -->', + '<!-- END CFD TEMPLATE -->') + + def test_strip_cfd_templates_without_spaces_in_comments(self): + """Test that CFD templates without spaces in the syntax are removed properly.""" + self._runtest_strip_cfd_templates('<!--BEGIN CFD TEMPLATE-->', + '<!--END CFD TEMPLATE-->') + + def _runtest_strip_cfd_templates(self, template_start, template_end): + """Run a CFD template stripping test with the given CFD start/end templates.""" + bot = CategoryMoveRobot(oldcat='Old', newcat='New') + bot.newcat.text = '\n'.join(( + 'Preamble', + template_start, + 'Random text inside template', + 'Even another template: {{cfr-speedy}}', + template_end, + 'Footer stuff afterwards', + '', + '[[Category:Should remain]]' + )) + expected = '\n'.join(( + 'Preamble', + 'Footer stuff afterwards', + '', + '[[Category:Should remain]]' + )) + bot._strip_cfd_templates(commit=False) + self.assertEqual(bot.newcat.text, expected) + + +if __name__ == '__main__': + try: + unittest.main() + except SystemExit: + pass
pywikibot-commits@lists.wikimedia.org