Bug 41617

Summary: CSV export from item search results - incorrect spaces after comma separator causes issues
Product: Koha Reporter: Tomás Cohen Arazi (tcohen) <tomascohen>
Component: Architecture, internals, and plumbingAssignee: Tomás Cohen Arazi (tcohen) <tomascohen>
Status: Pushed to main --- QA Contact: Marcel de Rooy <m.de.rooy>
Severity: major    
Priority: P5 - low CC: david, dcook, kebliss, m.de.rooy, tomascohen
Version: Main   
Hardware: All   
OS: All   
GIT URL: Initiative type: ---
Sponsorship status: --- Comma delimited list of Sponsors:
Crowdfunding goal: 0 Patch complexity: Trivial patch
Documentation contact: Documentation submission:
Text to go in the release notes:
This fixes the CSV export from item search results in the staff interface (Search > Item search> Export select results (X) to CSV). It removes extra spaces after the comma separator, which causes issues when using the CSV file with some applications (such as Microsoft Excel).
Version(s) released in:
26.05.00
Circulation function:
Bug Depends on: 40880    
Bug Blocks: 41620    
Attachments: Bug 41617: Remove extra spaces breaking CSV formatting
Bug 41617: Remove extra spaces breaking CSV formatting
Bug 41617: Remove extra spaces breaking CSV formatting
Bug 41617: Remove extra spaces breaking CSV formatting

Description Tomás Cohen Arazi (tcohen) 2026-01-14 18:37:05 UTC
I've been following the CSV generation code in Koha for security reasons, and didn't notice this particular case until now.

This script generates CSV using... templates instead of Text::CSV.

Besides the fact this is wrong, there's a bug in the generated CSV. Bug 40880 solved an issue with the headers, but the produced file contains things like this:

```
"First field", "Second, field", "Third"
```

the extra space after the comma makes it get interpreted like:

```
First field
<space>"Second
<space>field"
<space>"Third"
```
Comment 1 Tomás Cohen Arazi (tcohen) 2026-01-14 19:02:08 UTC
Created attachment 191439 [details] [review]
Bug 41617: Remove extra spaces breaking CSV formatting

This patch removes extra spaces breaking CSV parsing in tools like Excel.

To test:
1. Perform an item search selecting the CSV option
2. Open the CSV with a TEXT EDITOR of your choice
=> FAIL: Notice the extra spaces after the commas (those used as separators, no the ones within the double quotes)
3. Apply this patch
4. Repeat 1-2
=> SUCCESS: No extra spaces!
5. Sign off :-D

Note: On the first attempt you can open the file on Excel if you have it and notice things are not aligned.
Note2: Using double quotes on all fields regardless of the context works for most parsers, but is not correct CSV. Will deal with it on a separate bug.

Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Comment 2 David Nind 2026-01-16 18:16:34 UTC
Created attachment 191582 [details] [review]
Bug 41617: Remove extra spaces breaking CSV formatting

This patch removes extra spaces breaking CSV parsing in tools like Excel.

To test:
1. Perform an item search selecting the CSV option
2. Open the CSV with a TEXT EDITOR of your choice
=> FAIL: Notice the extra spaces after the commas (those used as separators, no the ones within the double quotes)
3. Apply this patch
4. Repeat 1-2
=> SUCCESS: No extra spaces!
5. Sign off :-D

Note: On the first attempt you can open the file on Excel if you have it and notice things are not aligned.
Note2: Using double quotes on all fields regardless of the context works for most parsers, but is not correct CSV. Will deal with it on a separate bug.

Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Comment 3 Marcel de Rooy 2026-01-23 10:44:55 UTC
This does not improve the readability of this include. Could you add newlines and remove them again with the TT syntax in the output?
Comment 4 Tomás Cohen Arazi (tcohen) 2026-01-23 14:14:24 UTC
(In reply to Marcel de Rooy from comment #3)
> This does not improve the readability of this include. Could you add
> newlines and remove them again with the TT syntax in the output?

Even if I remove the file on bug 41620?
Comment 5 Marcel de Rooy 2026-01-26 07:13:26 UTC
(In reply to Tomás Cohen Arazi (tcohen) from comment #4)
> (In reply to Marcel de Rooy from comment #3)
> > This does not improve the readability of this include. Could you add
> > newlines and remove them again with the TT syntax in the output?
> 
> Even if I remove the file on bug 41620?

Haha. There is nothing above improvement before deletion ;) Clean garbage can.
Comment 6 Martin Renvoize (ashimema) 2026-02-02 18:01:06 UTC
Created attachment 192337 [details] [review]
Bug 41617: Remove extra spaces breaking CSV formatting

This patch removes extra spaces breaking CSV parsing in tools like Excel.

To test:
1. Perform an item search selecting the CSV option
2. Open the CSV with a TEXT EDITOR of your choice
=> FAIL: Notice the extra spaces after the commas (those used as separators, no the ones within the double quotes)
3. Apply this patch
4. Repeat 1-2
=> SUCCESS: No extra spaces!
5. Sign off :-D

Note: On the first attempt you can open the file on Excel if you have it and notice things are not aligned.
Note2: Using double quotes on all fields regardless of the context works for most parsers, but is not correct CSV. Will deal with it on a separate bug.

Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Comment 7 Marcel de Rooy 2026-02-06 10:38:38 UTC
Created attachment 192603 [details] [review]
Bug 41617: Remove extra spaces breaking CSV formatting

This patch removes extra spaces breaking CSV parsing in tools like Excel.

To test:
1. Perform an item search selecting the CSV option
2. Open the CSV with a TEXT EDITOR of your choice
=> FAIL: Notice the extra spaces after the commas (those used as separators, no the ones within the double quotes)
3. Apply this patch
4. Repeat 1-2
=> SUCCESS: No extra spaces!
5. Sign off :-D

Note: On the first attempt you can open the file on Excel if you have it and notice things are not aligned.
Note2: Using double quotes on all fields regardless of the context works for most parsers, but is not correct CSV. Will deal with it on a separate bug.

Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 8 Lucas Gass (lukeg) 2026-02-06 22:21:47 UTC
Nice work everyone!

Pushed to main for 26.05