XZise has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/201446

Change subject: [IMPROV] lonelypages: Simplify orphan configuration
......................................................................

[IMPROV] lonelypages: Simplify orphan configuration

Instead of storing the orphan templates separatly (one mapping for the
used template and one regex for the aliases) it's dynamically creating
both depending on the given aliases and names. It'll also automatically
choose all template namespace prefixes in the given language. The regex
to match the template will be also compiled only once and not for each
page.

Change-Id: Ie9c86c196ef33ff7bb336bb2bac9e688b41938a3
---
M scripts/lonelypages.py
M tests/script_tests.py
2 files changed, 62 insertions(+), 19 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/46/201446/1

diff --git a/scripts/lonelypages.py b/scripts/lonelypages.py
index af36aa1..3455786 100644
--- a/scripts/lonelypages.py
+++ b/scripts/lonelypages.py
@@ -52,6 +52,7 @@
     '&params;':     pagegenerators.parameterHelp,
 }
 
+# TODO: Deprecated
 template = {
     'ar': u'{{يتيمة|تاريخ={{نسخ:اسم_شهر}} {{نسخ:عام}}}}',
     'ca': u'{{Orfe|date={{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}}}',
@@ -61,6 +62,7 @@
     'zh': u'{{subst:Orphan/auto}}',
 }
 
+# TODO: Deprecated
 # Use regex to prevent to put the same template twice!
 exception_regex = {
     'ar': [u'\\{\\{(?:قالب:|)(يتيمة)[\\|\\}]'],
@@ -70,6 +72,50 @@
     'it': [r'\{\{(?:template:|)(o|a)[\|\}]'],
     'ja': [u'\\{\\{(?:template:|)(孤立)[\\|\\}]'],
     'zh': [r'\{\{(?:template:|)(orphan)[\|\}]'],
+}
+
+
+class OrphanTemplate(object):
+
+    """The orphan template configuration."""
+
+    def __init__(self, name, parameters, aliases=None, subst=False):
+        self._name = name
+        if not aliases:
+            aliases = []
+        elif not subst:
+            aliases = list(aliases) + [name]
+        else:
+            name = 'subst:' + name
+        if parameters:
+            name += '|' + parameters
+        self._template = u'{{' + name + '}}'
+        self._names = frozenset(aliases)
+
+    def generate(self, site):
+        """Create regex from the given names."""
+        template_ns = site.namespaces[10]
+        # TODO: Add redirects to self.names too
+        if not pywikibot.Page(site, self._name, template_ns.id).exists():
+            raise ValueError(u'Orphan template "{0}" does not exist on '
+                             u'"{1}".'.format(self._name, site))
+        for name in self._names:
+            if not pywikibot.Page(site, name, template_ns.id).exists():
+                pywikibot.warning(u'Orphan template alias "{0}" does not exist 
'
+                                  u'on "{1}"'.format(name, site))
+        return re.compile(r'\{\{(?:' + u':|'.join(template_ns) + '|)(' +
+                          u'|'.join(re.escape(name) for name in self._names) +
+                          r')[\|\}]', re.I)
+
+
+# The orphan template names in the different languages.
+templates = {
+    'ar': OrphanTemplate(u'ﻲﺘﻴﻣﺓ', u'ﺕﺍﺮﻴﺧ={{ﻦﺴﺧ:ﺎﺴﻣ_ﺶﻫﺭ}} {{ﻦﺴﺧ:ﻉﺎﻣ}}'),
+    'ca': OrphanTemplate('Orfe', 'date={{subst:CURRENTMONTHNAME}} 
{{subst:CURRENTYEAR}}'),
+    'en': OrphanTemplate('Orphan', 'date={{subst:CURRENTMONTHNAME}} 
{{subst:CURRENTYEAR}}', ['wi']),
+    'it': OrphanTemplate('O', '||mese={{subst:CURRENTMONTHNAME}} 
{{subst:CURRENTYEAR}}', ['a']),
+    'ja': OrphanTemplate(u'孤立', '{{subst:DATE}}'),
+    'zh': OrphanTemplate(u'Orphan/auto', '', ['orphan'], True),
 }
 
 
@@ -94,11 +140,19 @@
             self.site, 'lonelypages-comment-add-template')
         self.commentdisambig = i18n.twtranslate(
             self.site, 'lonelypages-comment-add-disambig-template')
-        self.template = i18n.translate(self.site, template)
-        self.exception = i18n.translate(self.site, exception_regex)
-        if self.template is None or self.exception is None:
+        orphan_template = i18n.translate(self.site, templates)
+        if orphan_template is None:
             pywikibot.showHelp()
             sys.exit(u'Missing configuration for site %s' % self.site)
+        try:
+            self._exception = orphan_template.generate(self.site)
+        except ValueError as e:
+            pywikibot.showHelp()
+            pywikibot.error(e)
+            sys.exit(u'Missing configuration for site %s' % self.site)
+        else:
+            self.exception = [self._exception.pattern]  # for backwards 
compatibility
+            self.template = orphan_template._template
         # DisambigPage part
         if self.getOption('disambigPage') is not None:
             self.disambigpage = pywikibot.Page(self.site, 
self.getOption('disambigPage'))
@@ -134,9 +188,7 @@
         if not self.enable_page():
             pywikibot.output('The bot is disabled')
             return
-        # Main Loop
-        for page in self.generator:
-            self.treat(page)
+        super(LonelyPagesBot, self).run()
 
     def treat(self, page):
         pywikibot.output(u"Checking %s..." % page.title())
@@ -157,18 +209,10 @@
             except pywikibot.IsRedirectPage:
                 pywikibot.output(u"%s is a redirect! Skip..." % page.title())
                 return
-            # I've used a loop in a loop. If I use continue in the second loop,
-            # it won't do anything in the first. So let's create a variable to
-            # avoid this problem.
-            for regexp in self.exception:
-                res = re.findall(regexp, oldtxt.lower())
-                # Found a template! Let's skip the page!
-                if res != []:
-                    pywikibot.output(
-                        u'Your regex has found something in %s, skipping...'
-                        % page.title())
-                    break
-            else:
+            if self._exception.search(oldtxt):
+                pywikibot.output(
+                    u'Your regex has found something in %s, skipping...'
+                    % page.title())
                 return
             if page.isDisambig() and self.getOption('disambigPage') is not 
None:
                 pywikibot.output(u'%s is a disambig page, report..'
diff --git a/tests/script_tests.py b/tests/script_tests.py
index 7d7f430..5fd375e 100644
--- a/tests/script_tests.py
+++ b/tests/script_tests.py
@@ -333,7 +333,6 @@
                                'script_wui',      # Error on any user except 
DrTrigonBot
                                'upload',          # raises custom ValueError
                                ] + failed_dep_script_list or (
-                    (config.family != 'wikipedia' and script_name == 
'lonelypages') or
                     (config.family == 'wikipedia' and script_name == 
'disambredir') or
                     (config.family == 'wikipedia' and config.mylang != 'en' 
and script_name == 'misspelling')):
                 dct[test_name] = unittest.expectedFailure(dct[test_name])

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie9c86c196ef33ff7bb336bb2bac9e688b41938a3
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to