[MediaWiki-l] Making article wikitext appear as an SVG image file in another page

Adam Nielsen a.nielsen at shikadi.net
Sun Dec 20 04:00:59 UTC 2015

Hi all,

I'd like to write an extension that allows wiki pages to contain SVG
code (which can be edited like any other page - templates can be used,
version history is kept, etc.)  I would then like to be able to embed
these pages inside others, so the SVG code is then rendered as an
image elsewhere on the wiki.

I am a little unsure on the best way to do this, so I'd appreciate any

I think it is best not to repurpose the File: namespace for this,
because that will avoid having to deal with the difference between
wikitext, files, and figuring out where template replacements should
happen, so I guess a new SVG: namespace would be best.

The problem is then how to embed these images in other pages.  I would
like to keep to existing syntax as closely as possible, so something
like [[SVG:Example]] would display the image, just like
[[File:Example.png]] would.

I tried using the LinkBegin hook, replacing the wikitext with an <img>
tag that links to the ?action=raw version of the SVG: page.  This does
work, but I lose all the standard MW image formatting methods, like
being able to specify the image size or add a caption, so this isn't

In an attempt to maintain access to this formatting code, I tried
hooking ImageBeforeProduceHTML instead, and using wikicode like
[[File:SVG:Example]].  This will show the image, but the hook runs
before all the formatting takes place so I still lose access to the MW
image formatting code.

It seems like it would be better to hook in at a lower level, so the
existing [[File:blah]] code runs per normal, providing resizing/caption
support per normal.  I'm not sure how best to do this though.

One method that comes to mind is hooking ImageBeforeProduceHTML, then
writing the processed wikitext to a temporary .svg file, and returning
that in the ImageBeforeProduceHTML $file parameter.  However I am not
sure how to do this, and guessing it may not work anyway as the
final HTML code would probably have an <img> link pointing to the
temporary file, which by then would have been deleted.

But in theory this idea would also allow thumbnails to be generated for
browsers that don't support SVG, giving the best user experience.

Can anyone point me in the right direction for how this kind of
extension could be implemented?  It looks like the most reliable way
is going to be an extension that can somehow make the processed wikitext
from an article appear to MW as if it's an image file just like any


More information about the MediaWiki-l mailing list