Exemplo n.º 1
0
    def pages(self):
        self.pages = {}
        formatings = {'xhtml': (1, 'html'), 'self': (2, 'rst')}
        if not path.exists(self.dst_page_path):
            makedirs(self.dst_page_path)
        print("Migrating pages:")
        with self.dst_db.transaction(logger=info) as dst_cur:
            with self.src_db.transaction(logger=info) as src_cur:
                src_cur.execute("""
                    SELECT text_id, user_id, l.shortcut, t.name, formating,
                        text
                    FROM tb_morias_text AS t
                        LEFT JOIN tb_falias_language AS l
                            ON (l.lang_id = t.lang_id)
                    ORDER BY text_id
                    """)
                for row in src_cur:
                    text_id, user_id, shortcut, name, formating, text = row
                    file_name = "%s_%s.%s" % (Article.make_uri(name),
                                              shortcut.lower(),
                                              formatings[formating][1])
                    dst_cur.execute("""
                        INSERT INTO page_files (author_id, name, title, locale,
                                format)
                            VALUES (%s, %s, %s, %s, %d)
                    """, (self.users.get(user_id, None),
                          file_name, name, shortcut, formatings[formating][0]))
                    page_id = dst_cur.lastrowid

                    dst_path = self.dst_page_path + '/' + file_name
                    with open(dst_path, 'w+') as src:
                        src.write(text.encode('utf-8'))
                    self.pages[text_id] = (page_id,
                                           self.users.get(user_id, None))
                    print("\t%s (%s)" % (name, file_name))
        print("-> need to regenerate outputs if not dynamics are set")
Exemplo n.º 2
0
    def articles(self):
        self.articles = {}
        print("Migrating articles:")
        with self.dst_db.transaction(logger=info) as dst_cur:
            with self.src_db.transaction(logger=info) as src_cur:
                src = "/clanek/(?P<uri>\w+)$"
                dst = '/a/{0}'
                dst_cur.execute("""
                    INSERT INTO redirects (src, dst, code, state)
                        VALUES (%s, %s, 301, 1)
                    """, (src, dst))
                print("\t%s -> %s" % (src, dst))
                src = "/rss_articles.rss"
                dst = '/articles/rss.xml'
                dst_cur.execute("""
                    INSERT INTO redirects (src, dst, code, state)
                        VALUES (%s, %s, 301, 1)
                    """, (src, dst))
                print("\t%s -> %s" % (src, dst))

                src_cur.execute("""
                    SELECT article_id, user_id, category_tree, l.shortcut,
                        create_date, public, forum, title, seo_url, formating,
                        home_text, body_text, counter, rating_count,
                        rating_value
                    FROM tb_morias_article AS a
                        LEFT JOIN tb_falias_language AS l
                            ON (l.lang_id = a.lang_id)
                    ORDER BY article_id
                    """)
                for row in src_cur:
                    article_id, user_id, category_tree, shortcut, create_date, \
                        public, forum, title, seo_url, formating, home_text, \
                        body_text, counter, rating_count, rating_value = row

                    author_id = self.users[user_id]
                    uri = Article.make_uri(title)
                    create_date = int(mktime(create_date.timetuple()))
                    public_date = create_date if public else 0
                    formating = 2 if formating == 'wiki' else 1
                    data = {'visits': counter,
                            'rating_count': rating_count,
                            'rating_value': rating_value,
                            'discussion': bool(forum)}

                    dst_cur.execute("""
                        INSERT INTO articles
                                (uri, author_id, create_date, public_date,
                                 title, locale, perex, body, format, state,
                                 data)
                            VALUES (%s, %d, %d, %d, %s, %s, %s, %s, %d, 2, %s)
                        """, (uri, author_id, create_date, public_date,
                              title, shortcut, home_text, body_text, formating,
                              dumps(data)))
                    article_new_id = dst_cur.lastrowid
                    dst_cur.execute("""
                        INSERT INTO articles_tags (article_id, tag_id)
                            VALUES (%d, %d)
                        """, (article_new_id, self.categories[category_tree]))
                    self.articles[article_id] = (article_new_id, author_id)
                    print("\t%s" % title)
                    if seo_url != uri:
                        src = "/clanek/%s" % seo_url
                        dst = "/a/%s" % uri
                        dst_cur.execute("""
                            INSERT INTO redirects (src, dst, code, state)
                                VALUES (%s, %s, 301, 1)
                            """, (src, dst))
                        print("\t%s \n\t-> %s" % (src, dst))