Home > Tech > Wordpress DB Insertion

Wordpress DB Insertion

January 6th, 2008

In the old layout I had a series of pages which were copies of a column I had written for my dorm’s news some 15 years ago. The most recent iterations of these pages were done in php. All the php really did was control the layout and such but instead of converting them by hand I figured I’d write a python script to do all the heavy lifting. It was fairly successful.

import os, os.path, re, datetime
import MySQLdb

srcDir = '/tmp/working'

db = MySQLdb.connect(host='xxxxx', db='xxxxx', user='xxxxx', passwd='xxxxx')
c = db.cursor()

for root, dir, files in os.walk(srcDir):
  for file in files:
    date=editor=volume=number=None
    fileNum = file.replace('zen', '').replace('.php', '')
    try:
      # Eliminate files which aren't the ones I'm trying to convert
      fileNum = int(fileNum)
    except ValueError, e:
      continue

    # The Slug
    name = 'zen-%s' % fileNum

    text = open(os.path.join(root, file)).read()

    # Extract the Date (or a string of any sort) from a variable
    matches = re.search('$Date/s*=/s*["']([^'"]+)["']', text)
    if matches != None:
      date= matches.group(1)
    elif date == None:
      date = 'September 1, 1992'
      date = datetime.datetime.strptime(date, '%B %d, %Y')
    # Extract a number
    matches = re.search('$Volume/s*=/s*([/d]+)', text)
    if matches != None:
      volume= matches.group(1)
    # Snip volume and other extractions

    title = "Sir High Lord Zen - Volume %s, Number %s" % (volume, number)

    # Pull off the header
    text = text.split('WriteNavControls', 1)[1]
    text = text.split('?>', 1)[1]
    # Pull off the footer
    text = text.split('?>')[-1]
    # In the wordpress db n (linefeeds) mean something so I need
    # replace them with spaces in the source text.
    text = text.replace('n', ' ')

    #print text

    sql = """
      insert into wp_posts
      (post_author, post_date, post_date_gmt, post_content,
      post_title, post_status, post_name, post_modified,
      post_modified_gmt, post_type) values
      (2, %s, %s, %s, %s, 'publish', %s, %s,
      %s, 'post')
      """

    c.execute(sql, (date, date, text, title, name, date, date))

So the only real problem is that no category is created. Opening the created post and saving it will add the post to the ‘Uncategorized’ category. You can always add it to another category at that point instead.

There is probably some sql that could be written to automatically add an entry to the wp_post2cat table for any post that lacks such and entry but after five minutes I couldn’t think of simple way of doing it and since it was only a few dozen posts I just opened and saved them all.

insert into wp_post2cat (post_id, category_id) values (412, 22);

Will add a single post. If you have a large number of posts to add my script it would be pretty easy to write a script to generate a number of sql statements to populate the wp_post2cat table.

Tech

  1. No comments yet.
  1. No trackbacks yet.