Bug 41619

Summary: Add `Koha::CSV`
Product: Koha Reporter: Tomás Cohen Arazi (tcohen) <tomascohen>
Component: Architecture, internals, and plumbingAssignee: Tomás Cohen Arazi (tcohen) <tomascohen>
Status: Signed Off --- QA Contact: Testopia <testopia>
Severity: enhancement    
Priority: P5 - low CC: jonathan.druart, kyle, m.de.rooy, martin.renvoize, tomascohen
Version: Main   
Hardware: All   
OS: All   
GIT URL: Initiative type: ---
Sponsorship status: --- Comma delimited list of Sponsors:
Crowdfunding goal: 0 Patch complexity: Medium patch
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:
Bug Depends on:    
Bug Blocks: 41620, 41622, 41625, 41626, 41627, 41628, 41629, 41630, 41631, 41632, 41633    
Attachments: Bug 41619: Add Koha::CSV
Bug 41619: Add Koha::CSV

Description Tomás Cohen Arazi (tcohen) 2026-01-14 19:35:10 UTC
I propose we add a wrapper class on top of `Text::CSV` that takes care of applying all the required settings we might need. It should provide a way to override those settings (e.g. separator character, formula, etc).
Comment 1 Tomás Cohen Arazi (tcohen) 2026-01-14 19:50:15 UTC
Created attachment 191466 [details] [review]
Bug 41619: Add Koha::CSV

This patch introduces Koha::CSV, a wrapper around Text::CSV_XS that
provides consistent defaults and methods for CSV generation across Koha.

The class:
- Inherits CSV delimiter from CSVDelimiter system preference
- Enforces security defaults (binary=1, formula='empty')
- Provides convenient methods for common CSV operations
- Allows per-instance configuration overrides

This establishes a foundation for standardizing CSV exports throughout
Koha, eliminating inconsistencies in CSV generation and providing a
testable, maintainable approach to CSV handling.

Test plan:
1. Apply patch
2. Run:
   $ ktd --shell
  k$ prove t/Koha/CSV.t
=> SUCCESS: Tests pass!
3. Tests cover:
   - Default initialization with CSVDelimiter preference
   - Preference value inheritance (comma, semicolon, tabulation)
   - Constructor parameter overrides
   - add_row() method with various data types
   - combine() and string() methods
   - print() to filehandle
   - Proper quote escaping and empty field handling
4. Sign off :-D
Comment 2 Tomás Cohen Arazi (tcohen) 2026-01-15 12:43:25 UTC
I used AI to analyze which `Text::CSV` methods we use across the codebase, and so required being added to `Koha::CSV`. Forgot to mention in the commit.
Comment 3 David Nind 2026-01-15 14:05:09 UTC
Created attachment 191491 [details] [review]
Bug 41619: Add Koha::CSV

This patch introduces Koha::CSV, a wrapper around Text::CSV_XS that
provides consistent defaults and methods for CSV generation across Koha.

The class:
- Inherits CSV delimiter from CSVDelimiter system preference
- Enforces security defaults (binary=1, formula='empty')
- Provides convenient methods for common CSV operations
- Allows per-instance configuration overrides

This establishes a foundation for standardizing CSV exports throughout
Koha, eliminating inconsistencies in CSV generation and providing a
testable, maintainable approach to CSV handling.

Test plan:
1. Apply patch
2. Run:
   $ ktd --shell
  k$ prove t/Koha/CSV.t
=> SUCCESS: Tests pass!
3. Tests cover:
   - Default initialization with CSVDelimiter preference
   - Preference value inheritance (comma, semicolon, tabulation)
   - Constructor parameter overrides
   - add_row() method with various data types
   - combine() and string() methods
   - print() to filehandle
   - Proper quote escaping and empty field handling
4. Sign off :-D

Signed-off-by: David Nind <david@davidnind.com>