示例#1
0
def build_search_index():
    """
    Builds the title and full text search indexes.
    """
    if not config.has_xapian:
        print ("You don't have Xapian installed..."
               "skipping configuration of search index.")
        return

    if not os.path.exists(config.search_db_location):
        # create it if it doesn't exist, we don't want to create
        # intermediates though
        os.mkdir(config.search_db_location)

    # prune existing db directories, do this explicitly in case third party
    # extensions use this directory (they really shouldn't)
    for db in ('title', 'text'):
        dbpath = os.path.join(config.search_db_location, db)
        if os.path.exists(dbpath):
            shutil.rmtree(dbpath)

    print "Building search index..."
    from Sycamore import wikiutil, search
    pages = wikiutil.getPageList(req, objects=True)
    for page in pages:
        print "  %s added to search index." % page.page_name
        # don't use remote server on initial build
        search.add_to_index(page, try_remote=False) 
示例#2
0
def revert_to_page(oldpg, request, pg, comment=None, permanent=False,
                   showrc=True):
    _ = request.getText
    if permanent:
        delete_all_newer(oldpg, request, showrc)  
        if not showrc:
            set_current_pagetext(oldpg, request)
    
    try:
        # don't log on RC if admin doesn't want it
        if not (permanent and not showrc):
            pg.saveText(oldpg.get_raw_body(), '0', stripspaces=0, notify=1,
                        comment=comment, action="SAVE/REVERT")
            pagename = pg.proper_name()
        else:
            #doing hard work ourselves..
            # should be abstracted into the page object.
            pg.set_raw_body(oldpg.get_raw_body()) 
            # deal with the case of macros / other items that change state
            # by /not/ being in the page
            search.add_to_index(pg)
            pg.buildCache()
            caching.CacheEntry(pg.page_name, request).clear()
            caching.updateRecentChanges(pg)
            # if we revert to a version with a differently-cased pagename
            pagename = _set_proper_pagename(request, oldpg)
        savemsg = _("Page reverted to version %s" % oldpg.version)
    except pg.Unchanged:
        savemsg = _("The current page is the same as the older page you wish "
                    "to revert to!")
    except pg.SaveError:
        savemsg = _("An error occurred while reverting the page.")
    
    # clear req cache so user sees proper page state (exist)
    request.req_cache['pagenames'][(
        pagename.lower(), request.config.wiki_name)] = pagename

    return savemsg
示例#3
0
def revert_to_page(oldpg, request, pg, comment=None, permanent=False,
                   showrc=True):
    _ = request.getText
    if permanent:
        delete_all_newer(oldpg, request, showrc)  
        if not showrc:
            set_current_pagetext(oldpg, request)
    
    try:
        # don't log on RC if admin doesn't want it
        if not (permanent and not showrc):
            pg.saveText(oldpg.get_raw_body(), '0', stripspaces=0, notify=1,
                        comment=comment, action="SAVE/REVERT")
            pagename = pg.proper_name()
        else:
            #doing hard work ourselves..
            # should be abstracted into the page object.
            pg.set_raw_body(oldpg.get_raw_body()) 
            # deal with the case of macros / other items that change state
            # by /not/ being in the page
            search.add_to_index(pg)
            pg.buildCache()
            caching.CacheEntry(pg.page_name, request).clear()
            caching.updateRecentChanges(pg)
            # if we revert to a version with a differently-cased pagename
            pagename = _set_proper_pagename(request, oldpg)
        savemsg = _("Page reverted to version %s" % oldpg.version)
    except pg.Unchanged:
        savemsg = _("The current page is the same as the older page you wish "
                    "to revert to!")
    except pg.SaveError:
        savemsg = _("An error occurred while reverting the page.")
    
    # clear req cache so user sees proper page state (exist)
    request.req_cache['pagenames'][(
        pagename.lower(), request.config.wiki_name)] = pagename

    return savemsg
示例#4
0
文件: farm.py 项目: ivanov/sycamore
def build_search_index(request):
    pages = wikiutil.getPageList(request, objects=True)
    for page in pages:
        search.add_to_index(page)
