Uploading KML files to MediaWiki

So you want to upload KML files to your MediaWiki install? Simple as putting ‘kml’ in your allowed file extensions right?


Through the almighty power of a string match that’s not actually a regular expression, but instead a strpos match in the SpecialUpload:detectScript function (yes, that’s right, a strpos match, not a stripos match — but a strtolower takes care of that a few lines before — that’s probably faster anyway).

The strpos looks for <head in the chunk o’ text that’s in the uploaded file — which of course matches the KML heading tag — producing a detectScript match

Yes, one could modify the function in MediaWiki to handle KML file uploads — pulling <head out of the following code block:

		$tags = array(			'<span class="nt"><body</span><span class="err">',</span>			<span class="err">'<</span><span class="na">head</span><span class="err">',</span>			<span class="err">'<</span><span class="na">html</span><span class="err">',</span>   <span class="err">#</span><span class="na">also</span> <span class="na">in</span> <span class="na">safari</span>			<span class="err">'<</span><span class="na">img</span><span class="err">',</span>			<span class="err">'<</span><span class="na">pre</span><span class="err">',</span>			<span class="err">'<</span><span class="na">script</span><span class="err">',</span> <span class="err">#</span><span class="na">also</span> <span class="na">in</span> <span class="na">safari</span>			<span class="err">'<</span><span class="na">table</span><span class="err">'</span>			<span class="err">);</span>		<span class="na">if</span><span class="err">(</span> <span class="err">!</span> <span class="err">$</span><span class="na">wgAllowTitlesInSVG</span> <span class="err">&&</span> <span class="err">$</span><span class="na">extension</span> <span class="err">!==</span> <span class="err">'</span><span class="na">svg</span><span class="err">'</span> <span class="err">&&</span> <span class="err">$</span><span class="na">mime</span> <span class="err">!==</span> <span class="err">'</span><span class="na">image</span><span class="err">/</span><span class="na">svg</span><span class="err">'</span> <span class="err">)</span> <span class="err">{</span>			<span class="err">$</span><span class="na">tags</span><span class="err">[]</span> <span class="err">=</span> <span class="err">'<</span><span class="na">title</span><span class="err">';</span>		<span class="err">}</span>    		<span class="na">foreach</span><span class="err">(</span> <span class="err">$</span><span class="na">tags</span> <span class="na">as</span> <span class="err">$</span><span class="na">tag</span> <span class="err">)</span> <span class="err">{</span>			<span class="na">if</span><span class="err">(</span> <span class="na">false</span> <span class="err">!==</span> <span class="na">strpos</span><span class="err">(</span> <span class="err">$</span><span class="na">chunk</span><span class="err">,</span> <span class="err">$</span><span class="na">tag</span> <span class="err">)</span> <span class="err">)</span> <span class="err">{</span>				<span class="na">return</span> <span class="na">true</span><span class="err">;</span>			<span class="err">}</span>		<span class="err">}</span>

And then writing a regex to match <head and not <heading

Which is certainly do-able due to the beauty of open-source software(*) But thankfully (very thankfully) there’s a KMZ (scroll down) format. Which should upload just fine with just a file extension addition. (And the bonus is that it’s far more feature-rich to use.)

(* which, of course, making custom local modifications to your open-source software packages that don’t merit patch submissions back to the package authors is a whole other discussion topic, look for the future post and/or Conversations with Plastic Dinosaurs about being on the hook to maintain custom changes change for any and all future updates to the open-source software packages you use, which inevitably, you’ll forget you made, and then you’ll upgrade, and you’ll break expected functionality, which won’t be noticed for months after you’ve forgotten you ever even upgraded, at which point someone will complain, memos will be written, you’ll get blamed, you’ll bitch about getting blamed, everyone but you, given that you actually do the work, will promise not to forget about it again, which you’ll promptly do six months and thousands of tasks later on the next upgrade. Lather, Rinse, Repeat. )