The topic discusses a bug where linking to Wikimedia image info pages (e.g., commons.wikimedia.org/wiki/File:...) incorrectly triggers the standard image onebox, which attempts to display HTML content as an image. phw proposes two solutions: ignoring these links in the existing onebox or fetching the actual image URL via the Wikipedia API. While zogstrip notes the misleading nature of the server response, he offers to merge a Pull Request to the onebox gem.
When tibarra expresses interest in learning how to contribute, phw outlines a two-step implementation plan: modifying the existing image onebox’s regex to exclude Wikimedia URLs and creating a new wikimedia_onebox provider that utilizes the API. phw also shares a helper script for testing onebox rendering. riking suggests an alternative approach of adjusting engine priorities instead of modifying regexes. Ultimately, nbianca implemented the solution in PR #344, which was successfully merged to fix the issue.
If you link to Wikimedia with a link like File:Stones members montage2.jpg - Wikimedia Commons the image onebox is triggered, trying to display this link as an image (which it isn’t, it is just the info page about that image):
To solve this issue I would actually do it in two steps:
Make sure the existing image onebox located at lib/onebox/engine/image_onebox.rb does not load commons.wikimedia.org URLs anymore. That could be done by modifying the regular expression in matches_regexp
Add a new wikimedia_onebox.rb provider, which does first parse the provided URL and then makes a request to the Wikipedia API to get the actual image URL. Take a look at the tutorial in the README and the existing oneboxes in lib/onebox/engine/i. For the Wikimedia part you can reuse some of the code I linked above.
I have only played around with it a little myself. But you will need some way to test your work, and when I actually began implementing a onebox I wrote myself a helper script in the root directory of the onebox source code which looked something like this:
require_relative "lib/onebox"
# Set this path to your actual template path
Onebox.options = {
load_paths: [
File.join(File.dirname(__FILE__), "templates")
]
}
# This is the URL to test:
url = "https://commons.wikimedia.org/wiki/File:Stones_members_montage2.jpg"
# Create the onebox for the URL:
preview = Onebox.preview(url)
# This will print the generated onebox HTML:
puts preview