3 # This script is to get the changes needed to update the
4 # files on a mir mirror.
6 # Full details (among other stuff) at
7 # http://lists.indymedia.org/pipermail/imc-uk-tech/2006-May/0531-ib.html
11 # Mir will simply publish a list of file changes. It will produce one
12 # file per day. The file will be publicly viewable on the web at
14 # /changes/changesYYYYMMDD.txt
16 # So on May 28th, 2006, the file name would be
18 # /changes/changes20060528.txt
20 # The format of the file will also be simple. There will be three
21 # columns. First will be the time, then (add/change/delete) depending
22 # whether the file is new, has been updated or deleted, and then the full
23 # relative path of the file. So an example line would be
25 # 2006:01:24T09:08:17 add /en/2006/05/341547.html
27 # This will have to be done for HTML files, media files and the include
42 # by default, this is not a test
45 # by default, this is the working directory
46 my $workingdir = getcwd();
48 # get the time and date - using UTC
51 # if this is 1, we only list the change files that are needed and stop
52 # otherwise we do our normal processing
53 my $listchangefiles = 0;
57 # The root URL to fetch files from
60 ################################
61 # process command line arguments
62 ################################
64 foreach my $argnum (0 .. $#ARGV) {
65 my $argument = $ARGV[$argnum];
66 if ($argument eq "--test") {
68 } elsif ($argument =~ /^--workingdir=(.*)$/) {
70 } elsif ($argument =~ /^--now=(.*)$/) {
71 $timenow = str2time($1);
72 } elsif ($argument =~ /^--output=(.*)$/) {
74 } elsif ($argument =~ /^--remoteroot=(.*)$/) {
76 } elsif ($argument =~ /^--list-change-files/) {
81 # the directory where all files with state are kept
82 my $statedir = "$workingdir/state";
84 mkdir $statedir or die "Can't create $statedir";
87 # the directory where changes files are kept
88 my $changesdir = "$workingdir/changes";
90 # the file we want with the last time in it is
91 my $lastupdatefile = "$statedir/lastupdate.txt";
93 # where we get changes files from
94 my $changesroot = "$remoteroot/changes";
96 # LWP user agent for fetching files
97 # keep_alive is important, to avoid the overhead of
98 # establishing a new connection for each file we fetch
99 my $ua = LWP::UserAgent->new(agent => "MirMirror/0.1",
107 # first work out when the last time we were up to date is and
109 my $timeoflastupdate = findLastUpdateTime();
112 print STDERR "timenow is ".time2str("%c",$timenow)." \n";
113 print STDERR "timeoflastupdate is ".time2str("%c",$timeoflastupdate)." \n\n";
116 # Now we know which days' changes we need to get from the server
117 my @changesfiles = getChangesFileList();
118 if ($istest or $listchangefiles) {
119 foreach my $file (@changesfiles) { print STDERR "using changes file $file\n"; }
120 exit 0 if $listchangefiles;
123 # get the changes files
124 my @changesfilecontent;
125 foreach my $file (@changesfiles) { push @changesfilecontent, getChangesFile($file); }
127 # if the file has not changed (response code 304) then ignore it
129 # iterate over all the fetched files, building up a list of files
132 foreach my $changes (@changesfilecontent) {
133 my @changes = split /[\r\n]+/, $changes;
134 foreach my $change (@changes) {
135 my ($time, $op, $path) = split ' ', $change;
136 # TODO: Ignore changes prior to $timeoflastupdate
137 # TODO: Ignore malformed lines, especially wacky paths that could be malicious
139 print STDERR "Marked $path as '$op'\n" if $istest;
143 # Fetch all files whose last operation was "add" or "change"
144 # Delete all files whose last operation was "delete"
145 while (my ($file, $op) = each %files) {
146 if ($op eq "delete") {
147 if (-e "$workingdir/$file") {
148 # delete: if the file exists, remove it
149 print STDERR "deleting $workingdir/$file\n" if $istest;
150 unlink "$workingdir/$file" or die "Can't delete $workingdir/$file ($!)";
153 print STDERR "not deleting $workingdir/$file beacuse it doesn't exist\n" if $istest;
156 elsif ($op eq "add" or $op eq "change") {
157 # add/change: re-fetch the file
158 my $content = fetchFile("$remoteroot/$file","$workingdir/$file");
161 die "Unknown operation '$op'";
166 # update the last "up-to-date" time
167 saveLastUpdateTime() unless $istest;
176 # find the last update time
177 # if the file with the update time has disappeared, alert the admin
178 # and use the datestamp on the startpage file ( /en/index.html )
179 sub findLastUpdateTime() {
180 open (UPDATETIME, "<", $lastupdatefile) or return $timenow;
181 my $lastupdatetimestr = <UPDATETIME>;
184 chomp ($lastupdatetimestr);
185 return str2time ($lastupdatetimestr);
188 # convert the date into a correctly formatted string
190 return time2str ("%Y:%m:%dT%T", $_[0]);
193 # convert the date into RFC2616 format
194 sub date2HTTPstr($) {
195 return time2str ("%Y:%m:%dT%T", $_[0]);
198 # write the time now into the last update file
199 sub saveLastUpdateTime() {
200 open (UPDATETIME, ">", $lastupdatefile) or die "Can't open $lastupdatefile for writing ($!)";
201 print UPDATETIME date2ISOstr($timenow);
205 # return an array of filename
206 sub getChangesFileList()
209 for (my $time = str2time(time2str("%Y:%m:%dT00:00:00", $timeoflastupdate));
212 push @files, time2str("changes%Y%m%d.txt", $time);
217 # get the directory part of a filename
220 $dir =~ s{/[^/]*$}{};
224 # get a file, optionally saving it locally
226 my ($remotefile, $localfile) = @_;
229 print STDERR "fetching $remotefile";
230 print STDERR " as $localfile" if $localfile;
233 my $req = HTTP::Request->new(GET => "$remotefile");
234 # TODO: If-Modified-Since
235 my $resp = $ua->request(HTTP::Request->new(GET => "$remotefile"));
236 if ($resp->is_success) {
238 print STDERR " -> success\n" if $istest;
239 my $localdir = dirPart($localfile);
240 if (! -e $localdir) {
241 mkdir $localdir or die "Can't create directory $localdir ($!)";
244 open (LOCAL, ">", "$localfile") or die "Can't open $localfile for writing ($!)";
245 print LOCAL $resp->content or die "Error writing $localfile ($!)";
246 close LOCAL or die "Error writing $localfile ($!)";
247 # TODO: set mtime from Last-Modified
249 return $resp->content;
252 print STDERR " -> failed\n" if $istest;
253 die "Can't fetch $remotefile (".$resp->status_line.")";
258 sub getChangesFile($) {
259 return fetchFile($changesroot."/".$_[0], $changesdir."/".$_[0]);