changeset 4146:3b36f0cfc656

(Re)moved normalizePagename from Request into wikiutil library
author Florian Krupicka <florian.krupicka@googlemail.com>
date Sat, 07 Jun 2008 19:55:27 +0200
parents 5bd2da7053a3
children 95e9e24da409
files MoinMoin/_tests/test_wikiutil.py MoinMoin/action/CopyPage.py MoinMoin/action/PackagePages.py MoinMoin/action/RenamePage.py MoinMoin/formatter/__init__.py MoinMoin/mail/mailimport.py MoinMoin/request/__init__.py MoinMoin/request/_tests/test_request.py MoinMoin/theme/__init__.py MoinMoin/user.py MoinMoin/wikiutil.py
diffstat 11 files changed, 133 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Sat Jun 07 19:55:27 2008 +0200
@@ -954,4 +954,86 @@
         assert relative_page == wikiutil.RelPageName(current_page, absolute_page)
 
 
+class TestNormalizePagename(object):
+
+    def testPageInvalidChars(self):
+        """ request: normalize pagename: remove invalid unicode chars
+
+        Assume the default setting
+        """
+        test = u'\u0000\u202a\u202b\u202c\u202d\u202e'
+        expected = u''
+        result = wikiutil.normalize_pagename(test)
+        assert result == expected
+
+    def testNormalizeSlashes(self):
+        """ request: normalize pagename: normalize slashes """
+        cases = (
+            (u'/////', u''),
+            (u'/a', u'a'),
+            (u'a/', u'a'),
+            (u'a/////b/////c', u'a/b/c'),
+            (u'a b/////c d/////e f', u'a b/c d/e f'),
+            )
+        for test, expected in cases:
+            result = wikiutil.normalize_pagename(test)
+            assert result == expected
+
+    def testNormalizeWhitespace(self):
+        """ request: normalize pagename: normalize whitespace """
+        cases = (
+            (u'         ', u''),
+            (u'    a', u'a'),
+            (u'a    ', u'a'),
+            (u'a     b     c', u'a b c'),
+            (u'a   b  /  c    d  /  e   f', u'a b/c d/e f'),
+            # All 30 unicode spaces
+            (config.chars_spaces, u''),
+            )
+        for test, expected in cases:
+            result = wikiutil.normalize_pagename(test)
+            assert result == expected
+
+    def testUnderscoreTestCase(self):
+        """ request: normalize pagename: underscore convert to spaces and normalized
+
+        Underscores should convert to spaces, then spaces should be
+        normalized, order is important!
+        """
+        cases = (
+            (u'         ', u''),
+            (u'  a', u'a'),
+            (u'a  ', u'a'),
+            (u'a  b  c', u'a b c'),
+            (u'a  b  /  c  d  /  e  f', u'a b/c d/e f'),
+            )
+        for test, expected in cases:
+            result = wikiutil.normalize_pagename(test)
+            assert result == expected
+
+class TestGroupPages(object):
+
+    def setup_method(self, method):
+        self.config = self.TestConfig(page_group_regex=r'.+Group')
+
+    def teardown_method(self, method):
+        del self.config
+
+    def testNormalizeGroupName(self):
+        """ request: normalize pagename: restrict groups to alpha numeric Unicode
+
+        Spaces should normalize after invalid chars removed!
+        """
+        cases = (
+            # current acl chars
+            (u'Name,:Group', u'NameGroup'),
+            # remove than normalize spaces
+            (u'Name ! @ # $ % ^ & * ( ) + Group', u'Name Group'),
+            )
+        for test, expected in cases:
+            # validate we are testing valid group names
+            if wikiutil.isGroupPage(test, self.request.cfg):
+                result = wikiutil.normalize_pagename(test)
+                assert result == expected
+
 coverage_modules = ['MoinMoin.wikiutil']
--- a/MoinMoin/action/CopyPage.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/action/CopyPage.py	Sat Jun 07 19:55:27 2008 +0200
@@ -47,7 +47,7 @@
         _ = self._
         form = self.form
         newpagename = form.get('newpagename', [u''])[0]
-        newpagename = self.request.normalizePagename(newpagename)
+        newpagename = wikiutil.normalize_pagename(newpagename, self.cfg)
         comment = form.get('comment', [u''])[0]
         comment = wikiutil.clean_input(comment)
 
--- a/MoinMoin/action/PackagePages.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/action/PackagePages.py	Sat Jun 07 19:55:27 2008 +0200
@@ -187,7 +187,7 @@
 
         pages = []
         for pagename in pagelist:
-            pagename = self.request.normalizePagename(pagename)
+            pagename = wikiutil.normalize_pagename(pagename, self.request.cfg)
             if pagename:
                 page = Page(self.request, pagename)
                 if page.exists() and self.request.user.may.read(pagename):
