annotate Lib/DOM/Providers/Headlines.pm @ 16:75d55f4ee263

Окончательная концепция описания схем и построения DOM документов
author Sergey
date Tue, 08 Sep 2009 17:29:07 +0400
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package DOM::Providers::Headlines::Headline;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use Time::Local;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 DeclareProperty(Id => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 DeclareProperty(DateModify => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 DeclareProperty(DateExpire => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 DeclareProperty(URL => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 DeclareProperty(Text => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 DeclareProperty(Channel => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 sub str2time {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 my $str = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 if ($str =~ /^(\d{4})-(\d{2})-(\d{2})(?:\s(\d{2}):(\d{2}):(\d{2}))?$/) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 my ($year,$month,$day,$hh,$mm,$ss) = ($1,$2-1,$3,(defined $4 ? $4 : 0),(defined $5 ? $5 : 0),(defined $6 ? $6 : 0));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 return timelocal($ss,$mm,$hh,$day,$month,$year);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 die new Exception("A string '$str' isn't an ISO standard time");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 sub IsActive {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 my $timeExpire = str2time($this->{$DateExpire});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 return ($timeExpire > time());
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 package DOM::Providers::Headlines::Collection;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 our @ISA = qw (Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 DeclareProperty(Items => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 my ($this,%args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 foreach my $headline (@{$args{'Items'}}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 $this->{$Items}->{$headline->Id()} = $headline if ($headline->IsActive)
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 sub as_list {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 return [ map { $this->{$Items}->{$_} } sort keys %{$this->{$Items}} ];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 sub GenerateRandomSequence {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56 my ($count,$max) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 my %hash;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 $hash{rand()} = $_ foreach (0 .. $max - 1);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 my @sequence = map { $hash{$_} } sort keys %hash;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 return splice @sequence,0,$count;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 sub Random {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 my ($this,$count) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 my $list = $this->as_list();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 return [map { $list->[$_] } GenerateRandomSequence($count,scalar(@$list))];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 sub Recent {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 my ($this,$count) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 my @result = sort { $b->DateModify() cmp $a->DateModify() } values %{$this->{$Items}};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 splice @result,$count;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 return \@result;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 sub AddItem {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 my ($this,$newItem) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 $this->{$Items}->{$newItem->Id()} = $newItem;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 package DOM::Providers::Headlines;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 use ObjectStore::Headlines;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91 our $DBPath;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 our $Encoding;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 my %Channels;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 eval {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 LoadHeadlines();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 if ($@) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 my $err = $@;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102 if (ref $err eq 'Exception') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103 die $err->ToString();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 die $err;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110 sub GetProviderInfo {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111 return {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112 Name => 'Headlines',
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 Host => 'DOM::Site',
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114 Objects => \%Channels
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
116 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
117
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
118 sub LoadHeadlines {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
119 my $dsHeadlines = new ObjectStore::Headlines(DBPath => $DBPath, HeadlineClass => 'DOM::Providers::Headlines::Headline', Encoding => $Encoding);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
120
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
121 foreach my $headline (@{$dsHeadlines->Search(Filter => sub { return $_[0]->IsActive(); } )}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
122 my $channel = $headline->Channel() || 'main';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123 $Channels{$channel} = new DOM::Providers::Headlines::Collection() if not exists $Channels{$channel};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124 $Channels{$channel}->AddItem($headline);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
126 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
127
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
128 1;