示例#5
0
    def saveText(self, newtext, datestamp, **kw):
        """
        Save new text for a page.

        @param newtext: text to save for this page
        @param datestamp: ...
        @keyword stripspaces: strip whitespace from line ends (default: 0)
        @keyword notify: send email notice tp subscribers (default: 0)
        @keyword comment: comment field (when preview is true)
        @keyword action: action for log (default: SAVE)
        @keyword proper_name: properly-cased pagename (for renames)
        @keyword ignore_edit_conflicts: force a save regardless of status
                                        (boolean)
        @keyword force_save: ignore "page content the same" error
        @rtype: string
        @return: error msg
        """
        self.page_name = self.page_name.strip() # to ensure consistency
        _ = self._
        newtext = self._normalize_text(newtext, **kw)

        # for inline editing we want things to be as smooth as we can
        no_save_msg = False
        if (self.request.form.has_key('no_save_msg') and
            self.request.form['no_save_msg'][0]):
            no_save_msg = True

        msg = ""
        merged_changes = False
        ignore_edit_conflicts = kw.get('ignore_edit_conflicts', False)
        if not self.request.user.may.save(self, newtext, datestamp, **kw):
            msg = _('You are not allowed to edit this page!')
            raise self.AccessDenied, msg
        elif not newtext.strip():
            msg = _('You cannot save empty pages.')
            raise self.EmptyPage, msg
        elif (not ignore_edit_conflicts and datestamp != '0' and
              (datestamp < self.mtime()) and self.exists()):
            from Sycamore.util import diff3
            savetext = newtext
            original_text = Page(self.page_name, self.request,
                                 prev_date=datestamp).get_raw_body()
            saved_text = self.get_raw_body()
            verynewtext, had_conflict = diff3.text_merge(
                original_text, saved_text, savetext,
                marker1='----- /!\ Edit conflict! Your version: -----\n',
                marker2='----- /!\ Edit conflict! Other version: -----\n',
                marker3='----- /!\ End of edit conflict -----\n')
            msg = _("Someone else changed this page while you were editing.")

            if (had_conflict and self.request.user.valid and
                (self.request.user.id == self.last_edit_info()[1])):
               # user pressed back button or did something weird
               had_conflict = False
               msg = None
            else:
                # we did some sort of merging or we had a conflict,
                # so let them know
                if had_conflict:
                    raise self.EditConflict, (msg, verynewtext)
                merged_changes = True
                msg = _("""%s Your changes were successfully merged! """ % msg)
                newtext = verynewtext
        elif (newtext == self.get_raw_body() and not
              self._rename_lowercase_condition() and
              kw.get('action') != 'SAVE/REVERT' and not
              kw.get('force_save')):
            # check to see if they're saving the page with the same content
            # it had before
            msg = _('You did not change the page content, not saved!')
            raise self.Unchanged, msg
        elif (config.max_page_size and 
            len(newtext.encode(config.charset)) > (config.max_page_size*1024)):
            msg = _('This page is too big!  Pages can be, at most, %sK.  '
                    'Consider splitting the page up into multiple pages '
                    'instead!' % config.max_page_size)
            raise self.TooBig, msg

        # save only if no error occured (msg is empty)
        if not msg or merged_changes:
            # set success msg
            if not merged_changes and not no_save_msg:
              msg = _("Thank you for your changes. "
                      "Your attention to detail is appreciated. ")

            # determine action for edit logging
            action = kw.get('action', 'SAVE')
            if action=='SAVE' and not self.exists():
                action = 'SAVENEW'
                           
            # write the page file
            mtime = self._write_to_db(newtext, action, kw.get('comment',''),
                                      self.request.remote_addr,
                                      kw.get('proper_name',None))
            # deal with the case of macros / other items that change state by
            # /not/ being in the page
            wikiutil.macro_delete_checks(self)
            
            # we'll try to change the stats early-on
            if self.request.user.name:
                self.userStatAdd(self.request.user, action, self.page_name)

            # add the page to the search index or update its index
            if action != 'DELETE':
                search.add_to_index(self)

            # note the change in recent changes.  this explicit call is needed
            # because of the way we cache our change information
            caching.updateRecentChanges(self)

            return msg
