File:  [Local Repository] / db / prgsrc / validate.pl
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Mon Mar 7 02:52:12 2005 UTC (19 years ago) by boris
Branches: MAIN
CVS tags: HEAD
wrote

#!/usr/local/bin/perl 

=head1 NAME

validate.pl - a script to validate files for db

=head1 SYNOPSIS

validate.pl  I<file1> I<file2>....


=head1 DESCRIPTION

Validates files

=head1 OPTIONS

=item B<-i> 

=head1 AUTHOR

Boris Veytsman, based on a script by Dmitry Rubinstein

=head1 $Id: validate.pl,v 1.2 2005/03/07 02:52:12 boris Exp $

=cut


my (%RevMonths) = 
    ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6',
     'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11',
     'Dec', '12', 
     'JAN', '1', 'FEB', '2', 'MAR', '3', 'APR', '4', 'MAY', '5', 'JUN', '6',
     'JUL', '7', 'AUG', '8', 'SEP', '9', 'OCT', '10', 'NOV', '11',
     'DEC', '12', 
     'Янв', '1', 'Фев', '2', 'Мар', '3', 'Апр', '4', 'Май', '5',
     'Июн', '6', 'Июл', '7', 'Авг', '8', 'Сен', '9', 
     'Окт', '10', 'Ноя', '11', 'Дек', '12');
my ($sth);





use strict;


sub getField {
    my($desc) = @_;
    my($key);
    my($value) = ('');
    while (<$desc>) {
	s/[
]//g;
	if ($key && /^\s*$/) {
	    chomp $value;
            $value =~ s/\s+$//;
	    chomp $key;
	    if ($key eq 'Дата') {
		$value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
		my($month) = $RevMonths{$2} || '01';
		$value =~ s/-(.*)-/-$month-/;
		$value =~ s/-00*$/-01/;
	    }
	    if ($key eq 'Автор') {$value=~s/\.$//;}
	    return ($key, $value);
	}
	next if (/^\s*$/);
	
	if (!$key && /^(.*?)[:\.]\s*(.*)$/s) {
	    $key = $1;
	    $value=$2;
	    next;
	}
	if ($key) {
	    $value .= $_;
	    next;
	}
    }
    if ($key && $value) {
        $value=~s/\s+$//;
	return ($key, $value);
    }
    return (0, 0);
}

		

MAIN: 
{
    my($key, $value, $addition);
    #
    # Inherited fields for a Tour or Tournament
    #
    my %TourFields = ('Копирайт' => 'Copyright',
		      'Инфо' => 'Info', 'URL' => 'URL',
		      'Ссылка' => 'URL', 'Редактор' => 'Editors',
		      'Обработан'=> 'EnteredBy',
		      'Дата'=>'PlayedAt');
    #
    # Inherited fields for a Question
    #
    my %QuestionFields = ('Тип'=> 'Type', 'Вид'=> 'Type', 
			  'Автор' => 'Authors', 'Рейтинг'=>'Rating', 
			  'Источник' => 'Sources',
			  'Тема' => 'Topic');
			  
		      
    my($source);
    
    my @sources;	
    while ($source = shift) {
       push @sources,glob($source);
    }
    foreach $source(@sources) {
	my $TourNum=0;
	my($PlayedAt) = '';
	my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
	my($tournum, $qnum, $all_qnum) = (0, 0, 0);
	my (@d) = (localtime((stat($source))[9]))[5,4,3];
	$d[1]++;
	$d[1]=sprintf("%02d",$d[1]);
	$d[2]=sprintf("%02d",$d[2]);
	$d[0]+=1900;
	my $CreatedAt=join('-', @d);

	open INFD, $source 
	    or die "Can't open input file: $!\n";
	
	$source =~ s/^.*\/([^\/]*)$/$1/;
	print STDERR "Файл: $source, дата: $CreatedAt ";
	while (($key, $value) = getField(\*INFD)) {
	    last if (!$key);
	    
	    if ($key =~ /Мета/) {
		next;   # This is obsolete
	    }
	    if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {		
		next;
	    }
	    if ($key =~ /Тур/) {
		$qnum = 0;
		$TourNum++;
		$QuestionId=0;
		next;	
	    }
	    if ($key =~ /Вопрос/) {
		$qnum++;
		$all_qnum++;
		next;
	    }

	    if ($key =~ /Ответ/) {
		next;
	    }


	    if ($key =~ /Зач[её]т/) {
		next;
	    }



	    if ($key =~ /Комментари/) {
		next;
	    }


	    
	    my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;

	    if (scalar @Fields) {
		next;
	    }

	    @Fields = grep { $key =~ /$_/ } keys %TourFields;

	    if (scalar @Fields) {
		next;
	    }


	    
	    #
	    # If we are here, something got wrong!
	    #
	    print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
	    
	}
	print STDERR "Всего вопросов: $all_qnum \n";
    }
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>