Subjects or Publishers with special characters in name cause problems
A number of Subjects or Publishers have special characters, like slashes "/", in their names which result in link URLs leading to 404s.
e.g. If you do a Subject search for "business economics", the first links are:
- https://openlibrary.org/subjects/business_&economics/_finance
- https://openlibrary.org/subjects/business_&economics/_management
These have textual labels of "BUSINESS & ECONOMICS / Finance" and "BUSINESS & ECONOMICS / Management" and lead to 404 pages.
I'm also noticing this for publishers with slashes in names, eg https://openlibrary.org/publishers/DEMON/PHANTOM_SOUND linked to from this work: https://openlibrary.org/works/OL10033335W/Crazy_Cajun_Recordings
Note, I'm planning to remove both work and publisher as part of https://github.com/internetarchive/openlibrary/issues/152 , but I'm mentioning it to show this problem with slashes is not just limited to subjects.
Since publishers are not yet under authority control, it's just a text string. I'm changing that one to "Demon - Phantom Sound" in each record found at https://openlibrary.org/search/authors?q=Cddemn
This is the search query URL from the original report: https://openlibrary.org/search/subjects?q=business+economics&mode=everything
The other subjects linked to from this page are correctly formed or escaped: https://openlibrary.org/subjects/business_&_economics__industries__transportation
This is the line that needs adjusting:
https://github.com/internetarchive/openlibrary/blob/22da07aef6f8aa83571d43a89e5ab22fda28a438/openlibrary/templates/search/subjects.html#L46
Exactly how is left as an exercise to the reader.
If there are still problems with publishers, let's create a separate issue for that.
Can i try to fix this???
Or anyone working on it
I believe this will fix the issue
$ key = '/subjects/' + url_map.get(doc['type'], '') + n.lower().replace(' ', '_').replace('?', '').replace(',', '').replace('/','_')
However, there are no books with "/" in their subjects in my docker instance and I can't find any to add to my local instance because all the links go to 404... I'm not sure how to go about testing the above code: can someone make a suggestion?
Edit a work on your local dev env and add a subject like 10/18. The link on the work's page to the subject will 404; a regular subject would display something like https://openlibrary.org/subjects/foobar .
There are 2 parts to this problem:
- Anywhere a subjects or publishers link is displayed, it should be URL encoded; i.e. spaces replaced with
_, and then a regular encoding (e.g.encodeURIComponent('10/18') == "10%2F18"in JS). - the
/subjects/handler needs to decode the rest of the path (e.g.decodeURIComponent('10%2F18') == '10/18'in JS).
There are python methods to do these, but I can't remember their names :P
Also note it looks like publishers have a similar issue; there might be a way to fix this in one of the super class and have it fix both of these issues (see #2202 ).
My apologies @Himanshu-Garg ! I missed your previous message. Are you still interested in working on it? I think @guyjeangilles has already gotten some headway on it.
Update:
I swapped this line https://github.com/internetarchive/openlibrary/blob/e0d2de10683b676593b4bfc6e53bbf6e433a404a/openlibrary/templates/search/subjects.html#L46
with this line.
$ key = '/subjects/' + url_map.get(doc['type'], '') + n.lower().replace(' ', '_').replace('?', '').replace(',', '').replace('/' , '%2F')
I opted not to use python's url enconding because I didn't think it made sense to import the encoder for every search. The above worked...kinda

You'll see that there's no longer a 404 and the url is parsed properly. The search doesn't find the work I edited though which may be a separate issue. The 404 problem isn't completely gone though. On the works page, clicking the subject link with a "/" still routes to a 404. Note in the bottom left corner of my web browser that the page routes to "0.0.0.0:8080/subjects/test/test"


I suspected the problem was with the view template for works. Specifically I thought it was the line below. (note: in my local repo the line # is 142, but I don't think this difference is causing the problem). https://github.com/internetarchive/openlibrary/blob/e0d2de10683b676593b4bfc6e53bbf6e433a404a/openlibrary/templates/type/work/view.html#L128
So I changed this line to the following, which I thought would be the fix but I'm still getting 404 errors when clicking the subjects link.
<a href="/subjects/$prefix$utf8(subject.lower().replace(' ', '_').replace(',','').replace('/','%2F'))">$subject</a>$cond(not loop.last,",","")
I thought there was a subjects.py controller directing the routing per the Wiki, but I didn't find one. At this point, I don't think I understand the routing system enough to provide a robust solution to this problem. @cdrini Can you provide advise or point me to someone who can?
Nice, @guyjeangilles ! You're close. The subjects are stored in the work record unencoded; like /subjects/test/test. When we render them in the works page, we need to encode them (like /subjects/test%2Ftest); your code change is doing this. The last step is that when visiting /subjects/test%2Ftest, the code needs to decode the subject key to /subjects/test/test so it can match what's been stored on the record.
See this comment: https://github.com/internetarchive/openlibrary/issues/2202#issuecomment-541353182
That's talking about publishers, but publishers uses ~same logic as subjects.
Also, we should use full python url encoding/decoding; I don't think the performance hit will be that large. We can always adjust if we discover it is (premature optimization is the root of all evil :)). The benefits of using a full url encoding/decoding library is that we handle all the cases at once. Not just /, but also é, or ?, or #; most of which would break the URLs. It's best practice to always decode/encode urls parameters; the fact that we haven't been doing it is pretty bad :P
As @cdrini pointed out to me, I believe updating the following class will provide the most robust solution
https://github.com/internetarchive/openlibrary/blob/e0d2de10683b676593b4bfc6e53bbf6e433a404a/openlibrary/plugins/worksearch/subjects.py#L52
Specifically I think the following line is the step in the right direction:
...
def GET(self, key):
key = urllib.quote_plus(key)
...
Unfortunately following the steps in the Wiki to debug the problem was less than fruitful, specifically I can't log in as an admin despite switching browsers and clearing cookies. Hopefully someone else can take the progress I've made and close out this issue.
Are we leaving this as a permanently open "good first issue" or should I just restate my https://github.com/internetarchive/openlibrary/issues/333#issuecomment-489479606 in the form of a one line PR and be done with it?
@tfmorris feel free to open a PR if you have the time. If my memory serves I had hurdles testing my solution because I couldn't create a subject or publisher with a special character in their name in the dev environment.
@tfmorris @guyjeangilles I would like to work on this issue. Can you please guide me on what all changes do I need to make?
If this is such a good first issue, why has it taken 5 years to close it out? Gonna try to tackle it anyways. Wish me luck~
@cdrini relating to my PR #5940 and your comment above:
Nice, @guyjeangilles ! You're close. The subjects are stored in the work record unencoded; like
/subjects/test/test. When we render them in the works page, we need to encode them (like/subjects/test%2Ftest); your code change is doing this. The last step is that when visiting/subjects/test%2Ftest, the code needs to decode the subject key to/subjects/test/testso it can match what's been stored on the record.See this comment: #2202 (comment)
That's talking about publishers, but publishers uses ~same logic as subjects.
My PR skips any decoding of %2F to 'match what's been stored on the record' -- I'm not convinced the slash has to be matched to a record , it has to match how it was indexed, but that is in Solr.
This example shows a slash is magically returned without any slash in the URL: https://openlibrary.org/subjects/business__economics__general page title: Business & Economics / General
Also: https://openlibrary.org/subjects/business__economics__finance No slashes in URL, three on the subject page.
Here's an example where the slash isn't in the title: https://openlibrary.org/subjects/business_&_economics__general
Even though that appears to be where 'Business & Economics / General' subjects get indexed to.
I think this is because Solr has a subject field and a subject_key, which is some sort of normalised
https://github.com/internetarchive/openlibrary/blob/4ba715b31eedc12444757f245c922a380f2c3159/openlibrary/solr/update_work.py#L149-L158
.. of course with a duplicate method elsewhere : https://github.com/internetarchive/openlibrary/blob/4ba715b31eedc12444757f245c922a380f2c3159/openlibrary/utils/init.py#L10-L11
and inconsistent translations back... see the differences in #5940
This is not a good first issue since most people have tripped up on the URL encoding vs. matching the normalised Solr subject_key.
It's possible I've got something wrong here -- I haven't completely followed how Solr is indexing these subjects in the first place, but I'm pretty sure the UI links need to match what is stored in solr's subject_key .
There is also some kind of title replacement logic going on with the subject page, compare:
- https://openlibrary.org/subjects/business__economics__general
- https://openlibrary.org/subjects/business_&_economics__general
- https://openlibrary.org/subjects/business_&&&&&&&&_economics__general[]&&
They all give the same matches, but only the first one returns a title cased subject name.