Subversion Repositories wimsdev

Rev

Rev 971 | Rev 11930 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #!/usr/bin/perl
  2. #use Date::Format;
  3.  
  4. require Time::Local;
  5.  
  6. use vars qw(@DoW @MoY %MoY);
  7. @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
  8. @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  9. @MoY{@MoY} = (1..12);
  10.  
  11. my %GMT_ZONE = (GMT => 1, UTC => 1, UT => 1, Z => 1);
  12.  
  13.  
  14. $wims_ref='http://127.0.0.1/wims';
  15. $file='' ;
  16. push (@ARGV,split(' ', $ENV{'wims_exec_parm'})) if ($ENV{'wims_exec_parm'}) ;
  17.  
  18. while ($_ = shift (@ARGV))
  19. {
  20.   last if (!/^--/);
  21.      if (/^--wims=(.*)$/) { $wims_ref  = $1; }
  22.      elsif (/^--file=(.*)$/) { $file   = $1 ; }
  23.      elsif (/^--out=(.*)$/) { $out     = $1 . "/" ; }
  24.      elsif (/^--type=(.*)$/) { $modif     = $1 ; }
  25. } ;
  26. $wims_ref =~ s,/wims.cgi,, ;
  27. $head= "<\?xml version=\"1.0\" encoding=\"ISO-8859-1\"\?>
  28. <rss version=\"2.0\">
  29.    <channel>
  30.        <title>WIMS</title>
  31.        <link>$wims_ref</link>
  32.        <description>NEWS</description>
  33.        \n"
  34. ;
  35.  
  36. $tail="</channel></rss>";
  37.  
  38. open(IN, "$file.$modif") or die $file ;
  39. my @lignes = <IN>;
  40. my ($cnt, $used)= (1) ;
  41. foreach( reverse( @lignes ) ) {
  42.   my @l=split(":", $_)  ;
  43.   my $date= convdate($l[0]) ;
  44.   my @ta=split(" ", $l[1]) ;
  45.   next if !($l[1]) || ($used =~ /$l[1]/) || ($cnt > 100) ;
  46.   $cnt ++ ;
  47.   $used .= " " . $l[1] ;
  48.   $text .=
  49. "<item>
  50.   <title>$l[1]</title>
  51.   <link>$wims_ref/wims.cgi?module=$ta[0]</link>
  52.   <guid isPermaLink=\"true\">$wims_ref/wims.cgi?module=$ta[0]</guid>
  53.   <description>$ta[0] version $ta[1]</description>
  54.  <pubDate>$date</pubDate>
  55. </item>\n"
  56. }
  57. out("$out$modif.xml" , $head . $text . $tail) ;
  58.  
  59. sub convdate { my ($d)= @_ ;
  60.      $d =~ s/(\d{4,4})(\d{2,2})(\d{2,2})/$1-$2-$3 03:00:00/ ;
  61.      time2str(str2time($d));
  62. }
  63.  
  64. sub out { my ($bloc, $text) = @_ ;
  65.   open  (OUT, ">$bloc") || die $bloc ;
  66.   print OUT $text ; close OUT ;
  67. }
  68.  
  69.  
  70. ########################### from HTML::date.pm
  71. sub time2str (;$)
  72. {
  73.     my $time = shift;
  74.     $time = time unless defined $time;
  75.     my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($time);
  76.     sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
  77.             $DoW[$wday],
  78.             $mday, $MoY[$mon], $year+1900,
  79.             $hour, $min, $sec);
  80. }
  81.  
  82.  
  83. sub str2time ($;$)
  84. {
  85.     my $str = shift;
  86.     return undef unless defined $str;
  87.  
  88.     # fast exit for strictly conforming string
  89.     if ($str =~ /^[SMTWF][a-z][a-z], (\d\d) ([JFMAJSOND][a-z][a-z]) (\d\d\d\d) (\d\d):(\d\d):(\d\d) GMT$/) {
  90.         return eval {
  91.             my $t = Time::Local::timegm($6, $5, $4, $1, $MoY{$2}-1, $3-1900);
  92.             $t < 0 ? undef : $t;
  93.         };
  94.     }
  95.  
  96.     my @d = parse_date($str);
  97.     return undef unless @d;
  98.     $d[0] -= 1900;  # year
  99.     $d[1]--;        # month
  100.  
  101.     my $tz = pop(@d);
  102.     unless (defined $tz) {
  103.         unless (defined($tz = shift)) {
  104.             return eval { my $t = Time::Local::timelocal(reverse @d);
  105.                           $t < 0 ? undef : $t;
  106.                         };
  107.         }
  108.     }
  109.  
  110.     my $offset = 0;
  111.     if ($GMT_ZONE{uc $tz}) {
  112.         # offset already zero
  113.     }
  114.     elsif ($tz =~ /^([-+])?(\d\d?):?(\d\d)?$/) {
  115.         $offset = 3600 * $2;
  116.         $offset += 60 * $3 if $3;
  117.         $offset *= -1 if $1 && $1 eq '-';
  118.     }
  119.     else {
  120.         eval { require Time::Zone } || return undef;
  121.         $offset = Time::Zone::tz_offset($tz);
  122.         return undef unless defined $offset;
  123.     }
  124.  
  125.     return eval { my $t = Time::Local::timegm(reverse @d);
  126.                   $t < 0 ? undef : $t - $offset;
  127.                 };
  128. }
  129.  
  130.  
  131. sub parse_date ($)
  132. {
  133.     local($_) = shift;
  134.     return unless defined;
  135.  
  136.     # More lax parsing below
  137.     s/^\s+//;  # kill leading space
  138.     s/^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*//i; # Useless weekday
  139.  
  140.     my($day, $mon, $yr, $hr, $min, $sec, $tz, $ampm);
  141.  
  142.     # Then we are able to check for most of the formats with this regexp
  143.     (($day,$mon,$yr,$hr,$min,$sec,$tz) =
  144.         /^
  145.          (\d\d?)               # day
  146.             (?:\s+|[-\/])
  147.          (\w+)                 # month
  148.             (?:\s+|[-\/])
  149.          (\d+)                 # year
  150.          (?:
  151.                (?:\s+|:)       # separator before clock
  152.             (\d\d?):(\d\d)     # hour:min
  153.             (?::(\d\d))?       # optional seconds
  154.          )?                    # optional clock
  155.             \s*
  156.          ([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone
  157.             \s*
  158.          (?:\(\w+\))?          # ASCII representation of timezone in parens.
  159.             \s*$
  160.         /x)
  161.  
  162.     ||
  163.  
  164.     # Try the ctime and asctime format
  165.     (($mon, $day, $hr, $min, $sec, $tz, $yr) =
  166.         /^
  167.          (\w{1,3})             # month
  168.             \s+
  169.          (\d\d?)               # day
  170.             \s+
  171.          (\d\d?):(\d\d)        # hour:min
  172.          (?::(\d\d))?          # optional seconds
  173.             \s+
  174.          (?:([A-Za-z]+)\s+)?   # optional timezone
  175.          (\d+)                 # year
  176.             \s*$               # allow trailing whitespace
  177.         /x)
  178.  
  179.     ||
  180.  
  181.     # Then the Unix 'ls -l' date format
  182.     (($mon, $day, $yr, $hr, $min, $sec) =
  183.         /^
  184.          (\w{3})               # month
  185.             \s+
  186.          (\d\d?)               # day
  187.             \s+
  188.          (?:
  189.             (\d\d\d\d) |       # year
  190.             (\d{1,2}):(\d{2})  # hour:min
  191.             (?::(\d\d))?       # optional seconds
  192.          )
  193.          \s*$
  194.        /x)
  195.  
  196.     ||
  197.  
  198.     # ISO 8601 format '1996-02-29 12:00:00 -0100' and variants
  199.     (($yr, $mon, $day, $hr, $min, $sec, $tz) =
  200.         /^
  201.           (\d{4})              # year
  202.              [-\/]?
  203.           (\d\d?)              # numerical month
  204.              [-\/]?
  205.           (\d\d?)              # day
  206.          (?:
  207.                (?:\s+|[-:Tt])  # separator before clock
  208.             (\d\d?):?(\d\d)    # hour:min
  209.             (?::?(\d\d(?:\.\d*)?))?  # optional seconds (and fractional)
  210.          )?                    # optional clock
  211.             \s*
  212.          ([-+]?\d\d?:?(:?\d\d)?
  213.           |Z|z)?               # timezone  (Z is "zero meridian", i.e. GMT)
  214.             \s*$
  215.         /x)
  216.  
  217.     ||
  218.  
  219.     # Windows 'dir' 11-12-96  03:52PM
  220.     (($mon, $day, $yr, $hr, $min, $ampm) =
  221.         /^
  222.           (\d{2})                # numerical month
  223.              -
  224.           (\d{2})                # day
  225.              -
  226.           (\d{2})                # year
  227.              \s+
  228.           (\d\d?):(\d\d)([APap][Mm])  # hour:min AM or PM
  229.              \s*$
  230.         /x)
  231.  
  232.     ||
  233.     return;  # unrecognized format
  234.  
  235.     # Translate month name to number
  236.     $mon = $MoY{$mon} ||
  237.            $MoY{"\u\L$mon"} ||
  238.            ($mon >= 1 && $mon <= 12 && int($mon)) ||
  239.            return;
  240.  
  241.     # If the year is missing, we assume first date before the current,
  242.     # because of the formats we support such dates are mostly present
  243.     # on "ls -l" listings.
  244.     unless (defined $yr) {
  245.         my $cur_mon;
  246.         ($cur_mon, $yr) = (localtime)[4, 5];
  247.         $yr += 1900;
  248.         $cur_mon++;
  249.         $yr-- if $mon > $cur_mon;
  250.     }
  251.     elsif (length($yr) < 3) {
  252.         # Find "obvious" year
  253.         my $cur_yr = (localtime)[5] + 1900;
  254.         my $m = $cur_yr % 100;
  255.         my $tmp = $yr;
  256.         $yr += $cur_yr - $m;
  257.         $m -= $tmp;
  258.         $yr += ($m > 0) ? 100 : -100
  259.             if abs($m) > 50;
  260.     }
  261.  
  262.     # Make sure clock elements are defined
  263.     $hr  = 0 unless defined($hr);
  264.     $min = 0 unless defined($min);
  265.     $sec = 0 unless defined($sec);
  266.  
  267.     # Compensate for AM/PM
  268.     if ($ampm) {
  269.         $ampm = uc $ampm;
  270.         $hr = 0 if $hr == 12 && $ampm eq 'AM';
  271.         $hr += 12 if $ampm eq 'PM' && $hr != 12;
  272.     }
  273.  
  274.     return($yr, $mon, $day, $hr, $min, $sec, $tz)
  275.         if wantarray;
  276.  
  277.     if (defined $tz) {
  278.         $tz = "Z" if $tz =~ /^(GMT|UTC?|[-+]?0+)$/;
  279.     } else {
  280.         $tz = "";
  281.     }
  282.     return sprintf("%04d-%02d-%02d %02d:%02d:%02d%s",
  283.                    $yr, $mon, $day, $hr, $min, $sec, $tz);
  284. }
  285.  
  286.