changeset 3032:c242772754c5

stats macro which shows the preferred language distribution of the wiki users (Thanks to Nina Kuisma) (ported from 1.6)
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sun, 03 Feb 2008 20:22:23 +0100
parents 29b51bbecc2c
children 6d9bdb12163f
files MoinMoin/i18n/__init__.py MoinMoin/stats/languages.py MoinMoin/user.py MoinMoin/userprefs/prefs.py
diffstat 4 files changed, 113 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/i18n/__init__.py	Sun Feb 03 19:28:34 2008 +0100
+++ b/MoinMoin/i18n/__init__.py	Sun Feb 03 20:22:23 2008 +0100
@@ -305,11 +305,7 @@
 
     # Or try to return one of the user browser accepted languages, if it
     # is available on this wiki...
-    available = wikiLanguages()
-    if not request.cfg.language_ignore_browser:
-        for lang in browserLanguages(request):
-            if lang in available:
-                return lang
+    return get_browser_language(request)
 
     # Or return the wiki default language...
     if request.cfg.language_default in available:
@@ -319,6 +315,7 @@
         lang = 'en'
     return lang
 
+
 def wikiLanguages():
     """
     Return the available user languages in this wiki.
@@ -326,6 +323,7 @@
     """
     return languages
 
+
 def browserLanguages(request):
     """
     Return the accepted languages as set in the user browser.
@@ -353,3 +351,17 @@
                 fallback.append(baselang)
     return fallback
 
+def get_browser_language(request):
+    """
+    Return the language that is supported by wiki and what user browser
+    would prefer to get. Return nothing if there is no such language.
+
+    @param request: the request object
+    @rtype: string
+    @return: ISO language code, e.g. 'en'
+    """
+    available = wikiLanguages()
+    if not request.cfg.language_ignore_browser:
+        for lang in browserLanguages(request):
+            if lang in available:
+                return lang
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/stats/languages.py	Sun Feb 03 20:22:23 2008 +0100
@@ -0,0 +1,89 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - Language Statistics
+
+    @copyright: 2002-2004 Juergen Hermann <jh@web.de>,  # Part of the code is
+                2007 MoinMoin:ThomasWaldmann,           # from useragents.py
+                2007 Nina Kuisma <ninnnu@gmail.com>
+    @license: GNU GPL, see COPYING for details.
+"""
+
+
+from MoinMoin import user, wikiutil, i18n
+
+
+def linkto(pagename, request, params=''):
+    return used_languages(request)
+
+
+def get_data(request):
+    _ = request.getText
+    data = {}
+
+    users = user.getUserList(request)
+    for userID in users:
+        current_user = user.User(request, userID)
+        if current_user.language == u'':
+            # User is using <Browser setting>, attempting to look up if we've managed to store the real language...
+            try:
+                data[current_user.real_language] = data.get(current_user.real_language, 0) + 1
+            except AttributeError: # Couldn't find the used language at all...
+                data[u''] = data.get(u'', 0) + 1
+        else:
+            data[current_user.language] = data.get(current_user.language, 0) + 1
+    if u'' in data:
+        data[u'browser'] = data.pop(u'') # In case we have users whose languages aren't detectable.
+    data = [(cnt, current_user_language) for current_user_language, cnt in data.items()]
+    data.sort()
+    data.reverse()
+    return data
+
+
+def used_languages(request):
+    from MoinMoin.util.dataset import TupleDataset, Column
+    from MoinMoin.widget.browser import DataBrowserWidget
+
+    fmt = request.formatter
+    _ = request.getText
+
+    data = get_data(request)
+
+    sum = 0.0
+    for cnt, lang in data:
+        sum += cnt
+
+
+    languages = TupleDataset()
+    languages.columns = [Column('language', label=_("Language", formatted=False), align='left'),
+                         Column('value', label='%', align='right')]
+
+    cnt_printed = 0
+    data = data[:10]
+
+    # Preparing "<Browser setting>"
+    browserlang = _('<Browser setting>', formatted=False)
+    browserlang = browserlang[1:len(browserlang) - 1].capitalize()
+    if sum <> 0:
+        for cnt, lang in data:
+            try:
+                if lang == u'browser':
+                    languages.addRow((browserlang, "%(percent).2f (%(count)s)" % {
+                        'percent': 100.0 * cnt / sum,
+                        'count': cnt}))
+                else:
+                    lang = i18n.wikiLanguages()[lang]['x-language-in-english']
+                    languages.addRow((lang, "%(percent).2f (%(count)s)" % {
+                        'percent': 100.0 * cnt / sum,
+                        'count': cnt}))
+                cnt_printed += cnt
+            except UnicodeError:
+                pass
+    else: # If we don't have any users, we can safely assume that the only real user is the visitor (who is normally ignored, though) who is using "Browser setting"
+        languages.addRow((browserlang, "100% (1)"))
+
+    if sum > cnt_printed:
+        languages.addRow((_('Others', formatted=False), "%.2f" % (100 * (sum - cnt_printed) / sum)))
+
+    table = DataBrowserWidget(request)
+    table.setData(languages)
+    return table.toHTML()
--- a/MoinMoin/user.py	Sun Feb 03 19:28:34 2008 +0100
+++ b/MoinMoin/user.py	Sun Feb 03 20:22:23 2008 +0100
@@ -325,6 +325,7 @@
         #self.edit_cols = 80
         self.tz_offset = int(float(self._cfg.tz_offset) * 3600)
         self.language = ""
+        self.real_language = "" # In case user uses "Browser setting". For language-statistics
         self.loaded = False
         self.date_fmt = ""
         self.datetime_fmt = ""
--- a/MoinMoin/userprefs/prefs.py	Sun Feb 03 19:28:34 2008 +0100
+++ b/MoinMoin/userprefs/prefs.py	Sun Feb 03 20:22:23 2008 +0100
@@ -158,6 +158,11 @@
 
         # try to get the (optional) preferred language
         theuser.language = form.get('language', [''])[0]
+        if theuser.language == u'': # For language-statistics
+            from MoinMoin import i18n
+            theuser.real_language = i18n.get_browser_language(self.request)
+        else:
+            theuser.real_language = ''
 
         # I want to handle all inputs from user_form_fields, but
         # don't want to handle the cases that have already been coded
@@ -170,7 +175,7 @@
         already_handled = ['name', 'email',
                            'aliasname', 'edit_rows', 'editor_default',
                            'editor_ui', 'tz_offset', 'datetime_fmt',
-                           'theme_name', 'language', 'jid']
+                           'theme_name', 'language', 'real_language', 'jid']
         for field in self.cfg.user_form_fields:
             key = field[0]
             if ((key in self.cfg.user_form_disable)