Back to index

moin  1.9.0~rc2
edit.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - edit a page
00004 
00005     This either calls the text or the GUI page editor.
00006 
00007     @copyright: 2000-2004 Juergen Hermann <jh@web.de>,
00008                 2006 MoinMoin:ThomasWaldmann
00009     @license: GNU GPL, see COPYING for details.
00010 """
00011 from MoinMoin import wikiutil
00012 from MoinMoin.Page import Page
00013 from MoinMoin.web.utils import check_surge_protect
00014 
00015 def execute(pagename, request):
00016     """ edit a page """
00017     _ = request.getText
00018 
00019     if 'button_preview' in request.form and 'button_spellcheck' in request.form:
00020         # multiple buttons pressed at once? must be some spammer/bot
00021         check_surge_protect(request, kick=True) # get rid of him
00022         return
00023 
00024     if not request.user.may.write(pagename):
00025         page = wikiutil.getLocalizedPage(request, 'PermissionDeniedPage')
00026         page.body = _('You are not allowed to edit this page.')
00027         page.page_name = pagename
00028         page.send_page(send_special=True)
00029         return
00030 
00031     valideditors = ['text', 'gui', ]
00032     editor = ''
00033     if request.user.valid:
00034         editor = request.user.editor_default
00035     if editor not in valideditors:
00036         editor = request.cfg.editor_default
00037 
00038     editorparam = request.values.get('editor', editor)
00039     if editorparam == "guipossible":
00040         lasteditor = editor
00041     elif editorparam == "textonly":
00042         editor = lasteditor = 'text'
00043     else:
00044         editor = lasteditor = editorparam
00045 
00046     if request.cfg.editor_force:
00047         editor = request.cfg.editor_default
00048 
00049     # if it is still nothing valid, we just use the text editor
00050     if editor not in valideditors:
00051         editor = 'text'
00052 
00053     rev = request.rev or 0
00054     savetext = request.form.get('savetext')
00055     comment = request.form.get('comment', u'')
00056     category = request.form.get('category')
00057     rstrip = int(request.form.get('rstrip', '0'))
00058     trivial = int(request.form.get('trivial', '0'))
00059 
00060     if 'button_switch' in request.form:
00061         if editor == 'text':
00062             editor = 'gui'
00063         else: # 'gui'
00064             editor = 'text'
00065 
00066     # load right editor class
00067     if editor == 'gui':
00068         from MoinMoin.PageGraphicalEditor import PageGraphicalEditor
00069         pg = PageGraphicalEditor(request, pagename)
00070     else: # 'text'
00071         from MoinMoin.PageEditor import PageEditor
00072         pg = PageEditor(request, pagename)
00073 
00074     # is invoked without savetext start editing
00075     if savetext is None or 'button_load_draft' in request.form:
00076         pg.sendEditor()
00077         return
00078 
00079     # did user hit cancel button?
00080     cancelled = 'button_cancel' in request.form
00081 
00082     if request.cfg.edit_ticketing:
00083         ticket = request.form.get('ticket', '')
00084         if not wikiutil.checkTicket(request, ticket):
00085             request.theme.add_msg(_('Please use the interactive user interface to use action %(actionname)s!') % {'actionname': 'edit' }, "error")
00086             pg.send_page()
00087             return
00088 
00089     from MoinMoin.error import ConvertError
00090     try:
00091         if lasteditor == 'gui':
00092             # convert input from Graphical editor
00093             format = request.form.get('format', 'wiki')
00094             if format == 'wiki':
00095                 converter_name = 'text_html_text_moin_wiki'
00096             else:
00097                 converter_name = 'undefined' # XXX we don't have other converters yet
00098             convert = wikiutil.importPlugin(request.cfg, "converter", converter_name, 'convert')
00099             savetext = convert(request, pagename, savetext)
00100 
00101         # IMPORTANT: normalize text from the form. This should be done in
00102         # one place before we manipulate the text.
00103         savetext = pg.normalizeText(savetext, stripspaces=rstrip)
00104     except ConvertError:
00105         # we don't want to throw an exception if user cancelled anyway
00106         if not cancelled:
00107             raise
00108 
00109     if cancelled:
00110         pg.sendCancel(savetext or "", rev)
00111         pagedir = pg.getPagePath(check_create=0)
00112         import os
00113         if not os.listdir(pagedir):
00114             os.removedirs(pagedir)
00115         return
00116 
00117     comment = wikiutil.clean_input(comment)
00118 
00119     # Add category
00120 
00121     # TODO: this code does not work with extended links, and is doing
00122     # things behind your back, and in general not needed. Either we have
00123     # a full interface for categories (add, delete) or just add them by
00124     # markup.
00125 
00126     if category and category != _('<No addition>'): # opera 8.5 needs this
00127         # strip trailing whitespace
00128         savetext = savetext.rstrip()
00129 
00130         # Add category separator if last non-empty line contains
00131         # non-categories.
00132         lines = [line for line in savetext.splitlines() if line]
00133         if lines:
00134 
00135             #TODO: this code is broken, will not work for extended links
00136             #categories, e.g ["category hebrew"]
00137             categories = lines[-1].split()
00138 
00139             if categories:
00140                 confirmed = wikiutil.filterCategoryPages(request, categories)
00141                 if len(confirmed) < len(categories):
00142                     # This was not a categories line, add separator
00143                     savetext += u'\n----\n'
00144 
00145         # Add new category
00146         if savetext and savetext[-1] != u'\n':
00147             savetext += ' '
00148         savetext += category + u'\n' # Should end with newline!
00149 
00150     # Preview, spellcheck or spellcheck add new words
00151     if ('button_preview' in request.form or
00152         'button_spellcheck' in request.form or
00153         'button_newwords' in request.form):
00154         pg.sendEditor(preview=savetext, comment=comment)
00155 
00156     # Preview with mode switch
00157     elif 'button_switch' in request.form:
00158         pg.sendEditor(preview=savetext, comment=comment, staytop=1)
00159 
00160     # Save new text
00161     else:
00162         try:
00163             from MoinMoin.security.textcha import TextCha
00164             if not TextCha(request).check_answer_from_form():
00165                 raise pg.SaveError(_('TextCha: Wrong answer! Go back and try again...'))
00166             savemsg = pg.saveText(savetext, rev, trivial=trivial, comment=comment)
00167         except pg.EditConflict, e:
00168             msg = e.message
00169 
00170             # Handle conflict and send editor
00171             pg.set_raw_body(savetext, modified=1)
00172 
00173             pg.mergeEditConflict(rev)
00174             # We don't send preview when we do merge conflict
00175             pg.sendEditor(msg=msg, comment=comment)
00176             return
00177 
00178         except pg.SaveError, msg:
00179             # msg contains a unicode string
00180             savemsg = unicode(msg)
00181 
00182         # Send new page after save or after unsuccessful conflict merge.
00183         request.reset()
00184         pg = Page(request, pagename)
00185 
00186         # sets revision number to default for further actions
00187         request.rev = 0
00188         request.theme.add_msg(savemsg, "info")
00189         pg.send_page()