#!/usr/bin/perl use DBI; # getPurls # jody DeRidder, 4/7/10 # reads in a plain text file containing one valid identifier per line # spits out a plain text file which adds a purl for each identifier on the same line ## Copyright (c) 2010, The University of Alabama Libraries. ## Contributed by Jody DeRidder, 7/30/10. ## All rights reserved. ## Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ## * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. ## * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in ## the documentation and/or other materials provided with the distribution. ## * Neither the name of The University of Alabama Libraries nor the names of its contributors may be used to endorse or promote products ## derived from this software without specific prior written permission. ##THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ##THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ##CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ##PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ##LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, ##EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ($in, $out) = @ARGV; #"./idList.txt"; if (!($in && $out)){ print "\nPlease type names of infile and outfile after command, then hit enter\n\n"; exit;} $dbuser = "user"; $dbpass = "password*"; print "\nreading in file $in and printing results to $out\n\n"; $out = ">./".$out; #exit; #$dbh = DBI->connect ("dbi:mysql:InfoTrack:libcontent1.lib.ua.edu:3306", $dbuser, $dbpass) # or die "can't connect from encompass to mysql on libcontent1 --InfoTrack: ".$dbh->error(); $dbh = DBI->connect ("dbi:mysql:InfoTrack",$dbuser, $dbpass) or die "can't connect to mysql InfoTrack: ".$dbh->error(); $h->{PrintError} = 1; $h->{RaiserError} = 1; # lock the table so we don't get interrupted $sth = $dbh->prepare("lock tables lookup write") or die "Error locking: ".$dbh->errstr(); $sth->execute or die ("can\'t lock table lookup : ".$dbh->errstr()); $sth->finish(); # get the last purlnum used; will increment this for each new item $sth = $dbh->prepare("select max(purlnum) from lookup"); $sth->execute or die ("can\'t select max purlnum from lookup: ".$dbh->errstr()); $last = $sth->fetchrow_array(); $sth->finish(); print "the last purlnum is $last\n"; open (IN, $in) or die "can't open $in\n"; open (OUT, $out) or die "can't open $out\n"; while ($line = ){ undef $purlnum; chomp ($line); # remove newline if ($line =~ /u0015/){ die "ERROR! Cannot use this script on born digital content!!\n";} elsif ($line =~ /([a-z]{1}\d{4}\_\d{7}(\_\d{7}(\_\d{4})?)?)/){ # print "$line:\n #1 is $1\n #2 is $2\n #3 is $3\n\n"; $id = $1; $myid = $dbh->quote($id); # check to see if we have a purl for this yet $sth = $dbh->prepare("select purlnum, realurl, datestamp from lookup where id_2009 = $myid"); $sth->execute or die ("can\'t look for $id in lookup: ".$dbh->errstr()); ($purlnum, $real, $date) = $sth->fetchrow_array(); $sth->finish(); if ($purlnum){ print "WARNING: $id already has a purl: \nhttp://purl.lib.ua.edu/$purlnum\n assigned $date for $real\n\n"; $purl = "http://purl.lib.ua.edu/$purlnum"; print OUT "$id\t$purl\n"; } else{ # if not in there, get a new one $last ++; # increment... this will be the new purlnum $purl = "http://purl.lib.ua.edu/$last"; $purlnum = $dbh->quote($last); $realurl = $dbh->quote("http://acumen.lib.ua.edu/$id"); $sth = $dbh->prepare("insert into lookup (dnum, id_2009, purlnum, realurl) values (NULL, $myid,$purlnum,$realurl)"); $sth->execute or die ("can\'t insert $id in lookup: ".$dbh->errstr()); $sth->finish(); print OUT "$id\t$purl\n"; } } else{ print "ERROR: $line does not contain a valid identifier\nNo Purl assigned\n\n"; print OUT $line."\t\n"; } } close(IN); close(OUT); $sth = $dbh->prepare("unlock tables "); $sth->execute or die ("can't unlock table lookup : ".$dbh->errstr()); $sth->finish(); $dbh->disconnect();