--- a/MoinMoin/action/RenamePage.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/action/RenamePage.py	Sat Jun 07 19:55:27 2008 +0200
@@ -46,7 +46,7 @@
         _ = self._
         form = self.form
         newpagename = form.get('newpagename', [u''])[0]
-        newpagename = self.request.normalizePagename(newpagename)
+        newpagename = wikiutil.normalize_pagename(newpagename, self.cfg)
         comment = form.get('comment', [u''])[0]
         comment = wikiutil.clean_input(comment)
 
--- a/MoinMoin/formatter/__init__.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/formatter/__init__.py	Sat Jun 07 19:55:27 2008 +0200
@@ -94,7 +94,7 @@
             return ''
         if not pagename and page:
             pagename = page.page_name
-        pagename = self.request.normalizePagename(pagename)
+        pagename = wikiutil.normalize_pagename(pagename, self.request.cfg)
         if pagename and pagename not in self.pagelinks:
             self.pagelinks.append(pagename)
 
--- a/MoinMoin/mail/mailimport.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/mail/mailimport.py	Sat Jun 07 19:55:27 2008 +0200
@@ -184,7 +184,7 @@
         generate_summary = True
         pagename = pagename[1:].lstrip()
 
-    pagename = request.normalizePagename(pagename)
+    pagename = wikiutil.normalize_pagename(pagename, request.cfg)
 
     if choose_html and msg['html']:
         content = "{{{#!html\n%s\n}}}" % msg['html'].replace("}}}", "} } }")
--- a/MoinMoin/request/__init__.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/request/__init__.py	Sat Jun 07 19:55:27 2008 +0200
@@ -910,47 +910,6 @@
         name = u'/'.join(decoded)
         return name
 
-    def normalizePagename(self, name):
-        """ Normalize page name
-
-        Prevent creating page names with invisible characters or funny
-        whitespace that might confuse the users or abuse the wiki, or
-        just does not make sense.
-
-        Restrict even more group pages, so they can be used inside acl lines.
-
-        @param name: page name, unicode
-        @rtype: unicode
-        @return: decoded and sanitized page name
-        """
-        # Strip invalid characters
-        name = config.page_invalid_chars_regex.sub(u'', name)
-
-        # Split to pages and normalize each one
-        pages = name.split(u'/')
-        normalized = []
-        for page in pages:
-            # Ignore empty or whitespace only pages
-            if not page or page.isspace():
-                continue
-
-            # Cleanup group pages.
-            # Strip non alpha numeric characters, keep white space
-            if wikiutil.isGroupPage(self, page):
-                page = u''.join([c for c in page
-                                 if c.isalnum() or c.isspace()])
-
-            # Normalize white space. Each name can contain multiple
-            # words separated with only one space. Split handle all
-            # 30 unicode spaces (isspace() == True)
-            page = u' '.join(page.split())
-
-            normalized.append(page)
-
-        # Assemble components into full pagename
-        name = u'/'.join(normalized)
-        return name
-
     def read(self, n=None):
         """ Read n bytes from input stream. """
         raise NotImplementedError
@@ -1173,7 +1132,7 @@
                     path = '/' + path
 
             if path.startswith('/'):
-                pagename = self.normalizePagename(path)
+                pagename = wikiutil.normalize_pagename(path, self.cfg)
             else:
                 pagename = None
 
--- a/MoinMoin/request/_tests/test_request.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/request/_tests/test_request.py	Sat Jun 07 19:55:27 2008 +0200
@@ -13,89 +13,6 @@
 
 from MoinMoin.request import HeadersAlreadySentException
 