示例#6
0
    def saveText(self, newtext, datestamp, **kw):
        """
        Save new text for a page.

        @param newtext: text to save for this page
        @param datestamp: ...
        @keyword stripspaces: strip whitespace from line ends (default: 0)
        @keyword notify: send email notice tp subscribers (default: 0)
        @keyword comment: comment field (when preview is true)
        @keyword action: action for log (default: SAVE)
        @keyword proper_name: properly-cased pagename (for renames)
        @keyword ignore_edit_conflicts: force a save regardless of status
                                        (boolean)
        @keyword force_save: ignore "page content the same" error
        @rtype: string
        @return: error msg
        """
        self.page_name = self.page_name.strip()  # to ensure consistency
        _ = self._
        newtext = self._normalize_text(newtext, **kw)

        # for inline editing we want things to be as smooth as we can
        no_save_msg = False
        if (self.request.form.has_key('no_save_msg')
                and self.request.form['no_save_msg'][0]):
            no_save_msg = True

        msg = ""
        merged_changes = False
        ignore_edit_conflicts = kw.get('ignore_edit_conflicts', False)
        if not self.request.user.may.save(self, newtext, datestamp, **kw):
            msg = _('You are not allowed to edit this page!')
            raise self.AccessDenied, msg
        elif not newtext.strip():
            msg = _('You cannot save empty pages.')
            raise self.EmptyPage, msg
        elif (not ignore_edit_conflicts and datestamp != '0'
              and (datestamp < self.mtime()) and self.exists()):
            from Sycamore.util import diff3
            savetext = newtext
            original_text = Page(self.page_name,
                                 self.request,
                                 prev_date=datestamp).get_raw_body()
            saved_text = self.get_raw_body()
            verynewtext, had_conflict = diff3.text_merge(
                original_text,
                saved_text,
                savetext,
                marker1='----- /!\ Edit conflict! Your version: -----\n',
                marker2='----- /!\ Edit conflict! Other version: -----\n',
                marker3='----- /!\ End of edit conflict -----\n')
            msg = _("Someone else changed this page while you were editing.")

            if (had_conflict and self.request.user.valid
                    and (self.request.user.id == self.last_edit_info()[1])):
                # user pressed back button or did something weird
                had_conflict = False
                msg = None
            else:
                # we did some sort of merging or we had a conflict,
                # so let them know
                if had_conflict:
                    raise self.EditConflict, (msg, verynewtext)
                merged_changes = True
                msg = _("""%s Your changes were successfully merged! """ % msg)
                newtext = verynewtext
        elif (newtext == self.get_raw_body()
              and not self._rename_lowercase_condition()
              and kw.get('action') != 'SAVE/REVERT'
              and not kw.get('force_save')):
            # check to see if they're saving the page with the same content
            # it had before
            msg = _('You did not change the page content, not saved!')
            raise self.Unchanged, msg
        elif (config.max_page_size and len(newtext.encode(config.charset)) >
              (config.max_page_size * 1024)):
            msg = _('This page is too big!  Pages can be, at most, %sK.  '
                    'Consider splitting the page up into multiple pages '
                    'instead!' % config.max_page_size)
            raise self.TooBig, msg

        # save only if no error occured (msg is empty)
        if not msg or merged_changes:
            # set success msg
            if not merged_changes and not no_save_msg:
                msg = _("Thank you for your changes. "
                        "Your attention to detail is appreciated. ")

            # determine action for edit logging
            action = kw.get('action', 'SAVE')
            if action == 'SAVE' and not self.exists():
                action = 'SAVENEW'

            # write the page file
            mtime = self._write_to_db(newtext, action, kw.get('comment', ''),
                                      self.request.remote_addr,
                                      kw.get('proper_name', None))
            # deal with the case of macros / other items that change state by
            # /not/ being in the page
            wikiutil.macro_delete_checks(self)

            # we'll try to change the stats early-on
            if self.request.user.name:
                self.userStatAdd(self.request.user, action, self.page_name)

            # add the page to the search index or update its index
            if action != 'DELETE':
                search.add_to_index(self)

            # note the change in recent changes.  this explicit call is needed
            # because of the way we cache our change information
            caching.updateRecentChanges(self)

            return msg
示例#7
0
        timenow = time.time()
        # search location is created by buildDB, but
        # let's make it in case it got removed 
        if not os.path.exists(config.search_db_location):
            os.mkdir(config.search_db_location)
        tmp_db_location = os.path.join(os.path.join(config.search_db_location,
            ".."), "search.%s" % timenow)
        os.mkdir(tmp_db_location)

        wiki_list = wikiutil.getWikiList(req)
        for wiki_name in wiki_list:
            req.switch_wiki(wiki_name)
            plist = wikiutil.getPageList(req)
            for pagename in plist:
                page = Page(pagename, req, wiki_name=wiki_name)
                search.add_to_index(page, db_location=tmp_db_location,
                                    try_remote=False)
                print "  -->", page.page_name.encode(config.charset)

        req.db_disconnect()

        # make sure search directory is set up properly
        if not os.path.exists(config.search_db_location):
            os.mkdir(config.search_db_location)
        if not os.path.exists(os.path.join(config.search_db_location,
                                           "title")):
            os.mkdir(os.path.join(config.search_db_location, "title"))
        if not os.path.exists(os.path.join(config.search_db_location,
                                           "text")):
            os.mkdir(os.path.join(config.search_db_location, "text"))

        # in posix os.rename is atomic