Annotation of nsdl/harvestsmete/matti.pl, revision 1.1

1.1     ! www         1: #!/usr/local/bin/perl -w
        !             2: 
        !             3: #
        !             4: # cstc.pl - Use OAI MHP to harvest metadata from CSTS in oai_ims format
        !             5: #
        !             6: # Written by Andy Dong <adong@smete.org> 11/01/2001
        !             7: #
        !             8: 
        !             9: use strict;
        !            10: use Getopt::Std;
        !            11: use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
        !            12: 
        !            13: use HTTP::Request;
        !            14: use LWP::UserAgent;
        !            15: 
        !            16: use XML::Element;
        !            17: use XML::Parser;
        !            18: use XML::TreeBuilder;
        !            19: 
        !            20: use DBI;
        !            21: use DBD::ODBC;
        !            22: 
        !            23: require OAIvocabulary_v2;
        !            24: require OAIcataloging_v2;
        !            25: 
        !            26: # -u flag specifies [u]pdate database; otherwise output to STDOUT
        !            27: 
        !            28: my $usage = << "EOT";
        !            29: Usage: cstc.pl -u
        !            30: 
        !            31:     -u (U)pdate the database
        !            32: 
        !            33:     Without -u it simply prints to STDOUT
        !            34: EOT
        !            35: 
        !            36: my %args;
        !            37: getopts('u', \%args) || die $usage;
        !            38: 
        !            39: my $inserted = 0;
        !            40: my $updated = 0;
        !            41: 
        !            42: my $useDatabase = 1 if ($args{'u'});
        !            43: my $dbh;
        !            44: # Database Configuration
        !            45: if ( $useDatabase ) {
        !            46: 	print "Updating the database\n";
        !            47: 	my $DBI_DSN='dbi:ODBC:mel.odbc';
        !            48: 	my $DBI_USER='autocataloger';
        !            49: 	my $DBI_PWD='regolatacotua';
        !            50: 	$dbh= DBI->connect($DBI_DSN, $DBI_USER, $DBI_PWD, { RaiseError => 1, AutoCommit => 0 }) || die "Unable to connect to database $DBI_DSN as $DBI_USER\n";;
        !            51: }
        !            52: 
        !            53: my $content;
        !            54: my @cstc;
        !            55: 
        !            56: # All possible LOM record variables
        !            57: my $logeneralDescription = "";
        !            58: my $logeneralTitle1 = "";
        !            59: my $logeneralCreation_Date = "";
        !            60: my $logeneralPub_Year = "";
        !            61: my $logeneralPub_Month = "";
        !            62: my $logeneralPub_Day = "";
        !            63: my $logeneralLanguage1 = "";
        !            64: my $logeneralKeywords;
        !            65: my $publisher = "";
        !            66: my $personLastname = "";
        !            67: my $personFirstname = "";
        !            68: my $personEmail = "";
        !            69: my $personCompany = "";
        !            70: my $platformVersion = "";
        !            71: my $platformType = "";
        !            72: my $platformOS = "";
        !            73: my @pedagogyEndUserRole = ();
        !            74: my $pedagogyLContext = "";
        !            75: my $platformFormat = "";
        !            76: my $platformLocation_URL = "";
        !            77: my $role = "";
        !            78: my $pedagogy_description = "Unknown";
        !            79: 
        !            80: #my $url = 'http://www.cstc.org/cgi-bin/OAI/CSTC.pl?verb=ListRecords&metadataPrefix=ims1_1';
        !            81: 
        !            82: #my $ua = new LWP::UserAgent;
        !            83: #my $request = HTTP::Request->new('GET', $url);
        !            84: #my $response = $ua->request( $request );
        !            85: 
        !            86: #if ( $response->is_success ) {
        !            87: #	$content = $response->content;
        !            88: #} else {
        !            89: #	warn 'OAI request failed: ' . $response->message;
        !            90: #	exit 1;
        !            91: #}
        !            92: 
        !            93: my $tree = XML::TreeBuilder->new();
        !            94: $tree->parse_file('matti.xml');
        !            95: 
        !            96: my $t0 = [gettimeofday];
        !            97: 
        !            98: my @records = $tree->find_by_tag_name('record');
        !            99: foreach my $record (@records){
        !           100: 	# Extract information from <metametadata> tag
        !           101: 	my $header = $record->find_by_tag_name('metametadata');
        !           102: 	next if ! $header;
        !           103: 	$logeneralCreation_Date = $header->find_by_tag_name('datetime')->as_text;
        !           104: 	($logeneralPub_Year, $logeneralPub_Month, $logeneralPub_Day) = ($logeneralCreation_Date =~ /^(\d{4})-(\d{2})-(\d{2})$/);
        !           105: 	# Extract information from <general> tag
        !           106: 	my $general = $record->find_by_tag_name('general');
        !           107: 	my $title = $general->find_by_tag_name('title');
        !           108: 	$logeneralTitle1= $title->find_by_tag_name('langstring')->as_text;
        !           109: 	my $description = $general->find_by_tag_name('description');
        !           110: 	$logeneralDescription = $description->find_by_tag_name('langstring')->as_text;
        !           111: 	$logeneralDescription =~ s/\n/ /g;
        !           112: 	$logeneralLanguage1 = $general->find_by_tag_name('language')->as_text;
        !           113: 	my $keywordsElement = $general->find_by_tag_name('keywords');
        !           114: 	my $logeneralKeywords = $keywordsElement->find_by_tag_name('langstring')->as_text;
        !           115: 	# Extract information from <lifecycle> tag
        !           116: 	my $lifecycle = $record->find_by_tag_name('lifecycle');
        !           117: 	my $version = $lifecycle->find_by_tag_name('version');
        !           118: 	$platformVersion = $version->find_by_tag_name('langstring')->as_text;
        !           119: 	my @contributeElement = $lifecycle->find_by_tag_name('contribute');
        !           120: 	foreach my $contribute (@contributeElement) {
        !           121: 		# We will only take the author information
        !           122: 		my $roletype = $contribute->find_by_tag_name('role');
        !           123: 		my $langstring = $roletype->find_by_tag_name('langstring')->as_text;
        !           124: 		if ( $langstring eq "author" ) {
        !           125: 		$role = 'Author';
        !           126: 		my $centity = $contribute->find_by_tag_name('centity');
        !           127: 		my $entity = $centity->find_by_tag_name('vcard')->as_text;
        !           128: 		($personLastname, $personFirstname, $personEmail, $personCompany) = OAIv_parseVcard_matti($entity);
        !           129: 	}
        !           130: 	}
        !           131: 	# Extract information from <technical> tag
        !           132: 	my $technical = $record->find_by_tag_name('technical');
        !           133: 	my $platform = $technical->find_by_tag_name('format');
        !           134: 	$platformFormat = $technical->find_by_tag_name('langstring')->as_text;
        !           135: 	# All MATTI are 1-Generate Automatically from MIME Type
        !           136: 	$platformFormat = 1;
        !           137: 	$platformLocation_URL = $technical->find_by_tag_name('location')->as_text;
        !           138: 	$platformLocation_URL =~ tr/ //d;
        !           139: 	$platformLocation_URL =~ s/^\n(.*)$/$1/;
        !           140: 	chomp($platformLocation_URL);
        !           141: 	# ALL MATTI are Java applets
        !           142: 	$platformType = 64;
        !           143: 	$platformOS = '';
        !           144: 	# Special Java applet tag to go in version.installation_note
        !           145: 	my $installation_note = $technical->find_by_tag_name('applettag')->as_XML();
        !           146: 	# Extract information from <educational> tag
        !           147: 	my $educational = $record->find_by_tag_name('educational');
        !           148: 	# Use Learner (end_user_type = 2)
        !           149: 	my $pedagogyEndUserType = '2';
        !           150: # 	Learning context must be mapped to grade levels
        !           151: 	my $learningcontextElement = $educational->find_by_tag_name('learningcontext');
        !           152: 	my $langstring = $learningcontextElement->find_by_tag_name('langstring')->as_text;
        !           153: 	my @learningcontext = ($langstring =~ /^(.*),\s(.*)$/);
        !           154: 	$pedagogyLContext = OAIv_findLContext(@learningcontext);
        !           155: 
        !           156: 	my $difficulty = $educational->find_by_tag_name('difficulty')->as_text;
        !           157: 	my ($difficulty_id) = ($difficulty =~ /^(\d{1})-\w+$/);
        !           158: 	my @pedagogy_description_element = $educational->find_by_tag_name('description');
        !           159: 	foreach my $p_d_e (@pedagogy_description_element) {
        !           160: 		$pedagogy_description = $p_d_e->find_by_tag_name('langstring')->as_text;
        !           161: 	}
        !           162: 	my $interactivity_level = $educational->find_by_tag_name('interactivitylevel')->as_text;
        !           163: 	my ($interactivity_level_id) = ($interactivity_level =~ /^(\d{1})-\w+$/);
        !           164: 	# Java Applets
        !           165: 	my $resource_type_id = 100;
        !           166: 
        !           167: 
        !           168: if ( $useDatabase ) {
        !           169: 	# Some specific configuration information for MATTI
        !           170: 	# Logo
        !           171: 	my $image = "http://www.smete.org/images/affiliation/matti.gif";
        !           172: 	my $submitter_key = '{710FE693-46E9-4002-BA94-1BE2E6218CD6}'; # Andy Dong
        !           173: 	my $collection = 'National Library of Virtual Manipulatives for Interactive Mathematics';
        !           174: 	my $collection_reg_key = '{8D09C011-23B6-4F23-A690-6C74EFF5E4C7}';
        !           175: 	my $publisher = 'MATTI Associates LLC';
        !           176: 	my $publisher_reg_key = '{120E9D92-6F22-4FEC-A9C0-F95C571174BA}';
        !           177: 	# Determine if this author already exists in the database (person and entity tables)
        !           178: 	my $author_reg_key;
        !           179: 	if ( ! ($author_reg_key = OAIc_personexists($dbh,$personEmail)) ) {
        !           180: 		printf("Inserting person email=%s\n",$personEmail);
        !           181: 		my $success = OAIc_insert_person($dbh,$publisher_reg_key,$submitter_key,$personLastname,$personFirstname,$personEmail,$personCompany);
        !           182: 		$author_reg_key = OAIc_personexists($dbh,$personEmail);
        !           183: 	}
        !           184: 	if ( my $general_key = OAIc_loexists($dbh,$logeneralTitle1) ) {
        !           185: 		my $success = OAIc_update_matti($dbh, $general_key, $installation_note);
        !           186: #		my $success = OAIc_update_lo($dbh, $general_key, $logeneralTitle1, $logeneralLanguage1, $logeneralDescription, $image, $logeneralPub_Month, $logeneralPub_Year, $logeneralKeywords, $submitter_key, join(" ", $personFirstname, $personLastname), $publisher, $collection, $platformFormat, $platformType, $platformOS, $platformLocation_URL, $pedagogyLContext, $pedagogyEndUserType, $author_reg_key, $publisher_reg_key, $collection_reg_key);
        !           187: 		$updated = $updated + 1;
        !           188: 		} else {
        !           189: 		printf("Inserting new record for %s\n",$logeneralTitle1);
        !           190: 		my $success = OAIc_insert_lo($dbh, $logeneralTitle1, $logeneralLanguage1, $logeneralDescription, $image, $logeneralPub_Month, $logeneralPub_Year, $logeneralKeywords, $submitter_key, join(" ", $personFirstname, $personLastname), $publisher, $collection, $platformFormat, $platformType, $platformOS, $platformLocation_URL, $pedagogyLContext, $pedagogyEndUserType, $author_reg_key, $publisher_reg_key, $collection_reg_key, $difficulty_id, $interactivity_level_id, $pedagogy_description, $resource_type_id);
        !           191: 		$inserted = $inserted + 1;
        !           192:         	}
        !           193: 	} else {
        !           194: 		# Print Results
        !           195: 		printf("Title: %s\tDescription: %s\tKeywords: %s\n", $logeneralTitle1,$logeneralDescription, $logeneralKeywords);
        !           196: 		printf("Creation Date: %s\tPublication Year: %4d\tPublication Month: %02d\n", $logeneralCreation_Date, $logeneralPub_Year, $logeneralPub_Month);
        !           197: 		printf("Role: %s\n", $role);
        !           198: 		printf("Firstname: %s\tLastname: %s\tEmail: %s\tOrganization: %s\n", $personFirstname, $personLastname, $personEmail, $personCompany);
        !           199: 		printf("Language: %s\n", $logeneralLanguage1);
        !           200: 		printf("Format: %s\tURL: %s\tPlatform: %s\tOS: %s\n", $platformFormat, $platformLocation_URL,$platformType,$platformOS);
        !           201: 		printf("IntendedEndUserRole: %s\tLearningContext: %s\n", $pedagogyEndUserType, $pedagogyLContext);
        !           202: 		printf("Pedagogy Description: %s\tDifficulty id: %d\tInteractivity Level id: %d\n", $pedagogy_description, $difficulty_id, $interactivity_level_id);
        !           203: 		printf("Installation Note: %s\n", $installation_note);
        !           204: 	}
        !           205: } # end for loop
        !           206: $tree->delete;
        !           207: 
        !           208: if ( $useDatabase ) {
        !           209: 	$dbh->commit;
        !           210: 	$dbh->disconnect();
        !           211: }
        !           212: 
        !           213: printf("Inserted %d records and Updated %d records in %f seconds.\n", $inserted, $updated, tv_interval($t0));
        !           214: 
        !           215: exit 0;

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