# fixup_mod_dates # this script reads files in a web and fixes up the modification dates so an order="modified" search will work correctly. # run this script in the data directory of a foswiki installation. # V1.0 -- Raymond Lutz -- http://www.cognisys.com process_a_web ('.'); sub process_a_web { my ($dirname) = @_; # Note: this function is recursive. print "Processing directory '$dirname'\n"; opendir DIRHANDLE, $dirname; my @filelist = readdir DIRHANDLE; closedir DIRHANDLE; my $filename = ''; my $pathname = ''; my @filestat = (); my @newfilestat = (); my ($fs_mtime, $fs_atime, $meta_mtime) = ('', '', ''); foreach $filename (@filelist) { $pathname = $dirname.'/'.$filename; # prepend current directory name next if ($filename =~ /^\./); # skip entries starting with '.' if (-d $pathname) { process_a_web ($pathname); next; } next unless ($filename =~ /\.txt$/); # must end in .txt to be of interest @filestat = stat $pathname; #stat FILE # Returns a 13-element array (0:$dev, 1:$ino, 2:$mode, 3:$nlink, 4:$uid, # 5:$gid, 6:$rdev, 7:$size, 8:$atime, 9:$mtime, 10:$ctime, 11:$blksize, # 12:$blocks). # FILE can be a filehandle, an expression evaluating to a filename, or _ # to refer to the last file test operation or stat call. # Returns a null list if the stat fails. $fs_mtime = $filestat[9]; $fs_atime = $filestat[8]; my $content = readfile ($pathname); # a typical topic header looks like this: #%META:TOPICINFO{author="JoeBlow" date="1190143837" format="1.1" version="1.1"}% $meta_mtime = ''; ($meta_mtime) = $content =~ /date="(\d+)"/; unless ($meta_mtime) { # something is wrong here. Must not be a topic file. print "Skipping $pathname, not recognized\n"; next; } my $str_fs_mtime = localtime ($fs_mtime+0); my $str_meta_mtime = localtime ($meta_mtime+0); if ($fs_mtime+0 == $meta_mtime+0) { print "$pathname date unchanged: $str_fs_mtime\n"; } else { print "Changing $pathname modtime from $str_fs_mtime to $str_meta_mtime -- "; utime $fs_atime,$meta_mtime,$pathname; @newfilestat = stat $pathname; if ($newfilestat[9]+0 == $meta_mtime+0) { print "OK\n"; } else {print "ERROR\n"; } } } } #------------------------------------------------------------------ sub readfile { my ($filename, $noassertopen, $record_separator) = @_; # returns array if called for, else string. Does not convert line endings. #-# my (@content, $content) = (); die "File $filename expected, not found\n" unless ($noassertopen || -e $filename); my $retval = open (READFILEHANDLE, '<'.$filename); die "Open of file '$filename' failed\n" unless ($noassertopen || $retval); if (wantarray) { unless ($retval) { close READFILEHANDLE; return (); } my $tmp = $/; #save record separator $/ = $record_separator|| "\n"; # want array means $record_separator should be defined. binmode READFILEHANDLE; @content = ; close READFILEHANDLE; $/ = $tmp; #recover record separator return @content; } unless ($retval) { close READFILEHANDLE; return (); } my $tmp = $/; #save record separator $/ = $record_separator; #undef means no separator used. binmode READFILEHANDLE; $content = ; close (READFILEHANDLE); $/ = $tmp; #recover record separator return $content; }