예제 #1
0
    def mainloop(self):
        # Insert config dir or the current directory to the start of the
        # path.
        config_dir = self.options.config_dir
        if config_dir and not os.path.isdir(config_dir):
            _util.fatal("bad path given to --config-dir option")
        sys.path.insert(0, os.path.abspath(config_dir or os.curdir))

        # Create request 
        if self.options.wiki_url:
            self.request = RequestCLI(self.options.wiki_url)
        else:
            self.request = RequestCLI()
        self.command()
예제 #2
0
    def mainloop(self):
        """ moin-dump's main code. """

        if len(sys.argv) == 1:
            self.parser.print_help()
            sys.exit(1)

        # Prepare output directory
        outputdir = self.args[0]
        outputdir = os.path.abspath(outputdir)
        try:
            os.mkdir(outputdir)
            _util.log("Created output directory '%s'!" % outputdir)
        except OSError, err:
            if err.errno != errno.EEXIST:
                _util.fatal("Cannot create output directory '%s'!" % outputdir)
예제 #3
0
    def mainloop(self):
        """ moin-dump's main code. """

        if len(sys.argv) == 1:
            self.parser.print_help()
            sys.exit(1)

        # Prepare output directory
        outputdir = self.args[0]
        outputdir = os.path.abspath(outputdir)
        if not os.path.isdir(outputdir):
            try:
                os.mkdir(outputdir)
                _util.log("Created output directory '%s'!" % outputdir)
            except OSError:
                _util.fatal("Cannot create output directory '%s'!" % outputdir)

        # Load the configuration
        configdir = self.options.configdir
        if configdir:
            if os.path.isfile(configdir):
                configdir = os.path.dirname(configdir)
            if not os.path.isdir(configdir):
                _util.fatal("Bad path given to --config parameter")
            configdir = os.path.abspath(configdir)
            sys.path[0:0] = [configdir]
            os.chdir(configdir)

        # Dump the wiki
        request = RequestCLI(self.options.wiki_url)
        request.form = request.args = request.setup_args()

        # fix url_prefix so we get relative paths in output html
        request.cfg.url_prefix = url_prefix

        if self.options.page:
            pages = [self.options.page]
        else:
            # Get all existing pages in the wiki
            pages = list(request.rootpage.getPageList(user=""))
        pages.sort()

        wikiutil.quoteWikinameURL = lambda pagename, qfn=wikiutil.quoteWikinameFS: (qfn(pagename) + HTML_SUFFIX)

        errfile = os.path.join(outputdir, "error.log")
        errlog = open(errfile, "w")
        errcnt = 0

        page_front_page = wikiutil.getSysPage(request, "FrontPage").page_name
        page_title_index = wikiutil.getSysPage(request, "TitleIndex").page_name
        page_word_index = wikiutil.getSysPage(request, "WordIndex").page_name

        navibar_html = ""
        for p in [page_front_page, page_title_index, page_word_index]:
            navibar_html += '&nbsp;[<a href="%s">%s</a>]' % (wikiutil.quoteWikinameFS(p), wikiutil.escape(p))

        for pagename in pages:
            file = wikiutil.quoteWikinameURL(pagename)  # we have the same name in URL and FS
            _util.log('Writing "%s"...' % file)
            try:
                pagehtml = ""
                page = Page.Page(request, pagename)
                try:
                    request.reset()
                    out = StringIO.StringIO()
                    request.redirect(out)
                    page.send_page(request, count_hit=0, content_only=1)
                    pagehtml = out.getvalue()
                    request.redirect()
                except:
                    errcnt = errcnt + 1
                    print >>sys.stderr, "*** Caught exception while writing page!"
                    print >> errlog, "~" * 78
                    print >> errlog, file  # page filename
                    import traceback

                    traceback.print_exc(None, errlog)
            finally:
                timestamp = time.strftime("%Y-%m-%d %H:%M")
                filepath = os.path.join(outputdir, file)
                fileout = codecs.open(filepath, "w", config.charset)
                fileout.write(
                    page_template
                    % {
                        "charset": config.charset,
                        "pagename": pagename,
                        "pagehtml": pagehtml,
                        "logo_html": logo_html,
                        "navibar_html": navibar_html,
                        "timestamp": timestamp,
                        "theme": request.cfg.theme_default,
                    }
                )
                fileout.close()

        # copy FrontPage to "index.html"
        indexpage = page_front_page
        if self.options.page:
            indexpage = self.options.page
        shutil.copyfile(
            os.path.join(outputdir, wikiutil.quoteWikinameFS(indexpage) + HTML_SUFFIX),
            os.path.join(outputdir, "index" + HTML_SUFFIX),
        )

        errlog.close()
        if errcnt:
            print >>sys.stderr, "*** %d error(s) occurred, see '%s'!" % (errcnt, errfile)
    def mainloop(self):
        """ moin-usercheck's main code.
        """
        import os, sys

        # we don't expect non-option arguments
        if len(self.args) != 0:
            self.parser.error("incorrect number of arguments")

        # check for correct option combination
        flags_given = (
               self.options.usersunique 
            or self.options.emailsunique 
            or self.options.wikinames)

        if flags_given and self.options.disableuser:
            # XXX: why is this? only because the former option parser code was braindead?
            self.parser.error("--disableuser can't be combined with other options!")

        # no option given ==> show usage
        if not (flags_given or self.options.disableuser):
            self.parser.print_help()
            sys.exit(1)

        #
        # Load the configuration
        #
        configdir = self.options.configdir
        if configdir:
            if os.path.isfile(configdir): configdir = os.path.dirname(configdir)
            if not os.path.isdir(configdir):
                _util.fatal("Bad path %r given to --config parameter" % configdir)
            configdir = os.path.abspath(configdir)
            sys.path[0:0] = [configdir]
            os.chdir(configdir)

        global config
        from MoinMoin import config
        if config.default_config:
            _util.fatal("You have to be in the directory containing wikiconfig.py, "
                "or use the --config option!")

        # XXX: globals bad bad bad!
        #global users, names, emails, uids_noemail
        users = {} # uid : UserObject
        names = {} # name : [uid, uid, uid]
        emails = {} # email : [uid, uid, uid]
        uids_noemail = {} # uid : name

        # XXX: Refactor to methods!
        from MoinMoin import user, wikiutil

        def collect_data():
            import re

            for uid in user.getUserList():
                u = user.User(None, uid)
                users[uid] = u
        
                # collect name duplicates:
                if names.has_key(u.name):
                    names[u.name].append(uid)
                else:
                    names[u.name] = [uid]
        
                # collect email duplicates:
                if u.email:
                    if emails.has_key(u.email):
                        emails[u.email].append(uid)
                    else:
                        emails[u.email] = [uid]
        
                # collect account with no or invalid email address set:
                if not u.email or not re.match(".*@.*\..*", u.email):
                    uids_noemail[uid] = u.name
        
        
        def hasmagicpage(uid):
            u = users[uid]
            return u.isSubscribedTo(magicpages)
        
        
        def disableUser(uid):
            u = users[uid]
            print " %-20s %-25s %-35s" % (uid, u.name, u.email),
            keepthis = hasmagicpage(uid)
            if keepthis:
                print "- keeping (magicpage)!"
                u.save() # update timestamp, so this will be latest next run
            elif not u.disabled: # only disable once
                u.disabled = 1
                u.name = "%s-%s" % (u.name, uid)
                if u.email:
                    u.email = "%s-%s" % (u.email, uid)
                u.subscribed_pages = "" # avoid using email
                if self.options.save:
                    u.save()
                    print "- disabled."
                else:
                    print "- would be disabled."
        
        
        def getsortvalue(uid,user):
            t_ls = float(user.last_saved) # when user did last SAVE of his account data
            if self.options.lastsaved:
                return t_ls
            else: # last USED (we check the page trail for that)
                try:
                    t_lu = float(os.path.getmtime(os.path.join(config.user_dir, uid+".trail")))
                except OSError:
                    t_lu = t_ls # better than having nothing
                return t_lu
        
        
        def process(uidlist):
            sortlist = []
            for uid in uidlist:
                u = users[uid]
                sortlist.append((getsortvalue(uid,u),uid))
            sortlist.sort()
            #print sortlist
            # disable all, but the last/latest one
            for t,uid in sortlist[:-1]:
                disableUser(uid)
            # show what will be kept
            uid = sortlist[-1][1]
            u = users[uid]
            print " %-20s %-25s %-35s - keeping%s!" % (uid, u.name, u.email, hasmagicpage(uid) and " (magicpage)" or "")
        
        
        def make_users_unique():
            for name in names.keys():
                if len(names[name])>1:
                    process(names[name])
        
        
        def make_emails_unique():
            for email in emails.keys():
                if len(emails[email])>1:
                    process(emails[email])
        
        
        # taken from string.py of py 2.3
        def capwords(s, sep=None):
            """capwords(s, [sep]) -> string
        
            Split the argument into words using split, capitalize each
            word using capitalize, and join the capitalized words using
            join. Note that this replaces runs of whitespace characters by
            a single space.                                          
                                                                     
            """
            import string
            return string.join(map(string.capitalize, s.split(sep)), sep or ' ')
        
        
        def make_WikiNames():
            for uid in users.keys():
                u = users[uid]
                if u.disabled: continue
                if not wikiutil.isStrictWikiname(u.name):
                    newname = capwords(u.name).replace(" ","").replace("-","")
                    if not wikiutil.isStrictWikiname(newname):
                        print " %-20s %-25s - no WikiName, giving up" % (uid, u.name)
                    else:
                        print " %-20s %-25s - no WikiName -> %s" % (uid, u.name, newname)
                        if self.options.save:
                            u.name = newname
                            u.save()

        collect_data()
        if self.options.disableuser:
            disableUser(self.options.disableuser)
        else:
            if self.options.usersunique:
                make_users_unique()
            if self.options.emailsunique: 
                make_emails_unique()
            if self.options.wikinames:
                make_WikiNames()