# Image Onebox breaks on Wikimedia links

**URL:** https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197
**Category:** Bug
**Created:** [March 17, 2016, 2:25pm UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197 "2016-03-17T14:25:25Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![phw](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/phw/32/285652_2.png) [@phw](https://meta.discourse.org/u/phw)
#### Post date: [March 17, 2016, 2:25pm UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197/1 "2016-03-17T14:25:26Z")

</div>

If you link to Wikimedia with a link like [File:Stones members montage2.jpg - Wikimedia Commons](https://commons.wikimedia.org/wiki/File:Stones_members_montage2.jpg) 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):

> **[File:Stones members montage2.jpg](https://commons.wikimedia.org/wiki/File:Stones_members_montage2.jpg)**

There are two possible solutions:

1. Just ignore links to [common.wikimedia.org](http://common.wikimedia.org) in the image onebox
2. Use the Wikipedia API to get the actual link to the image itself

The latter requires an additional request, the resolved URL looks something like [https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Stones\_members\_montage2.jpg/250px-Stones\_members\_montage2.jpg](https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Stones_members_montage2.jpg/250px-Stones_members_montage2.jpg) . Not sure whether this should be added to the existing image onebox or if there should be a separate wikimedia onebox. I have some crude code to do the conversion from the [commons.wikimedia.org](http://commons.wikimedia.org) URL to the actual image URL here:

[https://github.com/phw/discourse-musicbrainz-onebox/blob/master/engine/wikimedia.rb](https://github.com/phw/discourse-musicbrainz-onebox/blob/master/engine/wikimedia.rb)

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [March 17, 2016, 3:17pm UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197/2 "2016-03-17T15:17:03Z")

</div>

Well, they are misleading us by sending back html content when the file extension is `.jpg`…

I’d be happy to merge a PR adding this to the onebox gem 😉

---

<div class="post-metadata">

### Author: ![phw](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/phw/32/285652_2.png) [@phw](https://meta.discourse.org/u/phw)
#### Post date: [March 17, 2016, 3:20pm UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197/3 "2016-03-17T15:20:53Z")

</div>

> [@zogstrip](#):
>
> I’d be happy to merge a PR adding this to the onebox gem 😉

Great to hear that. But it will take some time from my side, got to tackle a view other projects before…

---

<div class="post-metadata">

### Author: ![tibarra](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tibarra/32/52222_2.png) [@tibarra](https://meta.discourse.org/u/tibarra)
#### Post date: [March 17, 2016, 6:47pm UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197/4 "2016-03-17T18:47:20Z")

</div>

I want to solve this, can you give me a quick “how to”? it isn’t just installing the gem right? Sorry, i want to learn and contribute.

---

<div class="post-metadata">

### Author: ![phw](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/phw/32/285652_2.png) [@phw](https://meta.discourse.org/u/phw)
#### Post date: [March 17, 2016, 8:09pm UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197/5 "2016-03-17T20:09:26Z")

</div>

> [@tibarra](#):
>
> I want to solve this, can you give me a quick “how to”? it isn’t just installing the gem right? Sorry, i want to learn and contribute.

Awesome, this is actually a great way to get some coding practice 🙂 To solve this you will need to fork [GitHub - discourse/onebox: (DEPRECATED) A gem for turning URLs into website previews · GitHub](https://github.com/discourse/onebox) and add your changes there. They have actually a pretty nice [tutorial on how to add a new onebox provider](https://github.com/discourse/onebox#adding-support-for-a-new-url) in the README.

To solve this issue I would actually do it in two steps:

1. 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`
2. 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:

```ruby
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

```

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [March 27, 2016, 3:32am UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197/6 "2016-03-27T03:32:10Z")

</div>

> [@phw](#):
>
> Make sure the existing image onebox located at lib/onebox/engine/image\_onebox.rb does not load [commons.wikimedia.org](http://commons.wikimedia.org) URLs anymore. That could be done by modifying the regular expression in matches\_regexp

You can also achieve this by raising the `priority` of the engine.

---

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [March 12, 2017, 9:36pm UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197/7 "2017-03-12T21:36:01Z")

</div>

I implemented this in [PR #344](https://github.com/discourse/onebox/pull/344). I tested using Discourse and it seems to behave as expected.

---

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [March 14, 2017, 4:25pm UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197/8 "2017-03-14T16:25:19Z")

</div>

The pull request was merged.

[https://github.com/discourse/onebox/pull/344](https://github.com/discourse/onebox/pull/344)

---

<div class="post-metadata">

### Author: ![erlend\_sh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erlend_sh/32/119475_2.png) [@erlend\_sh](https://meta.discourse.org/u/erlend_sh)
#### Post date: [March 17, 2017, 4:25pm UTC](https://meta.discourse.org/t/image-onebox-breaks-on-wikimedia-links/41197/9 "2017-03-17T16:25:43Z")

</div>

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
