Writing Utility Scripts or Plugins for Movable Type Made Easy

| No Comments | No TrackBacks |
Even if you have dabbled in Perl scripting a bit before, writing plugins for Movable Type can seem a bit daunting at first.  Even though there is a step by step guide explaining how to create a Movable Type plugin, you still need to know quite a bit about Perl to make sense of it.  However, what many people don't know is that you can script quite a lot of actions in a Movable Type system without needing to write a full-blown plugin at all.
Example
What if you just want to write a quick utility script to do something to all entries in your installation?  You don't need a description under Movable Type's plugins screen, or fancy settings pages or extra database tables for that.  Is it possible?  Of course it is!  Take this bit of code as an example:

use lib 'lib';
use MT::Entry;

@entries = MT::Entry->load();
foreach $entry (@entries){
    $title = $entry->title;
    $title =~ s/foobar/bla/gsi;
    $entry->title($title);
    $entry->save
}

Saved as test.pl in your Movable Type folder and called with the command 'perl test.pl' it will change the word 'foobar' to 'bla' in the titles of all your entries.  (Note that I have kept the code deliberately simple so it is understandable to a novice Perl coder, even though this makes the code a bit less safe and maintainable)

Movable Type's Perl API
Almost all things in a Movable Type system (entries, comments, users, blogs, settings, permissions...) are represented internally as Perl objects.  These objects use a common set of methods to load and save them and almost all of them have various properties that can be read and/or changed.  Some of these objects also have specific methods associated with them.

The complete set of objects, properties and methods comprises the Movable Type Perl API.  It is documented here (but parts of it are out of date).  Most of the objects have really descriptive names, so it is immediately clear what they are: MT::Entry, MT::Author, MT::Blog... One of the better places to start is with the MT::Object documentation, since it is from this class that most other ones inherit their basic methods to load and save data.

As soon as you get the hang of this, it becomes very easy to whip up little utility scripts like the one above.  You will find that often this is all you really need instead of a complete plugin.  In any case, writing scripts like these is a great way to learn more about what you can do using Movable Type's Perl API.  And who knows, after a while you might even take the next step up and write a full plugin...

A final example
Here is a short little script that creates an entry in the blog with id 5, written by the author with the id 1 and with "This is a test" as the title.  Finally it also publishes the entry so it becomes visible on the published blog:

#!/usr/bin/perl
use lib 'lib';
use MT;
use MT::Entry;

my $mt = MT->instance();
my $entry = MT::Entry->new();
$entry->title("This is a test");
$entry->blog_id('5');
$entry->author_id('1');
$entry->status('2');
$entry->save;
$mt->rebuild_entry(Entry => $entry->id);

I'm sure that with a little bit of modification you can do a lot of interesting things with this code: automatically publish an entry at set times containing system information, publishing the output of some scripting code as an entry on your blog...  Let me know in the comments if you do!


No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/52

Leave a comment


Movable Type Help?

"Can my Movable Type issue or project be taken care of by a professional?" Yes It Can Be! Contact YesItCan.be
Powered by Movable Type 5.2.7