-class TestNormalizePagename(object):
-
-    def testPageInvalidChars(self):
-        """ request: normalize pagename: remove invalid unicode chars
-
-        Assume the default setting
-        """
-        test = u'\u0000\u202a\u202b\u202c\u202d\u202e'
-        expected = u''
-        result = self.request.normalizePagename(test)
-        assert result == expected
-
-    def testNormalizeSlashes(self):
-        """ request: normalize pagename: normalize slashes """
-        cases = (
-            (u'/////', u''),
-            (u'/a', u'a'),
-            (u'a/', u'a'),
-            (u'a/////b/////c', u'a/b/c'),
-            (u'a b/////c d/////e f', u'a b/c d/e f'),
-            )
-        for test, expected in cases:
-            result = self.request.normalizePagename(test)
-            assert result == expected
-
-    def testNormalizeWhitespace(self):
-        """ request: normalize pagename: normalize whitespace """
-        cases = (
-            (u'         ', u''),
-            (u'    a', u'a'),
-            (u'a    ', u'a'),
-            (u'a     b     c', u'a b c'),
-            (u'a   b  /  c    d  /  e   f', u'a b/c d/e f'),
-            # All 30 unicode spaces
-            (config.chars_spaces, u''),
-            )
-        for test, expected in cases:
-            result = self.request.normalizePagename(test)
-            assert result == expected
-
-    def testUnderscoreTestCase(self):
-        """ request: normalize pagename: underscore convert to spaces and normalized
-
-        Underscores should convert to spaces, then spaces should be
-        normalized, order is important!
-        """
-        cases = (
-            (u'         ', u''),
-            (u'  a', u'a'),
-            (u'a  ', u'a'),
-            (u'a  b  c', u'a b c'),
-            (u'a  b  /  c  d  /  e  f', u'a b/c d/e f'),
-            )
-        for test, expected in cases:
-            result = self.request.normalizePagename(test)
-            assert result == expected
-
-
-class TestGroupPages(object):
-
-    def setup_method(self, method):
-        self.config = self.TestConfig(page_group_regex=r'.+Group')
-
-    def teardown_method(self, method):
-        del self.config
-
-    def testNormalizeGroupName(self):
-        """ request: normalize pagename: restrict groups to alpha numeric Unicode
-
-        Spaces should normalize after invalid chars removed!
-        """
-        cases = (
-            # current acl chars
-            (u'Name,:Group', u'NameGroup'),
-            # remove than normalize spaces
-            (u'Name ! @ # $ % ^ & * ( ) + Group', u'Name Group'),
-            )
-        for test, expected in cases:
-            # validate we are testing valid group names
-            if wikiutil.isGroupPage(self.request, test):
-                result = self.request.normalizePagename(test)
-                assert result == expected
-
 class TestHTTPHeaders(object):
     std_headers = ['Status: 200 OK', 'Content-type: text/html; charset=%s' % config.charset]
 
--- a/MoinMoin/theme/__init__.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/theme/__init__.py	Sat Jun 07 19:55:27 2008 +0200
@@ -361,7 +361,7 @@
             pass
 
         # Handle regular pagename like "FrontPage"
-        pagename = request.normalizePagename(pagename)
+        pagename = wikiutil.normalize_pagename(pagename, request.cfg)
 
         # Use localized pages for the current user
         if localize:
--- a/MoinMoin/user.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/user.py	Sat Jun 07 19:55:27 2008 +0200
@@ -201,7 +201,7 @@
     @param name: user name, unicode
     """
     normalized = normalizeName(name)
-    return (name == normalized) and not wikiutil.isGroupPage(request, name)
+    return (name == normalized) and not wikiutil.isGroupPage(name, request.cfg)
 
 
 def encodeList(items):
--- a/MoinMoin/wikiutil.py	Fri Jun 06 14:26:55 2008 +0200
+++ b/MoinMoin/wikiutil.py	Sat Jun 07 19:55:27 2008 +0200
@@ -709,14 +709,14 @@
     return request.cfg.cache.page_template_regexact.search(pagename) is not None
 
 
-def isGroupPage(request, pagename):
+def isGroupPage(pagename, cfg):
     """ Is this a name of group page?
 
     @param pagename: the page name
     @rtype: bool
     @return: true if page is a form page
     """
-    return request.cfg.cache.page_group_regexact.search(pagename) is not None
+    return cfg.cache.page_group_regexact.search(pagename) is not None
 
 
 def filterCategoryPages(request, pagelist):
@@ -2222,6 +2222,47 @@
 #############################################################################
 ### Misc
 #############################################################################
+def normalize_pagename(name, cfg):
+    """ Normalize page name
+
+    Prevent creating page names with invisible characters or funny
+    whitespace that might confuse the users or abuse the wiki, or
+    just does not make sense.
+
+    Restrict even more group pages, so they can be used inside acl lines.
+
+    @param name: page name, unicode
+    @rtype: unicode
+    @return: decoded and sanitized page name
+    """
+    # Strip invalid characters
+    name = config.page_invalid_chars_regex.sub(u'', name)
+
+    # Split to pages and normalize each one
+    pages = name.split(u'/')
+    normalized = []
+    for page in pages:
+        # Ignore empty or whitespace only pages
+        if not page or page.isspace():
+            continue
+
+        # Cleanup group pages.
+        # Strip non alpha numeric characters, keep white space
+        if isGroupPage(page, cfg):
+            page = u''.join([c for c in page
+                             if c.isalnum() or c.isspace()])
+
+        # Normalize white space. Each name can contain multiple
+        # words separated with only one space. Split handle all
+        # 30 unicode spaces (isspace() == True)
+        page = u' '.join(page.split())
+
+        normalized.append(page)
+
+    # Assemble components into full pagename
+    name = u'/'.join(normalized)
+    return name
+
 def taintfilename(basename):
     """
     Make a filename that is supposed to be a plain name secure, i.e.