10 $Gruta::VERSION
= '2.0.0-rc3';
12 sub sources
{ return @
{$_[0]->{sources
}}; }
13 sub template
{ return $_[0]->{template
}; }
14 sub cgi
{ return $_[0]->{cgi
}; }
16 sub version
{ return $Gruta::VERSION
; }
22 print STDERR
$self->{id
}, ' ', scalar(localtime), ': ', $msg, "\n";
33 foreach my $s ($self->sources()) {
34 if (my $m = $s->can($method)) {
35 my @pr = $m->($s, @_);
45 return wantarray ?
@r : $r[0];
48 sub topic
{ my $self = shift; return $self->_call('topic', 1, @_); }
49 sub topics
{ my $self = shift; return $self->_call('topics', 0); }
51 sub user
{ my $self = shift; return $self->_call('user', 1, @_); }
52 sub users
{ my $self = shift; return $self->_call('users', 0); }
59 if (! $topic_id || ! $id) {
64 my $ck = $topic_id . '/' . $id;
66 if ($story = $self->{story_cache
}->{$ck}) {
70 if (not $story = $self->_call('story', 1, $topic_id, $id)) {
74 my $format = $story->get('format') || 'grutatxt';
76 if (my $rndr = $self->{renderers_h
}->{$format}) {
80 return $self->{story_cache
}->{$ck} = $story;
84 sub stories
{ my $self = shift; return $self->_call('stories', 0, @_); }
92 $topics = [ $self->topics() ];
95 my @r = sort { $b->[2] cmp $a->[2] } $self->_call('stories_by_date', 0, $topics, %opts);
97 if (defined($opts{num
}) && scalar(@r) > $opts{num
}) {
98 @r = @r[0 .. $opts{num
} - 1];
106 my $topic_id = shift;
108 my @l = $self->_call('search_stories', 1, $topic_id, @_);
110 return sort { $self->story($topic_id, $a)->get('title') cmp
111 $self->story($topic_id, $b)->get('title') } @l;
114 sub stories_top_ten
{
117 my @l = $self->_call('stories_top_ten', 0, @_);
119 return sort { $b->[0] <=> $a->[0] } @l;
122 sub search_stories_by_tag
{
125 my @l = $self->_call('search_stories_by_tag', 0, @_);
127 return sort { $self->story($a->[0], $a->[1])->get('title') cmp
128 $self->story($b->[0], $b->[1])->get('title') } @l;
134 my @l = $self->_call('tags', 0, @_);
136 return sort { $a->[0] cmp $b->[0] } @l;
139 sub insert_topic
{ my $self = shift; return $self->_call('insert_topic', 1, @_); }
140 sub insert_user
{ my $self = shift; return $self->_call('insert_user', 1, @_); }
141 sub insert_story
{ my $self = shift; return $self->_call('insert_story', 1, @_); }
147 if (@_) { $self->{auth
} = shift; } # Gruta::Data::User
149 return $self->{auth
};
160 $self->_call('purge_old_sessions', 0);
162 if (my $session = $self->_call('session', 1, $sid)) {
163 $u = $session->source->user( $session->get('user_id') );
164 $u->set('sid', $sid);
180 if (my $u = $self->user( $user_id )) {
182 # account expired? go!
183 if (my $xdate = $u->get('xdate')) {
184 if (Gruta
::Data
::today
() > $xdate) {
189 my $p = $u->get('password');
191 if (crypt($passwd, $p) eq $p) {
195 my $session = Gruta
::Data
::Session
->new(
201 $u->source->insert_session( $session );
203 $u->set('sid', $sid);
215 if (my $auth = $self->auth()) {
216 if( my $sid = $auth->get('sid')) {
217 if (my $session = $auth->source->session( $sid )) {
218 $session->delete() if $session->can('delete');
223 $self->auth( undef );
230 my $topic_id = shift;
234 if (my $t = $self->topic($topic_id)) {
235 $ret = "<a href='?t=TOPIC;topic=$topic_id'>" .
236 $t->get('name') . '</a>';
239 $ret = "Bad topic $topic_id";
248 my $topic_id = shift;
249 my $story_id = shift;
253 if (my $s = $self->story($topic_id, $story_id)) {
254 $ret = "<a href='?t=STORY;topic=$topic_id;id=$story_id'>" .
255 $s->get('title') . '</a>';
258 $ret = "Bad story '$topic_id/$story_id'";
269 $string =~ s!topic://([\w\d_]+)!$self->_link_to_topic($1)!ge;
270 $string =~ s!story://([\w\d_]+)/([\w\d_]+)!$self->_link_to_story($1,$2)!ge;
276 sub transfer_to_source
{
280 foreach my $id ($self->users()) {
281 my $u = $self->user($id);
282 $dst->insert_user($u);
285 foreach my $topic_id (sort $self->topics()) {
286 my $t = $self->topic($topic_id);
291 if ($nti =~ /-arch$/) {
292 # don't insert topic, just rename
296 $dst->insert_topic($t);
299 foreach my $id ($self->stories($topic_id)) {
301 # get story and its tags
302 my $s = $self->story($topic_id, $id);
303 my @tags = $s->tags();
306 $s->set('topic_id', $nti);
308 my $ns = $dst->insert_story($s);
320 sub flush_story_cache
{
323 $self->{story_cache
} = {};
330 my $g = bless( { @_ } , $class);
332 $g->{id
} ||= 'Gruta';
333 $g->{story_cache
} = {};
334 $g->{renderers_h
} = {};
336 if (ref($g->{sources
}) ne 'ARRAY') {
337 $g->{sources
} = [ $g->{sources
} ];
340 if ($g->{renderers
}) {
341 if (ref($g->{renderers
}) ne 'ARRAY') {
342 $g->{renderers
} = [ $g->{renderers
} ];
345 foreach my $r (@
{$g->{renderers
}}) {
346 $g->{renderers_h
}->{$r->{renderer_id
}} = $r;
350 if ($g->{template
}) {
351 $g->template->data($g);
360 if (not @u = $g->users()) {
361 my $u = Gruta
::Data
::User
->new(
366 email
=> 'webmaster@localhost'
369 $u->password('admin');