changeset 1018:f5b796658f93

moved edit and diff code to own files, strange: needs a http_headers() call at end of edit.py - why?
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 24 Jul 2006 00:19:55 +0200
parents 2408963718c8
children 6488692b1eb8
files MoinMoin/action/__init__.py MoinMoin/action/diff.py MoinMoin/action/edit.py
diffstat 3 files changed, 325 insertions(+), 299 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/__init__.py	Sun Jul 23 22:57:19 2006 +0200
+++ b/MoinMoin/action/__init__.py	Mon Jul 24 00:19:55 2006 +0200
@@ -303,310 +303,11 @@
     pg.send_page(request, msg=savemsg)
     return None
 
-def do_edit(pagename, request):
-    """ edit a page """
-    _ = request.getText
-
-    if not request.user.may.write(pagename):
-        Page(request, pagename).send_page(request,
-            msg=_('You are not allowed to edit this page.'))
-        return
-
-    valideditors = ['text', 'gui', ]
-    editor = ''
-    if request.user.valid:
-        editor = request.user.editor_default
-    if editor not in valideditors:
-        editor = request.cfg.editor_default
-
-    editorparam = request.form.get('editor', [editor])[0]
-    if editorparam == "guipossible":
-        lasteditor = editor
-    elif editorparam == "textonly":
-        editor = lasteditor = 'text'
-    else:
-        editor = lasteditor = editorparam
-
-    if request.cfg.editor_force:
-        editor = request.cfg.editor_default
-
-    # if it is still nothing valid, we just use the text editor
-    if editor not in valideditors:
-        editor = 'text'
-
-    savetext = request.form.get('savetext', [None])[0]
-    rev = int(request.form.get('rev', ['0'])[0])
-    comment = request.form.get('comment', [u''])[0]
-    category = request.form.get('category', [None])[0]
-    rstrip = int(request.form.get('rstrip', ['0'])[0])
-    trivial = int(request.form.get('trivial', ['0'])[0])
-
-    if request.form.has_key('button_switch'):
-        if editor == 'text':
-            editor = 'gui'
-        else: # 'gui'
-            editor = 'text'
-
-    # load right editor class
-    if editor == 'gui':
-        from MoinMoin.PageGraphicalEditor import PageGraphicalEditor
-        pg = PageGraphicalEditor(request, pagename)
-    else: # 'text'
-        from MoinMoin.PageEditor import PageEditor
-        pg = PageEditor(request, pagename)
-
-    # is invoked without savetext start editing
-    if savetext is None:
-        pg.sendEditor()
-        return
-
-    # did user hit cancel button?
-    cancelled = request.form.has_key('button_cancel')
-
-    # convert input from Graphical editor
-    from MoinMoin.converter.text_html_text_moin_wiki import convert, ConvertError
-    try:
-        if lasteditor == 'gui':
-            savetext = convert(request, pagename, savetext)
-
-        # IMPORTANT: normalize text from the form. This should be done in
-        # one place before we manipulate the text.
-        savetext = pg.normalizeText(savetext, stripspaces=rstrip)
-    except ConvertError:
-        # we don't want to throw an exception if user cancelled anyway
-        if not cancelled:
-            raise
-
-    if cancelled:
-        pg.sendCancel(savetext or "", rev)
-        return
-
-    comment = wikiutil.clean_comment(comment)
-
-    # Add category
-
-    # TODO: this code does not work with extended links, and is doing
-    # things behind your back, and in general not needed. Either we have
-    # a full interface for categories (add, delete) or just add them by
-    # markup.
-
-    if category and category != _('<No addition>', formatted=False): # opera 8.5 needs this
-        # strip trailing whitespace
-        savetext = savetext.rstrip()
-
-        # Add category separator if last non-empty line contains
-        # non-categories.
-        lines = filter(None, savetext.splitlines())
-        if lines:
-
-            #TODO: this code is broken, will not work for extended links
-            #categories, e.g ["category hebrew"]
-            categories = lines[-1].split()
-
-            if categories:
-                confirmed = wikiutil.filterCategoryPages(request, categories)
-                if len(confirmed) < len(categories):
-                    # This was not a categories line, add separator
-                    savetext += u'\n----\n'
-
-        # Add new category
-        if savetext and savetext[-1] != u'\n':
-            savetext += ' '
-        savetext += category + u'\n' # Should end with newline!
-
-    # Preview, spellcheck or spellcheck add new words
-    if (request.form.has_key('button_preview') or
-        request.form.has_key('button_spellcheck') or
-        request.form.has_key('button_newwords')):
-        pg.sendEditor(preview=savetext, comment=comment)
-
-    # Preview with mode switch
-    elif request.form.has_key('button_switch'):
-        pg.sendEditor(preview=savetext, comment=comment, staytop=1)
-
-    # Save new text
-    else:
-        try:
-            still_conflict = wikiutil.containsConflictMarker(savetext)
-            pg.setConflict(still_conflict)
-            savemsg = pg.saveText(savetext, rev, trivial=trivial, comment=comment)
-        except pg.EditConflict, e:
-            msg = e.message
-
-            # Handle conflict and send editor
-            pg.set_raw_body(savetext, modified=1)
-
-            pg.mergeEditConflict(rev)
-            # We don't send preview when we do merge conflict
-            pg.sendEditor(msg=msg, comment=comment)
-            return
-
-        except pg.SaveError, msg:
-            # msg contain a unicode string
-            savemsg = unicode(msg)
-
-        # Send new page after save or after unsuccessful conflict merge.
-        request.reset()
-        backto = request.form.get('backto', [None])[0]
-        if backto:
-            pg = Page(request, backto)
-
-        pg.send_page(request, msg=savemsg)
-
 def do_goto(pagename, request):
     """ redirect to another page """
     target = request.form.get('target', [''])[0]
     request.http_redirect(Page(request, target).url(request))
 
