New (dummy) 'stories_top_ten' Artemus function.
[gruta.git] / Gruta.pm
blobaa1303e8f9f0fa5d483d7f394af6d4430f9dfa0f
1 package Gruta;
3 use strict;
4 use warnings;
6 $Gruta::VERSION = '2.0-pre2';
8 sub sources { return @{$_[0]->{sources}}; }
9 sub template { return $_[0]->{template}; }
10 sub cgi { return $_[0]->{cgi}; }
12 sub today {
13 my $self = shift;
15 if (not $self->{_today}) {
16 my ($S,$M,$H,$d,$m,$y) = (localtime)[0..5];
17 $self->{_today} =
18 sprintf("%04d%02d%02d%02d%02d%02d",
19 1900 + $y, $m + 1, $d, $H, $M, $S);
22 return $self->{_today};
25 sub log {
26 my $self = shift;
27 my $msg = shift;
29 print STDERR $self->{id}, ' ', scalar(localtime), ': ', $msg, "\n";
33 sub _call {
34 my $self = shift;
35 my $method = shift;
36 my $short = shift;
38 my @r = ();
40 foreach my $s ($self->sources()) {
41 if (my $m = $s->can($method)) {
42 my @pr = $m->($s, @_);
44 if (@pr && $pr[0]) {
45 @r = (@r, @pr);
47 last if $short;
52 return wantarray ? @r : $r[0];
55 sub topic { my $self = shift; return $self->_call('topic', 1, @_); }
56 sub topics { my $self = shift; return $self->_call('topics', 0); }
58 sub user { my $self = shift; return $self->_call('user', 1, @_); }
59 sub users { my $self = shift; return $self->_call('users', 0); }
61 sub story {
62 my $self = shift;
63 my $topic_id = shift;
64 my $id = shift;
66 my $story = undef;
67 my $ck = $topic_id . '/' . $id;
69 if ($story = $self->{story_cache}->{$ck}) {
70 return $story;
73 if (not $story = $self->_call('story', 1, $topic_id, $id)) {
74 return undef;
77 if (my $rndr = $self->{renderers_h}->{$story->get('format')}) {
78 $rndr->story($story);
81 return $self->{story_cache}->{$ck} = $story;
85 sub stories { my $self = shift; return $self->_call('stories', 0, @_); }
86 sub stories_by_date { my $self = shift;
87 return $self->_call('stories_by_date', 1, @_, 'today' => $self->today()); }
88 sub search_stories { my $self = shift; return $self->_call('search_stories', 1, @_); }
90 sub insert_topic { my $self = shift; $self->_call('insert_topic', 1, @_); return $self; }
91 sub insert_user { my $self = shift; $self->_call('insert_user', 1, @_); return $self; }
92 sub insert_story { my $self = shift; $self->_call('insert_story', 1, @_); return $self; }
95 sub auth {
96 my $self = shift;
98 if (@_) { $self->{auth} = shift; } # Gruta::Data::User
100 return $self->{auth};
104 sub auth_from_sid {
105 my $self = shift;
106 my $sid = shift;
108 my $u = undef;
110 if ($sid) {
111 $self->_call('purge_old_sessions', 0);
113 if (my $session = $self->_call('session', 1, $sid)) {
114 $u = $session->source->user( $session->get('user_id') );
115 $u->set('sid', $sid);
116 $self->auth($u);
120 return $u;
124 sub login {
125 my $self = shift;
126 my $user_id = shift;
127 my $passwd = shift;
129 my $sid = undef;
131 if (my $u = $self->user( $user_id )) {
133 my $p = $u->get('password');
135 if (crypt($passwd, $p) eq $p) {
136 # create new sid
137 $sid = time() . $$;
139 my $session = Gruta::Data::Session->new(
140 id => $sid,
141 time => time(),
142 user_id => $user_id
145 $u->source->insert_session( $session );
147 $u->set('sid', $sid);
148 $self->auth($u);
152 return $sid;
156 sub logout {
157 my $self = shift;
159 if (my $auth = $self->auth()) {
160 if( my $sid = $auth->get('sid')) {
161 if (my $session = $auth->source->session( $sid )) {
162 $session->delete() if $session->can('delete');
167 $self->auth( undef );
168 return $self;
172 sub _link_to_topic {
173 my $self = shift;
174 my $topic_id = shift;
176 my $ret = undef;
178 if (my $t = $self->topic($topic_id)) {
179 $ret = "<a href='?t=TOPIC;topic=$topic_id'>" .
180 $t->get('name') . '</a>';
182 else {
183 $ret = "Bad topic $topic_id";
186 return $ret;
190 sub _link_to_story {
191 my $self = shift;
192 my $topic_id = shift;
193 my $story_id = shift;
195 my $ret = undef;
197 if (my $s = $self->story($topic_id, $story_id)) {
198 $ret = "<a href='?t=STORY;topic=$topic_id;id=$story_id'>" .
199 $s->get('title') . '</a>';
201 else {
202 $ret = "Bad story '$topic_id/$story_id'";
205 return $ret;
209 sub special_uris {
210 my $self = shift;
211 my $string = shift;
213 $string =~ s!topic://([\w\d_]+)!$self->_link_to_topic($1)!ge;
214 $string =~ s!story://([\w\d_]+)/([\w\d_]+)!$self->_link_to_story($1,$2)!ge;
216 return $string;
220 sub transfer_to_source {
221 my $self = shift;
222 my $dst = shift;
224 foreach my $id ($self->users()) {
225 my $u = $self->user($id);
226 $dst->insert_user($u);
229 foreach my $topic_id ($self->topics()) {
230 my $t = $self->topic($topic_id);
231 $dst->insert_topic($t);
233 foreach my $id ($self->stories($topic_id)) {
234 my $s = $self->story($topic_id, $id);
235 $dst->insert_story($s);
239 return $self;
243 sub flush_story_cache {
244 my $self = shift;
246 $self->{story_cache} = {};
250 sub new {
251 my $class = shift;
253 my $g = bless( { @_ } , $class);
255 $g->{id} ||= 'Gruta';
256 $g->{story_cache} = {};
258 if (ref($g->{sources}) ne 'ARRAY') {
259 $g->{sources} = [ $g->{sources} ];
261 if (ref($g->{renderers}) ne 'ARRAY') {
262 $g->{renderers} = [ $g->{renderers} ];
265 $g->{renderers_h} = {};
267 foreach my $r (@{$g->{renderers}}) {
268 $g->{renderers_h}->{$r->{renderer_id}} = $r;
271 $g->template->data($g) if $g->{template};
272 $g->cgi->data($g) if $g->{cgi};
274 return $g;
277 sub run {
278 my $self = shift;
280 if ($self->{cgi}) {
281 $self->cgi->run();