aboutsummaryrefslogtreecommitdiff
path: root/lib/WebCoso/Common.pm
blob: 6b6ba9e357b9f56bfd93e2e7425fec60b9d1d34c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package WebCoso::Common; 
use strict;
use warnings;
use File::Next;
use Path::Class;
use XML::LibXML::XPathContext;
use DateTime;
use DateTime::Format::DateParse;
use Log::Log4perl ':easy';
 
our $SRCPATH='src';
our $DSTPATH='dst';
our $DSTBASEURL='/';
our @TMPLPATH=('common/');
 
my $xpath=XML::LibXML::XPathContext->new();
$xpath->registerNs('x''http://www.w3.org/1999/xhtml');
 
sub langOf {
    my ($name)=@_;
    $name=~m{(^|/)document\.([^.]+)(\.|$)} and return $2;
    return;
}
 
sub typeOf {
    my ($name)=@_;
    $name=~m{(^|/)document\.[^.]+\.([^.]+\.[^.]+)$} and return $2;
    return;
}
 
sub typedAs {
    my ($name,$newtype)=@_;
    $name=~s{(^|/)(document\.[^.]+\.)([^.]+\.[^.]+)$}{$1$2$newtype};
    return $name;
}
 
sub dstUriFor {
    my ($name,$short)=(@_,1);
    DEBUG("dstUriFor($name,$short)");
    if ($short) {
        $name=~s{/[^/]+$}{/};
    }
    else {
        $name=typedAs($name,'html');
    }
    $name=~s{^\Q$SRCPATH\E/}{$DSTBASEURL};
    DEBUG("dstUriFor -> $name");
    return $name;
}
 
sub isLang {
    my ($lang,$name)=@_;
    DEBUG("isLang($lang,$name)");
    return 1 if $name=~m{/$}# assume that MultiViews on the server will work 
    return 1 if langOf($nameeq $lang;
    return;
}
 
sub getTitleFor {
    my ($fc,$lang,$path,$name)=@_;
    DEBUG("getTitleFor($lang,$path,$name)");
 
    my $doc_name=$name;
    $doc_name=~s{\.html$}{.du.xml};
    $doc_name=~s{/$}{/document.$lang.du.xml};
    if ($doc_name=~m{^\Q$DSTBASEURL\E}) {
        $doc_name=~s{^\Q$DSTBASEURL\E}{$SRCPATH/};
    }
    else {
        $doc_name=file($doc_name)->absolute(file($path)->parent)->relative($SRCPATH); # absolutize it 
        $doc_name="$SRCPATH/$doc_name";
    }
    DEBUG("getTitleFor -> $doc_name");
 
    my $doc=$fc->get($doc_name);
    unless ($doc) {
        LOGWARN("No document for <$doc_name>, returning <$name>");
        return $name;
    }
    my $title=$xpath->findnodes(
                q{/document/title},
                $doc);
    return $title;
}
 
{
    my $zero=DateTime->from_epoch(epoch=>0);
sub getDatesFor {
    my ($fc,$lang,$path,$name)=@_;
    DEBUG("getDatesFor($lang,$path,$name)");
 
    my $doc_name=$name;
    $doc_name=~s{\.html$}{.du.xml};
    $doc_name=~s{/$}{/document.$lang.du.xml};
    if ($doc_name=~m{^\Q$DSTBASEURL\E}) {
        $doc_name=~s{^\Q$DSTBASEURL\E}{$SRCPATH/};
    }
    else {
        $doc_name=file($doc_name)->absolute(file($path)->parent)->relative($SRCPATH); # absolutize it 
        $doc_name="$SRCPATH/$doc_name";
    }
    DEBUG("getDatesFor -> $doc_name");
 
    my $doc=$fc->get($doc_name);
    unless ($doc) {
        LOGWARN("No document for <$doc_name>, returning <0>");
        return {creation=>$zero,last_change=>$zero};
    }
    my ($creation_date)=map {$_->textContent}
        $xpath->findnodes(
            q{/document/docinfo/field[field_name='CreationDate']/field_body},
            $doc);
    $creation_date=DateTime::Format::DateParse->parse_datetime($creation_date)||$zero;
    my $last_change=DateTime->from_epoch(epoch=>$fc->stat($doc_name)->mtime);
    return {creation=>$creation_date,
            last_change=>$last_change};
}
}
 
sub getDates {
    my ($fc,@docs)=@_;
 
    my %dates;
    for my $doc_name (@docs) {
        my $doc=$fc->get($doc_name);
        my ($creation_date)=map {$_->textContent}
            $xpath->findnodes(
                q{/document/docinfo/field[field_name='CreationDate']/field_body},
                $doc);
        $creation_date=DateTime::Format::DateParse->parse_datetime($creation_date);
        my $last_change=DateTime->from_epoch($doc->stat->mtime);
        $dates{$doc_name}={creation=>$creation_date,
                           last_change=>$last_change};
    }
    return \%dates;
}
 
sub getTags {
    my ($fc,@docs)=@_;
 
    my %tagged;
    for my $doc_name (@docs) {
        my $doc=$fc->get($doc_name);
        my @tags=map {$_->textContent}
            $xpath->findnodes(
                q{/document/docinfo/field[field_name='tags']/field_body/*/list_item|/document/docinfo/field[field_name='tags']/field_body/paragraph},
                $doc);
        chomp for @tags;
        push @{$tagged{$_}},$doc_name for @tags;
    }
    return \%tagged;
}
 
sub fromTo {
    my ($base,$opts)=@_;
    my $iter=File::Next::files(
        {
            file_filter=>$opts->{files},
            descend_filter=>$opts->{dirs},
        },
        $base);
    my (@ret,$file);
    if (defined $opts->{transform}) {
        push @ret,$opts->{transform}->($filewhile $file=$iter->();
    }
    else {
        push @ret,$file while $file=$iter->();
    }
    return @ret;
}
 
{
my %order=(
    'rest.tt'=>0,
    'rest.txt'=>1,
    'du.xml'=>2,
);
sub earliest {
    my ($a,$b)=@_;
    return $a unless $b;
    return $order{$a} < $order{$b}
        $a
        $b;
}
 
sub keepEarliest {
    my %dirs;
    for my $f (@_) {
        my $c=file($f);
        my $lang=langOf($c->basename);
        my $type=typeOf($c->basename);
        if (!defined $lang or !defined $type) {
            die "Weird document name <$f>";
        }
        $dirs{$c->parent}->{$lang}=earliest($type,$dirs{$c->parent}->{$lang});
    }
    my @ret;
    while (my ($d,$langs)=each %dirs) {
        while (my ($lang,$type)=each %$langs) {
            push @ret,file($d,"document.$lang.$type")->stringify;
        }
    }
    return @ret;
}
}
 
our %docfiles=(
    files=>sub{m{^document\.}},
    dirs=>sub{!m{^(tags|_webcoso|\.svn)$}});
 
1;