| 1 | package Miril::Store::File; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use autodie; | ||
| 6 | |||
| 7 | use Data::AsObject dao => { mode => 'silent' }; | ||
| 8 | use File::Slurp; | ||
| 9 | use XML::TreePP; | ||
| 10 | use Try::Tiny qw(try catch); | ||
| 11 | use IO::File; | ||
| 12 | use File::Spec; | ||
| 13 | use List::Util qw(first); | ||
| 14 | use Ref::List qw(list); | ||
| 15 | use Miril::DateTime; | ||
| 16 | use Miril::DateTime::ISO::Simple qw(time2iso iso2time); | ||
| 17 | use Miril::Exception; | ||
| 18 | use Miril::Store::File::Post; | ||
| 19 | use File::Spec::Functions qw(catfile); | ||
| 20 | use Miril::URL; | ||
| 21 | use Syntax::Keyword::Gather qw(gather take); | ||
| 22 | |||
| 23 | ### ACCESSORS ### | ||
| 24 | |||
| 25 | use Object::Tiny qw(miril tpp tree); | ||
| 26 | |||
| 27 | ### CONSTRUCTOR ### | ||
| 28 | |||
| 29 | sub new | ||
| 30 | { | ||
| 31 | my ($class, $miril) = @_; | ||
| 32 | |||
| 33 | my $self = bless {}, $class; | ||
| 34 | $self->{miril} = $miril; | ||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | ### PUBLIC METHODS ### | ||
| 39 | |||
| 40 | sub get_post | ||
| 41 | { | ||
| 42 | my ($self, $id) = @_; | ||
| 43 | |||
| 44 | my $miril = $self->miril; | ||
| 45 | my $cfg = $miril->cfg; | ||
| 46 | my $util = $miril->util; | ||
| 47 | |||
| 48 | my $filename = $util->inflate_in_path($id); | ||
| 49 | my $post_file; | ||
| 50 | |||
| 51 | try | ||
| 52 | { | ||
| 53 | $post_file = File::Slurp::read_file($filename); | ||
| 54 | } | ||
| 55 | catch | ||
| 56 | { | ||
| 57 | Miril::Exception->throw( | ||
| 58 | message => "Could not read data file", | ||
| 59 | errorvar => $_, | ||
| 60 | ); | ||
| 61 | }; | ||
| 62 | |||
| 63 | my ($meta, $source) = split( /\n\n/, $post_file, 2); | ||
| 64 | my ($teaser) = split( '<!-- BREAK -->', $source, 2); | ||
| 65 | |||
| 66 | my %meta = _parse_meta($meta); | ||
| 67 | |||
| 68 | my $modified = $util->inflate_date_modified($filename); | ||
| 69 | my $type = $util->inflate_type($meta{'type'}); | ||
| 70 | |||
| 71 | my $published = $meta{'published'} ? Miril::DateTime->new(iso2time($meta{'published'})) : undef; | ||
| 72 | |||
| 73 | return Miril::Store::File::Post->new( | ||
| 74 | id => $id, | ||
| 75 | title => $meta{'title'}, | ||
| 76 | body => $miril->filter->to_xhtml($source), | ||
| 77 | teaser => $miril->filter->to_xhtml($teaser), | ||
| 78 | source => $source, | ||
| 79 | out_path => $util->inflate_out_path($id, $type), | ||
| 80 | in_path => $filename, | ||
| 81 | modified => Miril::DateTime->new($modified), | ||
| 82 | published => $published, | ||
| 83 | type => $type, | ||
| 84 | url => $published ? $util->inflate_post_url($id, $type, $published) : undef, | ||
| 85 | author => $util->inflate_author($meta{'author'}), | ||
| 86 | topics => $util->inflate_topics( list $meta{'topics'} ), | ||
| 87 | ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub get_posts | ||
| 91 | { | ||
| 92 | my ($self, %params) = @_; | ||
| 93 | |||
| 94 | my $miril = $self->miril; | ||
| 95 | my $cfg = $miril->cfg; | ||
| 96 | my $util = $miril->util; | ||
| 97 | |||
| 98 | # read and parse cache file | ||
| 99 | my $tpp = XML::TreePP->new(); | ||
| 100 | $tpp->set( force_array => ['post', 'topic'] ); | ||
| 101 | $tpp->set( indent => 2 ); | ||
| 102 | $self->{tpp} = $tpp; | ||
| 103 | |||
| 104 | my ($tree, @posts, $dirty); | ||
| 105 | |||
| 106 | if (-e $cfg->cache_data) { | ||
| 107 | try | ||
| 108 | { | ||
| 109 | $tree = $tpp->parsefile( $cfg->cache_data ); | ||
| 110 | } catch { | ||
| 111 | Miril::Exception->throw( | ||
| 112 | message => "Could not read cache file", | ||
| 113 | erorvar => $_, | ||
| 114 | ); | ||
| 115 | }; | ||
| 116 | @posts = map { | ||
| 117 | my $type = $util->inflate_type($_->type); | ||
| 118 | my @topics = $_->topics->topic->list if $_->topics; | ||
| 119 | |||
| 120 | Miril::Store::File::Post->new( | ||
| 121 | id => $_->id, | ||
| 122 | title => $_->title, | ||
| 123 | in_path => $util->inflate_in_path($_->id), | ||
| 124 | out_path => $util->inflate_out_path($_->id, $type), | ||
| 125 | modified => Miril::DateTime->new($_->modified), | ||
| 126 | published => $_->published ? Miril::DateTime->new($_->published) : undef, | ||
| 127 | type => $type, | ||
| 128 | author => $util->inflate_author($_->author), | ||
| 129 | topics => $util->inflate_topics(@topics), | ||
| 130 | url => $_->published ? $util->inflate_post_url($_->id, $type, Miril::DateTime->new($_->published)) : undef, | ||
| 131 | ); | ||
| 132 | } dao list $tree->{xml}{post}; | ||
| 133 | } else { | ||
| 134 | # miril is run for the first time | ||
| 135 | $tree = {}; | ||
| 136 | } | ||
| 137 | |||
| 138 | my @post_ids; | ||
| 139 | |||
| 140 | # for each post, check if the data in the cache is older than the data in the filesystem | ||
| 141 | foreach my $post (@posts) { | ||
| 142 | if ( -e $post->in_path ) { | ||
| 143 | push @post_ids, $post->id; | ||
| 144 | my $modified = $util->inflate_date_modified($post->in_path); | ||
| 145 | if ( $modified > $post->modified->epoch ) { | ||
| 146 | $post = $self->get_post($post->id); | ||
| 147 | $dirty++; | ||
| 148 | } | ||
| 149 | } else { | ||
| 150 | undef $post; | ||
| 151 | $dirty++; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | # clean up posts deleted from the cache | ||
| 156 | @posts = grep { defined } @posts; | ||
| 157 | |||
| 158 | # check for entries missing from the cache | ||
| 159 | opendir(my $data_dir, $cfg->data_path); | ||
| 160 | while ( my $id = readdir($data_dir) ) { | ||
| 161 | next if -d $id; | ||
| 162 | unless ( first {$_ eq $id} @post_ids ) { | ||
| 163 | my $post = $self->get_post($id); | ||
| 164 | push @posts, $post; | ||
| 165 | $dirty++; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | while ( my $id = readdir($data_dir) ) { | ||
| 170 | next if -d $id; | ||
| 171 | unless ( first {$_ eq $id} @post_ids ) { | ||
| 172 | my $post = $self->get_post($id); | ||
| 173 | push @posts, $post; | ||
| 174 | $dirty++; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | # update cache file | ||
| 179 | if ($dirty) { | ||
| 180 | my $new_tree = $tree; | ||
| 181 | $new_tree->{xml}{post} = _generate_cache_hash(@posts); | ||
| 182 | |||
| 183 | try { | ||
| 184 | $self->tpp->writefile($cfg->cache_data, $new_tree); | ||
| 185 | } catch { | ||
| 186 | Miril::Exception->throw( | ||
| 187 | message => "Cannot update cache file", | ||
| 188 | errorvar => $_, | ||
| 189 | ); | ||
| 190 | }; | ||
| 191 | } | ||
| 192 | |||
| 193 | # filter posts | ||
| 194 | if (%params) | ||
| 195 | { | ||
| 196 | @posts = gather | ||
| 197 | { | ||
| 198 | for my $cur_post (@posts) | ||
| 199 | { | ||
| 200 | my $title_rx = $params{'title'}; | ||
| 201 | next if $params{'title'} && $cur_post->title !~ /$title_rx/i; | ||
| 202 | next if $params{'author'} && $cur_post->author ne $params{'author'}; | ||
| 203 | next if $params{'type'} && $cur_post->type->id ne $params{'type'}; | ||
| 204 | next if $params{'status'} && $cur_post->status ne $params{'status'}; | ||
| 205 | next if $params{'topic'} && !first {$_->id eq $params{'topic'}} list $cur_post->topics; | ||
| 206 | take $cur_post; | ||
| 207 | } | ||
| 208 | }; | ||
| 209 | } | ||
| 210 | |||
| 211 | if ($cfg->sort eq 'modified') | ||
| 212 | { | ||
| 213 | @posts = sort { $b->modified->epoch <=> $a->modified->epoch } @posts; | ||
| 214 | } | ||
| 215 | else | ||
| 216 | { | ||
| 217 | if ( first { !$_->published } @posts ) | ||
| 218 | { | ||
| 219 | @posts = sort { $b->modified->epoch <=> $a->modified->epoch } @posts; | ||
| 220 | } | ||
| 221 | else | ||
| 222 | { | ||
| 223 | @posts = sort { $b->published->epoch <=> $a->published->epoch } @posts; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | if ($params{'last'}) | ||
| 228 | { | ||
| 229 | my $count = ( $params{'last'} < @posts ? $params{'last'} : @posts ); | ||
| 230 | splice @posts, $count; | ||
| 231 | } | ||
| 232 | |||
| 233 | return @posts; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub save | ||
| 237 | { | ||
| 238 | my ($self, %post) = @_; | ||
| 239 | |||
| 240 | my $post = dao \%post; | ||
| 241 | my $miril = $self->miril; | ||
| 242 | my $cfg = $miril->cfg; | ||
| 243 | my $util = $miril->util; | ||
| 244 | |||
| 245 | my @posts = $self->get_posts; | ||
| 246 | |||
| 247 | if ($post->old_id) { | ||
| 248 | # this is an update | ||
| 249 | |||
| 250 | for (@posts) { | ||
| 251 | if ($_->id eq $post->old_id) { | ||
| 252 | $_->{id} = $post->id; | ||
| 253 | $_->{author} = $post->author; | ||
| 254 | $_->{title} = $post->title; | ||
| 255 | $_->{type} = $util->inflate_type($post->type); | ||
| 256 | $_->{topics} = $util->inflate_topics(list $post->topics); | ||
| 257 | $_->{status} = $post->status; | ||
| 258 | $_->{source} = $post->source; | ||
| 259 | if ($post->status eq 'published') | ||
| 260 | { | ||
| 261 | $_->{published} = $util->inflate_date_published($_->published, $post->status); | ||
| 262 | } | ||
| 263 | last; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | # delete the old file if we have changed the id | ||
| 268 | if ($post->old_id ne $post->id) { | ||
| 269 | try { | ||
| 270 | unlink($cfg->data_path . '/' . $post->old_id); | ||
| 271 | } catch { | ||
| 272 | Miril::Exception->throw( | ||
| 273 | message => "Cannot delete old version of renamed post", | ||
| 274 | errorvar => $_ | ||
| 275 | ); | ||
| 276 | }; | ||
| 277 | } | ||
| 278 | |||
| 279 | } else { | ||
| 280 | # this is a new post | ||
| 281 | my $new_post = Miril::Store::File::Post->new( | ||
| 282 | id => $post->id, | ||
| 283 | author => ($post->author or undef), | ||
| 284 | title => $post->title, | ||
| 285 | type => $util->inflate_type($post->type), | ||
| 286 | topics => $util->inflate_topics($post->topics), | ||
| 287 | published => $util->inflate_date_published(undef, $post->status), | ||
| 288 | status => $post->status, | ||
| 289 | source => $post->source, | ||
| 290 | ); | ||
| 291 | push @posts, $new_post; | ||
| 292 | } | ||
| 293 | |||
| 294 | # update the cache file | ||
| 295 | my $new_tree; | ||
| 296 | $new_tree->{xml}{post} = _generate_cache_hash(@posts); | ||
| 297 | $self->{tree} = $new_tree; | ||
| 298 | |||
| 299 | try | ||
| 300 | { | ||
| 301 | $self->tpp->writefile($cfg->cache_data, $new_tree) | ||
| 302 | } | ||
| 303 | catch | ||
| 304 | { | ||
| 305 | Miril::Exception->throw( | ||
| 306 | message => "Could noe update cache file", | ||
| 307 | erorvar => $_, | ||
| 308 | ); | ||
| 309 | }; | ||
| 310 | |||
| 311 | # update the data file | ||
| 312 | my $content; | ||
| 313 | |||
| 314 | $post = first { $_->id eq $post->id } @posts; | ||
| 315 | |||
| 316 | $content .= "Title: " . $post->title . "\n"; | ||
| 317 | $content .= "Author: " . $post->author . "\n" if $post->author; | ||
| 318 | $content .= "Type: " . $post->type->id . "\n"; | ||
| 319 | $content .= "Published: " . $post->published->iso . "\n" if $post->published; | ||
| 320 | $content .= "Topics: " . join(" ", map { $_->id } list $post->topics) . "\n\n"; | ||
| 321 | $content .= $post->source; | ||
| 322 | |||
| 323 | try | ||
| 324 | { | ||
| 325 | my $fh = IO::File->new( catfile($cfg->data_path, $post->id), "w") or die $!; | ||
| 326 | $fh->print($content); | ||
| 327 | $fh->close; | ||
| 328 | } | ||
| 329 | catch | ||
| 330 | { | ||
| 331 | Miril::Exception->throw( | ||
| 332 | message => "Could not update data file", | ||
| 333 | erorvar => $_, | ||
| 334 | ); | ||
| 335 | }; | ||
| 336 | } | ||
| 337 | |||
| 338 | sub delete | ||
| 339 | { | ||
| 340 | my ($self, $id) = @_; | ||
| 341 | |||
| 342 | try | ||
| 343 | { | ||
| 344 | unlink catfile($self->miril->cfg->data_path, $id); | ||
| 345 | } | ||
| 346 | catch | ||
| 347 | { | ||
| 348 | Miril::Exception->throw( | ||
| 349 | message => "Could not delete data file", | ||
| 350 | erorvar => $_, | ||
| 351 | ); | ||
| 352 | }; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub get_latest | ||
| 356 | { | ||
| 357 | my ($self) = @_; | ||
| 358 | |||
| 359 | my $cfg = $self->miril->cfg; | ||
| 360 | |||
| 361 | my $tpp = XML::TreePP->new(); | ||
| 362 | $tpp->set( force_array => ['post'] ); | ||
| 363 | my $tree; | ||
| 364 | my @posts; | ||
| 365 | |||
| 366 | return [] unless -e $cfg->latest_data; | ||
| 367 | |||
| 368 | try | ||
| 369 | { | ||
| 370 | $tree = $tpp->parsefile( $cfg->latest_data ); | ||
| 371 | @posts = dao list $tree->{xml}{post}; | ||
| 372 | } | ||
| 373 | catch | ||
| 374 | { | ||
| 375 | Miril::Exception->throw( | ||
| 376 | message => "Could not get list of latest files", | ||
| 377 | errorvar => $_, | ||
| 378 | ); | ||
| 379 | }; | ||
| 380 | |||
| 381 | |||
| 382 | return \@posts; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub add_to_latest | ||
| 386 | { | ||
| 387 | my ($self, $id, $title) = @_; | ||
| 388 | |||
| 389 | my $cfg = $self->miril->cfg; | ||
| 390 | my $tpp = XML::TreePP->new(); | ||
| 391 | $tpp->set( force_array => ['post'] ); | ||
| 392 | my $tree; | ||
| 393 | my @posts; | ||
| 394 | |||
| 395 | if ( -e $cfg->latest_data ) { | ||
| 396 | try | ||
| 397 | { | ||
| 398 | $tree = $tpp->parsefile( $cfg->latest_data ); | ||
| 399 | @posts = list $tree->{xml}{post}; | ||
| 400 | } | ||
| 401 | catch | ||
| 402 | { | ||
| 403 | Miril::Exception->throw( | ||
| 404 | message => "Could not add to list of latest files", | ||
| 405 | errorvar => $), | ||
| 406 | ); | ||
| 407 | }; | ||
| 408 | } | ||
| 409 | |||
| 410 | @posts = grep { $_->{id} ne $id } @posts; | ||
| 411 | unshift @posts, { id => $id, title => $title }; | ||
| 412 | @posts = @posts[0 .. 9] if @posts > 10; | ||
| 413 | |||
| 414 | $tree->{xml}{post} = \@posts; | ||
| 415 | |||
| 416 | try | ||
| 417 | { | ||
| 418 | $tpp->writefile( $cfg->latest_data, $tree ); | ||
| 419 | } | ||
| 420 | catch | ||
| 421 | { | ||
| 422 | Miril::Exception->throw( | ||
| 423 | message => "Could not write list of latest files", | ||
| 424 | errorvar => $), | ||
| 425 | ); | ||
| 426 | }; | ||
| 427 | } | ||
| 428 | |||
| 429 | ### PRIVATE FUNCTIONS ### | ||
| 430 | |||
| 431 | sub _parse_meta | ||
| 432 | { | ||
| 433 | my ($meta) = @_; | ||
| 434 | |||
| 435 | my @lines = split /\n/, $meta; | ||
| 436 | my %meta; | ||
| 437 | |||
| 438 | foreach my $line (@lines) { | ||
| 439 | if ($line =~ /^(Published|Title|Type|Author|Status):\s+(.+)/) { | ||
| 440 | my $name = lc $1; | ||
| 441 | my $value = $2; | ||
| 442 | $value =~ s/\s+$//; | ||
| 443 | $meta{$name} = $value; | ||
| 444 | } elsif ($line =~ /Topics:\s+(.+)/) { | ||
| 445 | my $value = lc $1; | ||
| 446 | $value =~ s/\s+$//; | ||
| 447 | my @values = split /\s+/, $value; | ||
| 448 | $meta{topics} = \@values; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | |||
| 452 | $meta{topics} = [] unless defined $meta{topics}; | ||
| 453 | |||
| 454 | return %meta; | ||
| 455 | } | ||
| 456 | |||
| 457 | sub _generate_cache_hash | ||
| 458 | { | ||
| 459 | my (@posts) = @_; | ||
| 460 | |||
| 461 | my @cache_posts = map {{ | ||
| 462 | id => $_->id, | ||
| 463 | title => $_->title, | ||
| 464 | modified => $_->modified ? $_->modified->epoch : Miril::DateTime->new(time)->epoch, | ||
| 465 | published => $_->published ? $_->published->epoch : undef, | ||
| 466 | type => $_->type->id, | ||
| 467 | author => $_->author, | ||
| 468 | topics => { topic => [ map {$_->id} list $_->topics ] }, | ||
| 469 | }} @posts; | ||
| 470 | |||
| 471 | return \@cache_posts; | ||
| 472 | } | ||
| 473 | |||
| 474 | 1; |