aboutsummaryrefslogtreecommitdiffstats
path: root/english/template/debian/recent_list_common.wml
blob: 3280fa19778ae031361ca8896c1b77b1681a6fd1 (plain) (blame)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<perl>
use Encode;

    # common functions for all recent_list_*.wml
    
sub get_recent_list {
    my ($time, $number, $rel_path, $english_dir, $files_match, $data_callback, $format_list_callback, $format_item_callback) =@_;

    #warn "get_recent_list($time, $number, $rel_path, $english_dir, $files_match, $data_callback, $format_list_callback, $format_item_callback)\n";
    my $since_date = determine_since_date($time);
    #warn "since_date=$since_date\n";
    my $files = get_matching_filenames_by_time($rel_path, $english_dir, $files_match, $since_date);
    my $data = get_files_data($files, $english_dir, $data_callback);
    my $filtered_data = filter_items($data, $since_date, $number);

    my $str = $format_list_callback->($filtered_data, $format_item_callback);

    return $str;
}

    
sub slurp_file_openrecode {
    my ($file, $eng_dir) = @_;

    (my $trans_title = $file) =~ s/\.wml$/\.title/;
    # read file in
    my $fh = openrecode($file, $trans_title, "$eng_dir/$file")
	or die "couldn't open $eng_dir/$file: $!\n";
    my $content;
    <protect pass=2>
    {
	local $/;
	$content = <$fh>;
    }
    </protect>
	close $fh;

    return $content;
}
sub slurp_file {
    my ($file) = @_;

    open my $fh, '<', $file
	or die "couldn't open $file: $!\n";
    my $content;
    <protect pass=2>
    {
	local $/;
	$content = <$fh>;
    }
    </protect>
    close $fh;

    return $content;
}

sub match_tag {
    my ($content, $tag) = @_;

    my $value;
    $content = Encode::decode_utf8($content);
    <protect pass=2>
    if ($content =~ m|^<define-tag $tag>\s*(.*?)\s*</define-tag>|ms) {
	$value = Encode::encode_utf8(qq/$1/); }      # all
    </protect>
    return $value;
}

sub match_tag_first_p {
    my ($content, $tag) = @_;

    my $value;
    <protect pass=2>
    if ($content =~ m"^<define-tag $tag>\s*(?:(.*?</p>)|(.*?)</define-tag>)"ms) {
	$value = qq/$1/; }      # all
    </protect>
    return $value;
}

sub determine_since_date {
    my ($time) = @_;

    return parse_time($time) if $time;
    return '';
}
	
sub determine_relevant_years {
    my ($since_date) = @_;
	
    my $year = $(CUR_YEAR);
    $since_year = $year;
    if ($since_date){
	$since_year = (gmtime($since_date))[5] + 1900;
	if ($since_year > $year) {
	    warn "since_year > year ($since_year > $year)\n";
	}
    }
    # djpig: take $since_year-1, perhaps better define an $oldest_year?
    # djpig: but there should be no more updates to an item after a year
    # djpig: we're saving time so.
    return [($since_year-1) .. $year];
}
    
sub get_matching_filenames_by_time {
    my ($rel_path, $english_dir, $match, $since_date) =@_;

    my @files;

    my $years = determine_relevant_years($since_date);
    for my $act_year (@$years) {
	my $act_path = $rel_path eq '.' ? $act_year : "$rel_path/$act_year"; 
	my $new_files = get_matching_filenames($act_path, "$english_dir/$act_year", $match);

	push @files, @$new_files;
    }

    return \@files;
}
    
sub get_matching_filenames {
    my ($rel_path, $eng_dir, $match) = @_;

    #warn "get_match_filesnames( $rel_path, $eng_dir, $match )\n";
    opendir my $dir_h, $eng_dir
	or die "couldn't open dir $eng_dir: $!\n";
    my @files = grep { ($_ =~ $match)
			   && -f "$eng_dir/$_"
			   && ($_="$rel_path/$_")
    } readdir($dir_h);
    closedir $dir_h;

    return \@files;
}

sub get_files_data {
    my ($files, $eng_dir, $callback) = @_;

    my %data;
    foreach my $file (@$files) {
	$callback->($file, $eng_dir, \%data);
    }

    return \%data;
}

sub filter_items {
    my ($data, $since_date, $minnumber) = @_;

    my @dates = sort { $b <=> $a } keys %$data;
    #warn "since_date: ".scalar gmtime($since_date)." minnum: $minnumber\n";
    my $count = 0;
    my %filtered_data;
    foreach my $date (@dates) {
	#warn "date: ".scalar gmtime($date)." ($count >= $minnumber) && ($date < $since_date)\n";
	if ($count >= $minnumber) {
	    if((!$since_date && $minnumber)
	       || (($since_date || !$minnum)
		   && ($date lt $since_date)) ) {
		last;
	    }
	}
	$filtered_data{$date} = $data->{$date};
	$count += scalar @{$data->{$date}};
    }

    return \%filtered_data;
}

# parse_time gets as argument a string and returns a unix timestamp
# Input: $time_str - String with the following format
#                    $time_str ::= <integer>(d|w|m|y)
# Output: integer timestamp
#
# parse_time subtracts <integer> days/weeks/months from the actual time and
# returns the corresponding timestamp. Years are handled special: 1y means
# "since January, 1st of actual year", 2y means "since January, 1st of
# last year", etc.
sub parse_time {
    my $time_str = shift;
    my $year = (gmtime())[5] + 1900;
    my $time = time();
    my $res;

    for ($time_str) {
	/\d{4}/ && do {
	    $res = timegm(0,0,0,1,0,$year);
	    last;
	};

	/(\d+)d/ && do {
	    $res = $time - 86400 * $1;
	    last;
	};

	/(\d+)w/ && do {
	    $res = $time - 86400 * 7 * $1;
	    last;
	};

	/(\d+)m/ && do {
	    # All months have 30 days,
	    # all other would be far more complicated
	    $res = $time - 86400 * 30 * $1;
	    last;
	};

	/(\d+)y/ && do {
	    # years are handled special
	    my $ryear = $year - $1 + 1; # the actual year count as a whole one
	    $res = timegm(0,0,0,1,0,$ryear); # 01.01.$ryear 00:00:00
	    last;
	};

    }

    return $res;
}

# iso2stamp converts a date in ISO format (YYYY-MM-DD) to an
# unix timestamp for 23:59:59 on the specified day
# Input: $time - String with the ISO date
# Output: integer timestamp
sub iso2stamp {
    my $time = shift;

    if ($time =~ /undated/) {
	return 0;
    }
    my ($year, $month, $day) = ($time =~ /(\d{4})-(\d{1,2})-(\d{1,2})/);
    unless ($year && $month && $day) { warn "not an ISO date: $time\n"; }

    return timegm( 59, 59, 23, $day, $month-1, $year);
}

</perl>

© 2014-2024 Faster IT GmbH | imprint | privacy policy