About this blog…

I am employed by Netnod as head of engineering, research and development and am among other things chair of the Security and Stability Advisory Committee at ICANN. You can find CV and photos of me at this page.

As I wear so many hats, I find it being necessary to somewhere express my personal view on things. This is the location where that happens. Postings on this blog, or at Facebook, Twitter etc, falls under this policy.

The views expressed on this post are mine and do not necessarily reflect the views of Netnod or any other of the organisations I have connections to.

Non-ascii in filenames of images in MediaWiki

If one have added images to MediaWiki and used non-ascii in the filenames (for example the Swedish characters åäö) there might be problem if you move the wiki to a file system with different encoding mechanisms. Or upgrade or change something else in your environment.

This is possible to solve, but you have to rename the files and the links in the database in a coordinated way.

Here is how you do it:

First, I found that turning on “Move” command in MediaWiki was to be done via adding the following to the LocalSettings.php. I am to be honest unsure whether this is needed as I did things in the wrong order myself. Anyway, I have it there now, and it works:

$wgAllowImageMoving = true;

Go to the directory in the file system where the image files are stored. It should be the subdirectory images in the root of the wiki.

Add the following script somewhere so you can run it repeatedly, and adjust according to your flavors:

#!/bin/sh
BASE="/wikiroot/images"
USER=user
GROUP=group

cd "$BASE"
OLD="$1"
NEW="$2"
BO=`basename "$OLD" | sed 's/[^abcdefghijklmnopqrstuvwxyz]/*/ig'`
DO=`dirname "$OLD"`
OLDFILENAME=`find "$DO" -name "$BO" -print`
NEWDIR=`dirname "$NEW"`
mkdir -p "$NEWDIR"
cp "$OLDFILENAME" "$NEW"
chown -R $USER:$GROUP .

Go to the special page in the wiki that have the list of all uploaded files. You should in that page see errors for all the files that have file names that are broken. Click on the name of the first one that have an error.

You see now for this image see an error similar to this one:

Fel vid skapande av miniatyrbild: convert: unable to open image `/srv/www/matitse/wwwroot/images/e/e2/Omelett_med_sallad,_fyllning_och_stekt_lök.JPG': gick-6.6.0/modules-Q16/coders/jpeg.la @ error/blob.c/OpenBlob/2489.
convert: unable to open image `/srv/www/matitse/wwwroot/images/e/e2/Omelett_med_sallad,_fyllning_och_stekt_lök.JPG': @ error/blob.c/OpenBlob/2489.
convert: missing an image filename `/tmp/transform_9b2b7586eb2d-1.jpg' @ error/convert.c/ConvertImageCommand/2940.

The interesting piece is the path to the file, in this case e/e2/Omelett_med_sallad,_fyllning_och_stekt_lök.JPG.

Click on the move tab in the top.

Rename the file to something that only includes safe characters. Do the actual move by submitting the form.

You should get a success page. Click on the target of the file, i.e. the new file name.

On the new page you still get an error, but slightly different one. This time because the file is missing (i.e. the path is correct):

Fel vid skapande av miniatyrbild: convert: unable to open image `/srv/www/matitse/wwwroot/images/a/a4/Omelett_med_sallad_fyllning_och_stekt_lok.JPG': gick-6.6.0/modules-Q16/coders/jpeg.la @ error/blob.c/OpenBlob/2489.
convert: unable to open image `/srv/www/matitse/wwwroot/images/a/a4/Omelett_med_sallad_fyllning_och_stekt_lok.JPG': @ error/blob.c/OpenBlob/2489.
convert: missing an image filename `/tmp/transform_13f03b8a7bea-1.jpg' @ error/convert.c/ConvertImageCommand/2940.

See the new path: a/a4/Omelett_med_sallad_fyllning_och_stekt_lok.JPG with the new safe filename.

Run the script with the two paths as arguments:

./imagefix.sh e/e2/Omelett_med_sallad,_fyllning_och_stekt_lök.JPG a/a4/Omelett_med_sallad_fyllning_och_stekt_lok.JPG

Do a reload of the page, and the image should be visible.

Comments are closed.