-def do_diff(pagename, request):
-    """ Handle "action=diff"
-        checking for either a "rev=formerrevision" parameter
-        or rev1 and rev2 parameters
-    """
-    if not request.user.may.read(pagename):
-        Page(request, pagename).send_page(request)
-        return
-
-    try:
-        date = request.form['date'][0]
-        try:
-            date = long(date) # must be long for py 2.2.x
-        except StandardError:
-            date = 0
-    except KeyError:
-        date = 0
-
-    try:
-        rev1 = int(request.form.get('rev1', [-1])[0])
-    except StandardError:
-        rev1 = 0
-    try:
-        rev2 = int(request.form.get('rev2', [0])[0])
-    except StandardError:
-        rev1 = 0
-
-    if rev1 == -1 and rev2 == 0:
-        try:
-            rev1 = int(request.form.get('rev', [-1])[0])
-        except StandardError:
-            rev1 = -1
-
-    # spacing flag?
-    ignorews = int(request.form.get('ignorews', [0])[0])
-
-    _ = request.getText
-
-    # get a list of old revisions, and back out if none are available
-    currentpage = Page(request, pagename)
-    revisions = currentpage.getRevList()
-    if len(revisions) < 2:
-        currentpage.send_page(request, msg=_("No older revisions available!"))
-        return
-
-    if date: # this is how we get called from RecentChanges
-        rev1 = 0
-        log = editlog.EditLog(request, rootpagename=pagename)
-        for line in log.reverse():
-            if date >= line.ed_time_usecs and int(line.rev) != 99999999:
-                rev1 = int(line.rev)
-                break
-        else:
-            rev1 = 1
-        rev2 = 0
-
-    # Start output
-    # This action generate content in the user language
-    request.setContentLanguage(request.lang)
-
-    request.http_headers()
-    request.theme.send_title(_('Diff for "%s"') % (pagename,), pagename=pagename, allow_doubleclick=1)
-
-    if rev1 > 0 and rev2 > 0 and rev1 > rev2 or rev1 == 0 and rev2 > 0:
-        rev1, rev2 = rev2, rev1
-
-    oldrev1, oldcount1 = None, 0
-    oldrev2, oldcount2 = None, 0
-
-    # get the filename of the version to compare to
-    edit_count = 0
-    for rev in revisions:
-        edit_count += 1
-        if rev <= rev1:
-            oldrev1, oldcount1 = rev, edit_count
-        if rev2 and rev >= rev2:
-            oldrev2, oldcount2 = rev, edit_count
-        if oldrev1 and oldrev2 or oldrev1 and not rev2:
-            break
-
-    if rev1 == -1:
-        oldpage = Page(request, pagename, rev=revisions[1])
-        oldcount1 -= 1
-    elif rev1 == 0:
-        oldpage = currentpage
-        # oldcount1 is still on init value 0
-    else:
-        if oldrev1:
-            oldpage = Page(request, pagename, rev=oldrev1)
-        else:
-            oldpage = Page(request, "$EmptyPage$") # hack
-            oldpage.set_raw_body("")    # avoid loading from disk
-            oldrev1 = 0 # XXX
-
-    if rev2 == 0:
-        newpage = currentpage
-        # oldcount2 is still on init value 0
-    else:
-        if oldrev2:
-            newpage = Page(request, pagename, rev=oldrev2)
-        else:
-            newpage = Page(request, "$EmptyPage$") # hack
-            newpage.set_raw_body("")    # avoid loading from disk
-            oldrev2 = 0 # XXX
-
-    edit_count = abs(oldcount1 - oldcount2)
-
-    # this should use the formatter, but there is none?
-    request.write('<div id="content">\n') # start content div
-    request.write('<p class="diff-header">')
-    request.write(_('Differences between revisions %d and %d') % (oldpage.get_real_rev(), newpage.get_real_rev()))
-    if edit_count > 1:
-        request.write(' ' + _('(spanning %d versions)') % (edit_count,))
-    request.write('</p>')
-
-    if request.user.show_fancy_diff:
-        from MoinMoin.util.diff import diff
-        request.write(diff(request, oldpage.get_raw_body(), newpage.get_raw_body()))
-        newpage.send_page(request, count_hit=0, content_only=1, content_id="content-below-diff")
-    else:
-        lines = wikiutil.linediff(oldpage.getlines(), newpage.getlines())
-        if not lines:
-            msg = _("No differences found!")
-            if edit_count > 1:
-                msg = msg + '<p>' + _('The page was saved %(count)d times, though!') % {
-                    'count': edit_count}
-            request.write(msg)
-        else:
-            if ignorews:
-                request.write(_('(ignoring whitespace)') + '<br>')
-            else:
-                qstr = 'action=diff&ignorews=1'
-                if rev1: qstr = '%s&rev1=%s' % (qstr, rev1)
-                if rev2: qstr = '%s&rev2=%s' % (qstr, rev2)
-                request.write(Page(request, pagename).link_to(request,
-                    text=_('Ignore changes in the amount of whitespace'),
-                    querystr=qstr, rel='nofollow') + '<p>')
-
-            request.write('<pre>')
-            for line in lines:
-                if line[0] == "@":
-                    request.write('<hr>')
-                request.write(wikiutil.escape(line)+'\n')
-            request.write('</pre>')
-
-    request.write('</div>\n') # end content div
-    request.theme.send_footer(pagename)
-    request.theme.send_closing_html()
-
 def do_quicklink(pagename, request):
     """ Add the current wiki page to the user quicklinks 
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/action/diff.py	Mon Jul 24 00:19:55 2006 +0200
@@ -0,0 +1,161 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - show diff between 2 page revisions
+
+    @copyright: 2000-2004 by Jürgen Hermann <jh@web.de>,
+                2006 by MoinMoin:ThomasWaldmann
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin.logfile import editlog
+from MoinMoin.Page import Page
+
+def execute(pagename, request):
+    """ Handle "action=diff"
+        checking for either a "rev=formerrevision" parameter
+        or rev1 and rev2 parameters
+    """
+    if not request.user.may.read(pagename):
+        Page(request, pagename).send_page(request)
+        return
+
+    try:
+        date = request.form['date'][0]
+        try:
+            date = long(date) # must be long for py 2.2.x
+        except StandardError:
+            date = 0
+    except KeyError:
+        date = 0
+
+    try:
+        rev1 = int(request.form.get('rev1', [-1])[0])
+    except StandardError:
+        rev1 = 0
+    try:
+        rev2 = int(request.form.get('rev2', [0])[0])
+    except StandardError:
+        rev1 = 0
+
+    if rev1 == -1 and rev2 == 0:
+        try:
+            rev1 = int(request.form.get('rev', [-1])[0])
+        except StandardError:
+            rev1 = -1
+
+    # spacing flag?
+    ignorews = int(request.form.get('ignorews', [0])[0])
+
+    _ = request.getText
+
+    # get a list of old revisions, and back out if none are available
+    currentpage = Page(request, pagename)
+    revisions = currentpage.getRevList()
+    if len(revisions) < 2:
+        currentpage.send_page(request, msg=_("No older revisions available!"))
+        return
+
+    if date: # this is how we get called from RecentChanges
+        rev1 = 0
+        log = editlog.EditLog(request, rootpagename=pagename)
+        for line in log.reverse():
+            if date >= line.ed_time_usecs and int(line.rev) != 99999999:
+                rev1 = int(line.rev)
+                break
+        else:
+            rev1 = 1
+        rev2 = 0
+
+    # Start output
+    # This action generate content in the user language
+    request.setContentLanguage(request.lang)
+
+    request.http_headers()
+    request.theme.send_title(_('Diff for "%s"') % (pagename,), pagename=pagename, allow_doubleclick=1)
+
+    if rev1 > 0 and rev2 > 0 and rev1 > rev2 or rev1 == 0 and rev2 > 0:
+        rev1, rev2 = rev2, rev1
+
+    oldrev1, oldcount1 = None, 0
+    oldrev2, oldcount2 = None, 0
+
+    # get the filename of the version to compare to
+    edit_count = 0
+    for rev in revisions:
+        edit_count += 1
+        if rev <= rev1:
+            oldrev1, oldcount1 = rev, edit_count
+        if rev2 and rev >= rev2:
+            oldrev2, oldcount2 = rev, edit_count
+        if oldrev1 and oldrev2 or oldrev1 and not rev2:
+            break
+
+    if rev1 == -1:
+        oldpage = Page(request, pagename, rev=revisions[1])
+        oldcount1 -= 1
+    elif rev1 == 0:
+        oldpage = currentpage
+        # oldcount1 is still on init value 0
+    else:
+        if oldrev1:
+            oldpage = Page(request, pagename, rev=oldrev1)
+        else:
+            oldpage = Page(request, "$EmptyPage$") # hack
+            oldpage.set_raw_body("")    # avoid loading from disk
+            oldrev1 = 0 # XXX
+
+    if rev2 == 0:
+        newpage = currentpage
+        # oldcount2 is still on init value 0
+    else:
+        if oldrev2:
+            newpage = Page(request, pagename, rev=oldrev2)
+        else:
+            newpage = Page(request, "$EmptyPage$") # hack
+            newpage.set_raw_body("")    # avoid loading from disk
+            oldrev2 = 0 # XXX
+
+    edit_count = abs(oldcount1 - oldcount2)
+
+    # this should use the formatter, but there is none?
+    request.write('<div id="content">\n') # start content div
+    request.write('<p class="diff-header">')
+    request.write(_('Differences between revisions %d and %d') % (oldpage.get_real_rev(), newpage.get_real_rev()))
+    if edit_count > 1:
+        request.write(' ' + _('(spanning %d versions)') % (edit_count,))
+    request.write('</p>')
+
+    if request.user.show_fancy_diff:
+        from MoinMoin.util.diff import diff
+        request.write(diff(request, oldpage.get_raw_body(), newpage.get_raw_body()))
+        newpage.send_page(request, count_hit=0, content_only=1, content_id="content-below-diff")
+    else:
+        lines = wikiutil.linediff(oldpage.getlines(), newpage.getlines())
+        if not lines:
+            msg = _("No differences found!")
+            if edit_count > 1:
+                msg = msg + '<p>' + _('The page was saved %(count)d times, though!') % {
+                    'count': edit_count}
+            request.write(msg)
+        else:
+            if ignorews:
+                request.write(_('(ignoring whitespace)') + '<br>')
+            else:
+                qstr = 'action=diff&ignorews=1'
+                if rev1: qstr = '%s&rev1=%s' % (qstr, rev1)
+                if rev2: qstr = '%s&rev2=%s' % (qstr, rev2)
+                request.write(Page(request, pagename).link_to(request,
+                    text=_('Ignore changes in the amount of whitespace'),
+                    querystr=qstr, rel='nofollow') + '<p>')
+
+            request.write('<pre>')
+            for line in lines:
+                if line[0] == "@":
+                    request.write('<hr>')
+                request.write(wikiutil.escape(line)+'\n')
+            request.write('</pre>')
+
+    request.write('</div>\n') # end content div
+    request.theme.send_footer(pagename)
+    request.theme.send_closing_html()
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/action/edit.py	Mon Jul 24 00:19:55 2006 +0200
@@ -0,0 +1,164 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - edit a page
+
+    This either calls the text or the GUI page editor.
+
+    @copyright: 2000-2004 by Jürgen Hermann <jh@web.de>,
+                2006 by MoinMoin:ThomasWaldmann
+    @license: GNU GPL, see COPYING for details.
+"""
+from MoinMoin import wikiutil
+from MoinMoin.Page import Page
+
+def execute(pagename, request):
+    """ edit a page """
+    _ = request.getText
+
+    if not request.user.may.write(pagename):
+        Page(request, pagename).send_page(request,
+            msg=_('You are not allowed to edit this page.'))
+        return
+
+    valideditors = ['text', 'gui', ]
+    editor = ''
+    if request.user.valid:
+        editor = request.user.editor_default
+    if editor not in valideditors:
+        editor = request.cfg.editor_default
+
+    editorparam = request.form.get('editor', [editor])[0]
+    if editorparam == "guipossible":
+        lasteditor = editor
+    elif editorparam == "textonly":
+        editor = lasteditor = 'text'
+    else:
+        editor = lasteditor = editorparam
+
+    if request.cfg.editor_force:
+        editor = request.cfg.editor_default
+
+    # if it is still nothing valid, we just use the text editor
+    if editor not in valideditors:
+        editor = 'text'
+
+    savetext = request.form.get('savetext', [None])[0]
+    rev = int(request.form.get('rev', ['0'])[0])
+    comment = request.form.get('comment', [u''])[0]
+    category = request.form.get('category', [None])[0]
+    rstrip = int(request.form.get('rstrip', ['0'])[0])
+    trivial = int(request.form.get('trivial', ['0'])[0])
+
+    if request.form.has_key('button_switch'):
+        if editor == 'text':
+            editor = 'gui'
+        else: # 'gui'
+            editor = 'text'
+
+    # load right editor class
+    if editor == 'gui':
+        from MoinMoin.PageGraphicalEditor import PageGraphicalEditor
+        pg = PageGraphicalEditor(request, pagename)
+    else: # 'text'
+        from MoinMoin.PageEditor import PageEditor
+        pg = PageEditor(request, pagename)
+
+    # is invoked without savetext start editing
+    if savetext is None:
+        pg.sendEditor()
+        return
+
+    # did user hit cancel button?
+    cancelled = request.form.has_key('button_cancel')
+
+    # convert input from Graphical editor
+    from MoinMoin.converter.text_html_text_moin_wiki import convert, ConvertError
+    try:
+        if lasteditor == 'gui':
+            savetext = convert(request, pagename, savetext)
+
+        # IMPORTANT: normalize text from the form. This should be done in
+        # one place before we manipulate the text.
+        savetext = pg.normalizeText(savetext, stripspaces=rstrip)
+    except ConvertError:
+        # we don't want to throw an exception if user cancelled anyway
+        if not cancelled:
+            raise
+
+    if cancelled:
+        pg.sendCancel(savetext or "", rev)
+        return
+
+    comment = wikiutil.clean_comment(comment)
+
+    # Add category
+
+    # TODO: this code does not work with extended links, and is doing
+    # things behind your back, and in general not needed. Either we have
+    # a full interface for categories (add, delete) or just add them by
+    # markup.
+
+    if category and category != _('<No addition>', formatted=False): # opera 8.5 needs this
+        # strip trailing whitespace
+        savetext = savetext.rstrip()
+
+        # Add category separator if last non-empty line contains
+        # non-categories.
+        lines = filter(None, savetext.splitlines())
+        if lines:
+
+            #TODO: this code is broken, will not work for extended links
+            #categories, e.g ["category hebrew"]
+            categories = lines[-1].split()
+
+            if categories:
+                confirmed = wikiutil.filterCategoryPages(request, categories)
+                if len(confirmed) < len(categories):
+                    # This was not a categories line, add separator
+                    savetext += u'\n----\n'
+
+        # Add new category
+        if savetext and savetext[-1] != u'\n':
+            savetext += ' '
+        savetext += category + u'\n' # Should end with newline!
+
+    # Preview, spellcheck or spellcheck add new words
+    if (request.form.has_key('button_preview') or
+        request.form.has_key('button_spellcheck') or
+        request.form.has_key('button_newwords')):
+        pg.sendEditor(preview=savetext, comment=comment)
+
+    # Preview with mode switch
+    elif request.form.has_key('button_switch'):
+        pg.sendEditor(preview=savetext, comment=comment, staytop=1)
+
+    # Save new text
+    else:
+        try:
+            still_conflict = wikiutil.containsConflictMarker(savetext)
+            pg.setConflict(still_conflict)
+            request.http_headers() # XXX WHY? XXX
+            savemsg = pg.saveText(savetext, rev, trivial=trivial, comment=comment)
+        except pg.EditConflict, e:
+            msg = e.message
+
+            # Handle conflict and send editor
+            pg.set_raw_body(savetext, modified=1)
+
+            pg.mergeEditConflict(rev)
+            # We don't send preview when we do merge conflict
+            pg.sendEditor(msg=msg, comment=comment)
+            return
+
+        except pg.SaveError, msg:
+            # msg contains a unicode string
+            savemsg = unicode(msg)
+
+        # Send new page after save or after unsuccessful conflict merge.
+        request.reset()
+        backto = request.form.get('backto', [None])[0]
+        if backto:
+            pg = Page(request, backto)
+
+        pg.send_page(request, msg=savemsg)
+