9 use Gruta
::Source
::DBI
;
10 use Gruta
::Source
::FS
;
11 use Gruta
::Source
::Mbox
;
16 'Copies the full source {src} to {dst}',
20 my $dst = new_source
( $new_src );
24 $g->transfer_to_source( $dst );
29 'Lists the topics in {src}',
33 foreach my $t ($g->topics()) {
39 'topic {src} {topic_id}',
45 print get_topic
($g, $topic_id);
49 'edit_topic {src} {topic_id}',
55 my $fh = File
::Temp
->new();
56 print $fh get_topic
($g, $topic_id);
57 my $fn = $fh->filename();
60 my $mtime = (stat($fn))[9];
61 system('$EDITOR ' . $fn);
63 if ($mtime != (stat($fn))[9]) {
64 save_topic
($g, $topic_id, $fn);
70 'stories {src} {topic_id}',
71 'Lists all stories of a topic',
76 foreach my $s ($g->stories($topic_id)) {
82 'story {src} {topic_id} {id}',
89 print get_story
($g, $topic_id, $id);
93 'edit_story {src} {topic_id} {id}',
94 'Calls $EDITOR to edit story data',
100 my $fh = File
::Temp
->new();
101 print $fh get_story
($g, $topic_id, $id);
102 my $fn = $fh->filename();
105 my $mtime = (stat($fn))[9];
106 system('$EDITOR ' . $fn);
108 if ($mtime != (stat($fn))[9]) {
109 save_story
($g, $topic_id, $id, $fn);
114 'filter_story {src} {topic_id} {id} {command}',
115 'Filters story data through command (STDIN, STDOUT)',
118 my $topic_id = arg
();
120 my $filter_cmd = arg
();
122 my $fhr = File
::Temp
->new();
123 print $fhr get_story
($g, $topic_id, $id);
124 my $fnr = $fhr->filename();
127 my $fhw = File
::Temp
->new();
128 my $fnw = $fhw->filename();
131 system("$filter_cmd < $fnr > $fnw");
133 save_story
($g, $topic_id, $id, $fnw);
145 'Lists all tags in {src}',
149 foreach my $t ($g->tags()) {
150 print join(' ', @
{$t}), "\n";
154 'stories_by_date' => [
155 'stories_by_date {src} {topic(s)} {num} {offset} [{from}] [{to}] [{future}]',
156 'Searches stories by date',
164 my $future = arg_o
();
167 $topics = [ split(':', $topics) ];
170 foreach my $s ($g->stories_by_date(
178 print join(' ', @
{$s}), "\n";
184 'Dumps statistics for {src}',
191 foreach my $t ($g->topics()) {
194 foreach my $s ($g->stories($t)) {
197 my $story = $g->story($t, $s);
199 $n_hits += $story->get('hits') || 0;
203 print "Topics: $n_topics, Stories: $n_stories, Hits: $n_hits\n";
206 'stories_by_tag' => [
207 'stories_by_tag {src} {topic(s)} {tag(s)}',
208 'Searches stories by tag(s)',
215 $topics = [ split(':', $topics) ];
218 foreach my $s ($g->stories_by_tag($topics, $tags)) {
219 print join(' ', @
{$s}), "\n";
229 if (not $c = $X->{$cmd}) {
252 return shift(@ARGV) || shift;
257 my $src = new_source
(arg
());
258 my $g = Gruta
->new( sources
=> $src );
269 print "Usage: gruta ", $c->[0], "\n\n\t", $c->[1], "\n";
272 print "Usage: gruta {cmd} [args...]\n\n";
274 foreach my $k (sort keys %{$X}) {
277 print ' ', sprintf('%-50s %s',
278 $c->[0], $c->[1]), "\n";
290 if ($src_str =~ /^dbi:/) {
291 $src = Gruta
::Source
::DBI
->new( string
=> $src_str );
293 elsif ($src_str =~ /^mbox:(.+)/) {
295 my ($topic_id) = ($file =~ /^(\w+)\.?.*$/);
297 $src = Gruta
::Source
::Mbox
->new(
299 topic_id
=> $topic_id,
300 topic_name
=> $topic_id,
301 index_file
=> '/tmp/' . $topic_id . '.idx'
305 $src = Gruta
::Source
::FS
->new( path
=> $src_str );
314 my $topic_id = shift;
318 my $story = $g->story($topic_id, $id);
320 foreach my $f ($story->fields()) {
321 if ($f ne 'content') {
322 push (@r, $f . ': ' . ($story->get($f) || ''));
326 push(@r, 'tags: ' . join(', ', $story->tags()));
328 push(@r, $story->get('content'));
331 return join("\n", @r);
338 my $topic_id = shift;
341 my $topic = $g->topic($topic_id);
343 foreach my $f ($topic->afields()) {
344 push(@r, $f . ': ' . ($topic->get($f) || ''));
347 return join("\n", @r);
354 my $topic_id = shift;
358 open F
, $fn or die "Can't open $fn";
363 $story = $g->story($topic_id, $id);
366 $story = Gruta
::Data
::Story
->new (
367 topic_id
=> $topic_id,
377 my ($key, $value) = (/^(\w+):\s*(.*)$/);
379 if ($key eq 'tags') {
380 $story->tags(split(/,\s*/, $value));
383 $story->set($key, $value);
387 my $c = join('', <F
>);
390 $story->set('content', $c);
392 if ($story->source()) {
396 $g->insert_story($story);
404 my $topic_id = shift;
407 open F
, $fn or die "Can't open $fn";
412 $topic = $g->topic($topic_id);
415 $topic = Gruta
::Data
::Topic
->new (
416 topic_id
=> $topic_id,
425 my ($key, $value) = (/^(\w+):\s*(.*)$/);
427 $topic->set($key, $value);
430 if ($topic->source()) {
434 $g->insert_topic($topic);