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}]',
115 'Creates a new story from STDIN',
118 my $topic_id = arg
();
121 my $fh = File
::Temp
->new();
122 print $fh join('', <>);
123 my $fn = $fh->filename();
126 save_story
($g, $topic_id, $id, $fn, 1);
130 'update_story {src} {topic_id} {id}',
131 'Updates a story from STDIN',
134 my $topic_id = arg
();
137 my $fh = File
::Temp
->new();
138 print $fh join('', <>);
139 my $fn = $fh->filename();
142 save_story
($g, $topic_id, $id, $fn);
146 'filter_story {src} {topic_id} {id} {command}',
147 'Filters story data through command (STDIN, STDOUT)',
150 my $topic_id = arg
();
152 my $filter_cmd = arg
();
154 my $fhr = File
::Temp
->new();
155 print $fhr get_story
($g, $topic_id, $id);
156 my $fnr = $fhr->filename();
159 my $fhw = File
::Temp
->new();
160 my $fnw = $fhw->filename();
163 system("$filter_cmd < $fnr > $fnw");
165 save_story
($g, $topic_id, $id, $fnw);
177 'Lists all tags in {src}',
181 foreach my $t ($g->tags()) {
182 print join(' ', @
{$t}), "\n";
186 'stories_by_date' => [
187 'stories_by_date {src} {topic(s)} {num} {offset} [{from}] [{to}] [{future}]',
188 'Searches stories by date',
196 my $future = arg_o
();
199 $topics = [ split(':', $topics) ];
202 foreach my $s ($g->stories_by_date(
210 print join(' ', @
{$s}), "\n";
216 'Dumps statistics for {src}',
223 foreach my $t ($g->topics()) {
226 foreach my $s ($g->stories($t)) {
229 my $story = $g->story($t, $s);
231 $n_hits += $story->get('hits') || 0;
235 print "Topics: $n_topics, Stories: $n_stories, Hits: $n_hits\n";
238 'stories_by_tag' => [
239 'stories_by_tag {src} {topic(s)} {tag(s)}',
240 'Searches stories by tag(s)',
247 $topics = [ split(':', $topics) ];
250 foreach my $s ($g->stories_by_tag($topics, $tags)) {
251 print join(' ', @
{$s}), "\n";
256 'search {src} {topic_id} {query}',
257 'Searches stories by content',
260 my $topic_id = arg
();
263 foreach my $s ($g->search_stories($topic_id, $query)) {
269 'top_ten {src} [{num}]',
270 'Shows the top N stories',
273 my $num = arg_o
() || 10;
275 foreach my $s ($g->stories_top_ten($num)) {
276 print join(' ', @
{$s}), "\n";
280 'users_by_xdate' => [
281 'users_by_xdate {src} [{max_date}]',
282 'Lists users by expiration date',
285 my $max_date = arg_o
() || '99999999999999';
287 foreach my $id ($g->users()) {
288 my $u = $g->user($id);
290 my $xdate = $u->get('xdate');
293 $xdate gt Gruta
::Data
::today
() &&
294 $xdate lt $max_date) {
295 print $id, ' ', $u->{email
}, ' ', $xdate, "\n";
301 'import_rss {src} {topic_id}',
302 'Import an RSS from STDIN into a topic',
305 my $topic_id = arg
();
310 my $feed = XML
::Feed
->parse(\
*STDIN
) or die XML
::Feed
->errstr;
312 foreach my $entry ($feed->entries()) {
313 my $title = $entry->title();
315 my $content = "<h1>$title</h1>" . $entry->content->body();
317 my $d = $entry->modified() || $entry->issued();
321 $date = sprintf("%04d%02d%02d%02d%02d%02d",
322 $d->year(), $d->month(), $d->day(),
323 $d->hour(), $d->minute(), $d->second());
326 $date = Gruta
::Data
::today
();
329 my $md5 = Digest
::MD5
->new();
332 my $id = $md5->hexdigest();
336 if (not $story = $g->story($topic_id, $id)) {
337 $story = Gruta
::Data
::Story
->new (
338 topic_id
=> $topic_id,
343 $story->set('date', $date);
344 $story->set('format', 'html');
345 $story->set('content', $content);
346 $story->set('ctime', time());
348 if ($story->source()) {
349 $story = $story->save();
352 $story = $g->insert_story($story);
363 if (not $c = $X->{$cmd}) {
386 return shift(@ARGV) || shift;
391 my $src = new_source
(arg
());
392 my $g = Gruta
->new( sources
=> $src );
403 print "Usage: gruta ", $c->[0], "\n\n\t", $c->[1], "\n";
406 print "Usage: gruta {cmd} [args...]\n\n";
408 foreach my $k (sort keys %{$X}) {
411 print ' ', sprintf('%-50s %s',
412 $c->[0], $c->[1]), "\n";
424 if ($src_str =~ /^dbi:/) {
425 $src = Gruta
::Source
::DBI
->new( string
=> $src_str );
427 elsif ($src_str =~ /^mbox:(.+)/) {
430 $src = Gruta
::Source
::Mbox
->new(
435 $src = Gruta
::Source
::FS
->new( path
=> $src_str );
444 my $topic_id = shift;
448 my $story = $g->story($topic_id, $id)
449 or die "Cannot find story '${topic_id}/${id}'";
451 foreach my $f ($story->fields()) {
452 if ($f ne 'content') {
453 push (@r, $f . ': ' . ($story->get($f) || ''));
457 push(@r, 'tags: ' . join(', ', $story->tags()));
459 push(@r, $story->get('content'));
462 return join("\n", @r);
469 my $topic_id = shift;
472 my $topic = $g->topic($topic_id);
474 foreach my $f ($topic->afields()) {
475 push(@r, $f . ': ' . ($topic->get($f) || ''));
478 return join("\n", @r);
485 my $topic_id = shift;
491 open F
, $fn or die "Can't open $fn";
496 $story = $g->story($topic_id, $id)
497 or die "Cannot find story '${topic_id}/${id}'";
500 $story = Gruta
::Data
::Story
->new (
501 topic_id
=> $topic_id,
503 date
=> Gruta
::Data
::today
(),
513 my ($key, $value) = (/^(\w+):\s*(.*)$/);
515 if ($key eq 'tags') {
519 $story->set($key, $value);
523 my $c = join('', <F
>);
526 $story->set('content', $c);
528 if ($story->source()) {
532 $g->insert_story($story);
536 $story->tags(split(/,\s*/, $tags));
544 my $topic_id = shift;
547 open F
, $fn or die "Can't open $fn";
552 $topic = $g->topic($topic_id);
555 $topic = Gruta
::Data
::Topic
->new (
556 topic_id
=> $topic_id,
565 my ($key, $value) = (/^(\w+):\s*(.*)$/);
567 $topic->set($key, $value);
570 if ($topic->source()) {
574 $g->insert_topic($topic);