John Vandenberg has uploaded a new change for review.

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

Change subject: [WIP] Move date page auto-translation to date module
......................................................................

[WIP] Move date page auto-translation to date module

Change-Id: I09bd2cba01c101bab60406a28fe7eb9b2893f38c
---
M pywikibot/date.py
M pywikibot/titletranslate.py
M tests/date_tests.py
3 files changed, 69 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/04/225704/1

diff --git a/pywikibot/date.py b/pywikibot/date.py
index 163b720..bb04083 100644
--- a/pywikibot/date.py
+++ b/pywikibot/date.py
@@ -2354,6 +2354,50 @@
     return None, None
 
 
+def get_format_titles(format_name, value, family):
+    """
+    Get titles for format in all sites of a family.
+
+    @param format_name: name of format
+    @type format_name: str
+    @param value: date value (a year, date, ...)
+    @type value: object
+    @param family: family
+    @type family: Family
+    @rtype: dict of Link
+    """
+    import pywikibot
+    result = {}
+    format_data = formats[format_name]
+    for code in family.langs:
+        if code not in format_data:
+            result[code] = None
+            continue
+
+        title = format_data[code](value)
+
+        result[code] = pywikibot.Link(
+            title,
+            pywikibot.Site(code=code,
+                           fam=family))
+
+    return result
+
+
+def page_translations(page):
+    """Get equivalent title on all sites in family."""
+    import pywikibot
+    format_name, value = getAutoFormat(page.site.code, page.title())
+    if not format_name:
+        raise pywikibot.Error('Not a date page.')
+
+    pywikibot.log(
+        'page_translations: %s:%s was recognized as %s with value %d'
+        % (page.site, page.title(), format_name, value))
+
+    return get_format_titles(format_name, value, page.site.family)
+
+
 class FormatDate(object):
 
     """Format a date."""
diff --git a/pywikibot/titletranslate.py b/pywikibot/titletranslate.py
index c535815..bacbde6 100644
--- a/pywikibot/titletranslate.py
+++ b/pywikibot/titletranslate.py
@@ -89,22 +89,13 @@
     # existing interwiki links.
     if auto and page:
         # search inside all dictionaries for this link
-        sitelang = page.site.code
-        dictName, value = date.getAutoFormat(sitelang, page.title())
-        if dictName:
-            if True:
-                pywikibot.output(
-                    u'TitleTranslate: %s was recognized as %s with value %d'
-                    % (page.title(), dictName, value))
-                for entryLang, entry in date.formats[dictName].items():
-                    if entryLang not in site.languages():
-                        continue
-                    if entryLang != sitelang:
-                        if True:
-                            newname = entry(value)
-                            x = pywikibot.Link(
-                                newname,
-                                pywikibot.Site(code=entryLang,
-                                               fam=site.family))
-                            result.add(x)
+        try:
+            date_links = date.page_translations(page)
+        except pywikibot.Error:
+            pass
+        else:
+            del date_links[page.site.code]
+            result += [link for link in date_links.values()
+                       if link is not None]
+
     return list(result)
diff --git a/tests/date_tests.py b/tests/date_tests.py
index c9560ee..cce2fe0 100644
--- a/tests/date_tests.py
+++ b/tests/date_tests.py
@@ -9,6 +9,8 @@
 
 from datetime import datetime
 
+import pywikibot
+
 from pywikibot import date
 
 from tests.aspects import unittest, MetaTestCaseClass, TestCase
@@ -94,6 +96,20 @@
         self.assertEqual(date.get_month_delta(datetime(2014, 3, 31), 
datetime(2013, 3, 31)), -12)
 
 
+class TestTitleTranslations(TestCase):
+
+    """Test date auto-translation of titles."""
+
+    family = 'wikipedia'
+    code = 'en'
+
+    def test_basic(self):
+        """Test date.page_translations."""
+        page = pywikibot.Page(self.site, '2000')
+        translations = date.page_translations(page)
+        self.assertEqual(translations['en'].title, '2000')
+        self.assertEqual(translations['ru'].title, '2000 год')
+
 if __name__ == '__main__':
     try:
         unittest.main()

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I09bd2cba01c101bab60406a28fe7eb9b2893f38c
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jay...@gmail.com>

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

Reply via email to