'Edit a wiki `page`'

@require_oauth2_scope('wikiedit')
@validate(VModhash(), pageandprevious=VWikiPageRevise(('page', 'previous'), restricted=True), content=nop('content'), page_name=VWikiPageName('page'), reason=VPrintable('reason', 256, empty_error=None))
@api_doc(api_section.wiki, uri='/api/wiki/edit', uses_site=True)
def POST_wiki_edit(self, pageandprevious, content, page_name, reason):

    (page, previous) = pageandprevious
    if c.user._spam:
        error = _('You    are    doing    that    too    much,    please    try    again    later.')
        self.handle_error(415, 'SPECIAL_ERRORS', special_errors=[error])
    if (not page):
        error = c.errors.get(('WIKI_CREATE_ERROR', 'page'))
        if error:
            self.handle_error(403, **(error.msg_params or {}))
        VNotInTimeout().run(action_name='wikirevise', details_text='create')
        try:
            page = WikiPage.create(c.site, page_name)
        except WikiPageExists:
            self.handle_error(400, 'WIKI_CREATE_ERROR')
    else:
        VNotInTimeout().run(action_name='wikirevise', details_text='edit', target=page)
        error = c.errors.get(('MAY_NOT_REVISE', 'page'))
        if error:
            self.handle_error(403, **(error.msg_params or {}))
    renderer = RENDERERS_BY_PAGE.get(page.name, 'wiki')
    if (renderer in ('wiki', 'reddit')):
        content = VMarkdown('content', renderer=renderer).run(content)
    previous = (previous._id if previous else request.POST.get('previous'))
    try:
        if (page.name == 'config/stylesheet'):
            (css_errors, parsed) = c.site.parse_css(content, verify=False)
            if g.css_killswitch:
                self.handle_error(403, 'STYLESHEET_EDIT_DENIED')
            if css_errors:
                error_items = [CssError(x).message for x in css_errors]
                self.handle_error(415, 'SPECIAL_ERRORS', special_errors=error_items)
        elif (page.name == 'config/automoderator'):
            try:
                rules = Ruleset(content)
            except ValueError as e:
                error_items = [e.message]
                self.handle_error(415, 'SPECIAL_ERRORS', special_errors=error_items)
        if (page.name == 'config/stylesheet'):
            c.site.change_css(content, parsed, previous, reason=reason)
        else:
            try:
                page.revise(content, previous, c.user._id36, reason=reason)
            except ContentLengthError as e:
                self.handle_error(403, 'CONTENT_LENGTH_ERROR', max_length=e.max_length)
            if (page.special and (page.name in ATTRIBUTE_BY_PAGE)):
                setattr(c.site, ATTRIBUTE_BY_PAGE[page.name], content)
                c.site._commit()
            if (page.special or c.is_wiki_mod):
                description = modactions.get(page.name, ('Page    %s    edited' % page.name))
                ModAction.create(c.site, c.user, 'wikirevise', details=description, description=reason)
    except ConflictException as e:
        self.handle_error(409, 'EDIT_CONFLICT', newcontent=e.new, newrevision=page.revision, diffcontent=e.htmldiff)
    return json.dumps({})
