File:  [Local Repository] / db / prgsrc / updateindex.pl
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Thu Oct 19 03:05:18 2000 UTC (23 years, 6 months ago) by boris
Branches: MAIN
CVS tags: HEAD
Wrote a working version.

#!/usr/local/bin/perl -w

=head1 NAME

updateindex.pl - a script for creation of new database. 

=head1 SYNOPSIS

updateind.pl [B<-i> I<indexfile>]


=head1 DESCRIPTION

Upadets metainformation in the B<chgk> databse. Uses file
    L<./index> unless [B<-i>] option is used

An example of the index file follows:

		 Авторские вопросы
			 Виктор Байрак
 bayrak.txt  			Вопросы В.Байрака
			 Борис Бурда
 burda.txt   			Вопросы Бориса Бурды
 burda1.txt  			Тренировки Бориса Бурды 1
 burda10.txt 			Тренировки Бориса Бурды 10
 burda11.txt 			Тренировки Бориса Бурды 11
 burda12.txt 			Тренировки Бориса Бурды 12


=head1 BUGS

The database, user and password are hardcoded. 

=head1 SEE ALSO

createindex.pl(1)

=head1 AUTHOR

Boris Veytsman

=head1 $Id: updateindex.pl,v 1.5 2000/10/19 03:05:18 boris Exp $

=cut

    use strict;
use vars qw($opt_i $opt_h);

use Getopt::Std;
use DBI;

MAIN: 
{
    my $USAGE="Usage: updateindex.pl -i indexfile\n";
    getopts('hi:') or die $USAGE;
    if ($opt_h) {
	print $USAGE;
	exit 0;
    }
    my($source) = $opt_i || 'index';
    my($depth, @depthId);
    my $filename;
    my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") 
	or die "Can't connect to DB chgk\n";

    open INFD, $source or die "Can't open input file: $!\n";
    while (<INFD>) {
	chomp;
	s/
//;
	next if (/^\s*$/);
	if (s/^(\S+) *//) { # File found
	    $filename = $1;
	    $depth = -1;
	} else {  # Group found
	    $filename = '';
	    $depth = -2;
	}
	s/^(\t*)//;
	$depth += length($1);
	if ($depth < 0) {
	    die "Wrong line $_\n";
	}
	s/^\s*//;
	s/\s$//;
	my $title = $_;
	my $ParentId = ($depth) ? $depthId[$depth - 1] : 0;
	my $Id = CheckId($dbh,$title,$ParentId,$filename);
	if (!$Id  || $filename) {
	    next;
	}
	$depthId[$depth] = $Id;

    }
    $dbh->disconnect;
}


sub CheckId {
    my ($dbh,$title,$ParentId,$filename) = @_;
    my $type;
    my $key;
    my $value;
    my $Id = 0;
    if ($filename) {
	$type=$dbh->quote('Ч');
	$key = "FileName";
	$value = $dbh->quote($filename);
    } else {
	$type=$dbh->quote('Г');
	$key = "Title";
	$value = $dbh->quote($title);
    }
    $title=$dbh->quote($title);    
    my $sth = $dbh->prepare("SELECT Id FROM Tournaments 
                             WHERE $key=$value");
    $sth->execute or die $dbh->errstr;
    my @arr = $sth->fetchrow;
    if (scalar @arr) {
	print "$value is already in the DB!\n";
	print "Заменить новым значением? [y/N]\n";
	my $answer = <STDIN>;
	if ($answer !~ /^[yY]/) {
	    return 0;
	} else {
	    $Id = $arr[0];
	} 
    }
    if ($Id) {
	$sth = $dbh->prepare("UPDATE Tournaments
                             SET Title=$title, ParentId=$ParentId, 
                             Type=$type
                             WHERE Id=$Id");

    } else {
	$sth = $dbh->prepare("INSERT INTO Tournaments
                             (Title, ParentId, Type) 
                             VALUES
                             ($title, $ParentId,$type)");
    }
    $sth->execute or die $dbh->errstr;
    if (!$Id) {
	$Id = $sth->{'mysql_insertid'};
    }
    if ($filename) {
	$filename=$dbh->quote($filename);
	$sth = $dbh->prepare("UPDATE Tournaments
                             SET FileName=$filename
                             WHERE Id=$Id");
	$sth->execute or die $dbh->errstr;
    }	
    return $Id;
}

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