annotate Lib/DOM/Providers/Gallery.pm @ 41:c442eb67fa22

parser in progress
author Sergey
date Mon, 21 Dec 2009 17:40:09 +0300
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 package DOM::Gallery;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use Common;
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(Name => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 DeclareProperty(Description => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 DeclareProperty(Images => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 DeclareProperty(CurrentImage => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 DeclareProperty(NextImage => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13 DeclareProperty(PrevImage => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17 my ($this,%args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 $this->{$Id} = $args{'Id'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 $this->{$Name} = $args{'Name'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 $this->{$Description} = $args{'Description'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 sub GroupList {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 my ($this,$GroupCount, $option) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 my @images = map { $this->{$Images}->{$_} } sort keys %{$this->{$Images}};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 my @listGroups;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 my $group;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 for (my $i = 0; $i < $GroupCount; $i++ ) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 #last unless scalar(@images) or $option =~ /align/i;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 push (@$group, shift(@images));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 if ($i == $GroupCount - 1) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 push @listGroups, $group;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36 undef $group;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 $i = -1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 last if not scalar(@images);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 return \@listGroups;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 sub SelectImage {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 my ($this,$imageId) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 my @images = sort keys %{$this->{$Images}};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 for (my $i=0; $i <= @images; $i++) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 if ($images[$i] eq $imageId) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 $this->{$CurrentImage} = $this->{$Images}->{$images[$i]};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 $this->{$PrevImage} = $i-1 >= 0 ? $this->{$Images}->{$images[$i-1]} : undef;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 $this->{$NextImage} = $i+1 < @images ? $this->{$Images}->{$images[$i+1]} : undef;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 die new Exception("An image '$imageId' not found in the gallery '$this->{$Id}'");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 sub AddImage {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 my ($this,$image) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 $this->{$Images}->{$image->Id()} = $image;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 package DOM::Gallery::Image;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 DeclareProperty(Id => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 DeclareProperty(Name => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 DeclareProperty(Gallery => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 DeclareProperty(URL => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 DeclareProperty(ThumbURL => ACCESS_READ);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 my ($this,%args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 $this->{$Id} = $args{'Id'} or die new Exception ('An Id should be specified for an image');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 $this->{$Name} = $args{'Name'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 $this->{$Gallery} = $args{'Gallery'} or die new Exception('An Gallery should be specified for an image');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 $this->{$URL} = $args{'URL'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 $this->{$ThumbURL} = $args{'ThumbURL'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88 package DOM::Providers::Gallery;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 our $RepoPath;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 our $ImagesURL;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 our $Encoding;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 DeclareProperty(GalleryCache => ACCESS_NONE);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 DeclareProperty(Repository => ACCESS_NONE);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102 my ($this,%args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 $this->{$Repository} = $args {'Repository'} or die new Exception('A path to an galleries repository should be specified');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107 sub GetProviderInfo() {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108 return {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109 Name => 'Gallery',
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110 Host => 'DOM::Site',
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111 Methods => {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112 LoadGallery => \&SiteLoadGallery #($this,$site,$galleryId)
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114 };
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
116
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
117 sub SiteLoadGallery {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
118 my ($this,$site,$galleryId) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
119
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
120 my $gallery = $this->LoadGallery($galleryId);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
121
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
122 $site->RegisterObject('Gallery',$gallery);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124 return $gallery;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
126
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
127 sub LoadGallery {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
128 my ($this,$galleryId) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
129
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
130 die new Exception("Invalid Gallery Id: $galleryId") if $galleryId =~ /\\|\//;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
131
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
132 my $galleryIdPath = $galleryId;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
133 $galleryIdPath =~ s/\./\//g;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
134
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
135 my $GalleryPath = $this->{$Repository} . $galleryIdPath .'/';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
136
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
137 die new Exception("A gallery '$galleryId' isn't found",$GalleryPath) if not -d $GalleryPath;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
138
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
139 open my $hDesc, "<:encoding($Encoding)", $GalleryPath.'index.htm' or die new Exception("Invalid gallery: $galleryId","Failed to open ${GalleryPath}index.htm: $!");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
140
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
141 my $GalleryName;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
142 while (<$hDesc>) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
143 if (/<title>(.+?)<\/title>/i) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
144 $GalleryName = $1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
145 last;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
146 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
147 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
148 undef $hDesc;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
149
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
150 my $ImagesPath = $GalleryPath.'images/';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
151 my $ThumbsPath = $GalleryPath.'thumbnails/';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
152
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
153 opendir my $hImages, $ImagesPath or die new Exception("Invalid gallery: $galleryId","Can't open images repository: $!");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
154
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
155 my @imageIds = grep { -f $ImagesPath.$_ } readdir $hImages;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
156
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
157 my %imageNames;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
158
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
159 if (-f $GalleryPath.'description.txt') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
160 local $/="\n";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
161 if (open my $hfile,"<:encoding($Encoding)",$GalleryPath.'description.txt') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
162 while (<$hfile>) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
163 chomp;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
164 my ($id,$name) = split /\s*=\s*/;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
165 $imageNames{$id} = $name;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
166 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
167 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
168 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
169
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
170 undef $hImages;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
171
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
172 if ($Common::Debug) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
173 foreach (@imageIds) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
174 warn "A tumb isn't found for an image: $_" if not -f $ThumbsPath.$_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
175 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
176 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
177
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
178 my $gallery = new DOM::Gallery(Id => $galleryId, Name => $GalleryName);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
179
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
180 foreach my $imageId (@imageIds) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
181 $gallery->AddImage(new DOM::Gallery::Image(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
182 Id => $imageId,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
183 URL => $ImagesURL.$galleryIdPath.'/images/'.$imageId,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
184 ThumbURL => $ImagesURL.$galleryIdPath.'/thumbnails/'.$imageId,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
185 Gallery => $gallery,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
186 Name => $imageNames{$imageId}
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
187 )
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
188 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
189 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
190
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
191 return $gallery;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
192 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
193
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
194 sub construct {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
195 my $self = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
196
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
197 return new DOM::Providers::Gallery( Repository => $RepoPath);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
198 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
199
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
200 1;