Exemplo n.º 1
0
def get_options(env, req, args, advanced=0):
    from Session import Session
    session = Session(env, req)

    def get_bool_option(session, args, name, default=0):
        pref = int(session.get('diff_' + name, default))
        arg = int(args.has_key(name))
        if args.has_key('update') and arg != pref:
            session.set_var('diff_' + name, arg)
        else:
            arg = pref
        return arg

    pref = session.get('diff_style', 'inline')
    arg = args.get('style', pref)
    if args.has_key('update') and arg != pref:
        session.set_var('diff_style', arg)
    req.hdf.setValue('diff.style', arg)

    if advanced:

        pref = int(session.get('diff_contextlines', 2))
        arg = int(args.get('contextlines', pref))
        if args.has_key('update') and arg != pref:
            session.set_var('diff_contextlines', arg)
        options = ['-U%d' % arg]
        req.hdf.setValue('diff.options.contextlines', str(arg))

        arg = get_bool_option(session, args, 'ignoreblanklines')
        if arg:
            options.append('-B')
        req.hdf.setValue('diff.options.ignoreblanklines', str(arg))

        arg = get_bool_option(session, args, 'ignorecase')
        if arg:
            options.append('-i')
        req.hdf.setValue('diff.options.ignorecase', str(arg))

        arg = get_bool_option(session, args, 'ignorewhitespace')
        if arg:
            options.append('-b')
        req.hdf.setValue('diff.options.ignorewhitespace', str(arg))

        return options

    return []
