jenkins-bot has submitted this change and it was merged.
Change subject: [FEAT] disambredir: Reimplement using Bot class
......................................................................
[FEAT] disambredir: Reimplement using Bot class
This uses the Bot class and adds a test case for it simulating the work on one
page.
Change-Id: I90c43f82a51b55e98358e9139cf3c5e2e2ae6588
---
M scripts/disambredir.py
A tests/disambredir_tests.py
M tests/utils.py
3 files changed, 172 insertions(+), 35 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/disambredir.py b/scripts/disambredir.py
index 2bc2d8c..741081f 100755
--- a/scripts/disambredir.py
+++ b/scripts/disambredir.py
@@ -22,7 +22,7 @@
import pywikibot
from pywikibot import i18n, textlib, pagegenerators
-from pywikibot.bot import InteractiveReplace
+from pywikibot.bot import CurrentPageBot, InteractiveReplace
msg = {
'ar': u'تغيير التحويلات في صفحة توضيح',
@@ -40,27 +40,33 @@
}
-def workon(page, links):
- """Execute treat for the given page which is linking to the given links."""
- text = page.get()
- # Show the title of the page we're working on.
- # Highlight the title in purple.
- pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
- % page.title())
- for page2 in links:
- try:
- target = page2.getRedirectTarget()
- except (pywikibot.Error, pywikibot.SectionError):
- continue
+class DisambiguationRedirectBot(CurrentPageBot):
+
+ """Change redirects from disambiguation pages."""
+
+ def _create_callback(self, old, new):
replace_callback = InteractiveReplace(
- page2, target, default='n', automatic_quit=False,
yes_shortcut=True)
+ old, new, default='n', automatic_quit=False, yes_shortcut=True)
replace_callback.allow_replace_label = True
- # TODO: Work on all links at the same time (would mean that the user
- # doesn't get them ordered like in links but how they appear in the
page)
- text = textlib.replace_links(text, replace_callback, page.site)
- if text != page.get():
- comment = i18n.translate(page.site, msg, fallback=True)
- page.put(text, comment)
+ return replace_callback
+
+ def treat_page(self):
+ """Iterate over the linked pages and replace redirects
conditionally."""
+ text = self.current_page.text
+ for linked_page in self.current_page.linkedPages():
+ try:
+ target = linked_page.getRedirectTarget()
+ except (pywikibot.Error, pywikibot.SectionError):
+ continue
+ # TODO: Work on all links at the same time (would mean that the
user
+ # doesn't get them ordered like in links but how they appear in
the page)
+ text = textlib.replace_links(
+ text, self._create_callback(linked_page, target),
+ self.current_page.site)
+
+ if text != self.current_page.get():
+ summary = i18n.translate(self.current_page.site, msg,
fallback=True)
+ self.put_current(text, summary=summary)
def main(*args):
@@ -87,21 +93,8 @@
generator = pagegenerators.CategorizedPageGenerator(
mysite.disambcategory(), start=start, content=True, namespaces=[0])
- # only work on articles
- pagestodo = []
- pagestoload = []
- for page in generator:
- if page.isRedirectPage():
- continue
- linked = page.linkedPages()
- pagestodo.append((page, linked))
- pagestoload += linked
- if len(pagestoload) > 49:
- pagestoload = pagegenerators.PreloadingGenerator(pagestoload)
- for page, links in pagestodo:
- workon(page, links)
- pagestoload = []
- pagestodo = []
+ bot = DisambiguationRedirectBot(generator=generator)
+ bot.run()
if __name__ == "__main__":
diff --git a/tests/disambredir_tests.py b/tests/disambredir_tests.py
new file mode 100644
index 0000000..5b34e47
--- /dev/null
+++ b/tests/disambredir_tests.py
@@ -0,0 +1,135 @@
+# -*- coding: utf-8 -*-
+"""
+DisambigurationRedirectBot test.
+
+These tests write to the wiki.
+"""
+#
+# (C) Pywikibot team, 2015
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import unicode_literals
+
+__version__ = '$Id$'
+#
+
+import pywikibot
+
+from scripts import disambredir
+
+from tests.aspects import unittest, TestCase
+from tests.utils import fixed_generator
+
+
+class TestDisambigurationRedirectBot(TestCase):
+
+ """
+ Test cases for DisambigurationRedirectBot.
+
+ It patches the bot instances in such a way that there is no user
interaction
+ required and it can be run autonomously. It also does not actually write to
+ the wiki.
+ """
+
+ family = 'wikipedia'
+ code = 'test'
+
+ def _patch_create_callback(self, choice):
+ """
+ Patch _create_callback in the Bot instance.
+
+ It still creates a InteractiveReplace instance but returns another
+ function which only calls 'handle_answer' from that InteractiveReplace
+ instance if the link matches.
+ """
+ def _patched_creator(old, new):
+ """Create the own callback callable."""
+ def _patched_callback(link, text, groups, rng):
+ """Return the result from handle_answer."""
+ if callback._old == link:
+ return callback.handle_answer(choice, link)
+ else:
+ return None
+
+ callback = original(old, new)
+ return _patched_callback
+
+ original = self.bot._create_callback
+ self.bot._create_callback = _patched_creator
+
+ @classmethod
+ def setUpClass(cls):
+ """Initialize page variable."""
+ super(TestDisambigurationRedirectBot, cls).setUpClass()
+ cls.page = pywikibot.Page(cls.site, 'User:BobBot/Test disambig')
+
+ def setUp(self):
+ """Set up the test page."""
+ def _save_page(*args, **kwargs):
+ self.assertIs(args[0], self.page)
+ self.save_called = True
+ return # avert actually saving
+
+ super(TestDisambigurationRedirectBot, self).setUp()
+ self.page.text = ('[[User:BobBot/Redir#Foo|Bar]]\n'
+ '[[User:BobBot/Redir|Baz]]\n'
+ '[[Main Page|Label]]\n')
+ self.bot = disambredir.DisambiguationRedirectBot(generator=[self.page])
+ self.bot.options['always'] = True
+ # Patch the page and bot to not actually save anything
+ self.page.linkedPages = fixed_generator(
+ [pywikibot.Page(self.site, 'User:BobBot/Redir'),
+ pywikibot.Page(self.site, 'Main Page')])
+ self.save_called = False
+ self.bot._save_page = _save_page
+
+ def test_unchanged(self):
+ """Test no change."""
+ self._patch_create_callback('n')
+ self.assertFalse(self.save_called)
+ self.bot.run()
+ self.assertEqual(self.page.text,
+ '[[User:BobBot/Redir#Foo|Bar]]\n'
+ '[[User:BobBot/Redir|Baz]]\n'
+ '[[Main Page|Label]]\n')
+ # No changes needed, won't call the save method
+ self.assertFalse(self.save_called)
+
+ def test_unlink(self):
+ """Test unlinking."""
+ self._patch_create_callback('u')
+ self.assertFalse(self.save_called)
+ self.bot.run()
+ self.assertEqual(self.page.text,
+ 'Bar\nBaz\n[[Main Page|Label]]\n')
+ self.assertTrue(self.save_called)
+
+ def test_replace_target(self):
+ """Test replacing just target page."""
+ self._patch_create_callback('t')
+ self.assertFalse(self.save_called)
+ self.bot.run()
+ self.assertEqual(self.page.text,
+ '[[Main Page#Foo|Bar]]\n'
+ '[[Main Page|Baz]]\n'
+ '[[Main Page|Label]]\n')
+ self.assertTrue(self.save_called)
+
+ def test_replace_all(self):
+ """Test replacing target and label."""
+ self._patch_create_callback('l')
+ self.assertFalse(self.save_called)
+ self.bot.run()
+ self.assertEqual(self.page.text,
+ '[[Main Page#Foo|Main Page]]\n'
+ '[[Main Page]]\n'
+ '[[Main Page|Label]]\n')
+ self.assertTrue(self.save_called)
+
+
+if __name__ == '__main__':
+ try:
+ unittest.main()
+ except SystemExit:
+ pass
diff --git a/tests/utils.py b/tests/utils.py
index 27c7222..dabb03c 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -102,6 +102,15 @@
return cls
+def fixed_generator(iterable):
+ """Return a dummy generator ignoring all parameters."""
+ def gen(*args, **kwargs):
+ for item in iterable:
+ yield item
+
+ return gen
+
+
class DryParamInfo(dict):
"""Dummy class to use instead of L{pywikibot.data.api.ParamInfo}."""
--
To view, visit https://gerrit.wikimedia.org/r/217729
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I90c43f82a51b55e98358e9139cf3c5e2e2ae6588
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Ricordisamoa <[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