[Bug 44086] New: Koha::Exporter::Record::export does not restore the previously selected filehandle when output_filepath is used
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44086 Bug ID: 44086 Summary: Koha::Exporter::Record::export does not restore the previously selected filehandle when output_filepath is used Initiative type: --- Sponsorship --- status: Product: Koha Version: Main Hardware: All OS: All Status: NEW Severity: normal Priority: P5 - low Component: Architecture, internals, and plumbing Assignee: koha-bugs@lists.koha-community.org Reporter: adolfo.rodriguez@xercode.es QA Contact: testopia@bugs.koha-community.org Target Milestone: --- When Koha::Exporter::Record::export() is called with output_filepath, it opens the file and selects it (select $fh), so that the internal prints without an explicit filehandle go to the file. At the end the file is closed, but the previously selected filehandle (usually STDOUT) is never restored. After the call, the selected filehandle is still the closed $fh, so any later print without an explicit filehandle in the calling code fails with "print() on closed filehandle $fh" and its output is lost. The exported file itself is fine. The same happens when export() dies because the csv profile is not valid: the check runs after the file has been selected (and created). Core callers are not affected by chance: tools/export.pl prints to STDOUT, and misc/export_records.pl redirects STDOUT (open STDOUT, '>', $filename) instead of passing output_filepath. Any script or plugin that passes output_filepath and prints something afterwards (a progress or summary message, for instance) is affected. To reproduce, from the Koha root (e.g. in koha-shell): perl -we 'use Koha::Exporter::Record; Koha::Exporter::Record::export({ record_type => "bibs", record_ids => [1], format => "xml", output_filepath => "/tmp/export.xml" }); print "Export finished\n";' => print() on closed filehandle $fh at -e line 1. => "Export finished" is never printed. -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44086 Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> changed: What |Removed |Added ---------------------------------------------------------------------------- Assignee|koha-bugs@lists.koha-commun |adolfo.rodriguez@xercode.es |ity.org | Status|NEW |Needs Signoff -- You are receiving this mail because: You are watching all bug changes. You are the assignee for the bug.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44086 --- Comment #1 from Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> --- Created attachment 211530 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211530&action=edit Bug 44086: (follow-up) Document output_filepath and the internal functions The POD of export() did not mention output_filepath and said the generated file was always displayed on STDOUT. Document the parameter, including that the previously selected filehandle is restored. Also add POD for the internal _get_*_for_export functions, so the module passes the POD coverage check, and close the =over list before =cut. To test: 1. podchecker Koha/Exporter/Record.pm 2. perldoc Koha/Exporter/Record.pm and read the new sections. -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44086 Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #211530|0 |1 is obsolete| | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44086 --- Comment #2 from Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> --- Created attachment 211531 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211531&action=edit Bug 44086: Restore the previously selected filehandle in Koha::Exporter::Record::export When output_filepath is passed, export() opens the file and selects it, so that the internal prints without an explicit filehandle go to the file. The file is closed at the end, but the previously selected filehandle (usually STDOUT) is never restored. Any later print without a filehandle in the calling code then fails with "print() on closed filehandle $fh", and its output is lost. The same happened when export() died because of an invalid csv profile: the check ran after the file had been selected, so the selected filehandle stayed on the export file. This patch saves the selected filehandle before selecting the output file and restores it after closing it. The csv profile check is moved before opening the file, so an invalid profile no longer changes the selected filehandle or leaves an empty file behind. Core callers are not affected today: tools/export.pl prints to STDOUT and misc/export_records.pl redirects STDOUT instead of passing output_filepath. Any script or plugin that passes output_filepath and prints anything afterwards is. Steps to follow: 1. Run this from the Koha root (in KTD, as the koha user, e.g. inside koha-shell): perl -we 'use Koha::Exporter::Record; Koha::Exporter::Record::export({ record_type => "bibs", record_ids => [1], format => "xml", output_filepath => "/tmp/export.xml" }); print "Export finished\n";' 2. Note the warning "print() on closed filehandle $fh" and that "Export finished" is not printed. /tmp/export.xml is written. 3. Apply this patch. 4. Run the same command again. 5. Note that "Export finished" is printed, with no warning, and that /tmp/export.xml is still written. 6. prove t/db_dependent/Exporter/Record.t -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44086 --- Comment #3 from Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> --- Created attachment 211532 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211532&action=edit Bug 44086: (follow-up) Document output_filepath and the internal functions The POD of export() did not mention output_filepath and said the generated file was always displayed on STDOUT. Document the parameter, including that the previously selected filehandle is restored. Also add POD for the internal _get_*_for_export functions, so the module passes the POD coverage check, and close the =over list before =cut. To test: 1. podchecker Koha/Exporter/Record.pm 2. perldoc Koha/Exporter/Record.pm and read the new sections. -- You are receiving this mail because: You are watching all bug changes.
participants (1)
-
bugzilla-daemon@bugs.koha-community.org