Post Versioning

I spent yesterday hacking the PEAR Text_Diff package into the wp_versioning plugin for WordPress. The idea is basically stolen from the MT-Revision plugin, which you can see in use here.

If I edit a post — as I just did this one, to remove a misguided rant which I blame on sleep deprivation — it is marked as a revision, and previous versions are kept available. The changes between them can be viewed as an inline diff. Previous edits were to fix unfortunate formatting mistakes, and the hilariously broken diffs show that, unfortunately, the diff function doesn’t play all that well with Markdown.

The latest diff, at time of writing, is available here.

It’s an acknowledgement that entries are liable to be updated, and an enforcement of accountability. If I attempt to take back my words — after posting a misogynistic denunciation of all women, for example, or making a “sure win” bet with an embarrassing penalty — the original version will still be there. (Not much of an enforcement, to be true, since I can always edit the versioning table in the database, but it’s something.)

My changes to the beta version of the plugin are available here. (Note that the contents of that file are liable to change as I tweak things.) You’ll also need Diff.php, Renderer.php and inline.php from the Text_Diff package.

It provides eight or so template tags, which I have documented in a half-assed possibly-phpDoc-compatible style.

Post content on single-post pages is replaced by old versions if version=* is requested, and by diffs if from=* is requested. If you ask for a diff from without a version, the diff is to the latest revision. Previous versions of the post title, excerpt etc. currently aren’t displayed, though the two versions are saved separately and it’s easy enough to add.

Currently I have it set to save at the publish_post plugin hook, with the side effect that I can edit drafts as much as I want without revisions being saved. (It’s more to provide transparency than edit-versioning.) It’s trivial to modify to run on the save_post and edit_post hooks if that’s what you want.

Update: now displaying raw Markdown input, since filtering the diff through it caused too many problems. The copy I uploaded does not reflect this change.

   
This entry was posted on Monday, April 25th, 2005, in the categories “”, “PHP”, “code”, “WordPress” and “plugins”.

Thus far, 18 responses.

Phil Ringnalda:

Outstanding! That’s one of the very few things on my list of “MT’s got and WP’s not” removed.

And not picking up draft changes I’d say is a virtue: most of the time I remember that I can’t save as a draft until after I’ve removed most of the flames and swearing, but not having to remember would be much nicer.

Reckoned random:

Ignoring drafts is definitely better for me, but IIRC some of the people who chimed in on the wp-hackers list wanted versioning for collaborative document editing, which doesn’t work at all if draft edits are ignored.

Answered mindamp:

I need some help with this plugin. I downloaded the PEAR Text_Diff package and placed diff.php inline.php and renderer.php into the wp-versioning plugins folder. i then updated my watershed plugin with your version of the php… i can see the changes under options>> versioning, however it only allows me to revert to the one before…

id like to be able to display a link to all the previous revisions and show differences from my themes index.php template… much like a wiki.. if you check my site out, youll see what i mean…

thanks in advance

mindamp
mindamp@gmail.com
http://www.mindamp.org

Recorded mindamp:

ok, i got http://mindamp.org/blog/index.php?p=277&version=# working….

but how can i display all versions in a pagination or something?

mindamp:

oh, and really sorry to fill your comments up, but they moderated.. so tough luck :-)

what about making the the diff display image code instead of just displaying the code.. cause when it does that it throws my whole template off…

http://mindamp.org/blog/?p=277&version=1&from=2

Sam Angove:

Hi. The list_versions() function provides some kind of pagination, and your image problem can probably be sorted by playing with get_version_diff(). Removing the $output = apply_filters ... line might do it, but I’m not sure and don’t have time to play with it to find out. This time of year is very busy for me.

Anyhow, this code was never for end-user consumption — it’s there so interested parties can re-use it if they want, but it’s not a plugin release.

Sorry!

Postulated mindamp:

does not do anything…

Posited mindamp:

?php list_versions(); ?

that is

Posited mindamp:

nevermind.. im a total retard… ive had a long week…

i was wondering however… how i could make the pagination display “no revisions” if there are no more stored copies of the post?

i.e. http://mindamp.org

Postulated Sam Angove:

You’ll have to edit the function. Just a wild guess, change if ($latest <= 1) return; to if ($latest <= 1) { echo 'no revisions'; return; }.

Held mindamp:

im loving the plugin.. it brings my wordpress blog one step closer to wikified…

but i dunno if i messed something up playing with permalinks or what… so i turned the permalinks back off…

but now all the version links point to to the first post on the page.. instead of each indivual posts…

ill keep messing, but i might need some help.. appreciate in advance

http://mindamp.org

Postulated mindamp:

for the life of me, it still shows the id on all posts revisions as the top post on the pages Id…

whats going on?

Reckoned Sam Angove:

mindamp, I told you that this wasn’t a plugin release. While I’m delighted that you’re finding it even partly useful, I’m not supporting it and I can’t help you.

Answered Scott CArle:

I found this update to your plugin here and it works outa the box with no changes with wp 2.1
http://benjismith.net/index.php/2006/08/04/wordpress-plugin-versioning/

Thought W. David Hardin:

Scott C - The plugin you point at is not quite the same. It drops the ability to let visitors view the previous versions and only allows the admin to revert back and forth.

Noted Manne:

Hi, does this plugin save post metadata? None of the other versioning plugins seems to be doing it..

Said Sam:

No, it doesn’t. Also, it’s abandoned and there’s approximately no chance it works with recent versions of WordPress — not a ringing endorsement, sorry.

Leave a Reply