Using the Perl API to create blog entries is well documented, but creating pages is not.
What I need to do is to generate to create unique page that correlates to some number. The number can be anything from 1 to 10,000,000 and I've been asked to have no more than 1000 files in a directory. So, my approach is to create the page corresponding to 12,345,678 in someblog/12/345/678.html - i.e. use 12/345 as the foldering.
I've figured out how create the foldering in MT 4.1 and I've figured out how to create the page, but... I haven't figured out how to get the page into the folder.
Can some kind soul point me in the right direction?
Here's my code (runs from the tools directory):
CODE
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use lib '../extlib', '../lib', '../plugins';
use MT;
use base qw/ MT::Folder MT::Page /;
my $mt = MT->new(Config => 'mt.cfg');
my $blog = MT::Blog->load({name => 'demo'}) or die "Unable to load blog: $!";
my $author = MT::Author->load({email => 'rstaveley@blahdiblah.com'}) or die "Unable to load author: $!";
# This is hard-coded for a test
my $v_id = '12345678';
# Get the parent folder
my $parent_directory = int($v_id/1000_000);
my $sub_directory = int($v_id/1000)%1000;
my $title = $v_id % 1000;
my $folder;
# Auto-vivify the foldering
my $parent_id = 0;
for my $basename($parent_directory, $sub_directory) {
# See if the folder aleady exists
$folder = MT::Folder->load({
blog_id => $blog->id,
parent => $parent_id,
basename => $basename,
});
# If it doesn't we create it
if (!$folder) {
$folder = MT::Folder->new;
$folder->blog_id($blog->id);
$folder->parent($parent_id);
$folder->basename($basename);
$folder->label($basename);
$folder->save or die _fault(MT->translate("Unable to save folder: [_1]", $folder->errstr));
}
$parent_id = $folder->id;
}
my $page = new MT::Page;
$page->blog_id($blog->id);
$page->author_id($author->id);
$page->title($title);
$page->text($v_id);
$page->status(MT::Entry::RELEASE());
#$page->folder($folder); ## <- I get an uninitialised value error in ../lib/MT/Entry.pm line 107 with this
$page->save;
use strict;
use Data::Dumper;
use lib '../extlib', '../lib', '../plugins';
use MT;
use base qw/ MT::Folder MT::Page /;
my $mt = MT->new(Config => 'mt.cfg');
my $blog = MT::Blog->load({name => 'demo'}) or die "Unable to load blog: $!";
my $author = MT::Author->load({email => 'rstaveley@blahdiblah.com'}) or die "Unable to load author: $!";
# This is hard-coded for a test
my $v_id = '12345678';
# Get the parent folder
my $parent_directory = int($v_id/1000_000);
my $sub_directory = int($v_id/1000)%1000;
my $title = $v_id % 1000;
my $folder;
# Auto-vivify the foldering
my $parent_id = 0;
for my $basename($parent_directory, $sub_directory) {
# See if the folder aleady exists
$folder = MT::Folder->load({
blog_id => $blog->id,
parent => $parent_id,
basename => $basename,
});
# If it doesn't we create it
if (!$folder) {
$folder = MT::Folder->new;
$folder->blog_id($blog->id);
$folder->parent($parent_id);
$folder->basename($basename);
$folder->label($basename);
$folder->save or die _fault(MT->translate("Unable to save folder: [_1]", $folder->errstr));
}
$parent_id = $folder->id;
}
my $page = new MT::Page;
$page->blog_id($blog->id);
$page->author_id($author->id);
$page->title($title);
$page->text($v_id);
$page->status(MT::Entry::RELEASE());
#$page->folder($folder); ## <- I get an uninitialised value error in ../lib/MT/Entry.pm line 107 with this
$page->save;