| 1 | package Net::MovableType; | ||
| 2 | |||
| 3 | # MovableType.pm,v 1.18 2004/08/14 08:31:32 sherzodr Exp | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use vars qw($VERSION $errstr $errcode); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use XMLRPC::Lite; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | $VERSION = '1.74'; | ||
| 11 | |||
| 12 | # Preloaded methods go here. | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | $class = ref($class) || $class; | ||
| 17 | |||
| 18 | my ($url, $username, $password) = @_; | ||
| 19 | |||
| 20 | my $self = { | ||
| 21 | proxy => undef, | ||
| 22 | blogid => undef, | ||
| 23 | username => $username, | ||
| 24 | password => $password | ||
| 25 | }; | ||
| 26 | |||
| 27 | bless $self, $class; | ||
| 28 | |||
| 29 | # if $url starts with 'http://' and ends in '.xml', we assume it was a | ||
| 30 | # location of rsd.xml file | ||
| 31 | if ( $url =~ m/^http:\/\/.+\.xml$/ ) { | ||
| 32 | $self->rsd_url($url) or return undef; | ||
| 33 | |||
| 34 | # if the URL just starts with 'http://', we assume it was a url for | ||
| 35 | # MT's XML-RPC server | ||
| 36 | } elsif ( $url =~ m/^http:\/\// ) { | ||
| 37 | $self->{proxy} = XMLRPC::Lite->proxy($url); | ||
| 38 | |||
| 39 | # in neither case, we assume it was a file system location of rsd.xml file | ||
| 40 | } elsif ( $url ) { | ||
| 41 | $self->rsd_file($url) or return undef; | ||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | return $self | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | # shortcut for XMLRPC::Lite's call() method. Main difference from original call() | ||
| 53 | # is, it returns native Perl data, instead of XMLRPC::SOM object | ||
| 54 | sub call { | ||
| 55 | my ($self, $method, @args) = @_; | ||
| 56 | |||
| 57 | unless ( $method ) { | ||
| 58 | die "call(): usage error" | ||
| 59 | } | ||
| 60 | |||
| 61 | my $proxy = $self->{proxy} or die "'proxy' is missing"; | ||
| 62 | my $som = $proxy->call($method, @args); | ||
| 63 | my $result= $som->result(); | ||
| 64 | |||
| 65 | unless ( defined $result ) { | ||
| 66 | $errstr = $som->faultstring; | ||
| 67 | $errcode= $som->faultcode; | ||
| 68 | return undef | ||
| 69 | } | ||
| 70 | |||
| 71 | return $result | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | sub process_rsd { | ||
| 78 | my ($self, $string_or_file ) = @_; | ||
| 79 | |||
| 80 | unless ( $string_or_file ) { | ||
| 81 | croak "process_rsd() usage error" | ||
| 82 | } | ||
| 83 | |||
| 84 | require XML::Simple; | ||
| 85 | my $xml = XML::Simple::XMLin(ref($string_or_file) ? $$string_or_file : $string_or_file ); | ||
| 86 | my $apilink = $xml->{service}->{apis}->{api}->{MetaWeblog}->{apiLink}; | ||
| 87 | my $blogid = $xml->{service}->{apis}->{api}->{MetaWeblog}->{blogID}; | ||
| 88 | |||
| 89 | unless ( $apilink && $blogid ) { | ||
| 90 | croak "Couldn't retrieve 'apiLink' and 'blogID' from $xml" | ||
| 91 | } | ||
| 92 | |||
| 93 | $self->blogId($blogid); | ||
| 94 | $self->{proxy} = XMLRPC::Lite->proxy($apilink); | ||
| 95 | |||
| 96 | # need to return a true value indicating success | ||
| 97 | return 1 | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | # fetches RSD file from a remote location, | ||
| 102 | # and configures Net::MovableType object properly | ||
| 103 | sub rsd_url { | ||
| 104 | my ($self, $url) = @_; | ||
| 105 | |||
| 106 | unless ( $url ) { | ||
| 107 | croak "rsd_url() usage error" | ||
| 108 | } | ||
| 109 | |||
| 110 | $self->{rsd_url} = $url; | ||
| 111 | |||
| 112 | require LWP::UserAgent; | ||
| 113 | |||
| 114 | my $ua = LWP::UserAgent->new(); | ||
| 115 | my $req= HTTP::Request->new('GET', $url); | ||
| 116 | my $response = $ua->request($req); | ||
| 117 | if ( $response->is_error ) { | ||
| 118 | $errstr = $response->base . ": " . $response->message; | ||
| 119 | $errcode= $response->code; | ||
| 120 | return undef | ||
| 121 | } | ||
| 122 | |||
| 123 | return $self->process_rsd($response->content_ref) | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | sub rsd_file { | ||
| 129 | my ($self, $file) = @_; | ||
| 130 | |||
| 131 | unless ( $file ) { | ||
| 132 | croak "rsd_file() usage error" | ||
| 133 | } | ||
| 134 | |||
| 135 | $self->{rsd_file} = $file; | ||
| 136 | |||
| 137 | return $self->process_rsd($file) | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | sub username { | ||
| 144 | my ($self, $username) = @_; | ||
| 145 | |||
| 146 | if ( defined $username ) { | ||
| 147 | $self->{username} = $username; | ||
| 148 | } | ||
| 149 | return $self->{username} | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | *error = \&errstr; | ||
| 155 | sub errstr { | ||
| 156 | return $errstr | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | sub errcode { | ||
| 161 | return $errcode | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | sub password { | ||
| 168 | my ($self, $password) = @_; | ||
| 169 | |||
| 170 | if ( defined $password ) { | ||
| 171 | $self->{password} = $password | ||
| 172 | } | ||
| 173 | return $self->{password} | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | sub proxy { | ||
| 179 | my ($self, $proxy) = @_; | ||
| 180 | |||
| 181 | if ( defined $proxy ) { | ||
| 182 | $self->{proxy} = $proxy | ||
| 183 | } | ||
| 184 | return $self->{proxy} | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | *blogid = \&blogId; | ||
| 190 | sub blogId { | ||
| 191 | my ($self, $blogid) = @_; | ||
| 192 | |||
| 193 | if ( defined $blogid ) { | ||
| 194 | $self->{blogid} = $blogid | ||
| 195 | } | ||
| 196 | return $self->{blogid} | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | sub resolveBlogId { | ||
| 202 | my ($self, $blogname) = @_; | ||
| 203 | |||
| 204 | unless ( $self->username && $self->password ) { | ||
| 205 | croak "username and password are missing\n" | ||
| 206 | } | ||
| 207 | |||
| 208 | my $blogs = $self->getUsersBlogs(); | ||
| 209 | while ( my $b = shift @$blogs ) { | ||
| 210 | if ( $b->{blogName} eq $blogname ) { | ||
| 211 | return $b->{blogid} | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | $errstr = "Couldn't find blog '$blogname'"; | ||
| 216 | return undef | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | sub getBlogInfo { | ||
| 223 | my ($self, $blogid) = @_; | ||
| 224 | |||
| 225 | $blogid ||= $self->blogId() or croak "no 'blogId' set"; | ||
| 226 | my $blogs = $self->getUsersBlogs() or return undef; | ||
| 227 | |||
| 228 | while ( my $b = shift @$blogs ) { | ||
| 229 | if ( $b->{blogid} == $blogid ) { | ||
| 230 | return $b | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | $errstr = "No blog found with id '$blogid"; | ||
| 235 | return undef | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | |||
| 244 | *getBlogs = \&getUsersBlogs; | ||
| 245 | sub getUsersBlogs { | ||
| 246 | my ($self, $username, $password) = @_; | ||
| 247 | |||
| 248 | $username = $self->username($username); | ||
| 249 | $password = $self->password($password); | ||
| 250 | |||
| 251 | unless ( $username && $password ) { | ||
| 252 | croak "username and password are missing"; | ||
| 253 | } | ||
| 254 | |||
| 255 | return $self->call('blogger.getUsersBlogs', "", $username, $password) | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | sub getUserInfo { | ||
| 264 | my ($self, $username, $password) = @_; | ||
| 265 | |||
| 266 | $username = $self->username($username); | ||
| 267 | $password = $self->password($password); | ||
| 268 | |||
| 269 | unless ( $username && $password ) { | ||
| 270 | croak "username and/or password are missing" | ||
| 271 | } | ||
| 272 | |||
| 273 | return $self->call('blogger.getUserInfo', "", $username, $password) | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | sub getPost { | ||
| 280 | my ($self, $postid, $username, $password) = @_; | ||
| 281 | |||
| 282 | $username = $self->username($username); | ||
| 283 | $password = $self->password($password); | ||
| 284 | |||
| 285 | unless ( $username && $password && $postid ) { | ||
| 286 | croak "getPost() usage error" | ||
| 287 | } | ||
| 288 | |||
| 289 | return $self->call('metaWeblog.getPost', $postid, $username, $password) | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | sub getRecentPosts { | ||
| 299 | my ($self, $numposts) = @_; | ||
| 300 | |||
| 301 | my $blogid = $self->blogId() or croak "no 'blogId' defined"; | ||
| 302 | my $username = $self->username() or croak "no 'username' defined"; | ||
| 303 | my $password = $self->password() or croak "no 'password' defined"; | ||
| 304 | $numposts ||= 1; | ||
| 305 | |||
| 306 | return $self->call('metaWeblog.getRecentPosts', $blogid, $username, $password, $numposts) | ||
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | sub getRecentPostTitles { | ||
| 312 | my ($self, $numposts) = @_; | ||
| 313 | |||
| 314 | my $blogid = $self->blogId() or croak "no 'blogId' defined"; | ||
| 315 | my $username= $self->username() or croak "no 'username' defined"; | ||
| 316 | my $password= $self->password() or croak "no 'password' defined"; | ||
| 317 | $numposts ||= 1; | ||
| 318 | |||
| 319 | return $self->call('mt.getRecentPostTitles', $blogid, $username, $password, $numposts) | ||
| 320 | } | ||
| 321 | |||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | *getCategories = \&getCategoryList; | ||
| 328 | sub getCategoryList { | ||
| 329 | my ($self, $blogid, $username, $password) = @_; | ||
| 330 | |||
| 331 | $blogid = $self->blogId($blogid) or croak "no 'blogId' defined"; | ||
| 332 | $username = $self->username($username) or croak "no 'username' defined"; | ||
| 333 | $password = $self->password($password) or croak "no 'password' defined"; | ||
| 334 | |||
| 335 | return $self->call('mt.getCategoryList', $blogid, $username, $password) | ||
| 336 | } | ||
| 337 | |||
| 338 | |||
| 339 | |||
| 340 | |||
| 341 | sub getPostCategories { | ||
| 342 | my ($self, $postid, $username, $password) = @_; | ||
| 343 | |||
| 344 | $username = $self->username($username) or croak "no 'username' defined"; | ||
| 345 | $password = $self->password($password) or croak "no 'password' defined"; | ||
| 346 | |||
| 347 | unless ( $postid ) { | ||
| 348 | croak "getPostCategories() usage error" | ||
| 349 | } | ||
| 350 | |||
| 351 | return $self->call('mt.getPostCategories', $postid, $username, $password) | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | |||
| 356 | sub setPostCategories { | ||
| 357 | my ($self, $postid, $cats) = @_; | ||
| 358 | |||
| 359 | unless ( ref $cats ) { | ||
| 360 | $cats = [$cats] | ||
| 361 | } | ||
| 362 | |||
| 363 | unless ( @$cats && $postid ) { | ||
| 364 | croak "setPostCategories() usage error" | ||
| 365 | } | ||
| 366 | |||
| 367 | my $blogid = $self->blogId() or croak "no 'blogId' set"; | ||
| 368 | |||
| 369 | my $category_list = $self->getCategoryList($blogid); | ||
| 370 | my $post_categories = []; | ||
| 371 | for my $cat ( @$cats ) { | ||
| 372 | for my $c ( @$category_list ) { | ||
| 373 | if ( lc $c->{categoryName} eq lc $cat ) { | ||
| 374 | push @$post_categories, {categoryId=>$c->{categoryId} } | ||
| 375 | } | ||
| 376 | } | ||
| 377 | } | ||
| 378 | |||
| 379 | my $username = $self->username() or croak "no 'username' defined"; | ||
| 380 | my $password = $self->password() or croak "no 'password' defined"; | ||
| 381 | $postid or croak "setPostCategories() usage error"; | ||
| 382 | |||
| 383 | return $self->call('mt.setPostCategories', $postid, $username, $password, $post_categories) | ||
| 384 | } | ||
| 385 | |||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | |||
| 393 | |||
| 394 | |||
| 395 | |||
| 396 | sub supportedMethods { | ||
| 397 | my ($self) = @_; | ||
| 398 | |||
| 399 | return $self->call('mt.supportedMethods') | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 403 | |||
| 404 | sub publishPost { | ||
| 405 | my ($self, $postid, $username, $password) = @_; | ||
| 406 | |||
| 407 | $username = $self->username($username) or croak "no 'username' set"; | ||
| 408 | $password = $self->password($password) or croak "no 'password' set"; | ||
| 409 | |||
| 410 | unless ( $postid ) { | ||
| 411 | croak "publishPost() usage error" | ||
| 412 | } | ||
| 413 | |||
| 414 | return $self->call('mt.publishPost', $postid, $username, $password) | ||
| 415 | } | ||
| 416 | |||
| 417 | |||
| 418 | |||
| 419 | |||
| 420 | |||
| 421 | sub newPost { | ||
| 422 | my ($self, $content, $publish) = @_; | ||
| 423 | |||
| 424 | my $blogid = $self->blogId() or croak "'blogId' is missing"; | ||
| 425 | my $username = $self->username() or croak "'username' is not set"; | ||
| 426 | my $password = $self->password() or croak "'password' is not set"; | ||
| 427 | |||
| 428 | unless ( $content && (ref($content) eq 'HASH') ) { | ||
| 429 | croak "newPost() usage error" | ||
| 430 | } | ||
| 431 | |||
| 432 | return $self->call('metaWeblog.newPost', $blogid, $username, $password, $content, $publish) | ||
| 433 | } | ||
| 434 | |||
| 435 | |||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | |||
| 440 | sub editPost { | ||
| 441 | my ($self, $postid, $content, $publish) = @_; | ||
| 442 | |||
| 443 | my $username = $self->username() or croak "'username' is not set"; | ||
| 444 | my $password = $self->password() or croak "'password' is not set"; | ||
| 445 | |||
| 446 | unless ( $content && (ref($content) eq 'HASH') ) { | ||
| 447 | croak "newPost() usage error" | ||
| 448 | } | ||
| 449 | |||
| 450 | return $self->call('metaWeblog.editPost', $postid, $username, $password, $content, $publish) | ||
| 451 | } | ||
| 452 | |||
| 453 | |||
| 454 | |||
| 455 | |||
| 456 | |||
| 457 | |||
| 458 | sub deletePost { | ||
| 459 | my ($self, $postid, $publish) = @_; | ||
| 460 | |||
| 461 | my $username = $self->username or croak "'username' not set"; | ||
| 462 | my $password = $self->password or croak "'password' not set"; | ||
| 463 | $postid or croak "deletePost() usage error"; | ||
| 464 | |||
| 465 | return $self->call('blogger.deletePost', "", $postid, $username, $password, $publish) | ||
| 466 | } | ||
| 467 | |||
| 468 | |||
| 469 | |||
| 470 | |||
| 471 | *upload = \&newMediaObject; | ||
| 472 | sub newMediaObject { | ||
| 473 | my ($self, $filename, $name, $type) = @_; | ||
| 474 | |||
| 475 | my $blogid = $self->blogId() or croak "'blogId' is missing"; | ||
| 476 | my $username = $self->username() or croak "'username' is not set"; | ||
| 477 | my $password = $self->password() or croak "'password' is not set"; | ||
| 478 | |||
| 479 | unless ( $filename ) { | ||
| 480 | croak "newMediaObject() usage error"; | ||
| 481 | } | ||
| 482 | |||
| 483 | my $blob = undef; | ||
| 484 | if ( ref $filename ) { | ||
| 485 | $blob = $$filename; | ||
| 486 | $filename = undef; | ||
| 487 | |||
| 488 | } else { | ||
| 489 | unless(open(FH, $filename)) { | ||
| 490 | $errstr = "couldn't open $filename: $!"; | ||
| 491 | return undef | ||
| 492 | } | ||
| 493 | local $/ = undef; | ||
| 494 | $blob = <FH>; close(FH); | ||
| 495 | } | ||
| 496 | |||
| 497 | if ( $filename && !$name ) { | ||
| 498 | require File::Basename; | ||
| 499 | $name = File::Basename::basename($filename); | ||
| 500 | } | ||
| 501 | |||
| 502 | unless ( $name ) { | ||
| 503 | croak "newMediaObject() usage error: \$name is missing" | ||
| 504 | } | ||
| 505 | |||
| 506 | my %content_hash = ( | ||
| 507 | bits => XMLRPC::Data->type(base64 => $blob), | ||
| 508 | name => $name, | ||
| 509 | type => $type || "" | ||
| 510 | ); | ||
| 511 | |||
| 512 | return $self->call('metaWeblog.newMediaObject', $blogid, $username, $password, \%content_hash) | ||
| 513 | } | ||
| 514 | |||
| 515 | |||
| 516 | |||
| 517 | |||
| 518 | |||
| 519 | |||
| 520 | |||
| 521 | |||
| 522 | |||
| 523 | |||
| 524 | sub dump { | ||
| 525 | my $self = shift; | ||
| 526 | |||
| 527 | require Data::Dumper; | ||
| 528 | my $d = new Data::Dumper([$self], [ref $self]); | ||
| 529 | return $d->Dump(); | ||
| 530 | } | ||
| 531 | |||
| 532 | |||
| 533 | |||
| 534 | package MovableType; | ||
| 535 | @MovableType::ISA = ('Net::MovableType'); | ||
| 536 | |||
| 537 | |||
| 538 | package MT; | ||
| 539 | @MT::ISA = ('Net::MovableType'); | ||
| 540 | |||
| 541 | |||
| 542 | 1; | ||
| 543 | __END__ | ||