Exemplo n.º 2
0
    def render(self):
        name = self.args.get('page', 'WikiStart')
        author = self.args.get('author', get_reporter_id(self.req))
        edit_version = self.args.get('edit_version', None)
        delete_ver = self.args.get('delete_ver', None)
        delete_page = self.args.get('delete_page', None)
        comment = self.args.get('comment', '')
        save = self.args.get('save', None)
        edit = self.args.get('edit', None)
        diff = self.args.get('diff', None)
        cancel = self.args.get('cancel', None)
        preview = self.args.get('preview', None)
        history = self.args.get('history', None)
        version = int(self.args.get('version', 0))
        readonly = self.args.get('readonly', None)

        # Ask web spiders to not index old version
        if diff or version:
            self.req.hdf.setValue('html.norobots', '1')

        if cancel:
            self.req.redirect(self.env.href.wiki(name))
            # Not reached

        if delete_ver and edit_version and name:
            # Delete only a specific page version
            self.perm.assert_permission(perm.WIKI_DELETE)
            cursor = self.db.cursor()
            cursor.execute('DELETE FROM wiki WHERE name=%s and version=%s',
                           name, int(edit_version))
            self.db.commit()
            self.log.info('Deleted version %d of page %s' %
                          (int(edit_version), name))
            if int(edit_version) > 1:
                self.req.redirect(self.env.href.wiki(name))
            else:
                # Delete orphaned attachments
                for attachment in self.env.get_attachments(
                        self.db, 'wiki', name):
                    self.env.delete_attachment(self.db, 'wiki', name,
                                               attachment[0])
                self.req.redirect(self.env.href.wiki())
            # Not reached

        if delete_page and name:
            # Delete a wiki page completely
            self.perm.assert_permission(perm.WIKI_DELETE)
            cursor = self.db.cursor()
            cursor.execute('DELETE FROM wiki WHERE name=%s', name)
            self.db.commit()
            self.log.info('Deleted version %d of page ' + name)
            # Delete orphaned attachments
            for attachment in self.env.get_attachments(self.db, 'wiki', name):
                self.env.delete_attachment(self.db, 'wiki', name,
                                           attachment[0])
            self.req.redirect(self.env.href.wiki())
            # Not reached

        self.req.hdf.setValue('wiki.name', escape(name))
        self.req.hdf.setValue('wiki.author', escape(author))
        self.req.hdf.setValue('wiki.comment', escape(comment))
        # Workaround so that we can attach files to wiki pages
        # even if the page name contains a '/'
        self.req.hdf.setValue('wiki.namedoublequoted',
                              urllib.quote(urllib.quote(name, '')))

        session = Session(self.env, self.req)
        editrows = self.args.get('editrows')
        if editrows:
            self.req.hdf.setValue('wiki.edit_rows', editrows)
            pref = session.get('wiki_editrows', '20')
            if editrows != pref:
                session.set_var('wiki_editrows', editrows)
        else:
            self.req.hdf.setValue('wiki.edit_rows',
                                  session.get('wiki_editrows', '20'))

        if save:
            self.req.hdf.setValue('wiki.action', 'save')
        elif edit:
            self.perm.assert_permission(perm.WIKI_MODIFY)
            self.req.hdf.setValue('wiki.action', 'edit')
            self.req.hdf.setValue('title', escape(name) + ' (edit)')
        elif preview:
            self.req.hdf.setValue('wiki.action', 'preview')
            self.req.hdf.setValue('wiki.scroll_bar_pos',
                                  self.args.get('scroll_bar_pos', ''))
            self.req.hdf.setValue('title', escape(name) + ' (preview)')
        elif diff and version > 0:
            self.req.hdf.setValue('wiki.action', 'diff')
            self.generate_diff(name, version)
            self.req.hdf.setValue('title', escape(name) + ' (diff)')
        elif history:
            self.req.hdf.setValue('wiki.action', 'history')
            self.generate_history(name)
            self.req.hdf.setValue('title', escape(name) + ' (history)')
        else:
            self.perm.assert_permission(perm.WIKI_VIEW)
            if version:
                self.add_link('alternate',
                              '?version=%d&format=txt' % version,
                              'Plain Text', 'text/plain')
            else:
                self.add_link('alternate', '?format=txt', 'Plain Text',
                              'text/plain')
            if self.args.has_key('text'):
                del self.args['text']
            self.req.hdf.setValue('wiki.action', 'view')
            if name == 'WikiStart':
                self.req.hdf.setValue('title', '')
            else:
                self.req.hdf.setValue('title', escape(name))
            self.env.get_attachments_hdf(self.db, 'wiki', name, self.req.hdf,
                                         'wiki.attachments')

        self.page = WikiPage(name, version, self.perm, self.db)
        if self.args.has_key('text'):
            self.page.set_content(self.args.get('text'))
        else:
            self.page.modified = 0

        # Modify the read-only flag if it has been changed and the user is WIKI_ADMIN
        if save and self.perm.has_permission(perm.WIKI_ADMIN):
            if readonly:
                self.page.readonly = 1
            else:
                self.page.readonly = 0

        self.req.hdf.setValue('wiki.readonly', str(self.page.readonly))
        # We store the page version when we start editing a page.
        # This way we can stop users from saving changes if they are
        # not based on the latest version any more
        if edit_version:
            self.req.hdf.setValue('wiki.edit_version', edit_version)
        else:
            self.req.hdf.setValue('wiki.edit_version', str(self.page.version))

        if save and edit_version != str(self.page.version - 1):
            raise TracError('Sorry, Cannot create new version, this page has '
                            'already been modified by someone else.')

        if save:
            self.page.commit(author, comment, self.req.remote_addr)
            self.req.redirect(self.env.href.wiki(self.page.name))

        self.req.hdf.setValue('wiki.current_href',
                              escape(self.env.href.wiki(self.page.name)))
        self.req.hdf.setValue(
            'wiki.history_href',
            escape(self.env.href.wiki(self.page.name, history=1)))
        self.req.hdf.setValue('wiki.page_name', escape(self.page.name))
        self.req.hdf.setValue('wiki.page_source', escape(self.page.text))
        out = StringIO.StringIO()
        Formatter(self.req.hdf, self.env, self.db).format(self.page.text, out)
        self.req.hdf.setValue('wiki.page_html', out.getvalue())