9 use Gruta
::Source
::DBI
;
10 use Gruta
::Source
::FS
;
12 # = new_source($src_str);
13 # = Gruta->new( sources => $src );
18 'Copies the full source {src} to {dst}',
22 my $dst = new_source
( $new_src );
26 $g->transfer_to_source( $dst );
31 'Lists the topics in {src}',
35 foreach my $t ($g->topics()) {
41 'topic {src} {topic_id}',
47 my $topic = $g->topic($topic_id);
49 foreach my $f ($topic->afields()) {
50 print $f, ': ', ($topic->get($f) || ''), "\n";
55 'stories {src} {topic_id}',
56 'Lists all stories of a topic',
61 foreach my $s ($g->stories($topic_id)) {
67 'story {src} {topic_id} {id}',
74 print get_story
($g, $topic_id, $id);
78 'edit_story {src} {topic_id} {id}',
79 'Calls $EDITOR to edit story data',
85 my $fh = File
::Temp
->new();
86 print $fh get_story
($g, $topic_id, $id);
87 my $fn = $fh->filename();
90 my $mtime = (stat($fn))[9];
91 system('$EDITOR ' . $fn);
93 if ($mtime != (stat($fn))[9]) {
94 save_story
($g, $topic_id, $id, $fn);
99 'filter_story {src} {topic_id} {id} {command}',
100 'Filters story data through command (STDIN, STDOUT)',
103 my $topic_id = arg
();
105 my $filter_cmd = arg
();
107 my $fhr = File
::Temp
->new();
108 print $fhr get_story
($g, $topic_id, $id);
109 my $fnr = $fhr->filename();
112 my $fhw = File
::Temp
->new();
113 my $fnw = $fhw->filename();
116 system("$filter_cmd < $fnr > $fnw");
118 save_story
($g, $topic_id, $id, $fnw);
130 'Lists all tags in {src}',
134 foreach my $t ($g->tags()) {
135 print join(' ', @
{$t}), "\n";
139 'stories_by_date' => [
140 'stories_by_date {src} {topic(s)} {num} {offset}',
141 'Searches stories by date',
149 $topics = [ split(':', $topics) ];
152 foreach my $s ($g->stories_by_date( $topics,
153 num
=> $num, offset
=> $offset) ) {
154 print join(' ', @
{$s}), "\n";
160 'Dumps statistics for {src}',
167 foreach my $t ($g->topics()) {
170 foreach my $s ($g->stories($t)) {
173 my $story = $g->story($t, $s);
175 $n_hits += $story->get('hits') || 0;
179 print "Topics: $n_topics, Stories: $n_stories, Hits: $n_hits\n";
188 if (not $c = $X->{$cmd}) {
211 my $src = new_source
(arg
());
212 my $g = Gruta
->new( sources
=> $src );
223 print "Usage: gruta ", $c->[0], "\n\n\t", $c->[1], "\n";
226 print "Usage: gruta {cmd} [args...]\n\n";
228 foreach my $c (values(%{$X})) {
229 print ' ', sprintf('%-50s %s',
230 $c->[0], $c->[1]), "\n";
242 if ($src_str =~ /^dbi:/) {
243 $src = Gruta
::Source
::DBI
->new( string
=> $src_str );
246 $src = Gruta
::Source
::FS
->new( path
=> $src_str );
255 my $topic_id = shift;
259 my $story = $g->story($topic_id, $id);
261 foreach my $f ($story->fields()) {
262 if ($f ne 'content') {
263 push (@r, $f . ': ' . ($story->get($f) || ''));
267 push(@r, 'tags: ' . join(', ', $story->tags()));
269 push(@r, $story->get('content'));
272 return join("\n", @r);
279 my $topic_id = shift;
283 open F
, $fn or die "Can't open $fn";
288 $story = $g->story($topic_id, $id);
291 $story = Gruta
::Data
::Story
->new (
292 topic_id
=> $topic_id,
302 my ($key, $value) = (/^(\w+):\s*(.*)$/);
304 if ($key eq 'tags') {
305 $story->tags(split(/,\s*/, $value));
308 $story->set($key, $value);
312 my $c = join('', <F
>);
315 $story->set('content', $c);
317 if ($story->source()) {
321 $g->insert_story($story);