"ExternalEdit Moinmoin action module" # imports import cgi, os, string, sys, Cookie from MoinMoin import webapi, i18n from MoinMoin.PageEditor import PageEditor SAVE_OPTION_ITEMS = { # key: ('description', 'default') 'notify': ('Send mail notification', 'No'), 'stripspaces': ('Remove trailing whitespace from each line', 'No'), 'comment': ('Comment', '') } def washComment(comment): """ grabbed from wikiaction.do_savepage, washes the comment option """ # delete any unwanted stuff, replace CR, LF, TAB by whitespace control_chars = '\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' remap_chars = string.maketrans('\t\r\n', ' ') comment = string.translate(comment, remap_chars, control_chars) return comment class ExternalEditor: def __init__(self, pagename): self.pagename = pagename # end def def getCookie(self, user): """Get the Set-Cookie header for the given user""" cookie = Cookie.SimpleCookie() cookie['MOIN_ID'] = user.id if user.remember_me: ret=cookie.output() + ' expires=Tuesday, 31-Dec-2013 12:00:00 GMT; Path=%s' % (self.request.getScriptname(),) else: loc=locale.setlocale(locale.LC_TIME, 'C') ret=cookie.output() + ' expires='+time.strftime("%A, %d-%b-%Y 23:59:59 GMT")+'; Path=%s' % (self.request.getScriptname(),) locale.setlocale(locale.LC_TIME, loc) return ret def handle_request(self, request): self.request = request self.editor = PageEditor(self.pagename, request) if request.form.has_key('save'): self.save_page() else: self.edit_page() # end if # end def def save_page(self): _ = self.request.getText #Check if use has the permissions to edit file if not self.request.user.may.edit(self.pagename): webapi.http_headers(self.request) request.write('result:failure\n') request.write('message:%s\n' % _('You do not have the rights to edit this page!')) else: options = self.getSaveOptions(sys.stdin) text = sys.stdin.read() self.editor.saveText(text, '0', **options) webapi.http_headers(self.request) request.write('result:successful\n') request.write('message:%s\n' % _('Page saved.')) # end if # end def def edit_page(self): _ = self.request.getText #Check if use has the permissions to edit file if not self.request.user.may.edit(self.pagename): self.editor.send_editor(self.request) else: headers = ['Content-Type: application/x-moin-edit', 'Pragma: no-cache'] put_headers = ['action=ExternalEdit','save=true'] ## First add metadata from the helper app metadata = [] # content type for the helper app metadata.append('content_type:application/x-www-form-urlencoded') # url to post the edits to url = '%s%s?%s' % (self.request.getQualifiedURL(), self.editor.url(self.request), '&'.join(put_headers)) metadata.append('url:%s' % url) # cookie metadata.append('cookie:%s' % self.getCookie(self.request.user)[12:]) metadata.append('meta_type:moinwiki') metadata.append('language:%s' % i18n.requestLanguage(self.request)) out_lines = '\n'.join(metadata) + '\n' #trailing newline ## Second, get the file body = self.editor.get_raw_body() ## Third, add edit options to file body = self.addSaveOptions(body) out_lines += '\n' out_lines += body headers.append('Content-Length: %s' % len(out_lines)) ## Fourth, send all to helper app self.request.http_headers(headers) self.request.write(out_lines) # end if # end def def addSaveOptions(self, text): _ = self.request.getText opttext = '' for value in SAVE_OPTION_ITEMS.values(): opttext += '%s: %s\n' % (_(value[0]), _(value[1])) opttext += '\n' opttext += text return opttext # end def def getSaveOptions(self, inputfile): _ = self.request.getText parseddic = {} while 1: line = inputfile.readline()[:-1] if not line: break sep = line.find(':') key = line[:sep].strip() val = line[sep+1:].strip() for option in SAVE_OPTION_ITEMS.items(): optquery = _(option[1][0]) optanswer = _(option[1][1]) optkey = option[0] if key == optquery: if optkey == 'comment': parseddic['comment'] = washComment(val) else: if val.lower() == optanswer.lower(): val = '' # end if parseddic[optkey] = val # end if # end if # end for # end while return parseddic # end def # end class def execute(pagename, request): editor = ExternalEditor(pagename) editor.handle_request(request) # end def