#!/usr/bin/perl -w ## This script inserts a row into the MySQL database table used by ## MovableType 2.64 ## It was hacked together by a user of MT, not an author or affiliate ## of MT. ## Use this if you dare. If it breaks anything, you keep all the pieces. ## It takes standard input, rips the top line off as a title, and ## leaves the rest as entry text. A few switches are allowed as outlined ## below. There is much more to be done here. # example usage: # cat my_new_wicked_post.txt | ./cli_post.pl -p # ## To get the post to show, you have to do a rebuild of MT. I found a ## drop-in replacement for MT's rebuild script out on the web somewhere ## It's handy. Google is your friend. ## If you want to notify anyone or send pings (which are hard-coded ## in this script as of now), you have to do that through regular MT ## channels. This script isn't meant to replace the MT interface, just make ## posting easier FOR ME. If you don't like how it gets ## things done, don't use it. ## Anyway, on with the show. #use strict; use DBI; use Getopt::Std; ## options # p is publish (defaults to Draft) # l is to convert line breaks (defaults to not converting) our ($opt_p, $opt_l); getopts('lp'); # database information $db="movabletype"; $host="localhost"; $port=""; $userid="mt"; $passwd="xxxxxxxxxx"; $connectionInfo="DBI:mysql:database=$db;$host:$port"; ## the following should maybe be read from arguments or a config file? $blog_id=1; $author_id=1; $ping_urls = "http://blo.gs\\nhttp://www.mod-pubsub.org/kn_apps/blogchatter/ping.php"; # allow comments? 0 = no, 1 = yes, duh. $comments=1; # same for pings $pings=1; #get entry status setting 1=draft(default) 2=publish if ($opt_p) {$entry_status=2} else {$entry_status=1}; #get line break setting 0 = no line break conversion __default__ = convert ## _I_ didn't make up 0 and __default__, it's how stuff shows up ## in the database at the hands of MT if ($opt_l) {$line_breaks="__default__"} else {$line_breaks=0}; # If there's no input, we can't do anything. # If there is input, use the first line as the entry title. die "No text to insert!" unless (defined (my $title = )) ; chomp $title; # ' and " have to be escaped for MySQL $title =~ s/(["'])/\\$1/g; # slurp the rest of the input into a big long string my $text; while (defined (my $string = )) { # escape the quotes again $string =~ s/(["'])/\\$1/g; $text .= $string ; } # Append a trailing
to keep things uniform with how MT line-breaks $text .= "
"; #Just a little status message print "Now inserting entry.\n"; # make connection to database $dbh = DBI->connect($connectionInfo,$userid,$passwd); # prepare and execute query $query = "INSERT INTO mt_entry VALUES (NULL,$blog_id,$entry_status,$author_id,$comments,$pings,'$line_breaks',NULL,'$title','','$text','','$ping_urls','',NULL,NULL,NOW(),NULL,NULL,NULL)"; $sth = $dbh->prepare($query); if ($sth->execute()) { print "Record inserted into the database.\nNow go rebuild your pages.\n"; } else { print "There was a database error!\nEntry not saved.\n"; } $sth->finish(); # disconnect from databse $dbh->disconnect;