Skip to content

Instantly share code, notes, and snippets.

@wodim
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wodim/6d0e0353693affc55e91 to your computer and use it in GitHub Desktop.
Save wodim/6d0e0353693affc55e91 to your computer and use it in GitHub Desktop.
Script para migrar de escriure a WordPress
# -*- coding: utf-8 -*-
import sqlite3
import MySQLdb
import os
import sys
import mimetypes
admin_nick = "wodim"
admin_email = "lalala@lalala.com"
escriure_db = "/shared/escriure.sqlite"
wordpress_blobs = "/shared/wordpress/wp-content/uploads/blobs"
# we will look for these in every post...
blob_formats = ("http://blog.vortigaunt.net/blob/ID/", "http://blog.vortigaunt.net/blob/ID", "/blob/ID/", "/blob/ID",)
# and then change them all for this one
new_blob_format = "/wp-content/uploads/blobs/ID"
if not os.path.isdir(wordpress_blobs):
print "The directory where blobs will be extracted does not exist. Create it."
print "It currently is: {0}".format(wordpress_blobs)
sys.exit(1)
sqlite_con = sqlite3.connect("/shared/escriure.sqlite")
sqlite_con.row_factory = sqlite3.Row
sqlite_cur = sqlite_con.cursor()
mysql_con = MySQLdb.connect(host="localhost", user="wordpress", passwd="wordpress", db="wordpress", charset="utf8")
mysql_cur = mysql_con.cursor(MySQLdb.cursors.DictCursor)
# First, we find and extract the blobs. These come first because later we have to
# identify blobs in posts and change them.
sqlite_cur.execute("SELECT * FROM blobs")
blobs = sqlite_cur.fetchall()
for blob in blobs:
extension = mimetypes.guess_extension(blob["mimetype"])
if extension == ".jpe":
extension = ".jpeg"
if not extension:
extension = ".bin"
filename = blob["name"] + extension
file = open(os.path.join(wordpress_blobs, filename), "w")
file.write(blob["content"])
file.close()
sqlite_cur.execute("SELECT * FROM posts")
posts = sqlite_cur.fetchall()
# and then posts.
for post in posts:
##### INSERT POST
# fix some fields
if post["status"] == "published":
post_status = "publish"
else:
post_status = "draft"
if post["comment_status"] == "open":
comment_status = "open"
else:
comment_status = "closed"
# actually insert
mysql_cur.execute("""
INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_status, comment_status, ping_status, post_name,
post_modified, post_modified_gmt, post_parent, guid, menu_order, post_type, comment_count)
VALUES (%s, FROM_UNIXTIME(%s), FROM_UNIXTIME(%s), %s, %s, %s, %s, %s, %s, FROM_UNIXTIME(%s), FROM_UNIXTIME(%s), %s, %s, %s, %s, %s)""",
(1, post["timestamp"], post["timestamp"] - 7200, post["text_html"], post["title"], post_status, comment_status, "open", post["permaid"],
post["timestamp"], post["timestamp"] - 7200, 0, "", 0, "post", post["comment_count"],))
##### INSERT COMMENTS
post_id = mysql_cur.lastrowid
sqlite_cur.execute("SELECT * FROM comments WHERE post_id = ?", (post["id"],))
comments = sqlite_cur.fetchall()
comment_id = 0
# we have to use a while + .pop() here because we have to be able to add new items to the list dynamically.
while comments:
comment = comments.pop()
# fix fields
if "is_reply" in comment:
comment_parent = last_comment_id
else:
comment_parent = 0
if comment["status"] == "shown":
comment_approved = 1
else:
comment_approved = 0
if comment["nick"] == admin_nick:
comment_author = 1
else:
comment_author = 0
mysql_cur.execute("""
INSERT INTO wp_comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP,
comment_date, comment_date_gmt, comment_content, comment_karma, comment_approved, comment_parent, user_id)
VALUES (%s, %s, %s, %s, %s, FROM_UNIXTIME(%s), FROM_UNIXTIME(%s), %s, %s, %s, %s, %s)""",
(post_id, comment["nick"], comment["mail"], comment["url"], comment["ip"],
comment["timestamp"], comment["timestamp"] - 7200, comment["text"], 0, comment_approved, comment_parent, comment_author,))
last_comment_id = mysql_cur.lastrowid
# "inject" a fake comment for replies
if comment["reply"]:
new_comment = {"nick": admin_nick, "mail": admin_email, "url": "", "ip": "127.0.0.1", "timestamp": comment["timestamp"] + 3600,
"text": comment["reply"], "is_reply": True, "author": 1, "status": "shown", "reply": None,}
comments.append(new_comment)
mysql_con.commit()
mysql_con.close()
sqlite_con.close()
print "Bye"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment