Jump to content
Wiki Home
EPrints Home
Bazaar
Files
Main menu
Main menu
move to sidebar
hide
Navigation
Front Page
EPrints 3 Manual
EPrints Training Course
EPrints Glossary
Installing EPrints
How-to Configure EPrints
EPrints 3.2+ API
EPrints 3 Reference
EPrints Plugins
Wiki
All categories
Recent changes
Main Page
Help
EPrints Documentation
Search
Search
Appearance
Log in
Request account
Personal tools
Log in
Request account
Pages for logged out editors
learn more
Contributions
Talk
Editing
Contribute: Plugins/ImportPluginsCSV
(section)
Page
Discussion
British English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== convert_input === This method takes data in a particular format, in this case [http://en.wikipedia.org/wiki/Comma-Separated_Values CSV] and transforms it into a hash of metadata field names and values. We take the second argument to the method and convert the array reference into an array. <pre> my @input = @{shift @_}; </pre> Here we setup another Text::CSV object. <pre> my $csv = Text::CSV->new(); </pre> We take the second element of our array and parse it. This is the record we wish to import. If anything goes wrong we return undef. <pre> my @record; if ($csv->parse($input[1])) { @record = $csv->fields(); } else { $plugin->error($csv->error_input); return undef; } </pre> We take the first element and get the field names. We then check that we have the same number of fields names as records. <pre> my @fields = split(',',$input[0]); if (scalar @fields != scalar @record) { $plugin->warning('Row length mismatch'); return undef; } </pre> This is the hash that we'll return later. <pre> my %output = (); </pre> For convenience we get the DataSet object. <pre> my $dataset = $plugin->{session}->{repository}->get_dataset('archive'); </pre> We now iterate over the fields. <pre> my $i = 0; foreach my $field (@fields) { </pre> If the field does not exist we look at the next one, remembering to increment our index. <pre> unless ($dataset->has_field($field)) { $i++; next; } </pre> We get the MetaField object corresponding to the current field. <pre> my $metafield = $dataset->get_field($field); </pre> We deal with multiple field types by separating individual values with a semi-colon. <pre> if ($metafield->get_property('multiple')) { my @values = split(';',$record[$i]); </pre> Name fields are dealt with by using regular expressions and constructing a hash from the parts matched. The plugin expects names to be of the form Surname, Forenames, Lineage (Sr, Jr, III etc). <pre> if ($metafield->{type} eq 'name') { my @names = (); foreach my $value (@values) { my $name = $value; next unless ($value =~ /^(.*?),(.*?)(,(.*?))?$/); push @names, {family => $1,given => $2,lineage => $4}; } $output{$field} = \@names; } </pre> Multiple fields which are not names are just added to the hash as an array reference. <pre> $output{$field} = \@values; </pre> Non-multiple fields are just added to the hash from the array of fields. <pre> $output{$field} = $record[$i]; </pre> Finally we return a hash reference. <pre> return \%output; </pre>
Summary:
Please note that all contributions to EPrints Documentation may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
EPrints Documentation:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)