changeset 1922:f486526720c5

renamed wikiutil.clean_comment > clean_input, moved translation map to config
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 01 Apr 2007 00:17:15 +0200
parents 4122148ceadb
children eefebea247a0
files MoinMoin/action/CopyPage.py MoinMoin/action/DeletePage.py MoinMoin/action/Load.py MoinMoin/action/RenamePage.py MoinMoin/action/edit.py MoinMoin/config/__init__.py MoinMoin/userform.py MoinMoin/wikiutil.py
diffstat 8 files changed, 32 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/CopyPage.py	Sat Mar 31 23:55:32 2007 +0200
+++ b/MoinMoin/action/CopyPage.py	Sun Apr 01 00:17:15 2007 +0200
@@ -53,7 +53,7 @@
         newpagename = form.get('newpagename', [u''])[0]
         newpagename = self.request.normalizePagename(newpagename)
         comment = form.get('comment', [u''])[0]
-        comment = wikiutil.clean_comment(comment)
+        comment = wikiutil.clean_input(comment)
 
         self.page = PageEditor(self.request, self.pagename)
         success, msg = self.page.copyPage(newpagename, comment)
--- a/MoinMoin/action/DeletePage.py	Sat Mar 31 23:55:32 2007 +0200
+++ b/MoinMoin/action/DeletePage.py	Sun Apr 01 00:17:15 2007 +0200
@@ -49,7 +49,7 @@
         """ Delete pagename """
         form = self.form
         comment = form.get('comment', [u''])[0]
-        comment = wikiutil.clean_comment(comment)
+        comment = wikiutil.clean_input(comment)
 
         # Create a page editor that does not do editor backups, because
         # delete generates a "deleted" version of the page.
--- a/MoinMoin/action/Load.py	Sat Mar 31 23:55:32 2007 +0200
+++ b/MoinMoin/action/Load.py	Sun Apr 01 00:17:15 2007 +0200
@@ -48,7 +48,7 @@
 
         target = filename
         if rename:
-            target = wikiutil.clean_comment(rename.strip())
+            target = wikiutil.clean_input(rename.strip())
 
         # preprocess the filename
         # strip leading drive and path (IE misbehaviour)
--- a/MoinMoin/action/RenamePage.py	Sat Mar 31 23:55:32 2007 +0200
+++ b/MoinMoin/action/RenamePage.py	Sun Apr 01 00:17:15 2007 +0200
@@ -52,7 +52,7 @@
         newpagename = form.get('newpagename', [u''])[0]
         newpagename = self.request.normalizePagename(newpagename)
         comment = form.get('comment', [u''])[0]
-        comment = wikiutil.clean_comment(comment)
+        comment = wikiutil.clean_input(comment)
 
         self.page = PageEditor(self.request, self.pagename)
         success, msg = self.page.renamePage(newpagename, comment)
--- a/MoinMoin/action/edit.py	Sat Mar 31 23:55:32 2007 +0200
+++ b/MoinMoin/action/edit.py	Sun Apr 01 00:17:15 2007 +0200
@@ -109,7 +109,7 @@
         pg.sendCancel(savetext or "", rev)
         return
 
-    comment = wikiutil.clean_comment(comment)
+    comment = wikiutil.clean_input(comment)
 
     # Add category
 
--- a/MoinMoin/config/__init__.py	Sat Mar 31 23:55:32 2007 +0200
+++ b/MoinMoin/config/__init__.py	Sun Apr 01 00:17:15 2007 +0200
@@ -47,6 +47,18 @@
     re.UNICODE | re.VERBOSE
     )
 
+# used for wikiutil.clean_input
+clean_input_translation_map = {
+    # these chars will be replaced by blanks
+    ord(u'\t'): u' ',
+    ord(u'\r'): u' ',
+    ord(u'\n'): u' ',
+}
+for c in u'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f' \
+          '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f':
+    # these chars will be removed
+    clean_input_translation_map[ord(c)] = None
+
 # Other stuff
 url_schemas = []
 
--- a/MoinMoin/userform.py	Sat Mar 31 23:55:32 2007 +0200
+++ b/MoinMoin/userform.py	Sun Apr 01 00:17:15 2007 +0200
@@ -64,7 +64,7 @@
                 return _("""This wiki is not enabled for mail processing.
 Contact the owner of the wiki, who can enable email.""")
             try:
-                email = wikiutil.clean_comment(form['email'][0].lower())
+                email = wikiutil.clean_input(form['email'][0].lower())
             except KeyError:
                 return _("Please provide a valid email address!")
 
@@ -125,7 +125,7 @@
                     return "Can't encode password: %s" % str(err)
 
             # try to get the (required) email
-            email = wikiutil.clean_comment(form.get('email', [''])[0])
+            email = wikiutil.clean_input(form.get('email', [''])[0])
             theuser.email = email.strip()
             if not theuser.email:
                 return _("Please provide your email address. If you lose your"
@@ -215,7 +215,7 @@
 
             if not 'email' in theuser.auth_attribs:
                 # try to get the email
-                email = wikiutil.clean_comment(form.get('email', [theuser.email])[0])
+                email = wikiutil.clean_input(form.get('email', [theuser.email])[0])
                 theuser.email = email.strip()
 
             # Require email
@@ -235,7 +235,7 @@
 
             if not 'aliasname' in theuser.auth_attribs:
                 # aliasname
-                theuser.aliasname = wikiutil.clean_comment(form.get('aliasname', [''])[0])
+                theuser.aliasname = wikiutil.clean_input(form.get('aliasname', [''])[0])
 
             # editor size
             theuser.edit_rows = util.web.getIntegerInput(self.request, 'edit_rows', theuser.edit_rows, 10, 60)
--- a/MoinMoin/wikiutil.py	Sat Mar 31 23:55:32 2007 +0200
+++ b/MoinMoin/wikiutil.py	Sun Apr 01 00:17:15 2007 +0200
@@ -241,31 +241,22 @@
         s = s.replace('"', "&quot;")
     return s
 
-def clean_comment(comment):
-    """ Clean comment - replace CR, LF, TAB by whitespace, delete control chars
-        TODO: move this to config, create on first call then return cached.
-        TODO: rename to clean_whatever
-        @param comment: unicode text to clean
+def clean_input(text, max_len=201):
+    """ Clean input:
+        replace CR, LF, TAB by whitespace
+        delete control chars
+
+        @param text: unicode text to clean
         @rtype: unicode
         @return: cleaned text
     """
-    # alias name could be empty
     # we only have input fields with max 200 chars, but spammers send us more
-    l = len(comment)
-    if l == 0 or l > 201:
+    l = len(text)
+    if l == 0 or l > max_len:
         return u''
-    
-    remap_chars = {
-        ord(u'\t'): u' ',
-        ord(u'\r'): u' ',
-        ord(u'\n'): u' ',
-    }
-    control_chars = u'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f' \
-                    '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'
-    for c in control_chars:
-        remap_chars[ord(c)] = None
-    comment = comment.translate(remap_chars)
-    return comment
+    else:
+        return text.translate(config.clean_input_translation_map)
+
 
 def make_breakable(text, maxlen):
     """ make a text breakable by inserting spaces into nonbreakable parts