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 'new_story {src} {topic_id} {id} {date} {date2} {format} {tags}',
115 'Creates a new story from STDIN',
118 my $topic_id = arg
();
125 my $content = join('', <>);
127 my $story = Gruta
::Data
::Story
->new(
128 topic_id
=> $topic_id,
132 $story->set('date', $date || Gruta
::Data
::today
());
133 $story->set('date2', $date2);
134 $story->set('format', $format || 'grutatxt');
135 $story->set('ctime', time());
136 $story->set('userid', $ENV{USER
});
137 $story->set('content', $content);
139 $story = $g->insert_story($story);
142 $story->tags(split(/\s*,\s*/, $tags));
147 'filter_story {src} {topic_id} {id} {command}',
148 'Filters story data through command (STDIN, STDOUT)',
151 my $topic_id = arg
();
153 my $filter_cmd = arg
();
155 my $fhr = File
::Temp
->new();
156 print $fhr get_story
($g, $topic_id, $id);
157 my $fnr = $fhr->filename();
160 my $fhw = File
::Temp
->new();
161 my $fnw = $fhw->filename();
164 system("$filter_cmd < $fnr > $fnw");
166 save_story
($g, $topic_id, $id, $fnw);
178 'Lists all tags in {src}',
182 foreach my $t ($g->tags()) {
183 print join(' ', @
{$t}), "\n";
187 'stories_by_date' => [
188 'stories_by_date {src} {topic(s)} {num} {offset} [{from}] [{to}] [{future}]',
189 'Searches stories by date',
197 my $future = arg_o
();
200 $topics = [ split(':', $topics) ];
203 foreach my $s ($g->stories_by_date(
211 print join(' ', @
{$s}), "\n";
217 'Dumps statistics for {src}',
224 foreach my $t ($g->topics()) {
227 foreach my $s ($g->stories($t)) {
230 my $story = $g->story($t, $s);
232 $n_hits += $story->get('hits') || 0;
236 print "Topics: $n_topics, Stories: $n_stories, Hits: $n_hits\n";
239 'stories_by_tag' => [
240 'stories_by_tag {src} {topic(s)} {tag(s)}',
241 'Searches stories by tag(s)',
248 $topics = [ split(':', $topics) ];
251 foreach my $s ($g->stories_by_tag($topics, $tags)) {
252 print join(' ', @
{$s}), "\n";
257 'search {src} {topic_id} {query}',
258 'Searches stories by content',
261 my $topic_id = arg
();
264 foreach my $s ($g->search_stories($topic_id, $query)) {
270 'top_ten {src} [{num}]',
271 'Shows the top N stories',
274 my $num = arg_o
() || 10;
276 foreach my $s ($g->stories_top_ten($num)) {
277 print join(' ', @
{$s}), "\n";
281 'users_by_xdate' => [
282 'users_by_xdate {src} [{max_date}]',
283 'Lists users by expiration date',
286 my $max_date = arg_o
() || '99999999999999';
288 foreach my $id ($g->users()) {
289 my $u = $g->user($id);
291 my $xdate = $u->get('xdate');
294 $xdate gt Gruta
::Data
::today
() &&
295 $xdate lt $max_date) {
296 print $id, ' ', $u->{email
}, ' ', $xdate, "\n";
307 if (not $c = $X->{$cmd}) {
330 return shift(@ARGV) || shift;
335 my $src = new_source
(arg
());
336 my $g = Gruta
->new( sources
=> $src );
347 print "Usage: gruta ", $c->[0], "\n\n\t", $c->[1], "\n";
350 print "Usage: gruta {cmd} [args...]\n\n";
352 foreach my $k (sort keys %{$X}) {
355 print ' ', sprintf('%-50s %s',
356 $c->[0], $c->[1]), "\n";
368 if ($src_str =~ /^dbi:/) {
369 $src = Gruta
::Source
::DBI
->new( string
=> $src_str );
371 elsif ($src_str =~ /^mbox:(.+)/) {
374 $src = Gruta
::Source
::Mbox
->new(
379 $src = Gruta
::Source
::FS
->new( path
=> $src_str );
388 my $topic_id = shift;
392 my $story = $g->story($topic_id, $id);
394 foreach my $f ($story->fields()) {
395 if ($f ne 'content') {
396 push (@r, $f . ': ' . ($story->get($f) || ''));
400 push(@r, 'tags: ' . join(', ', $story->tags()));
402 push(@r, $story->get('content'));
405 return join("\n", @r);
412 my $topic_id = shift;
415 my $topic = $g->topic($topic_id);
417 foreach my $f ($topic->afields()) {
418 push(@r, $f . ': ' . ($topic->get($f) || ''));
421 return join("\n", @r);
428 my $topic_id = shift;
432 open F
, $fn or die "Can't open $fn";
437 $story = $g->story($topic_id, $id);
440 $story = Gruta
::Data
::Story
->new (
441 topic_id
=> $topic_id,
451 my ($key, $value) = (/^(\w+):\s*(.*)$/);
453 if ($key eq 'tags') {
454 $story->tags(split(/,\s*/, $value));
457 $story->set($key, $value);
461 my $c = join('', <F
>);
464 $story->set('content', $c);
466 if ($story->source()) {
470 $g->insert_story($story);
478 my $topic_id = shift;
481 open F
, $fn or die "Can't open $fn";
486 $topic = $g->topic($topic_id);
489 $topic = Gruta
::Data
::Topic
->new (
490 topic_id
=> $topic_id,
499 my ($key, $value) = (/^(\w+):\s*(.*)$/);
501 $topic->set($key, $value);
504 if ($topic->source()) {
508 $g->insert_topic($topic);