8 $Gruta::VERSION
= '2.0.0-rc3';
10 sub sources
{ return @
{$_[0]->{sources
}}; }
11 sub template
{ return $_[0]->{template
}; }
12 sub cgi
{ return $_[0]->{cgi
}; }
14 sub version
{ return $Gruta::VERSION
; }
20 print STDERR
$self->{id
}, ' ', scalar(localtime), ': ', $msg, "\n";
31 foreach my $s ($self->sources()) {
32 if (my $m = $s->can($method)) {
33 my @pr = $m->($s, @_);
43 return wantarray ?
@r : $r[0];
46 sub topic
{ my $self = shift; return $self->_call('topic', 1, @_); }
47 sub topics
{ my $self = shift; return $self->_call('topics', 0); }
49 sub user
{ my $self = shift; return $self->_call('user', 1, @_); }
50 sub users
{ my $self = shift; return $self->_call('users', 0); }
57 if (! $topic_id || ! $id) {
62 my $ck = $topic_id . '/' . $id;
64 if ($story = $self->{story_cache
}->{$ck}) {
68 if (not $story = $self->_call('story', 1, $topic_id, $id)) {
72 my $format = $story->get('format') || 'grutatxt';
74 if (my $rndr = $self->{renderers_h
}->{$format}) {
78 return $self->{story_cache
}->{$ck} = $story;
82 sub stories
{ my $self = shift; return $self->_call('stories', 0, @_); }
90 $topics = [ $self->topics() ];
93 my @r = sort { $b->[2] cmp $a->[2] } $self->_call('stories_by_date', 0, $topics, %opts);
95 if (scalar(@r) > $opts{num
}) {
96 @r = @r[0 .. $opts{num
} - 1];
104 my $topic_id = shift;
106 my @l = $self->_call('search_stories', 1, $topic_id, @_);
108 return sort { $self->story($topic_id, $a)->get('title') cmp
109 $self->story($topic_id, $b)->get('title') } @l;
112 sub stories_top_ten
{
115 my @l = $self->_call('stories_top_ten', 0, @_);
117 return sort { $b->[0] <=> $a->[0] } @l;
120 sub search_stories_by_tag
{
123 my @l = $self->_call('search_stories_by_tag', 0, @_);
125 return sort { $self->story($a->[0], $a->[1])->get('title') cmp
126 $self->story($b->[0], $b->[1])->get('title') } @l;
132 my @l = $self->_call('tags', 0, @_);
134 return sort { $a->[0] cmp $b->[0] } @l;
137 sub insert_topic
{ my $self = shift; return $self->_call('insert_topic', 1, @_); }
138 sub insert_user
{ my $self = shift; return $self->_call('insert_user', 1, @_); }
139 sub insert_story
{ my $self = shift; return $self->_call('insert_story', 1, @_); }
145 if (@_) { $self->{auth
} = shift; } # Gruta::Data::User
147 return $self->{auth
};
158 $self->_call('purge_old_sessions', 0);
160 if (my $session = $self->_call('session', 1, $sid)) {
161 $u = $session->source->user( $session->get('user_id') );
162 $u->set('sid', $sid);
178 if (my $u = $self->user( $user_id )) {
180 # account expired? go!
181 if (my $xdate = $u->get('xdate')) {
182 if (Gruta
::Data
::today
() > $xdate) {
187 my $p = $u->get('password');
189 if (crypt($passwd, $p) eq $p) {
193 my $session = Gruta
::Data
::Session
->new(
199 $u->source->insert_session( $session );
201 $u->set('sid', $sid);
213 if (my $auth = $self->auth()) {
214 if( my $sid = $auth->get('sid')) {
215 if (my $session = $auth->source->session( $sid )) {
216 $session->delete() if $session->can('delete');
221 $self->auth( undef );
228 my $topic_id = shift;
232 if (my $t = $self->topic($topic_id)) {
233 $ret = "<a href='?t=TOPIC;topic=$topic_id'>" .
234 $t->get('name') . '</a>';
237 $ret = "Bad topic $topic_id";
246 my $topic_id = shift;
247 my $story_id = shift;
251 if (my $s = $self->story($topic_id, $story_id)) {
252 $ret = "<a href='?t=STORY;topic=$topic_id;id=$story_id'>" .
253 $s->get('title') . '</a>';
256 $ret = "Bad story '$topic_id/$story_id'";
267 $string =~ s!topic://([\w\d_]+)!$self->_link_to_topic($1)!ge;
268 $string =~ s!story://([\w\d_]+)/([\w\d_]+)!$self->_link_to_story($1,$2)!ge;
274 sub transfer_to_source
{
278 foreach my $id ($self->users()) {
279 my $u = $self->user($id);
280 $dst->insert_user($u);
283 foreach my $topic_id (sort $self->topics()) {
284 my $t = $self->topic($topic_id);
289 if ($nti =~ /-arch$/) {
290 # don't insert topic, just rename
294 $dst->insert_topic($t);
297 foreach my $id ($self->stories($topic_id)) {
299 # get story and its tags
300 my $s = $self->story($topic_id, $id);
301 my @tags = $s->tags();
304 $s->set('topic_id', $nti);
306 my $ns = $dst->insert_story($s);
318 sub flush_story_cache
{
321 $self->{story_cache
} = {};
328 my $g = bless( { @_ } , $class);
330 $g->{id
} ||= 'Gruta';
331 $g->{story_cache
} = {};
332 $g->{renderers_h
} = {};
334 if (ref($g->{sources
}) ne 'ARRAY') {
335 $g->{sources
} = [ $g->{sources
} ];
338 if ($g->{renderers
}) {
339 if (ref($g->{renderers
}) ne 'ARRAY') {
340 $g->{renderers
} = [ $g->{renderers
} ];
343 foreach my $r (@
{$g->{renderers
}}) {
344 $g->{renderers_h
}->{$r->{renderer_id
}} = $r;
348 if ($g->{template
}) {
349 $g->template->data($g);
358 if (not @u = $g->users()) {
359 my $u = Gruta
::Data
::User
->new(
364 email
=> 'webmaster@localhost'
367 $u->password('admin');