1 package Gruta
::Source
::FS
;
8 package Gruta
::Data
::FS
::BASE
;
12 sub ext
{ return '.META'; }
18 $self->source->_assert();
20 return $self->source->{path
} . $self->base() .
21 $self->get('id') . $self->ext();
29 $self->source( $driver );
31 if (not open F
, $self->_filename()) {
38 if(/^([^:]*): (.*)$/) {
39 my ($key, $value) = ($1, $2);
43 if (grep (/^$key$/, $self->fields())) {
44 $self->set($key, $value);
58 $self->source( $driver ) if $driver;
60 my $filename = $self->_filename();
62 open F
, '>' . $filename or croak
"Can't write " . $filename . ': ' . $!;
64 foreach my $k ($self->fields()) {
69 print F
$f . ': ' . ($self->get($k) || '') . "\n";
82 $self->source( $driver ) if $driver;
84 unlink $self->_filename();
89 package Gruta
::Data
::FS
::Story
;
91 use base
'Gruta::Data::Story';
92 use base
'Gruta::Data::FS::BASE';
96 sub base
{ return Gruta
::Data
::FS
::Topic
::base
() . $_[0]->get('topic_id') . '/'; }
98 sub fields
{ grep !/content/, $_[0]->SUPER::fields
(); }
99 sub vfields
{ return ($_[0]->SUPER::vfields
(), 'content'); }
105 # save current topic id
106 # (as it may be stored in the .META file and
107 # be false, v.g. if archived)
108 my $topic_id = $self->get('topic_id');
110 $self->SUPER::load
( $driver );
113 $self->set('topic_id', $topic_id);
122 my $filename = $self->_filename();
124 # destroy the topic index, to be rewritten
125 # in the future by _topic_index()
126 $filename =~ s!/[^/]+$!/.INDEX!;
134 $self->SUPER::save
( $driver );
136 my $filename = $self->_filename();
137 $filename =~ s/\.META$//;
139 open F
, '>' . $filename or croak
"Can't write " . $filename . ': ' . $!;
141 print F
$self->get('content') || '';
144 $self->_destroy_index();
152 my $hits = $self->get('hits') + 1;
154 $self->set('hits', $hits);
156 # call $self->SUPER::save() instead of $self->save()
157 # to avoid saving content (unnecessary) and deleting
158 # the topic INDEX (even probably dangerous)
159 $self->SUPER::save
();
161 $self->source->_update_top_ten($hits, $self->get('topic_id'),
171 my $filename = $self->_filename();
172 $filename =~ s/\.META$/.TAGS/;
175 if (open F
, '>' . $filename) {
176 print F
join(', ', map { lc($_) } @_), "\n";
181 if (open F
, $filename) {
186 @ret = split(/,\s+/, $l);
197 my $file = $self->_filename();
199 $self->SUPER::delete($driver);
201 # also delete content and TAGS
202 $file =~ s/\.META$//;
205 unlink $file . '.TAGS';
211 package Gruta
::Data
::FS
::Topic
;
213 use base
'Gruta::Data::Topic';
214 use base
'Gruta::Data::FS::BASE';
216 sub base
{ return '/topics/'; }
222 $self->SUPER::save
( $driver );
224 my $filename = $self->_filename();
225 $filename =~ s/\.META$//;
232 package Gruta
::Data
::FS
::User
;
234 use base
'Gruta::Data::User';
235 use base
'Gruta::Data::FS::BASE';
237 sub ext
{ return ''; }
238 sub base
{ return '/users/'; }
240 package Gruta
::Data
::FS
::Session
;
242 use base
'Gruta::Data::Session';
243 use base
'Gruta::Data::FS::BASE';
245 sub ext
{ return ''; }
246 sub base
{ return '/sids/'; }
248 package Gruta
::Source
::FS
;
255 $self->{path
} or croak
"Invalid path";
265 my $o = ${class}->new( id
=> $id );
269 sub topic
{ return _one
( @_, 'Gruta::Data::FS::Topic' ); }
276 my $path = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
();
278 if (opendir D
, $path) {
279 while (my $id = readdir D
) {
280 next unless -d
$path . $id;
281 next if $id =~ /^\./;
292 sub user
{ return _one
( @_, 'Gruta::Data::FS::User' ); }
299 my $path = $self->{path
} . Gruta
::Data
::FS
::User
::base
();
301 if (opendir D
, $path) {
302 while (my $id = readdir D
) {
303 next if -d
$path . $id;
315 my $topic_id = shift;
318 my $story = Gruta
::Data
::FS
::Story
->new( topic_id
=> $topic_id, id
=> $id );
319 if (not $story->load( $self )) {
321 $story = Gruta
::Data
::FS
::Story
->new( topic_id
=> $topic_id . '-arch',
324 if (not $story->load( $self )) {
329 # now load the content
330 my $file = $story->_filename();
331 $file =~ s/\.META$//;
333 open F
, $file or croak
"Can't open $file content: $!";
335 $story->set('content', join('', <F
>));
343 my $topic_id = shift;
347 my $path = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
() . $topic_id;
349 if (opendir D
, $path) {
350 while (my $id = readdir D
) {
351 if ($id =~ s/\.META$//) {
365 my $topic_id = shift;
367 my $index = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
() .
368 $topic_id . '/.INDEX';
370 if (not open I
, $index) {
373 foreach my $id ($self->stories($topic_id)) {
374 my $story = $self->story($topic_id, $id);
376 push(@i, $story->get('date') . ':' . $id);
379 open I
, '>' . $index or croak
"Can't create INDEX for $topic_id: $!";
382 foreach my $l (reverse(sort(@i))) {
393 sub _update_top_ten
{
396 my $topic_id = shift;
399 my $index = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
() . '/.top_ten';
404 if (open F
, $index) {
406 while (my $l = <F
>) {
409 my ($h, $t, $i) = split(':', $l);
411 if ($u == 0 && $h < $hits) {
413 push(@l, "$hits:$topic_id:$id");
416 if ($t ne $topic_id or $i ne $id) {
424 if ($u == 0 && scalar(@l) < 100) {
426 push(@l, "$hits:$topic_id:$id");
430 if (open F
, '>' . $index) {
450 sub stories_by_date
{
452 my $topic_id = shift;
456 $args{offset
} = 0 if $args{offset
} < 0;
458 open I
, $self->_topic_index($topic_id);
467 my ($date, $id) = (/^(\d*):(.*)$/);
469 # skip future stories
470 next if not $args{future
} and
472 $date > $args{today
};
474 # skip if date is above the threshold
475 next if $args{'to'} and $date > $args{'to'};
477 # exit if date is below the threshold
478 last if $args{'from'} and $date < $args{'from'};
480 # skip offset stories
481 next if $args{'offset'} and ++$o <= $args{'offset'};
485 # exit if we have all we need
486 last if $args{'num'} and $args{'num'} == scalar(@r);
496 my $topic_id = shift;
499 my @q = split(/\s+/,$query);
503 foreach my $id ($self->stories_by_date( $topic_id )) {
505 my $story = $self->story($topic_id, $id);
506 my $content = $story->get('content');
509 # try complete query first
510 if($content =~ /\b$query\b/i) {
516 if(length($q) > 1 and $content =~ /\b$q\b/i) {
523 push(@r, $id) if $found;
529 sub stories_top_ten
{
535 my $index = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
() . '/.top_ten';
537 if (open F
, $index) {
540 while (defined(my $l = <F
>) and $num--) {
542 push(@r, [ split(':', $l) ]);
552 sub search_stories_by_tag
{
558 foreach my $topic_id ($self->topics()) {
560 my $topic = $self->topic($topic_id);
562 my $files = $topic->_filename();
563 $files =~ s/\.META$/\/*.TAGS
/;
565 my @ls = glob($files);
567 foreach my $f (@ls) {
573 foreach my $t (split(/,\s+/, $tags)) {
574 if (grep(/$t/, @tags)) {
575 my ($id) = ($f =~ m{/([^/]+)\.TAGS});
577 push(@ret, [ $topic_id, $id ]);
596 sub session
{ return _one
( @_, 'Gruta::Data::FS::Session' ); }
598 sub purge_old_sessions
{
601 my $path = $self->{path
} . Gruta
::Data
::FS
::Session
::base
();
603 if (opendir D
, $path) {
604 while(my $s = readdir D
) {
632 sub insert_topic
{ $_[0]->_insert($_[1], 'Gruta::Data::FS::Topic'); }
633 sub insert_user
{ $_[0]->_insert($_[1], 'Gruta::Data::FS::User'); }
639 if (not $story->get('id')) {
640 # alloc an id for the story
643 while ($self->story($story->get('topic_id'), $id)) {
647 $story->set('id', $id);
650 $self->_insert($story, 'Gruta::Data::FS::Story');
654 sub insert_session
{ $_[0]->_insert($_[1], 'Gruta::Data::FS::Session'); }
660 mkdir $self->{path
}, 0755;
661 mkdir $self->{path
} . Gruta
::Data
::FS
::Topic
::base
(), 0755;
662 mkdir $self->{path
} . Gruta
::Data
::FS
::User
::base
(), 0755;
663 mkdir $self->{path
} . Gruta
::Data
::FS
::Session
::base
(), 0755;
670 my $s = bless( { @_ }, $class);