1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
|
##############################################################################
# The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. #
# #
# This program is free software; you can redistribute it and/or #
# modify it under the terms of the GNU General Public License #
# as published by the Free Software Foundation; either version 2 #
# of the License, or (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#
# #
# Jon Howell can be contacted at: #
# 6211 Sudikoff Lab, Dartmouth College #
# Hanover, NH 03755-3510 #
# jonh@cs.dartmouth.edu #
# #
# An electronic copy of the GPL is available at: #
# http://www.gnu.org/copyleft/gpl.html #
# #
##############################################################################
use strict;
use locale;
###
### The FAQ::OMatic::API provides ways to programmatically modify
### the contents of your FAQ-O-Matic.
###
package FAQ::OMatic::API;
=head1 NAME
FAQ::OMatic::API - a Perl API to manipulate FAQ-O-Matics
=head1 SYNOPSIS
use FAQ::OMatic::API;
my $fom_api = new FAQ::OMatic::API();
=head1 DESCRIPTION
C<FAQ::OMatic::API> is a class that makes HTTP requests to a FAQ-O-Matic.
It provides a way to manipulate a FAQ-O-Matic from Perl code. Operations
are performed by making HTTP requests on the FAQ-O-Matic server;
this ensures that any operation is performed in exactly the same environment
as it would be if it were requested by a human using a web browser.
=head1 Setup
The following methods are used to set up an API object.
=over 4
=cut
use FAQ::OMatic;
use LWP::UserAgent;
use URI;
use HTTP::Request::Common qw(GET POST);
=item $fom_api = new FAQ::OMatic::API([$url, [$id, $pass]]);
Constructs a new C<FAQ::OMatic::API> object that points at the FAQ
whose CGI is at C<$url>. Requests will be authenticated on behalf of
FAQ user C<$id> using password C<$pass>.
=cut
sub new {
my ($class) = shift;
my $api = {};
$api->{'auth'} = [];
$api->{'cacheTimeout'} = 24*60*60;
bless $api;
if (scalar(@_)>=1) {
$api->setURL(shift(@_));
}
if (scalar(@_)>=2) {
$api->setAuth(shift(@_), shift(@_));
}
return $api;
}
=item $fom_api->setURL($url);
Sets the URL of the target FAQ's CGI. This is where requests will be sent.
=cut
sub setURL {
my $self = shift;
my $url = shift;
$self->{'url'} = $url;
}
=item $fom_api->setAuth($id, $pass);
Sets the ID and password that will be used to authenticate requests.
=item $fom_api->setAuth('query');
Passing in 'query' will cause the script to query the terminal for
the C<id> and C<password> at runtime.
=cut
sub setAuth {
my $self = shift;
my $id = shift;
my $pass = shift;
if ($id eq 'query') {
# defer auth to input from user
$self->{'auth'} = 'query';
} else {
$self->{'auth'} =
[ 'auth'=>'pass', '_pass_id'=>$id, '_pass_pass'=>$pass ];
}
}
# THANKS to Dirk Husemann <hud@zurich.ibm.com> for this feature diff.
sub setCachePath {
my $self = shift;
my $cachePath = shift;
$self->{'cachePath'} = $cachePath;
}
sub setCacheTimeout {
my $self = shift;
$self->{'cacheTimeout'} = shift || 24*60*60;
}
=back
=head1 Queries
The following methods retrieve information about a FAQ-O-Matic.
=over 4
=item $fom_api->getCategories()
Returns a list of the categories (by file name) in the FAQ.
=cut
sub getCategories {
my $self = shift;
$self->getMirrorList();
return map { $_->[0]->[1] } @{$self->{'categories'}};
}
=item $item = $fom_api->getItem($filename)
Returns a local FAQ::OMatic::Item object created by retrieving C<$filename>
from the FAQ. You can perform operations on the result such as:
print $item->getTitle();
$parentItem = $item->getParent();
print $item->displayHTML({'render'=>'text'});
=cut
sub getItem {
my $self = shift;
my $filename = shift;
my ($rc, $msg) = $self->getMirrorList();
return ($rc, $msg) if (not $rc);
my $uri = new URI($self->{'url'});
$uri->path($self->{'itemURL'}.$filename);
my $req = GET($uri);
my $rep = $self->userAgent()->request($req);
if (not $rep->is_success()) {
if ($self->{'debug'} || '') {
print "Request: ".$uri->as_string()."\n";
$self->dumpresp($rep);
}
return (0, "\$rep reports failure: ".$rep->status_line."\nrequest was: ".$uri);
}
my $item = new FAQ::OMatic::Item();
$item->{'filename'} = $filename;
$item->loadFromString($rep->content(), $filename);
return (1, $item);
}
=item my ($rc, $result) = $fom_api->fuzzyMatch(['Parent Category','child cat'])
my ($rc, $result) = $fom_api->fuzzyMatch(['Parent Category','child cat'])
die $result if (not $rc);
my $item = $fom_api->getItem($result->[0]);
C<fuzzyMatch()> attempts to figure out which category the last string
in its array argument represents. The category name is matched "fuzzily"
against existing categories. If a unique match is found, it is returned
(as an array ref C<[$filename, $parent, $title]>). If the match is
ambiguous, the previous array element is matched against the parents
of the set of fuzzy matches. This is performed recursively until the
child category is disambiguated.
Fuzzy matching means that
=over 4
=item (a)
You don't have to get the parent category
names right if they're not needed to disambiguate the child category.
=item (b)
Category names are matched without respect to case, spacing, or punctuation:
only alphanumerics matter. Also, the name you supply only has to appear
somewhere inside the one you want to match. (Exact matches are preferred;
this allows you to match categories whose names are prefixes of other
category names.)
=back
=cut
# TODO: It wouldn't be a difficult modification to let you match
# answers as well as categories, which would be useful for appending
# to existing answers (rather than creating new answers in existing
# categories).
sub fuzzyMatch {
my $self = shift;
my $path = shift; # ary ref
my $cats = $self->catnames();
my @rpath = reverse (@{$path});
for (my $depth = 0; $depth<@rpath; $depth++) {
# look for match at leaf node; disambiguate by rising up tree
my $matchname = $rpath[$depth];
$matchname = lc($matchname); # lowercase
$matchname =~ s/\W//g; # only alphanumerics + '_'
# try for an exact match
my @matchcats = grep {
my $catname = $_->[2];
$catname = lc($catname); # lowercase
$catname =~ s/\W//g; # only alphanumerics + '_'
$catname =~ m/^$matchname$/; # check for exact match
} @{$cats};
if (@matchcats == 0) {
# fall back to fuzzier match (allow any prefix, suffix)
@matchcats = grep {
my $catname = $_->[2];
$catname = lc($catname); # lowercase
$catname =~ s/\W//g; # only alphanumerics + '_'
$catname =~ m/$matchname/; # check for inclusion
} @{$cats};
}
if (scalar(@matchcats)==1) {
# unambiguous -- unwind the parent chain to get to the leaf
my $cat = $matchcats[0];
while (defined $cat->[3]) {
$cat = $cat->[3]; # has child? unwind child.
}
return (1, $cat);
} elsif (scalar(@matchcats)==0) {
# unmatched
return (0, "no names match ".join(":", @{$path}));
} else {
# ambiguous: attempt to disambiguate at next higher depth
$cats = [ map {
my $parentcat = $self->catByName($_->[1]);
[ @{$parentcat}, $_ ];
} @matchcats ];
if ($self->{'debug'}) {
print "matched ".scalar(@{$cats})." items at level $depth\n";
print map {" m: ".join(",", @{$_})."\n"} @{$cats};
}
next;
}
}
# ran out of names to disambiguate with
return (0, join(":", @{$path})." -- not enough levels to disambiguate");
}
=back
=head1 Operations
The following methods perform operations on a FAQ-O-Matic.
=over 4
=item $fom_api->newAnswer($parent, $title, $text)
A new answer is created as a child of item C<$parent>.
The new answer has title C<$title> and a single text part containing
C<$text>.
=cut
sub newAnswer {
my $self = shift;
my $parent = shift;
my $title = shift;
my $text = shift;
my ($rc, $msg);
($rc, $msg) = $self->transaction(['cmd'=>'addItem', 'file'=>$parent]);
if (not $rc) {
return ($rc, "(addItem) ".$msg);
}
my $filename = $msg->{'file'} || die "no filename in reply";
my $seq = $msg->{'checkSequenceNumber'};
die "no sequenceNumber in reply" if (not defined $seq);
($rc, $msg) = $self->transaction(
['cmd'=>'submitItem',
'file'=>$filename,
'_Title'=>$title,
'checkSequenceNumber'=>$seq,
'_zzverify'=>'zz']);
if (not $rc) {
return ($rc, "(submitItem) ".$msg);
}
my $filename2 = $msg->{'file'} || die "no filename in reply";
if ($filename2 ne $filename) {
return (0, "submitItem(file=$filename) = $filename2");
}
$seq = $msg->{'checkSequenceNumber'};
die "no sequenceNumber in reply" if (not defined $seq);
($rc, $msg) = $self->transaction(
['cmd'=>'submitPart',
'_insertpart'=>'1',
'partnum'=>'-1',
'file'=>$filename,
'_Type'=>'',
'_newText'=>$text,
'checkSequenceNumber'=>$seq,
'_zzverify'=>'zz']);
my $seq2 = $msg->{'checkSequenceNumber'};
die "no sequenceNumber in reply" if (not defined $seq2);
die "sequence number didn't advance" if ($seq2 != $seq+1);
# THANKS Scott M Parrish <sparrish@fc.hp.com> for pointing out
# that by returning $filename allocated here, the caller will be able to
# add a hierarchy of answers.
return ($rc, $filename, "(submitPart) ".$msg);
# TODO: allow user to give alternate ID
}
=back
=head1 REQUIRES
=over 4
=item L<LWP> and its hierarchy of helper classes.
=back
=cut
# =head1 EXAMPLES
#
# =over 4
#
# my $item = $fom->getItem('1');
# print $fom->displayHTML({'render'=>'text'});
#
# Displays a sloppy text rendering of item number 1. (It is far better to
# ask the CGI to do the rendering, however.)
# private functions
sub getMirrorList {
my $self = shift;
if (not $self->{'force'}
and defined $self->{'mirrorCached'}) {
return (1, 'mirror list in cache');
}
# attempt to get a local copy from disk
my $mirrorFilename = $self->{'cachePath'} . "/.fomapi-mirrorCached";
my $cachetime = -M $mirrorFilename;
if (not defined $cachetime
or $cachetime > $self->{'cacheTimeout'}) {
# cache not there or too old to be useful. Go to server to get info.
my ($rc, $msg) = $self->transaction(['cmd'=>'mirrorServer'], 'raw');
return ($rc, $msg) if (not $rc);
$self->{'mirrorCached'} = $msg;
# write cache file
open MCACHE, ">$mirrorFilename";
print MCACHE $msg;
close MCACHE;
} else {
# cache is valid enough for today
open MCACHE, "<$mirrorFilename";
my @lines = <MCACHE>;
close MCACHE;
$self->{'mirrorCached'} = join('', @lines);
}
$self->parseMirrorList();
return (1, 'mirror list retrieved');
}
sub parseMirrorList {
my $self = shift;
my @lines = split(/\n/, $self->{'mirrorCached'});
my @items = grep { m/^item / } @lines;
@items = map { my @f = split(' '); \@f; } @items;
my @cats = grep { $_->[3] eq 'Category' } @items;
if ($self->{'debug'} || '') {
print map { "c: ".join(" ", @{$_})."\n" } @cats;
}
$self->{'categories'} = [ map { [ $_ ] } @cats ];
my $itemURL = (split(' ', (grep { m/^itemURL/ } @lines)[0]))[1];
$self->{'itemURL'} = $itemURL;
return (1, 'successfully acquired mirror list');
}
sub catByName {
my $self = shift;
my $fn = shift;
if (not $self->{'cathash'}) {
$self->{'cathash'} = { map { ($_->[0], $_) } @{$self->catnames()} };
}
return $self->{'cathash'}->{$fn};
}
sub catnames {
my $self = shift;
if ($self->{'force'}
or not defined $self->{'catsCached'}) {
my $catFilename = $self->{'cachePath'} . "/.fomapi-catCached";
my $cachetime = -M $catFilename;
if (not defined $cachetime
or $cachetime > $self->{'cacheTimeout'}) {
my @catlist = ();
$self->getMirrorList();
foreach my $cat ($self->getCategories()) {
my ($rc, $item) = $self->getItem($cat);
return(wantarray() ? (0, $item) : 0) if (not $rc);
my $itemfn = $item->getProperty('filename');
my $parentfn = $item->getProperty('Parent');
my $title = $item->getTitle();
if ($self->{'debug'}||'') {
print sprintf("%-5s %-5s %-30s\n",
$itemfn, $parentfn, $title);
}
push @catlist, [$itemfn, $parentfn, $title];
}
open CCACHE, ">$catFilename";
print CCACHE map {join(" ", @{$_})."\n"} @catlist;
close CCACHE;
$self->{'catsCached'} = \@catlist;
} else {
open CCACHE, "<$catFilename";
my @lines = <CCACHE>;
close CCACHE;
@lines = map {chomp; [split(' ',$_,3)];} @lines;
$self->{'catsCached'} = \@lines;
}
}
return wantarray()
? (1, $self->{'catsCached'})
: $self->{'catsCached'};
}
sub userAgent {
my $self = shift;
my $ua = $self->{'userAgent'};
if (not defined $ua) {
# create a user agent object
$ua = new LWP::UserAgent;
$ua->agent("FAQ-OMatic-API/".$FAQ::OMatic::VERSION
."-".$ua->agent());
$self->{'userAgent'} = $ua;
}
return $ua;
}
sub getAuth {
my $self = shift;
if ($self->{'auth'} eq 'query') {
print "FAQ username: ";
FAQ::OMatic::flush('STDOUT');
my $user = <STDIN>;
chomp $user;
print "FAQ password: ";
FAQ::OMatic::flush('STDOUT');
system('stty -echo'); # avoid echoing password if possible
my $pass = <STDIN>;
chomp $pass;
system('stty echo');
print "\n"; # we didn't echo the user's CR, so supply one
FAQ::OMatic::flush('STDOUT');
$self->setAuth($user, $pass);
}
return @{$self->{'auth'}};
}
sub transaction {
my $self = shift;
my $newparams = shift || [];
my $rawContent = shift || ''; # don't try to parse content for 'isapi'
# request the item to be added
my $url = $self->{'url'};
my $req = POST($url,
'Content_Type'=>'form-data',
'Content'=> [ 'isapi'=>'1', $self->getAuth(), @{$newparams}
]);
my $rep = $self->userAgent()->request($req);
my $auth_content = $rep->content();
# not sure why this comes out differently
if ($auth_content =~ m/cmd=authenticate/) {
return (undef, 'need authentication: '.$auth_content);
}
# return (0, 'debug');
my $content = $rep->content();
if (not $rawContent) {
if (not $content =~ m/^isapi=1/) {
$self->dumpresp($rep);
return (undef, 'CGI did not understand isapi mode: '.$content);
}
# my %values = split(/[=\n]/, $content);
my %values = ();
my $pair;
foreach $pair (split(/\n+/, $content)) {
my ($key,$value) = split(/=/, $pair);
$values{$key} = $value;
}
if ($self->{'debug'} || '') {
print "successful reply to ".$req->content().":\n";
print map { "v: $_ => ".$values{$_}."\n" } sort keys %values;
}
return (1, \%values);
} else {
# return unparsed content
return (1, $content);
}
}
sub dumpresp {
my $self = shift;
my $rep = shift;
if ($self->{'debug'} || '') {
print map { "r: $_ => ".$rep->{$_}."\n" } sort keys %{$rep};
my $hdrs = $rep->headers();
print map { "h: $_ => ".$hdrs->header($_)."\n" }
sort keys %{$hdrs};
}
}
1;
|