Non-ascii in filenames of images in MediaWiki

Created:

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.