« Why not MT 3.5? Or MT 4.0? | Main | Finally updated sidebar »

Converting from dirified title to basename

This is an archived entry from the Movable Type 3.2 beta test.
Entries from the current beta release (v3.3) can be found here.

For those of you who, like me, converted away from filenames for individual archvies with padded entry IDs (the old default in MT 2) to dirified entry title, you're probably wondering how to convert to using MT 3.2's basename field without breaking your permalinks. Luckily, the answer is pretty darn easy, as long as you're using a SQL database.

First of all, create an index template in the blog you wish to convert with the following template code:

<MTEntries lastn="99999"$>
update mt_entry set entry_basename = "<$MTEntryTitle dirify="1">" where entry_id = <$MTEntryID$>;
</MTEntries>

You should of course, change <$MTEntryTitle dirify="1"> to whatever you have been using in your archive mapping for Individual archvies.

So, save and rebuild and you will have a SQL statement file which you can either execute via the command line (if you have shell access) or upload via a web-based interface like phpMyAdmin. The MySQL restore instructions detail this nicely.

I'm pretty sure that both Postgres and SQLite have such web-based interfaces. More information from commenters on this would be appreciated.

Comments

Couldn't you turn that little bit into a mass-update script with a little bit of PHP?

If you put something like this in instead of the code above:

<?

$conn = mysql connect("localhost", "username", "somepass") or die(mysqlerror()); mysqlselectdb("databasename",$conn) or die(mesql_error());

$q = "<MTEntries lastn="99999"$> update mtentry set entrybasename = "<$MTEntryTitle dirify="1"<" where entryid = <$MTEntryID>; </MTEntries>"; $result= mysqlquery($q) or die ("Could not execute query : $q." . mysql_error()); ?>

Changing the database connection details to match your own of course. Then open the script in your browser and it should, theoretically, update the entire database, given that you don't have more than 99999 entries, after that you should delete the file from your server so that people can't come and wreck your database.

Couldn't you turn that little bit into a mass-update script with a little bit of PHP?

You could! In fact you did! :-)

Then open the script in your browser and it should, theoretically, update the entire database, given that you don't have more than 99999 entries, after that you should delete the file from your server so that people can't come and wreck your database.

You should also, obviously, back up your database before doing any of this.

Nice. I was still using entry IDs for filenames, but this let me change all my basenames to something longer than 15 characters. If I could make a feature request it would be to have the upgrader ask what length you want before converting your database.

Matt: upgraded blogs get a setting of 15 characters. The same as the 3.1x default. You can then change it to something longer through the new entry default configuration. New weblogs get a default of 30 characters.

Brad - Yes, I understand. But when you change the setting it doesn't go back and rewrite all the old entires basenames to the new default, does it? I know it's anal, but I wanted all my basenames to be the entire title.

Matt: doing that would change the URLs of your older posts, which makes permalinks les... permanent. So no, we don't do that. If you don't care, then you can use Jay's technique to just change them all to dirified titles. The basename field is long enough-- 255 characters wide.

The thing is if you're upgrading from 2.661 to 3.x then by switching to basenames you're breaking your permalinks anyway. So for an upgrade it would be nice if you could specify the basename length before the upgrade, or if you could change all the basenames to a standard length afterwards.

It's not a big deal, like I said, I used Jay's method to create new basenames for all entries and then set the default to 250.

Thanks, Jay! This took care of my problem quite easily!

The thing is if you're upgrading from 2.661 to 3.x then by switching to basenames you're breaking your permalinks anyway.

Actually, the purpose of the scrip above is to help you switch from dirified filename to basename WITHOUT breaking your old permalinks. It does this by taking what you are currently using (i.e. dirified title) for all of your old posts and saving THAT VALUE in the basename field. After that, you can switch your archive map to basename and you won't break a single link.

Yes, I know the original purpose of the post. I'm telling you that I used it as a workaround for what I see as a shortcoming in the upgrade script when going from 2.661 (using post numbers for filenames) to 3.2 (using basenames for filenames). Currently the upgrade assumes you'll want 15 character basenames. I didn't, but I couldn't find a way to then redo the old posts basenames with 250 characters.

Your post let me do exactly that, and I thought I'd mention it while saying that I think the upgrade script should have done the same thing. Instead I just confused everyone. I hope all is clear now.

Hmm. I wish I had thought about this before upgrading to 3.x though.

Your post let me do exactly that, and I thought I'd mention it while saying that I think the upgrade script should have done the same thing. Instead I just confused everyone. I hope all is clear now.

Well, most people don't have this problem and this would have only added complexity to an already complex process. Padded entry IDs were the 2.x default and basename has been the 3.x default. Changing your archive publishing paths would not be considered a normal upgrade procedure. I could totally see someone creating a plugin to help convert people, but this sort of functionality doesn't really belong in the upgrade script.

Hmm. I wish I had thought about this before upgrading to 3.x though.

Why? What have you missed out on?

I wasn't changing the archive publishing paths, though. I was creating new ones. All it would take is a simple setting in the config file for basename length that could be configured before upgrade, not that complex.

But I agree with you, there probably aren't that many people out there who have a 2.661 blog that they'd like to upgrade to 3.x with 250 character basenames. And if there are any, I'll point them to this post, and all their troubles will be solved.

I probably should have just said, "Thanks, that solves a problem that's been nagging at me for a couple of weeks," and left it at that. :)

Not sure anyone's reading this thread, but if so:

I'm currently using entry IDs as the archive entry filenames (category/00101.html, etc); I want to convert to category/title for new entries without breaking permalinks. I think this explains how to do it, but I admit I don't understand PHP and SQL nearly well enough to be sure (and I'd rather not break my DB). Can someone give a more explicit cookbook?

Andrew

Post a comment



Six Apart
Makers of weblog software and services for individuals, organizations and businesses.
This website is powered by Movable Type.