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|topic_id)/, $_[0]->SUPER::fields
(); }
99 sub vfields
{ return ($_[0]->SUPER::vfields
(), 'content', 'topic_id'); }
104 my $filename = $self->_filename();
106 # destroy the topic index, to be rewritten
107 # in the future by _topic_index()
108 $filename =~ s!/[^/]+$!/.INDEX!;
116 $self->SUPER::save
( $driver );
118 my $filename = $self->_filename();
119 $filename =~ s/\.META$//;
121 open F
, '>' . $filename or croak
"Can't write " . $filename . ': ' . $!;
123 print F
$self->get('content') || '';
126 $self->_destroy_index();
134 my $hits = $self->get('hits') + 1;
136 $self->set('hits', $hits);
138 # call $self->SUPER::save() instead of $self->save()
139 # to avoid saving content (unnecessary) and deleting
140 # the topic INDEX (even probably dangerous)
141 $self->SUPER::save
();
143 $self->source->_update_top_ten($hits, $self->get('topic_id'),
153 my $filename = $self->_filename();
154 $filename =~ s/\.META$/.TAGS/;
157 if (open F
, '>' . $filename) {
158 print F
join(', ', map { lc($_) } @_), "\n";
163 if (open F
, $filename) {
168 @ret = split(/\s*,\s*/, $l);
179 my $file = $self->_filename();
181 $self->SUPER::delete($driver);
183 # also delete content and TAGS
184 $file =~ s/\.META$//;
187 unlink $file . '.TAGS';
189 $self->_destroy_index();
195 package Gruta
::Data
::FS
::Topic
;
197 use base
'Gruta::Data::Topic';
198 use base
'Gruta::Data::FS::BASE';
200 sub base
{ return '/topics/'; }
206 $self->SUPER::save
( $driver );
208 my $filename = $self->_filename();
209 $filename =~ s/\.META$//;
216 package Gruta
::Data
::FS
::User
;
218 use base
'Gruta::Data::User';
219 use base
'Gruta::Data::FS::BASE';
221 sub ext
{ return ''; }
222 sub base
{ return '/users/'; }
224 package Gruta
::Data
::FS
::Session
;
226 use base
'Gruta::Data::Session';
227 use base
'Gruta::Data::FS::BASE';
229 sub ext
{ return ''; }
230 sub base
{ return '/sids/'; }
232 package Gruta
::Source
::FS
;
239 $self->{path
} or croak
"Invalid path";
249 my $o = ${class}->new( id
=> $id );
253 sub topic
{ return _one
( @_, 'Gruta::Data::FS::Topic' ); }
260 my $path = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
();
262 if (opendir D
, $path) {
263 while (my $id = readdir D
) {
264 next unless -d
$path . $id;
265 next if $id =~ /^\./;
276 sub user
{ return _one
( @_, 'Gruta::Data::FS::User' ); }
283 my $path = $self->{path
} . Gruta
::Data
::FS
::User
::base
();
285 if (opendir D
, $path) {
286 while (my $id = readdir D
) {
287 next if -d
$path . $id;
299 my $topic_id = shift;
302 my $story = Gruta
::Data
::FS
::Story
->new( topic_id
=> $topic_id, id
=> $id );
303 if (not $story->load( $self )) {
305 $story = Gruta
::Data
::FS
::Story
->new( topic_id
=> $topic_id . '-arch',
308 if (not $story->load( $self )) {
313 # now load the content
314 my $file = $story->_filename();
315 $file =~ s/\.META$//;
317 open F
, $file or croak
"Can't open $file content: $!";
319 $story->set('content', join('', <F
>));
327 my $topic_id = shift;
331 my $path = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
() . $topic_id;
333 if (opendir D
, $path) {
334 while (my $id = readdir D
) {
335 if ($id =~ s/\.META$//) {
349 my $topic_id = shift;
351 my $index = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
() .
352 $topic_id . '/.INDEX';
354 if (not open I
, $index) {
357 foreach my $id ($self->stories($topic_id)) {
358 my $story = $self->story($topic_id, $id);
360 push(@i, $story->get('date') . ':' . $id);
363 open I
, '>' . $index or croak
"Can't create INDEX for $topic_id: $!";
366 foreach my $l (reverse(sort(@i))) {
377 sub _update_top_ten
{
380 my $topic_id = shift;
383 my $index = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
() . '/.top_ten';
388 if (open F
, $index) {
390 while (my $l = <F
>) {
393 my ($h, $t, $i) = split(':', $l);
395 if ($u == 0 && $h < $hits) {
397 push(@l, "$hits:$topic_id:$id");
400 if ($t ne $topic_id or $i ne $id) {
408 if ($u == 0 && scalar(@l) < 100) {
410 push(@l, "$hits:$topic_id:$id");
414 if (open F
, '>' . $index) {
434 sub stories_by_date
{
436 my $topic_id = shift;
440 $args{offset
} = 0 if $args{offset
} < 0;
442 open I
, $self->_topic_index($topic_id);
451 my ($date, $id) = (/^(\d*):(.*)$/);
453 # skip future stories
454 next if not $args{future
} and
456 $date > $args{today
};
458 # skip if date is above the threshold
459 next if $args{'to'} and $date > $args{'to'};
461 # exit if date is below the threshold
462 last if $args{'from'} and $date < $args{'from'};
464 # skip offset stories
465 next if $args{'offset'} and ++$o <= $args{'offset'};
469 # exit if we have all we need
470 last if $args{'num'} and $args{'num'} == scalar(@r);
480 my $topic_id = shift;
483 my @q = split(/\s+/,$query);
487 foreach my $id ($self->stories_by_date( $topic_id )) {
489 my $story = $self->story($topic_id, $id);
490 my $content = $story->get('content');
493 # try complete query first
494 if($content =~ /\b$query\b/i) {
500 if(length($q) > 1 and $content =~ /\b$q\b/i) {
507 push(@r, $id) if $found;
513 sub stories_top_ten
{
519 my $index = $self->{path
} . Gruta
::Data
::FS
::Topic
::base
() . '/.top_ten';
521 if (open F
, $index) {
524 while (defined(my $l = <F
>) and $num--) {
526 push(@r, [ split(':', $l) ]);
536 sub search_stories_by_tag
{
542 foreach my $topic_id ($self->topics()) {
544 my $topic = $self->topic($topic_id);
546 my $files = $topic->_filename();
547 $files =~ s/\.META$/\/*.TAGS
/;
549 my @ls = glob($files);
551 foreach my $f (@ls) {
557 foreach my $t (split(/,\s+/, $tags)) {
558 if (grep(/$t/, @tags)) {
559 my ($id) = ($f =~ m{/([^/]+)\.TAGS});
561 push(@ret, [ $topic_id, $id ]);
580 sub session
{ return _one
( @_, 'Gruta::Data::FS::Session' ); }
582 sub purge_old_sessions
{
585 my $path = $self->{path
} . Gruta
::Data
::FS
::Session
::base
();
587 if (opendir D
, $path) {
588 while(my $s = readdir D
) {
616 sub insert_topic
{ $_[0]->_insert($_[1], 'Gruta::Data::FS::Topic'); }
617 sub insert_user
{ $_[0]->_insert($_[1], 'Gruta::Data::FS::User'); }
623 if (not $story->get('id')) {
624 # alloc an id for the story
627 while ($self->story($story->get('topic_id'), $id)) {
631 $story->set('id', $id);
634 $self->_insert($story, 'Gruta::Data::FS::Story');
638 sub insert_session
{ $_[0]->_insert($_[1], 'Gruta::Data::FS::Session'); }
644 mkdir $self->{path
}, 0755;
645 mkdir $self->{path
} . Gruta
::Data
::FS
::Topic
::base
(), 0755;
646 mkdir $self->{path
} . Gruta
::Data
::FS
::User
::base
(), 0755;
647 mkdir $self->{path
} . Gruta
::Data
::FS
::Session
::base
(), 0755;
654 my $s = bless( { @_ }, $class);