| 1 | # WebFetch::Output::TWiki | ||
| 2 | # ABSTRACT: save data from WebFetch into a TWiki web site | ||
| 3 | # | ||
| 4 | # Copyright (c) 2009-2022 Ian Kluft. This program is free software; you can | ||
| 5 | # redistribute it and/or modify it under the terms of the GNU General Public | ||
| 6 | # License Version 3. See https://www.gnu.org/licenses/gpl-3.0-standalone.html | ||
| 7 | |||
| 8 | # pragmas to silence some warnings from Perl::Critic | ||
| 9 | ## no critic (Modules::RequireExplicitPackage) | ||
| 10 | # This solves a catch-22 where parts of Perl::Critic want both package and use-strict to be first | ||
| 11 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | ## use critic (Modules::RequireExplicitPackage) | ||
| 15 | |||
| 16 | package WebFetch::Output::TWiki; | ||
| 17 | $WebFetch::Output::TWiki::VERSION = '0.1.0'; | ||
| 18 | 1 | ▶ | use WebFetch; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use base "WebFetch"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 0 | ▶ | use DB_File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 21 | |||
| 22 | # define exceptions/errors | ||
| 23 | use Try::Tiny; | ||
| 24 | use Exception::Class ( | ||
| 25 | "WebFetch::Output::TWiki::Exception::NoRoot" => { | ||
| 26 | isa => "WebFetch::Exception", | ||
| 27 | alias => "throw_twiki_no_root", | ||
| 28 | description => "WebFetch::Output::TWiki needs to be provided " | ||
| 29 | ."a twiki_root parameter", | ||
| 30 | }, | ||
| 31 | "WebFetch::Output::TWiki::Exception::NotFound" => { | ||
| 32 | isa => "WebFetch::Exception", | ||
| 33 | alias => "throw_twiki_not_found", | ||
| 34 | description => "the directory in the twiki_root parameter " | ||
| 35 | ."doesn't exist or doesn't have a lib subdirectory", | ||
| 36 | }, | ||
| 37 | "WebFetch::Output::TWiki::Exception::Require" => { | ||
| 38 | isa => "WebFetch::Exception", | ||
| 39 | alias => "throw_twiki_require", | ||
| 40 | description => "failed to import TWiki or TWiki::Func modules", | ||
| 41 | }, | ||
| 42 | "WebFetch::Output::TWiki::Exception::NoConfig" => { | ||
| 43 | isa => "WebFetch::Exception", | ||
| 44 | alias => "throw_twiki_no_config", | ||
| 45 | description => "WebFetch::Output::TWiki needs to be provided " | ||
| 46 | ."a config_topic parameter", | ||
| 47 | }, | ||
| 48 | "WebFetch::Output::TWiki::Exception::ConfigMissing" => { | ||
| 49 | isa => "WebFetch::Exception", | ||
| 50 | alias => "throw_twiki_config_missing", | ||
| 51 | description => "WebFetch::Output::TWiki is missing a required " | ||
| 52 | ."configuration parameter", | ||
| 53 | }, | ||
| 54 | "WebFetch::Output::TWiki::Exception::Oops" => { | ||
| 55 | isa => "WebFetch::Exception", | ||
| 56 | alias => "throw_twiki_oops", | ||
| 57 | description => "WebFetch::Output::TWiki returned errors from " | ||
| 58 | ."saving one or more entries", | ||
| 59 | }, | ||
| 60 | "WebFetch::Output::TWiki::Exception::FieldNotSpecified" => { | ||
| 61 | isa => "WebFetch::Exception", | ||
| 62 | alias => "throw_field_not_specified", | ||
| 63 | description => "a required field was not defined or found", | ||
| 64 | }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | |||
| 68 | # globals/defaults | ||
| 69 | our @Options = ( "twiki_root=s", "config_topic=s", "config_key=s" ); | ||
| 70 | our $Usage = "--twiki_root path-to-twiki --config_topic web.topic " | ||
| 71 | ."--config_key keyword"; | ||
| 72 | our @default_field_names = ( qw( key web parent prefix template form | ||
| 73 | options )); | ||
| 74 | |||
| 75 | # no user-servicable parts beyond this point | ||
| 76 | |||
| 77 | # register capabilities with WebFetch | ||
| 78 | __PACKAGE__->module_register( "cmdline", "output:twiki" ); | ||
| 79 | |||
| 80 | |||
| 81 | # read the TWiki configuation | ||
| 82 | sub get_twiki_config | ||
| 83 | { | ||
| 84 | my $self = shift; | ||
| 85 | WebFetch::debug "in get_twiki_config"; | ||
| 86 | |||
| 87 | # find the TWiki modules | ||
| 88 | if ( ! exists $self->{twiki_root}) { | ||
| 89 | throw_twiki_no_root( "TWiki root directory not defined" ); | ||
| 90 | } | ||
| 91 | if (( ! -d $self->{twiki_root}) or ( ! -d $self->{twiki_root}."/lib" )) | ||
| 92 | { | ||
| 93 | throw_twiki_not_found( "can't find TWiki root or lib at " | ||
| 94 | .$self->{twiki_root}); | ||
| 95 | } | ||
| 96 | |||
| 97 | # load the TWiki modules | ||
| 98 | WebFetch::debug "loading TWiki modules"; | ||
| 99 | push @INC, $self->{twiki_root}."/lib"; | ||
| 100 | my $result; | ||
| 101 | try { | ||
| 102 | $result = ( require TWiki and require TWiki::Func ); | ||
| 103 | } catch { | ||
| 104 | throw_twiki_require ( $@ ); | ||
| 105 | }; | ||
| 106 | if (not $result) { | ||
| 107 | throw_twiki_require ( "require failed" ); | ||
| 108 | } | ||
| 109 | |||
| 110 | # initiate TWiki library, create session as user "WebFetch" | ||
| 111 | $self->{twiki_obj} = TWiki->new( "WebFetch" ); | ||
| 112 | |||
| 113 | # get the contents of the TWiki topic which contains our configuration | ||
| 114 | if ( !exists $self->{config_topic}) { | ||
| 115 | throw_twiki_no_config( "TWiki configuration page for WebFetch " | ||
| 116 | ."not defined" ); | ||
| 117 | } | ||
| 118 | my ( $web, $topic ) = split /\./x, $self->{config_topic}; | ||
| 119 | WebFetch::debug "config_topic: ".$self->{config_topic} | ||
| 120 | ." -> $web, $topic"; | ||
| 121 | if (( ! defined $web ) or ( ! defined $topic )) { | ||
| 122 | throw_twiki_no_config( "TWiki configuration page for WebFetch " | ||
| 123 | ."must be defined in the format web.topic" ); | ||
| 124 | } | ||
| 125 | |||
| 126 | # check if a config_key was specified before we read the configuration | ||
| 127 | if ( !exists $self->{config_key}) { | ||
| 128 | throw_twiki_no_config( "TWiki configuration key for WebFetch " | ||
| 129 | ."not defined" ); | ||
| 130 | } | ||
| 131 | |||
| 132 | # read the configuration info | ||
| 133 | my $config = TWiki::Func::readTopic( $web, $topic ); | ||
| 134 | |||
| 135 | # if STARTINCLUDE and STOPINCLUDE are present, use only what's between | ||
| 136 | if ( $config =~ /%STARTINCLUDE%\s*(.*)\s*%STOPINCLUDE%/xs ) { | ||
| 137 | $config = $1; | ||
| 138 | } | ||
| 139 | |||
| 140 | # parse the configuration | ||
| 141 | WebFetch::debug "parsing configuration"; | ||
| 142 | my @fnames; | ||
| 143 | $self->{twiki_config_all} = []; | ||
| 144 | $self->{twiki_keys} = {}; | ||
| 145 | foreach my $line ( split /\r*\n+/xs, $config ) { | ||
| 146 | if ( $line =~ /^\|\s*(.*)\s*\|\s*$/x ) { | ||
| 147 | my @entries = split /\s*\|\s*/x, $1; | ||
| 148 | WebFetch::debug "read entries: ".join( ', ', @entries ); | ||
| 149 | |||
| 150 | # first line contains field headings | ||
| 151 | if ( ! @fnames) { | ||
| 152 | # save table headings as field names | ||
| 153 | foreach my $field ( @entries ) { | ||
| 154 | my $tmp = lc($field); | ||
| 155 | $tmp =~ s/\W//xg; | ||
| 156 | push @fnames, $tmp; | ||
| 157 | } | ||
| 158 | next; | ||
| 159 | } | ||
| 160 | WebFetch::debug "field names: ".join " ", @fnames; | ||
| 161 | |||
| 162 | # save the entries | ||
| 163 | # it isn't a heading row if we got here | ||
| 164 | # transfer array @entries to named fields in %config | ||
| 165 | WebFetch::debug "data row: ".join " ", @entries; | ||
| 166 | my ( $i, $key, %config ); | ||
| 167 | for ( $i=0; $i < scalar @fnames; $i++ ) { | ||
| 168 | $config{ $fnames[$i]} = $entries[$i]; | ||
| 169 | if ( $fnames[$i] eq "key" ) { | ||
| 170 | $key = $entries[$i]; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | # save the %config row in @{$self->{twiki_config_all}} | ||
| 175 | if (( defined $key ) | ||
| 176 | and ( !exists $self->{twiki_keys}{$key})) | ||
| 177 | { | ||
| 178 | push @{$self->{twiki_config_all}}, \%config; | ||
| 179 | $self->{twiki_keys}{$key} = ( scalar | ||
| 180 | @{$self->{twiki_config_all}}) - 1; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | # select the line which is for this request | ||
| 186 | if ( ! exists $self->{twiki_keys}{$self->{config_key}}) { | ||
| 187 | throw_twiki_no_config "no configuration found for key " | ||
| 188 | .$self->{config_key}; | ||
| 189 | } | ||
| 190 | $self->{twiki_config} = $self->{twiki_config_all}[$self->{twiki_keys}{$self->{config_key}}]; | ||
| 191 | WebFetch::debug "twiki_config: ".join( " ", %{$self->{twiki_config}}); | ||
| 192 | return; | ||
| 193 | } | ||
| 194 | |||
| 195 | # write to a TWiki page | ||
| 196 | sub write_to_twiki | ||
| 197 | { | ||
| 198 | my $self = shift; | ||
| 199 | my $config; | ||
| 200 | |||
| 201 | # get config variables | ||
| 202 | $config = $self->{twiki_config}; | ||
| 203 | |||
| 204 | # parse options | ||
| 205 | $self->{twiki_options} = {}; | ||
| 206 | foreach my $option ( split /\s+/x, $self->{twiki_config}{options}) { | ||
| 207 | if ( $option =~ /^([^=]+)=(.*)/x ) { | ||
| 208 | $self->{twiki_options}{$1} = $2; | ||
| 209 | } else { | ||
| 210 | $self->{twiki_options}{$option} = 1; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | # determine unique identifier field | ||
| 215 | my $id_field; | ||
| 216 | if ( exists $self->{twiki_options}{id_field}) { | ||
| 217 | $id_field = $self->{twiki_options}{id_field}; | ||
| 218 | } | ||
| 219 | if ( ! defined $id_field ) { | ||
| 220 | $id_field = $self->wk2fname( "id" ); | ||
| 221 | } | ||
| 222 | if ( ! defined $id_field ) { | ||
| 223 | $id_field = $self->wk2fname( "url" ); | ||
| 224 | } | ||
| 225 | if ( ! defined $id_field ) { | ||
| 226 | $id_field = $self->wk2fname( "title" ); | ||
| 227 | } | ||
| 228 | if ( ! defined $id_field ) { | ||
| 229 | throw_field_not_specified "identifier field not specified"; | ||
| 230 | } | ||
| 231 | $self->{id_field} = $id_field; | ||
| 232 | |||
| 233 | # determine from options whether each item is making metadata or topics | ||
| 234 | if ( exists $self->{twiki_options}{separate_topics}) { | ||
| 235 | $self->write_to_twiki_topics; | ||
| 236 | } else { | ||
| 237 | $self->write_to_twiki_metadata; | ||
| 238 | } | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | |||
| 242 | # write to separate TWiki topics | ||
| 243 | sub write_to_twiki_topics | ||
| 244 | { | ||
| 245 | my $self = shift; | ||
| 246 | |||
| 247 | # get config variables | ||
| 248 | my $config = $self->{twiki_config}; | ||
| 249 | foreach my $name ( qw( key web parent prefix template form )) { | ||
| 250 | if ( !exists $self->{twiki_config}{$name}) { | ||
| 251 | throw_twiki_config_missing( "missing config parameter " | ||
| 252 | .$name ); | ||
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | # get text of template topic | ||
| 257 | my ($meta, $template ) = TWiki::Func::readTopic( $config->{web}, | ||
| 258 | $config->{template}); | ||
| 259 | |||
| 260 | # open DB file for tracking unique IDs of articles already processed | ||
| 261 | my %id_index; | ||
| 262 | tie %id_index, 'DB_File', | ||
| 263 | $self->{dir}."/".$config->{key}."_id_index.db", | ||
| 264 | &DB_File::O_CREAT|&DB_File::O_RDWR, oct(640); | ||
| 265 | |||
| 266 | # determine initial topic name | ||
| 267 | my ( %topics, @topics ); | ||
| 268 | @topics = TWiki::Func::getTopicList( $config->{web}); | ||
| 269 | foreach ( @topics ) { | ||
| 270 | $topics{$_} = 1; | ||
| 271 | } | ||
| 272 | my $tnum_counter = 0; | ||
| 273 | my $tnum_format = $config->{prefix}."-%07d"; | ||
| 274 | |||
| 275 | # create topics with metadata from each WebFetch data record | ||
| 276 | my $entry; | ||
| 277 | my @oopses; | ||
| 278 | my $id_field = $self->{id_field}; | ||
| 279 | $self->data->reset_pos; | ||
| 280 | while ( $entry = $self->data->next_record ) { | ||
| 281 | |||
| 282 | # check that this entry hasn't already been forwarded to TWiki | ||
| 283 | if ( exists $id_index{$entry->byname( $id_field )}) { | ||
| 284 | next; | ||
| 285 | } | ||
| 286 | $id_index{$entry->byname( $id_field )} = time; | ||
| 287 | |||
| 288 | # select topic name | ||
| 289 | my $topicname = sprintf $tnum_format, $tnum_counter; | ||
| 290 | while ( exists $topics{$topicname}) { | ||
| 291 | $tnum_counter++; | ||
| 292 | $topicname = sprintf $tnum_format, $tnum_counter; | ||
| 293 | } | ||
| 294 | $tnum_counter++; | ||
| 295 | $topics{$topicname} = 1; | ||
| 296 | my $text = $template; | ||
| 297 | WebFetch::debug "write_to_twiki_topics: writing $topicname"; | ||
| 298 | |||
| 299 | # create topic metadata | ||
| 300 | #my $meta = TWiki::Meta->new ( $self->{twiki_obj}, $config->{web}, $topicname ); | ||
| 301 | $meta->put( "TOPICPARENT", | ||
| 302 | { name => $config->{parent}}); | ||
| 303 | $meta->put( "FORM", { name => $config->{form}}); | ||
| 304 | my $fnum; | ||
| 305 | for ( $fnum = 0; $fnum <= $self->data->num_fields; $fnum++ ) { | ||
| 306 | WebFetch::debug "meta: " | ||
| 307 | .$self->data->field_bynum($fnum) | ||
| 308 | ." = ".$entry->bynum($fnum); | ||
| 309 | ( defined $self->data->field_bynum($fnum)) or next; | ||
| 310 | ( $self->data->field_bynum($fnum) eq "xml") and next; | ||
| 311 | ( defined $entry->bynum($fnum)) or next; | ||
| 312 | WebFetch::debug "meta: OK"; | ||
| 313 | $meta->putKeyed( "FIELD", { | ||
| 314 | name => $self->data->field_bynum($fnum), | ||
| 315 | value => $entry->bynum($fnum)}); | ||
| 316 | } | ||
| 317 | |||
| 318 | # save a special title field for TWiki indexes | ||
| 319 | my $index_title = $entry->title; | ||
| 320 | $index_title =~ s/[\t\r\n\|]+/ /xgs; | ||
| 321 | $index_title =~ s/^\s*//x; | ||
| 322 | $index_title =~ s/\s*$//x; | ||
| 323 | if ( length($index_title) > 60 ) { | ||
| 324 | substr( $index_title, 56, -1, "..."); | ||
| 325 | } | ||
| 326 | WebFetch::debug "title: $index_title"; | ||
| 327 | $meta->putKeyed( "FIELD", { | ||
| 328 | name => "IndexTitle", | ||
| 329 | title => "Indexing title", | ||
| 330 | value => $index_title }); | ||
| 331 | |||
| 332 | # save the topic | ||
| 333 | my $oopsurl = TWiki::Func::saveTopic( $config->{web}, | ||
| 334 | $topicname, $meta, $text ); | ||
| 335 | if ( $oopsurl ) { | ||
| 336 | WebFetch::debug "write_to_twiki_topics: " | ||
| 337 | ."$topicname - $oopsurl"; | ||
| 338 | push @oopses, $entry->title." -> " | ||
| 339 | .$topicname." ".$oopsurl; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | # check for errors | ||
| 344 | if ( @oopses ) { | ||
| 345 | throw_twiki_oops( "TWiki saves failed:\n".join "\n", @oopses ); | ||
| 346 | } | ||
| 347 | return; | ||
| 348 | } | ||
| 349 | |||
| 350 | # write to successive items of TWiki metadata | ||
| 351 | sub write_to_twiki_metadata | ||
| 352 | { | ||
| 353 | my $self = shift; | ||
| 354 | |||
| 355 | # get config variables | ||
| 356 | my $config = $self->{twiki_config}; | ||
| 357 | foreach my $name ( qw( key web parent )) { | ||
| 358 | if ( !exists $self->{twiki_config}{$name}) { | ||
| 359 | throw_twiki_config_missing( "missing config parameter " | ||
| 360 | .$name ); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | # determine metadata title field | ||
| 365 | my $title_field; | ||
| 366 | if ( exists $self->{twiki_options}{title_field}) { | ||
| 367 | $title_field = $self->{twiki_options}{title_field}; | ||
| 368 | } | ||
| 369 | if ( ! defined $title_field ) { | ||
| 370 | $title_field = $self->wk2fname( "title" ); | ||
| 371 | } | ||
| 372 | if ( ! defined $title_field ) { | ||
| 373 | throw_field_not_specified "title field not specified"; | ||
| 374 | } | ||
| 375 | |||
| 376 | # determine metadata value field | ||
| 377 | my $value_field; | ||
| 378 | if ( exists $self->{twiki_options}{value_field}) { | ||
| 379 | $value_field = $self->{twiki_options}{value_field}; | ||
| 380 | } | ||
| 381 | if ( ! defined $value_field ) { | ||
| 382 | $value_field = $self->wk2fname( "summary" ); | ||
| 383 | } | ||
| 384 | if ( ! defined $value_field ) { | ||
| 385 | throw_field_not_specified "value field not specified"; | ||
| 386 | } | ||
| 387 | |||
| 388 | # open DB file for tracking unique IDs of articles already processed | ||
| 389 | my %id_index; | ||
| 390 | tie %id_index, 'DB_File', | ||
| 391 | $self->{dir}."/".$config->{key}."_id_index.db", | ||
| 392 | &DB_File::O_CREAT|&DB_File::O_RDWR, oct(640); | ||
| 393 | |||
| 394 | # get text of topic | ||
| 395 | my ($meta, $text) = TWiki::Func::readTopic( $config->{web}, | ||
| 396 | $config->{parent}); | ||
| 397 | |||
| 398 | # start metadata line counter | ||
| 399 | my $mnum_counter = 0; | ||
| 400 | my $mnum_format = "line-%07d"; | ||
| 401 | |||
| 402 | # create metadata lines for each entry | ||
| 403 | my $entry; | ||
| 404 | my $id_field = $self->{id_field}; | ||
| 405 | $self->data->reset_pos; | ||
| 406 | while ( $entry = $self->data->next_record ) { | ||
| 407 | # check that this entry hasn't already been forwarded to TWiki | ||
| 408 | if ( exists $id_index{$entry->byname( $id_field )}) { | ||
| 409 | next; | ||
| 410 | } | ||
| 411 | $id_index{$entry->byname( $id_field )} = time; | ||
| 412 | |||
| 413 | # select metadata field name | ||
| 414 | my ( $value, $metaname ); | ||
| 415 | $value = $meta->get( "FIELD", | ||
| 416 | $metaname = sprintf( $mnum_format, $mnum_counter )); | ||
| 417 | while ( defined $value ) { | ||
| 418 | $value = $meta->get( "FIELD", | ||
| 419 | $metaname = sprintf( $mnum_format, | ||
| 420 | ++$mnum_counter )); | ||
| 421 | } | ||
| 422 | |||
| 423 | # write the value | ||
| 424 | $meta->putKeyed( "FIELD", { | ||
| 425 | name => $metaname, | ||
| 426 | title => $entry->byname( $title_field ), | ||
| 427 | value => $entry->byname( $value_field ), | ||
| 428 | }); | ||
| 429 | } | ||
| 430 | |||
| 431 | # save the topic | ||
| 432 | my $oopsurl = TWiki::Func::saveTopic( $config->{web}, | ||
| 433 | $config->{parent}, $meta, $text ); | ||
| 434 | if ( $oopsurl ) { | ||
| 435 | throw_twiki_oops "TWiki saves failed: " | ||
| 436 | .$config->{parent}." ".$oopsurl; | ||
| 437 | } | ||
| 438 | return; | ||
| 439 | } | ||
| 440 | |||
| 441 | # TWiki format handler | ||
| 442 | sub fmt_handler_twiki | ||
| 443 | { | ||
| 444 | my $self = shift; | ||
| 445 | |||
| 446 | # get configuration from TWiki | ||
| 447 | $self->get_twiki_config; | ||
| 448 | |||
| 449 | # write to TWiki topic | ||
| 450 | $self->write_to_twiki; | ||
| 451 | |||
| 452 | # no savables - mark it OK so WebFetch::save won't call it an error | ||
| 453 | $self->no_savables_ok; | ||
| 454 | return 1; | ||
| 455 | } | ||
| 456 | |||
| 457 | |||
| 458 | 1; # End of WebFetch::Output::TWiki | ||
| 459 | |||
| 460 | __END__ | ||