James Gardner: Home > Work > Code > SiteTool > 0.3.0 > Wordpress Support

SiteTool v0.3.0 documentation

Wordpress Support

If you are used to using a WordPress blog powered by MySQL but instead have decided you would like to manage the site manually in reStructuredText format the sitetool.wordpress module could be useful.

Note

These instructions assume you have written all your blog posts in reStructuredText as described here: http://jimmyg.org/2007/09/01/using-restructuredtext-with-wordpress/

Exporting the Posts

To use it you’ll need the MySQLdb module:

$ easy_install mysql-python

You can then export your blog like this:

env/bin/python export_blog.py run -H pauli.3aims.com -p -u web6_u1 web6_db1

One error you are likely to see if you are not running the script on the same server as the MySQL database is this:

_mysql_exceptions.OperationalError: (1130, "Host 'host-84-9-43-197.dslgb.com' is not allowed to connect to this MySQL server")

To fix this you need to grant access from the machine you are using. You can do this by connecting to your MySQL database and running this command:

mysql> USE mysql;
mysql> GRANT SELECT ON web6_db1.* TO 'web6_u1'@'host-84-9-43-197.dslgb.com' IDENTIFIED BY 'password';
mysql> GRANT SELECT ON web6_db1.* TO 'web6_u1'@'%' IDENTIFIED BY 'password';

You’ll need to replace web6_db1 with the wordpress database name, web6_u1 with your username, password with your password and host-84-9-43-197.dslgb.com with the hostname which appeared in the error message you received.

It is important that the host you specify for the host option to export() is one of the hosts the server recognises for itself. You can check this by ensuring it is in /etc/hosts on the server. For example the MySQL server I’m using thinks it is at pauli.3aims.com not 3aims.com so this is what I should use.

Now try again.

Building the HTML

You have the posts in reStructuredText, now you need to produce HTML with a template. Assuming you want to apply a Dreamweaver template you would do this:

env/bin/python -m sitetool.wordpress html JimmyG/blog/ JimmyG/Templates/jimmyg.dwt -q

Building the Index Pages

Now the HTML files are built you will want summary pages for:

  • the 20 latest posts
  • all the posts in each year
  • all the posts with each tag

You will also want some section links for each post so you can navigate between each year. This will be generated in the Library/blog_section_nav.lbi Library Items file.

You can build these automatically like this:

env/bin/python -m sitetool.wordpress index JimmyG/blog/ JimmyG/Templates/jimmyg.dwt -q

Regenerating the Site

The pages are all in place but they are not integrated into the site. The breadcrumbs, navigation and other elements are not up to data and the other pages in the site don’t know about the blog posts yet.

Regenerate the site like this:

env/bin/python -m sitetool.dreamweaver regenerate JimmyG/

Build a Sitemap

With everything in place you probably want the blog pages included in the sitemap. Update it like this:

env/bin/python -m sitetool.dreamweaver sitemap JimmyG/ JimmyG/Templates/jimmyg.dwt

Downloading the Images

The easiest approach is to just copy them to your directory using SCP, FTP or similar. The files you’ve uploaded will be in the wp-content/uploads directory. Here’s what I did:

$ scp -r web6_james@jimmyg.org:web/wp-content/uploads ./

Of course, you may not want to keep the images in the same place on the new server. I wanted to put the images in the same directories as the files they were associated in each year.

To update the links you can use these commands in the blog directory:

$ grep -rl http://jimmyg.org/wp-content/uploads/2009 . | xargs perl -pi -w -e 's/http:\/\/jimmyg.org\/wp-content\/uploads\/2009/..\/2009/g;'
$ grep -rl http://jimmyg.org/wp-content/uploads/2008 . | xargs perl -pi -w -e 's/http:\/\/jimmyg.org\/wp-content\/uploads\/2008/..\/2008/g;'
$ grep -rl http://jimmyg.org/wp-content/uploads/2007 . | xargs perl -pi -w -e 's/http:\/\/jimmyg.org\/wp-content\/uploads\/2007/..\/2007/g;'
$ grep -rl http://jimmyg.org/wp-content/uploads/2006 . | xargs perl -pi -w -e 's/http:\/\/jimmyg.org\/wp-content\/uploads\/2006/..\/2006/g;'
$ grep -rl http://jimmyg.org/wp-content/uploads/2005 . | xargs perl -pi -w -e 's/http:\/\/jimmyg.org\/wp-content\/uploads\/2005/..\/2005/g;'

The escaping is important. Also, I’ve used ../2007 rather than nothing in case some of the posts link to uploads from different years. Repeat this command for all the years you want to update and then place the directories for the months in the same year as the blog posts. You may need to do it for URLs starting /wp-content, ie without the http://jimmyg.org part.

For example in the case of /wp-content/uploads/2008/02/IMG_9713.JPG, the 02 directory would get moved to the 2007 blog posts directory.

In Summary

In summary then you can export a site with these commands:

env/bin/python -m sitetool.wordpress  run -H pauli.3aims.com -p -u web6_u1 web6_db1 JimmyG/blog -q
env/bin/python -m sitetool.wordpress html JimmyG/blog/ JimmyG/Templates/jimmyg.dwt -q
env/bin/python -m sitetool.wordpress index JimmyG/blog/ JimmyG/Templates/jimmyg.dwt -q
env/bin/python -m sitetool.dreamweaver regenerate JimmyG/
env/bin/python -m sitetool.dreamweaver sitemap JimmyG/ JimmyG/Templates/jimmyg.dwt
James Gardner: Home > Work > Code > SiteTool > 0.3.0 > Wordpress Support