version 1.13, 2005/11/29 19:56:42
|
version 1.16, 2007/01/02 12:53:27
|
Line 30 package LONCAPA::lonmetadata;
|
Line 30 package LONCAPA::lonmetadata;
|
|
|
use strict; |
use strict; |
use DBI; |
use DBI; |
|
use HTML::TokeParser; |
|
use vars qw($Metadata_Table_Description $Portfolio_metadata_table_description |
|
$Portfolio_access_table_description $Fulltext_indicies $Portfolio_metadata_indices $Portfolio_access_indices $Portfolio_addedfields_table_description $Portfolio_addedfields_indices); |
|
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
Line 96 TYPE=MYISAM;
|
Line 99 TYPE=MYISAM;
|
|
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
my @Metadata_Table_Description = |
$Metadata_Table_Description = |
( |
[ |
{ name => 'title', type=>'TEXT'}, |
{ name => 'title', type=>'TEXT'}, |
{ name => 'author', type=>'TEXT'}, |
{ name => 'author', type=>'TEXT'}, |
{ name => 'subject', type=>'TEXT'}, |
{ name => 'subject', type=>'TEXT'}, |
Line 144 my @Metadata_Table_Description =
|
Line 147 my @Metadata_Table_Description =
|
{ name => 'depth', type=>'FLOAT'}, |
{ name => 'depth', type=>'FLOAT'}, |
{ name => 'hostname', type=> 'TEXT'}, |
{ name => 'hostname', type=> 'TEXT'}, |
#-------------------------------------------------- |
#-------------------------------------------------- |
); |
]; |
|
|
my @Fulltext_indicies = qw/ |
$Fulltext_indicies = [ qw/ |
title |
title |
author |
author |
subject |
subject |
Line 158 my @Fulltext_indicies = qw/
|
Line 161 my @Fulltext_indicies = qw/
|
mime |
mime |
language |
language |
owner |
owner |
copyright/; |
copyright/ ]; |
|
|
|
###################################################################### |
|
###################################################################### |
|
$Portfolio_metadata_table_description = |
|
[ |
|
{ name => 'title', type=>'TEXT'}, |
|
{ name => 'author', type=>'TEXT'}, |
|
{ name => 'subject', type=>'TEXT'}, |
|
{ name => 'url', type=>'TEXT', restrictions => 'NOT NULL' }, |
|
{ name => 'keywords', type=>'TEXT'}, |
|
{ name => 'version', type=>'TEXT'}, |
|
{ name => 'notes', type=>'TEXT'}, |
|
{ name => 'abstract', type=>'TEXT'}, |
|
{ name => 'mime', type=>'TEXT'}, |
|
{ name => 'language', type=>'TEXT'}, |
|
{ name => 'creationdate', type=>'DATETIME'}, |
|
{ name => 'lastrevisiondate', type=>'DATETIME'}, |
|
{ name => 'owner', type=>'TEXT'}, |
|
{ name => 'copyright', type=>'TEXT'}, |
|
{ name => 'domain', type=>'TEXT'}, |
|
{ name => 'groupname', type=>'TEXT'}, |
|
{ name => 'courserestricted', type=>'TEXT'}, |
|
{ name => 'addedfieldnames', type=>'TEXT'}, |
|
{ name => 'addedfieldvalues', type=>'TEXT'}, |
|
#-------------------------------------------------- |
|
{ name => 'dependencies', type=>'TEXT'}, |
|
{ name => 'modifyinguser', type=>'TEXT'}, |
|
{ name => 'authorspace', type=>'TEXT'}, |
|
{ name => 'lowestgradelevel', type=>'INT'}, |
|
{ name => 'highestgradelevel', type=>'INT'}, |
|
{ name => 'standards', type=>'TEXT'}, |
|
{ name => 'hostname', type=> 'TEXT'}, |
|
#-------------------------------------------------- |
|
]; |
|
|
|
$Portfolio_metadata_indices = [qw/ |
|
title |
|
author |
|
subject |
|
url |
|
keywords |
|
version |
|
notes |
|
abstract |
|
mime |
|
language |
|
owner/]; |
|
|
|
###################################################################### |
|
###################################################################### |
|
|
|
$Portfolio_access_table_description = |
|
[ |
|
{ name => 'url', type=>'TEXT', restrictions => 'NOT NULL' }, |
|
{ name => 'keynum', type=>'TEXT', restrictions => 'NOT NULL' }, |
|
{ name => 'scope', type=>'TEXT'}, |
|
{ name => 'start', type=>'DATETIME'}, |
|
{ name => 'end', type=>'DATETIME'}, |
|
]; |
|
|
|
$Portfolio_access_indices = [qw/ |
|
url |
|
keynum |
|
scope |
|
start |
|
end/]; |
|
|
|
###################################################################### |
|
###################################################################### |
|
|
|
$Portfolio_addedfields_table_description = |
|
[ |
|
{ name => 'url', type=>'TEXT', restrictions => 'NOT NULL' }, |
|
{ name => 'field', type=>'TEXT', restrictions => 'NOT NULL' }, |
|
{ name => 'courserestricted', type=>'TEXT', restrictions => 'NOT NULL' }, |
|
{ name => 'value', type=>'TEXT'}, |
|
]; |
|
|
|
$Portfolio_addedfields_indices = [qw/ |
|
url |
|
field |
|
value |
|
courserestricted/]; |
|
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
|
|
|
|
=pod |
=pod |
|
|
=item &describe_metadata_storage |
=item &describe_metadata_storage |
Line 176 of the metadata table(s).
|
Line 263 of the metadata table(s).
|
|
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
sub describe_metadata_storage { |
sub describe_metadata_storage { |
return (\@Metadata_Table_Description,\@Fulltext_indicies); |
my ($tabletype) = @_; |
|
my %table_description = ( |
|
metadata => $Metadata_Table_Description, |
|
portfolio_metadata => $Portfolio_metadata_table_description, |
|
portfolio_access => $Portfolio_access_table_description, |
|
portfolio_addedfields => $Portfolio_addedfields_table_description, |
|
); |
|
my %index_description = ( |
|
metadata => $Fulltext_indicies, |
|
portfolio_metadata => $Portfolio_metadata_indices, |
|
portfolio_access => $Portfolio_access_indices, |
|
portfolio_addedfields => $Portfolio_addedfields_indices, |
|
); |
|
if ($tabletype eq 'portfolio_search') { |
|
my @portfolio_search_table = @{$table_description{portfolio_metadata}}; |
|
foreach my $item (@{$table_description{portfolio_access}}) { |
|
if (ref($item) eq 'HASH') { |
|
if ($item->{'name'} eq 'url') { |
|
next; |
|
} |
|
} |
|
push(@portfolio_search_table,$item); |
|
} |
|
my @portfolio_search_indices = @{$index_description{portfolio_metadata}}; |
|
push(@portfolio_search_indices,('scope','keynum')); |
|
return (\@portfolio_search_table,\@portfolio_search_indices); |
|
} else { |
|
return ($table_description{$tabletype},$index_description{$tabletype}); |
|
} |
} |
} |
|
|
###################################################################### |
###################################################################### |
Line 197 metadata storage to be initialized.
|
Line 312 metadata storage to be initialized.
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
sub create_metadata_storage { |
sub create_metadata_storage { |
my ($tablename) = @_; |
my ($tablename,$tabletype) = @_; |
$tablename = 'metadata' if (! defined($tablename)); |
$tablename = 'metadata' if (! defined($tablename)); |
|
$tabletype = 'metadata' if (! defined($tabletype)); |
my $request = "CREATE TABLE IF NOT EXISTS ".$tablename." "; |
my $request = "CREATE TABLE IF NOT EXISTS ".$tablename." "; |
# |
# |
# Process the columns (this code is stolen from lonmysql.pm) |
# Process the columns (this code is stolen from lonmysql.pm) |
my @Columns; |
my @Columns; |
my $col_des; # mysql column description |
my $col_des; # mysql column description |
foreach my $coldata (@Metadata_Table_Description) { |
my ($table_columns,$table_indices) = |
|
&describe_metadata_storage($tabletype); |
|
my %coltype; |
|
foreach my $coldata (@{$table_columns}) { |
my $column = $coldata->{'name'}; |
my $column = $coldata->{'name'}; |
|
$coltype{$column} = $coldata->{'type'}; |
$col_des = ''; |
$col_des = ''; |
if (lc($coldata->{'type'}) =~ /(enum|set)/) { # 'enum' or 'set' |
if (lc($coldata->{'type'}) =~ /(enum|set)/) { # 'enum' or 'set' |
$col_des.=$column." ".$coldata->{'type'}."('". |
$col_des.=$column." ".$coldata->{'type'}."('". |
Line 231 sub create_metadata_storage {
|
Line 351 sub create_metadata_storage {
|
# skip blank items. |
# skip blank items. |
push (@Columns,$col_des) if ($col_des ne ''); |
push (@Columns,$col_des) if ($col_des ne ''); |
} |
} |
foreach my $colname (@Fulltext_indicies) { |
foreach my $colname (@{$table_indices}) { |
my $text = 'FULLTEXT idx_'.$colname.' ('.$colname.')'; |
my $text; |
|
if ($coltype{$colname} eq 'TEXT') { |
|
$text = 'FULLTEXT '; |
|
} else { |
|
$text = 'INDEX '; |
|
} |
|
$text .= 'idx_'.$colname.' ('.$colname.')'; |
push (@Columns,$text); |
push (@Columns,$text); |
} |
} |
$request .= "(".join(", ",@Columns).") TYPE=MyISAM"; |
$request .= "(".join(", ",@Columns).") TYPE=MyISAM"; |
Line 246 sub create_metadata_storage {
|
Line 372 sub create_metadata_storage {
|
|
|
=item store_metadata() |
=item store_metadata() |
|
|
Inputs: database handle ($dbh), a table name, and a hash or hash reference |
Inputs: database handle ($dbh), a table name, table type and a hash or hash |
containing the metadata for a single resource. |
reference containing the metadata for a single resource. |
|
|
Returns: 1 on success, 0 on failure to store. |
Returns: 1 on success, 0 on failure to store. |
|
|
Line 263 Returns: 1 on success, 0 on failure to s
|
Line 389 Returns: 1 on success, 0 on failure to s
|
## |
## |
## In most scripts, this will work fine. If the dbi is going to be |
## In most scripts, this will work fine. If the dbi is going to be |
## dropped and (possibly) later recreated, call &clear_sth. Yes it |
## dropped and (possibly) later recreated, call &clear_sth. Yes it |
## is annoying but $sth appearantly does not have a link back to the |
## is annoying but $sth apparently does not have a link back to the |
## $dbh, so we can't check our validity. |
## $dbh, so we can't check our validity. |
## |
## |
my $sth = undef; |
my $sth = undef; |
my $sth_table = undef; |
my $sth_table = undef; |
|
|
sub create_statement_handler { |
sub create_statement_handler { |
my $dbh = shift(); |
my ($dbh,$tablename,$tabletype) = @_; |
my $tablename = shift(); |
|
$tablename = 'metadata' if (! defined($tablename)); |
$tablename = 'metadata' if (! defined($tablename)); |
|
$tabletype = 'metadata' if (! defined($tabletype)); |
|
my ($table_columns,$table_indices) = |
|
&describe_metadata_storage($tabletype); |
$sth_table = $tablename; |
$sth_table = $tablename; |
my $request = 'INSERT INTO '.$tablename.' VALUES('; |
my $request = 'INSERT INTO '.$tablename.' VALUES('; |
foreach (@Metadata_Table_Description) { |
foreach (@{$table_columns}) { |
$request .= '?,'; |
$request .= '?,'; |
} |
} |
chop $request; |
chop $request; |
Line 287 sub create_statement_handler {
|
Line 415 sub create_statement_handler {
|
sub clear_sth { $sth=undef; $sth_table=undef;} |
sub clear_sth { $sth=undef; $sth_table=undef;} |
|
|
sub store_metadata { |
sub store_metadata { |
my ($dbh,$tablename,@Metadata)=@_; |
my ($dbh,$tablename,$tabletype,@Metadata)=@_; |
my $errors = ''; |
my $errors = ''; |
if (! defined($sth) || |
if (! defined($sth) || |
( defined($tablename) && ($sth_table ne $tablename)) || |
( defined($tablename) && ($sth_table ne $tablename)) || |
(! defined($tablename) && $sth_table ne 'metadata')) { |
(! defined($tablename) && $sth_table ne 'metadata')) { |
&create_statement_handler($dbh,$tablename); |
&create_statement_handler($dbh,$tablename,$tabletype); |
} |
} |
my $successcount = 0; |
my $successcount = 0; |
|
if (! defined($tabletype)) { |
|
$tabletype = 'metadata'; |
|
} |
|
my ($table_columns,$table_indices) = |
|
&describe_metadata_storage($tabletype); |
foreach my $mdata (@Metadata) { |
foreach my $mdata (@Metadata) { |
next if (ref($mdata) ne "HASH"); |
next if (ref($mdata) ne "HASH"); |
my @MData; |
my @MData; |
foreach my $field (@Metadata_Table_Description) { |
foreach my $field (@{$table_columns}) { |
my $fname = $field->{'name'}; |
my $fname = $field->{'name'}; |
if (exists($mdata->{$fname}) && |
if (exists($mdata->{$fname}) && |
defined($mdata->{$fname}) && |
defined($mdata->{$fname}) && |
Line 370 sub lookup_metadata {
|
Line 503 sub lookup_metadata {
|
$error = $sth->errstr; |
$error = $sth->errstr; |
} |
} |
} |
} |
} |
} |
return ($error,$returnvalue); |
return ($error,$returnvalue); |
} |
} |
|
|
Line 422 Inputs:
|
Line 555 Inputs:
|
$dbh, database handle |
$dbh, database handle |
$newmetadata, hash reference containing the new metadata |
$newmetadata, hash reference containing the new metadata |
$tablename, metadata table name. Defaults to 'metadata'. |
$tablename, metadata table name. Defaults to 'metadata'. |
|
$tabletype, type of table (metadata, portfolio_metadata, portfolio_access) |
|
|
Returns: |
Returns: |
$error on failure. undef on success. |
$error on failure. undef on success. |
Line 431 $error on failure. undef on success.
|
Line 565 $error on failure. undef on success.
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
sub update_metadata { |
sub update_metadata { |
my ($dbh,$tablename,$newmetadata)=@_; |
my ($dbh,$tablename,$tabletype,$newmetadata)=@_; |
my $error; |
my $error; |
$tablename = 'metadata' if (! defined($tablename)); |
$tablename = 'metadata' if (! defined($tablename)); |
|
$tabletype = 'metadata' if (! defined($tabletype)); |
if (! exists($newmetadata->{'url'})) { |
if (! exists($newmetadata->{'url'})) { |
$error = 'Unable to update: no url specified'; |
$error = 'Unable to update: no url specified'; |
} |
} |
Line 445 sub update_metadata {
|
Line 580 sub update_metadata {
|
' url='.$dbh->quote($newmetadata->{'url'}), |
' url='.$dbh->quote($newmetadata->{'url'}), |
undef,$tablename); |
undef,$tablename); |
return $error if ($error); |
return $error if ($error); |
my %metadata = &LONCAPA::lonmetadata::metadata_col_to_hash(@{$row->[0]}); |
my %metadata = &LONCAPA::lonmetadata::metadata_col_to_hash($tabletype,@{$row->[0]}); |
# |
# |
# Update metadata values |
# Update metadata values |
while (my ($key,$value) = each(%$newmetadata)) { |
while (my ($key,$value) = each(%$newmetadata)) { |
Line 458 sub update_metadata {
|
Line 593 sub update_metadata {
|
# |
# |
# Store updated metadata |
# Store updated metadata |
my $success; |
my $success; |
($success,$error) = &store_metadata($dbh,$tablename,\%metadata); |
($success,$error) = &store_metadata($dbh,$tablename,$tabletype,\%metadata); |
return $error; |
return $error; |
} |
} |
|
|
Line 479 passed in as values
|
Line 614 passed in as values
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
sub metadata_col_to_hash { |
sub metadata_col_to_hash { |
my @cols=@_; |
my ($tabletype,@cols)=@_; |
my %hash=(); |
my %hash=(); |
for (my $i=0; $i<=$#Metadata_Table_Description;$i++) { |
my ($columns,$indices) = &describe_metadata_storage($tabletype); |
$hash{$Metadata_Table_Description[$i]->{'name'}}=$cols[$i]; |
for (my $i=0; $i<@{$columns};$i++) { |
unless ($hash{$Metadata_Table_Description[$i]->{'name'}}) { |
$hash{$columns->[$i]->{'name'}}=$cols[$i]; |
if ($Metadata_Table_Description[$i]->{'type'} eq 'TEXT') { |
unless ($hash{$columns->[$i]->{'name'}}) { |
$hash{$Metadata_Table_Description[$i]->{'name'}}=''; |
if ($columns->[$i]->{'type'} eq 'TEXT') { |
} elsif ($Metadata_Table_Description[$i]->{'type'} eq 'DATETIME') { |
$hash{$columns->[$i]->{'name'}}=''; |
$hash{$Metadata_Table_Description[$i]->{'name'}}='0000-00-00 00:00:00'; |
} elsif ($columns->[$i]->{'type'} eq 'DATETIME') { |
|
$hash{$columns->[$i]->{'name'}}='0000-00-00 00:00:00'; |
} else { |
} else { |
$hash{$Metadata_Table_Description[$i]->{'name'}}=0; |
$hash{$columns->[$i]->{'name'}}=0; |
} |
} |
} |
} |
} |
} |
Line 793 sub dynamic_metadata_storage {
|
Line 929 sub dynamic_metadata_storage {
|
return %Store; |
return %Store; |
} |
} |
|
|
|
############################################################### |
|
############################################################### |
|
### ### |
|
### &portfolio_metadata($filepath,$dom,$uname,$group) ### |
|
### Retrieve metadata for the given file ### |
|
### Returns array - ### |
|
### contains reference to metadatahash and ### |
|
### optional reference to addedfields hash ### |
|
### ### |
|
############################################################### |
|
############################################################### |
|
|
|
sub portfolio_metadata { |
|
my ($fullpath,$dom,$uname,$group)=@_; |
|
my ($mime) = ( $fullpath=~/\.(\w+)$/ ); |
|
my %metacache=(); |
|
if ($fullpath !~ /\.meta$/) { |
|
$fullpath .= '.meta'; |
|
} |
|
my (@standard_fields,%addedfields); |
|
my $colsref = $Portfolio_metadata_table_description; |
|
if (ref($colsref) eq 'ARRAY') { |
|
my @columns = @{$colsref}; |
|
foreach my $coldata (@columns) { |
|
push(@standard_fields,$coldata->{'name'}); |
|
} |
|
} |
|
my $metastring=&getfile($fullpath); |
|
if (! defined($metastring)) { |
|
$metacache{'keys'}= 'owner,domain,mime'; |
|
$metacache{'owner'} = $uname.':'.$dom; |
|
$metacache{'domain'} = $dom; |
|
$metacache{'mime'} = $mime; |
|
if ($group ne '') { |
|
$metacache{'keys'} .= ',courserestricted'; |
|
$metacache{'courserestricted'} = 'course.'.$dom.'_'.$uname; |
|
} |
|
} else { |
|
my $parser=HTML::TokeParser->new(\$metastring); |
|
my $token; |
|
while ($token=$parser->get_token) { |
|
if ($token->[0] eq 'S') { |
|
my $entry=$token->[1]; |
|
if ($metacache{'keys'}) { |
|
$metacache{'keys'}.=','.$entry; |
|
} else { |
|
$metacache{'keys'}=$entry; |
|
} |
|
my $value = $parser->get_text('/'.$entry); |
|
if (!grep(/^\Q$entry\E$/,@standard_fields)) { |
|
my $clean_value = lc($value); |
|
$clean_value =~ s/\s/_/g; |
|
if ($clean_value ne $entry) { |
|
if (defined($addedfields{$entry})) { |
|
$addedfields{$entry} .=','.$value; |
|
} else { |
|
$addedfields{$entry} = $value; |
|
} |
|
} |
|
} else { |
|
$metacache{$entry} = $value; |
|
} |
|
} |
|
} # End of ($token->[0] eq 'S') |
|
} |
|
if (keys(%addedfields) > 0) { |
|
foreach my $key (sort keys(%addedfields)) { |
|
$metacache{'addedfieldnames'} .= $key.','; |
|
$metacache{'addedfieldvalues'} .= $addedfields{$key}.'&&&'; |
|
} |
|
$metacache{'addedfieldnames'} =~ s/,$//; |
|
$metacache{'addedfieldvalues'} =~ s/\&\&\&$//; |
|
if ($metacache{'keys'}) { |
|
$metacache{'keys'}.=',addedfieldnames'; |
|
} else { |
|
$metacache{'keys'}='addedfieldnames'; |
|
} |
|
$metacache{'keys'}.=',addedfieldvalues'; |
|
} |
|
return (\%metacache,$metacache{'courserestricted'},\%addedfields); |
|
} |
|
|
|
sub process_portfolio_access_data { |
|
my ($dbh,$simulate,$newnames,$url,$fullpath,$access_hash,$caller) = @_; |
|
my %loghash; |
|
if ($caller eq 'update') { |
|
# Delete old data (no error if deleting non-existent record). |
|
my $error=&delete_metadata($dbh,$newnames->{'access'},$url); |
|
if (defined($error)) { |
|
$loghash{'access'}{'err'} = "MySQL Error Delete: ".$error; |
|
return %loghash; |
|
} |
|
} |
|
# Check the file exists |
|
if (-e $fullpath) { |
|
foreach my $key (keys(%{$access_hash})) { |
|
my $acc_data; |
|
$acc_data->{url} = $url; |
|
$acc_data->{keynum} = $key; |
|
my ($num,$scope,$end,$start) = |
|
($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/); |
|
next if (($scope ne 'public') && ($scope ne 'guest')); |
|
$acc_data->{scope} = $scope; |
|
if ($end != 0) { |
|
$acc_data->{end} = &sqltime($end); |
|
} |
|
$acc_data->{start} = &sqltime($start); |
|
if (! $simulate) { |
|
my ($count,$err) = |
|
&store_metadata($dbh,$newnames->{'access'}, |
|
'portfolio_access',$acc_data); |
|
if ($err) { |
|
$loghash{$key}{'err'} = "MySQL Error Insert: ".$err; |
|
} |
|
if ($count < 1) { |
|
$loghash{$key}{'count'} = |
|
"Unable to insert record into MySQL database for $url"; |
|
} |
|
} |
|
} |
|
} |
|
return %loghash; |
|
} |
|
|
|
sub process_portfolio_metadata { |
|
my ($dbh,$simulate,$newnames,$url,$fullpath,$is_course,$dom,$uname,$group,$caller) = @_; |
|
my %loghash; |
|
if ($caller eq 'update') { |
|
# Delete old data (no error if deleting non-existent record). |
|
my $error=&delete_metadata($dbh,$newnames->{'portfolio'},$url); |
|
if (defined($error)) { |
|
$loghash{'metadata'}{'err'} = "MySQL Error delete metadata: ". |
|
$error; |
|
return %loghash; |
|
} |
|
$error=&delete_metadata($dbh,$newnames->{'addedfields'},$url); |
|
if (defined($error)) { |
|
$loghash{'addedfields'}{'err'}="MySQL Error delete addedfields: ".$error; |
|
} |
|
} |
|
# Check the file exists. |
|
if (-e $fullpath) { |
|
my ($ref,$crs,$addedfields) = &portfolio_metadata($fullpath,$dom,$uname, |
|
$group); |
|
&getfiledates($ref,$fullpath); |
|
if ($is_course) { |
|
$ref->{'groupname'} = $group; |
|
} |
|
my %Data; |
|
if (ref($ref) eq 'HASH') { |
|
%Data = %{$ref}; |
|
} |
|
%Data = ( |
|
%Data, |
|
'url'=>$url, |
|
'version'=>'current', |
|
); |
|
my %loghash; |
|
if (! $simulate) { |
|
my ($count,$err) = |
|
&store_metadata($dbh,$newnames->{'portfolio'},'portfolio_metadata', |
|
\%Data); |
|
if ($err) { |
|
$loghash{'metadata'."\0"}{'err'} = "MySQL Error Insert: ".$err; |
|
} |
|
if ($count < 1) { |
|
$loghash{'metadata'."\0"}{'count'} = "Unable to insert record into MySQL portfolio_metadata database table for $url"; |
|
} |
|
if (ref($addedfields) eq 'HASH') { |
|
if (keys(%{$addedfields}) > 0) { |
|
foreach my $key (keys(%{$addedfields})) { |
|
my $added_data = { |
|
'url' => $url, |
|
'field' => $key, |
|
'value' => $addedfields->{$key}, |
|
'courserestricted' => $crs, |
|
}; |
|
my ($count,$err) = |
|
&store_metadata($dbh,$newnames->{'addedfields'}, |
|
'portfolio_addedfields',$added_data); |
|
if ($err) { |
|
$loghash{$key}{'err'} = |
|
"MySQL Error Insert: ".$err; |
|
} |
|
if ($count < 1) { |
|
$loghash{$key}{'count'} = "Unable to insert record into MySQL portfolio_addedfields database table for url = $url and field = $key"; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
return %loghash; |
|
} |
|
|
|
###################################################################### |
|
###################################################################### |
|
|
|
## Utilities originally in searchcat.pl. Moved to be more widely available. |
|
|
|
sub getfile { |
|
my $file = shift(); |
|
if (! -e $file ) { |
|
return undef; |
|
} |
|
my $fh=IO::File->new($file); |
|
my $contents = ''; |
|
while (<$fh>) { |
|
$contents .= $_; |
|
} |
|
return $contents; |
|
} |
|
|
|
## |
|
## &getfiledates() |
|
## Converts creationdate and modifieddates to SQL format |
|
## Applies stat() to file to retrieve dates if missing |
|
sub getfiledates { |
|
my ($ref,$target) = @_; |
|
if (! defined($ref->{'creationdate'}) || |
|
$ref->{'creationdate'} =~ /^\s*$/) { |
|
$ref->{'creationdate'} = (stat($target))[9]; |
|
} |
|
if (! defined($ref->{'lastrevisiondate'}) || |
|
$ref->{'lastrevisiondate'} =~ /^\s*$/) { |
|
$ref->{'lastrevisiondate'} = (stat($target))[9]; |
|
} |
|
$ref->{'creationdate'} = &sqltime($ref->{'creationdate'}); |
|
$ref->{'lastrevisiondate'} = &sqltime($ref->{'lastrevisiondate'}); |
|
} |
|
|
|
## |
|
## &sqltime($timestamp) |
|
## |
|
## Convert perl $timestamp to MySQL time. MySQL expects YYYY-MM-DD HH:MM:SS |
|
## |
|
sub sqltime { |
|
my ($time) = @_; |
|
my $mysqltime; |
|
if ($time =~ |
|
/(\d+)-(\d+)-(\d+) # YYYY-MM-DD |
|
\s # a space |
|
(\d+):(\d+):(\d+) # HH:MM::SS |
|
/x ) { |
|
# Some of the .meta files have the time in mysql |
|
# format already, so just make sure they are 0 padded and |
|
# pass them back. |
|
$mysqltime = sprintf('%04d-%02d-%02d %02d:%02d:%02d', |
|
$1,$2,$3,$4,$5,$6); |
|
} elsif ($time =~ /^\d+$/) { |
|
my @TimeData = gmtime($time); |
|
# Alter the month to be 1-12 instead of 0-11 |
|
$TimeData[4]++; |
|
# Alter the year to be from 0 instead of from 1900 |
|
$TimeData[5]+=1900; |
|
$mysqltime = sprintf('%04d-%02d-%02d %02d:%02d:%02d', |
|
@TimeData[5,4,3,2,1,0]); |
|
} elsif (! defined($time) || $time == 0) { |
|
$mysqltime = 0; |
|
} else { |
|
&log(0," sqltime:Unable to decode time ".$time); |
|
$mysqltime = 0; |
|
} |
|
return $mysqltime; |
|
} |
|
|
###################################################################### |
###################################################################### |
###################################################################### |
###################################################################### |
## |
## |