Not a developer? Go to MovableType.com

Documentation

Creating Custom Import Handlers

Movable Type provides a framework for defining and handing custom import handlers for 3rd party formats. Movable Type comes with built in support for Wordpress’ WXR format. Support for additional formats can be defined by developers using this extensible import framework.

Note: The Wordpress Importer is implemented as a plugin and can be found in Movable Type’s $MT_HOME/plugins directory. This serves as an excellent example of a more complex and fully featured import handler.

To begin one needs to register the import handler with the Movable Type registry. The following is a code sample demonstrating how to register a custom import handler:

sub init_registry {
    my $plugin = shift;
    $plugin->registry({
        'import_formats' => {
            'import_wxr' => {
                label => 'My Importer',
                type => 'MyImporter::Import',
                handler => 'MyImporter::Import::import_contents',
                options => [ 'wp_path', 'mt_site_path', 'mt_extra_path' ],
                options_template => 'options.tmpl',
                options_param => 'MyImporter::Import::get_param',
            },
        },
    });
}

The following are the registry properties for an import format:

  • label - The display name of the import format. This is used within the user interface to allow administrators to select the format that they wish to import.
  • type - The class or package name of the importer. This package is loaded by Movable Type automatically.
  • handler - The specific method or subroutine that will handle the import.
  • options - (optional) An array of option parameter names that this importer may depend upon.
  • options_template - (optional) The template name that will be included below the primary import form inside the application. This can be used to collect import specific configuration parameters.
  • options_param - (optional) The method that is responsible for initializing the importer’s context.

Import Parameter Handler

The import parameter handler is responsible for populating the context of the Import Handler itself. The import parameter handler returns a hash reference that contains a sequence of key/value pairs that can be used by the import handler in processing the content being parsed and imported into Movable Type.

package MyImporter::Import;

sub get_param {
    my $class = shift;
    my ($blog_id) = @_;

    my $param = { blog_id => $blog_id };
    $param;
}

Import Handler

The Import Handler is the piece of code that is responsible for actually processing an import file and creating the content in the target blog programatically. The following code is just a fragment (an excerpt from the Wordpress importer actually) of an import handler.

package MyImporter::Import;

sub import_contents {
    my $class = shift;
    my %param = @_;
    my $iter = $param{Iter};
    my $blog = $param{Blog} or return $class->error(MT->translate("No Blog"));
    while (my $stream = $iter->()) {
        my $result = eval {
            if (ref($stream) eq 'Fh') {
              # open file $stream, read its contents, create entries in target blog
            }
        };
    }    
}
Back