Updated TODO.
[gruta.git] / Gruta.pm
blob431fae6e55617da14e9ad8d093d0f0fc9ba7a71a
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 my $format = $story->get('format') || 'grutatxt';
79 if (my $rndr = $self->{renderers_h}->{$format}) {
80 $rndr->story($story);
83 return $self->{story_cache}->{$ck} = $story;
87 sub stories { my $self = shift; return $self->_call('stories', 0, @_); }
88 sub stories_by_date { my $self = shift;
89 return $self->_call('stories_by_date', 1, @_, 'today' => $self->today()); }
90 sub search_stories { my $self = shift; return $self->_call('search_stories', 1, @_); }
92 sub stories_top_ten {
93 my $self = shift;
95 my @l = $self->_call('stories_top_ten', 0, @_);
97 return sort { $b->[0] cmp $a->[0] } @l;
100 sub stories_by_tag { my $self = shift; return $self->_call('stories_by_tag', 0, @_); }
102 sub tags {
103 my $self = shift;
105 my @l = $self->_call('tags', 0, @_);
107 return sort { $b->[1] cmp $a->[1] } @l;
110 sub insert_topic { my $self = shift; $self->_call('insert_topic', 1, @_); return $self; }
111 sub insert_user { my $self = shift; $self->_call('insert_user', 1, @_); return $self; }
112 sub insert_story { my $self = shift; $self->_call('insert_story', 1, @_); return $self; }
115 sub auth {
116 my $self = shift;
118 if (@_) { $self->{auth} = shift; } # Gruta::Data::User
120 return $self->{auth};
124 sub auth_from_sid {
125 my $self = shift;
126 my $sid = shift;
128 my $u = undef;
130 if ($sid) {
131 $self->_call('purge_old_sessions', 0);
133 if (my $session = $self->_call('session', 1, $sid)) {
134 $u = $session->source->user( $session->get('user_id') );
135 $u->set('sid', $sid);
136 $self->auth($u);
140 return $u;
144 sub login {
145 my $self = shift;
146 my $user_id = shift;
147 my $passwd = shift;
149 my $sid = undef;
151 if (my $u = $self->user( $user_id )) {
153 my $p = $u->get('password');
155 if (crypt($passwd, $p) eq $p) {
156 # create new sid
157 $sid = time() . $$;
159 my $session = Gruta::Data::Session->new(
160 id => $sid,
161 time => time(),
162 user_id => $user_id
165 $u->source->insert_session( $session );
167 $u->set('sid', $sid);
168 $self->auth($u);
172 return $sid;
176 sub logout {
177 my $self = shift;
179 if (my $auth = $self->auth()) {
180 if( my $sid = $auth->get('sid')) {
181 if (my $session = $auth->source->session( $sid )) {
182 $session->delete() if $session->can('delete');
187 $self->auth( undef );
188 return $self;
192 sub _link_to_topic {
193 my $self = shift;
194 my $topic_id = shift;
196 my $ret = undef;
198 if (my $t = $self->topic($topic_id)) {
199 $ret = "<a href='?t=TOPIC;topic=$topic_id'>" .
200 $t->get('name') . '</a>';
202 else {
203 $ret = "Bad topic $topic_id";
206 return $ret;
210 sub _link_to_story {
211 my $self = shift;
212 my $topic_id = shift;
213 my $story_id = shift;
215 my $ret = undef;
217 if (my $s = $self->story($topic_id, $story_id)) {
218 $ret = "<a href='?t=STORY;topic=$topic_id;id=$story_id'>" .
219 $s->get('title') . '</a>';
221 else {
222 $ret = "Bad story '$topic_id/$story_id'";
225 return $ret;
229 sub special_uris {
230 my $self = shift;
231 my $string = shift;
233 $string =~ s!topic://([\w\d_]+)!$self->_link_to_topic($1)!ge;
234 $string =~ s!story://([\w\d_]+)/([\w\d_]+)!$self->_link_to_story($1,$2)!ge;
236 return $string;
240 sub transfer_to_source {
241 my $self = shift;
242 my $dst = shift;
244 foreach my $id ($self->users()) {
245 my $u = $self->user($id);
246 $dst->insert_user($u);
249 foreach my $topic_id ($self->topics()) {
250 my $t = $self->topic($topic_id);
251 $dst->insert_topic($t);
253 foreach my $id ($self->stories($topic_id)) {
254 my $s = $self->story($topic_id, $id);
255 my $ns = $dst->insert_story($s);
257 $ns->tags( $s->tags() );
261 return $self;
265 sub flush_story_cache {
266 my $self = shift;
268 $self->{story_cache} = {};
272 sub new {
273 my $class = shift;
275 my $g = bless( { @_ } , $class);
277 $g->{id} ||= 'Gruta';
278 $g->{story_cache} = {};
280 if (ref($g->{sources}) ne 'ARRAY') {
281 $g->{sources} = [ $g->{sources} ];
283 if (ref($g->{renderers}) ne 'ARRAY') {
284 $g->{renderers} = [ $g->{renderers} ];
287 $g->{renderers_h} = {};
289 foreach my $r (@{$g->{renderers}}) {
290 $g->{renderers_h}->{$r->{renderer_id}} = $r;
293 $g->template->data($g) if $g->{template};
294 $g->cgi->data($g) if $g->{cgi};
296 return $g;
299 sub run {
300 my $self = shift;
302 if ($self->{cgi}) {
303 $self->cgi->run();