修复文章号不连续问题,Python解决方案

恰逢搬迁新写一篇基于Python的解决方案,干掉断号,并大幅度简化手工处理过程。

步骤一:对比删除数据表中的revision、autosave等
如果确保所有revision与autosave都是旧的,只保留post;
那么直接安装Disable revision插件并启用即可,可自动删除所有revision与autosave

如果需要对比分析,那只能派出“人脑”了。
以下使用python语言导出所有多版本文章,可利用beyond compare加以对比分析
选出值得保留者,删除其它

# -*- coding: utf8 -*-
# MySQLAdapter.py
# Author: Jiangmf
# Date: 2014-07-09
import MySQLdb
import os

class MySQLAdapter(object):
    default_file = os.path.split(os.path.realpath(__file__))[0] + "\\my.cnf"

    @classmethod
    def mysql_select(cls, sql):
    
        try:
            print "Connecting to mysql server..."
            conn = MySQLdb.connect(read_default_file=cls.default_file, charset='utf8')
            cur = conn.cursor()
            count = cur.execute(sql)
    
            results = cur.fetchall()
            conn.commit()
            cur.close()
            conn.close()
        except MySQLdb.Error, e:
            print "Mysql Error %d: %s" % (e.args[0], e.args[1])
            exit
        print "Mysql server returns %d results." % (count)
        return [count, results]
    
    @classmethod
    def mysql_update(cls, sql):
        try:
            print "Connecting to mysql server..."
            conn = MySQLdb.connect(read_default_file=cls.default_file, charset='utf8')
            cur = conn.cursor()
            count = cur.execute(sql)
            conn.commit()
            cur.close()
            conn.close()
    
        except MySQLdb.Error, e:
            print "Mysql Error %d: %s" % (e.args[0], e.args[1])
            exit
        print "Mysql server returns %d results." % (count)
        return count
    
    @classmethod
    def mysql_update_many(cls, sqls):
        try:
            print "Connecting to mysql server..."
            conn = MySQLdb.connect(read_default_file=cls.default_file, charset='utf8')
            cur = conn.cursor()
            for sql in sqls:
                count = cur.execute(sql)
            conn.commit()
            cur.close()
            conn.close()
    
        except MySQLdb.Error, e:
            print "Mysql Error %d: %s" % (e.args[0], e.args[1])
            exit
        print "Mysql server returns %d results." % (count)
        return count
    
    @classmethod
    def set_default_file(cls, file="my.cnf", relative=True):
        if(relative):
            os.path.split(os.path.realpath(__file__))[0] + "\\" + file
        else:
            cls.default_file = file

pass
# -*- coding: utf8 -*-
# Post_Export.py
# Author: Jiangmf
# Date: 2015-04-17
from MySQLAdapter import *
import codecs

def sync_post_files(results):
    for r in results:
        file = codecs.open(r[0]+'.post', "w", "utf-8")
        file.write(r[1])
        file.flush()
        file.close()
    pass


def get_posts():
    [count, results] = MySQLAdapter.mysql_select(
        "SELECT * FROM `wp_posts` WHERE `ID` IN ( select distinct `post_parent` FROM `wp_posts`)")
    return map(lambda x:[str(x[0]), x[4]], results)

def get_revisions():
    [count, results] = MySQLAdapter.mysql_select(
        "SELECT * FROM `wp_posts` WHERE `post_parent` != 0")
    return map(lambda x:[str(x[17])+'-'+str(x[0]), x[4]], results)
# Script
if __name__ == "__main__":
    sync_post_files(get_posts())
    sync_post_files(get_revisions())

步骤二:利用python生成sql改id脚本

# -*- coding: utf8 -*-
# Post_Renumber_sql.py
# Author: Jiangmf
# Date: 2015-04-17
from MySQLAdapter import *
import codecs

def sync_post_files(results):
    for r in results:
        file = codecs.open(r[0]+'.post', "w", "utf-8")
        file.write(r[1])
        file.flush()
        file.close()
    pass

def out_sql(seq):
    file = codecs.open('renumber.sql', "w", "utf-8")
    for x,y in seq:
        if(x!=y):
            file.write("update wp_posts set id = "+ str(x) +", guid = 'http://donneryst.com/blog/?p="+str(x)+"' where id = "+str(y)+";\n")
            file.write("update wp_term_relationships set object_id = "+str(x)+" where object_id = "+str(y)+";\n")
            file.write("update wp_postmeta set post_id = "+str(x)+" where post_id = "+str(y)+";\n")
            file.write("update wp_comments set comment_post_ID = "+str(x)+" where comment_post_ID = "+str(y)+";\n")

def get_post_ids():
    [count, results] = MySQLAdapter.mysql_select(
        "SELECT `ID` FROM `wp_posts` order by `ID`")
    return zip(range(1, count+1),map(lambda x:x[0], results))

# Script
if __name__ == "__main__":
    out_sql(get_post_ids())

步骤三:将wp_posts表导出再导入,以刷新计数

步骤四:重建其它插件针对的文章号做出的数据表