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!
=== input_fh === This method takes a filehandle, processes it, tries to import DataObjs in to the repository and then returns a List of the DataObjs imported. This array will be used to create a List of DataObjs later. <pre> my @ids; </pre> Here we open the filehandle passed, and read the lines into an array. <pre> my $fh = $opts{fh}; my @records = <$fh>; </pre> We create a Text::CSV object to handle the input. Using a dedicated [http://en.wikipedia.org/wiki/Comma-Separated_Values CSV] handling package is preferable to using Perl's split function as it handles a number of more complicated scenarios such as commas within records using double quotes. <pre> my $csv = Text::CSV->new(); </pre> After setting up an array for metadata field names, we attempt to parse the first line of our file. The parse method does not return an array of fields, but reports success or failure. In the event of success we use the fields method to return the last fields parsed. In the event of failure we use the error_input method to get the last error, and return undef. <pre> my @fields; if ($csv->parse(shift @records)) { @fields = $csv->fields(); } else { $plugin->error($csv->error_input); return undef; } </pre> Now that the row of column titles has been dealt with we move onto processing each record in the file. In import plugins the convert_input method converts individual records into a format that can be imported into the repository. That is a hash whose keys are metadata field names and values are the corresponding values. As a row on its own cannot be imported as we don't know to which field each value belongs we have to construct an array to pass to convert_input first. We pass an array whose first element is the fields row and whose second element is the row we want to import. <pre> foreach my $row (@records) { my @input_data = (join(',',@fields),$row); </pre> Here we call convert_input on our constructed input_data. If the conversion fails we simply move to the next record. <pre> my $epdata = $plugin->convert_input(\@input_data); next unless defined $epdata; </pre> The epdata_to_dataobj method takes our epdata hash reference and turns it into a new DataObj in our repository. If it is successful it returns the new DataObj, whose id we add to our array of ids. <pre> my $dataobj = $plugin->epdata_to_dataobj($opts{dataset},$epdata); if( defined $dataobj ) { push @ids, $dataobj->get_id; } </pre> Finally we return a List object containing the ids of the records we have successfully imported. <pre> return EPrints::List->new( dataset => $opts{dataset}, session => $plugin->{session}, ids=>\@ids ); </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)