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";
224 'search {src} {topic_id} {query}',
225 'Searches stories by content',
228 my $topic_id = arg
();
231 foreach my $s ($g->search_stories($topic_id, $query)) {
237 'top_ten {src} [{num}]',
238 'Shows the top N stories',
241 my $num = arg_o
() || 10;
243 foreach my $s ($g->stories_top_ten($num)) {
244 print join(' ', @
{$s}), "\n";
254 if (not $c = $X->{$cmd}) {
277 return shift(@ARGV) || shift;
282 my $src = new_source
(arg
());
283 my $g = Gruta
->new( sources
=> $src );
294 print "Usage: gruta ", $c->[0], "\n\n\t", $c->[1], "\n";
297 print "Usage: gruta {cmd} [args...]\n\n";
299 foreach my $k (sort keys %{$X}) {
302 print ' ', sprintf('%-50s %s',
303 $c->[0], $c->[1]), "\n";
315 if ($src_str =~ /^dbi:/) {
316 $src = Gruta
::Source
::DBI
->new( string
=> $src_str );
318 elsif ($src_str =~ /^mbox:(.+)/) {
320 my ($topic_id) = ($file =~ /^(\w+)\.?.*$/);
322 $src = Gruta
::Source
::Mbox
->new(
324 topic_id
=> $topic_id,
325 topic_name
=> $topic_id,
326 index_file
=> '/tmp/' . $topic_id . '.idx'
330 $src = Gruta
::Source
::FS
->new( path
=> $src_str );
339 my $topic_id = shift;
343 my $story = $g->story($topic_id, $id);
345 foreach my $f ($story->fields()) {
346 if ($f ne 'content') {
347 push (@r, $f . ': ' . ($story->get($f) || ''));
351 push(@r, 'tags: ' . join(', ', $story->tags()));
353 push(@r, $story->get('content'));
356 return join("\n", @r);
363 my $topic_id = shift;
366 my $topic = $g->topic($topic_id);
368 foreach my $f ($topic->afields()) {
369 push(@r, $f . ': ' . ($topic->get($f) || ''));
372 return join("\n", @r);
379 my $topic_id = shift;
383 open F
, $fn or die "Can't open $fn";
388 $story = $g->story($topic_id, $id);
391 $story = Gruta
::Data
::Story
->new (
392 topic_id
=> $topic_id,
402 my ($key, $value) = (/^(\w+):\s*(.*)$/);
404 if ($key eq 'tags') {
405 $story->tags(split(/,\s*/, $value));
408 $story->set($key, $value);
412 my $c = join('', <F
>);
415 $story->set('content', $c);
417 if ($story->source()) {
421 $g->insert_story($story);
429 my $topic_id = shift;
432 open F
, $fn or die "Can't open $fn";
437 $topic = $g->topic($topic_id);
440 $topic = Gruta
::Data
::Topic
->new (
441 topic_id
=> $topic_id,
450 my ($key, $value) = (/^(\w+):\s*(.*)$/);
452 $topic->set($key, $value);
455 if ($topic->source()) {
459 $g->insert_topic($topic);