neuhalfen.name

A random collection of posts

Empty Albums in Gallery 3.0.4: Solved

Permalink

The update to Gallery 3.0.4 leaves some albums empty, the only error is a cryptic [name] => illegal_data_file_extension in the logfiles. The culprit is that Gallery 3.0.4 disallows dots in the filename . It seems that the issue is resolved in 3.0.5 — which is yet to be released. To fix my installation I wrote a script to rename the files for me on disk, and in the database.

  1. Related issue: Dots in filenames disallowed
  2. I am not the only one

${GALLERY_ROOT} refers to the directory where gallery is installed. It is the directory that contains index.php, and the folder var with the images stored within.

The symptoms

After upgrading to Gallery 3.0.4 some albums show up blank. No error logs are to be found, until debug mode is enabled for Gallery (see below on how to to that).

Error in Gallery logfile.
error: ORM Validation has failed for items model
#0 /htdocs/gallery3/system/libraries/ORM.php(755): ORM_Validation_Exception_Core::handle_validation('items', Object(Validation))
#1 /htdocs/gallery3/modules/gallery/models/item.php(773): ORM_Core->validate(NULL)
#2 /htdocs/gallery3/system/libraries/ORM.php(778): Item_Model_Core->validate()
#3 /htdocs/gallery3/modules/gallery/libraries/MY_ORM.php(34): ORM_Core->save()
#4 /htdocs/gallery3/modules/gallery/libraries/ORM_MPTT.php(79): ORM->save()
#5 /htdocs/gallery3/modules/gallery/models/item.php(523): ORM_MPTT_Core->save()
#6 /htdocs/gallery3/modules/gallery/models/item.php(291): Item_Model_Core->save()
#7 /htdocs/gallery3/modules/gallery/models/item.php(138): Item_Model_Core->relative_url()
#8 /htdocs/gallery3/themes/browny_wind/views/album.html.php(18): Item_Model_Core->url()
#9 /htdocs/gallery3/system/libraries/View.php(318): include('/data/23/2/84/1...')
#10 /htdocs/gallery3/system/libraries/View.php(260): View_Core->load_view('/data/23/2/84/1...', Array)
#11 /htdocs/gallery3/modules/gallery/libraries/MY_View.php(75): View_Core->render(false, false, false)
#12 /htdocs/gallery3/system/libraries/View.php(226): View->render()
#13 /htdocs/gallery3/themes/browny_wind/views/page.html.php(128): View_Core->__toString()
#14 /htdocs/gallery3/system/libraries/View.php(318): include('/data/23/2/84/1...')
#15 /htdocs/gallery3/system/libraries/View.php(260): View_Core->load_view('/data/23/2/84/1...', Array)
#16 /htdocs/gallery3/modules/gallery/libraries/MY_View.php(75): View_Core->render(false, false, false)
#17 /htdocs/gallery3/system/libraries/View.php(226): View->render()
#18 /htdocs/gallery3/modules/gallery/controllers/albums.php(77): View_Core->__toString()
#19 [internal function]: Albums_Controller->show(Object(Item_Model))
#20 /htdocs/gallery3/system/core/Kohana.php(331): ReflectionMethod->invokeArgs(Object(Albums_Controller), Array)
#21 [internal function]: Kohana_Core::instance(NULL)
#22 /htdocs/gallery3/system/core/Event.php(208): call_user_func_array(Array, Array)
#23 /htdocs/gallery3/application/Bootstrap.php(67): Event_Core::run('system.execute')
#24 /htdocs/gallery3/index.php(113): require('/data/23/2/84/1...')
#25 {main}
2012-06-17 09:00:38 -04:00 --- error: Validation errors: Array
(
[name] => illegal_data_file_extension
)

Enabling debug mode for Gallery3


Create the following file as local.php in the gallery3 root (${GALLERY_ROOT}). Place it next to index.php, and make sure that the webserver can read it. This will enable development mode for gallery which creates log files in ${GALLERY_ROOT}/var/logs/. Don’t forget to remove local.php for production!

${GALLERY_ROOT}/local.php
<? defined("SYSPATH") or die("No direct script access");
error_reporting(E_ALL);
ini_set('display_errors', true);
date_default_timezone_get();

Fixing the issue


This script renames pictures in gallery3 (3.0.4) that have more than one dot in the filename. It has been hacked together in an hour or so and successfully managed to repair my installation. As always: make backups, shutdown gallery before you run it and carefully read the logs. If the script deletes all your files, and puts your house on fire please don’t blame me.

After running this script you will find two files in your cwd:

  1. updates.sql — A list of SQL commands that update the DB
  2. moves.sh — A list of “mv” commands that rename the files on the disk

Verify the scripts and run them (sudo move.sh / mysql -u gallery -p gallery < updates.sql)

If you are bold, modify rename_file() and update_db() to directly execute the commands.

rename_gallery3_images.sh link
#!/bin/bash
# This script renames pictures in gallery3 (3.0.4) that have more than one dot in the filename.
# After running this script you will find two files in your cwd:
# moves.sh -- A list of "mv" commands that rename the files on the disk
# updates.sql -- A list of SQL commands that update the DB
#
# Verify the scripts and run them (sudo move.sh / mysql -u gallery -p gallery < updates.sql)
#
# If you are bold, modify "rename_file()" and "update_db()" to directly
# execute the commands.
#
# This script can help you when some albums show up blank after the update.
#
# If you find exceptions like this in your gallery3 log ""
# error: Validation errors: Array
#(
#[name] => illegal_data_file_extension
#)
# - http://sourceforge.net/apps/trac/gallery/ticket/1894
# - http://gallery.menalto.com/node/107102
#
#
## Please configure here
GALLERY_DB_PW=
GALLERY_VAR=/var/www/gallery3/var
MYSQL="mysql -u gallery -p${GALLERY_DB_PW} gallery"
SED=gsed
FIND=gfind
MOVE=/opt/local/bin/mv
function warn()
{
echo WARN: $* >>/dev/stderr
}
function rename_file()
{
# $MOVE "$1" "$2" # >> moves.sh
echo $MOVE \"$1\" \"$2\" >> moves.sh
}
function update_db()
{
QUERY=$1
# sql "$QUERY" # >> updates.sql
echo "$QUERY" >> updates.sql
}
function sql(){
CMD=$1
echo Executing: $CMD
echo "$CMD" |$MYSQL
}
function mangle_name(){
orig_name=$1
suffix=$(echo $orig_name |$SED -n -e 's/.*\([.][^.]*\)/\1/p')
prefix=${orig_name%.[^.]*}
prefix=$(echo $prefix |$SED -e 's/[.]/_/g')
prefix=$(echo $prefix |$SED -e "s/[']/_/g") # no "'" b/c that would destroy our SQL
#prefix=${prefix/ /_}
#prefix=${prefix/[/_}
#prefix=${prefix/]/_}
echo ${prefix}${suffix}
}
#
# Returns a list of files:
#
#./resizes/2012/2012-05-Test/2012-05-26 19.17.19.jpg
#./albums/2012/2012-05-Test/2012-05-26 19.17.19.jpg
#./thumbs/2012/2012-05-Test/2012-05-26 19.17.19.jpg
#
function files_for_name()
{
name="$1"
if [[ ("$name" == *"["*) || ("$name" == *"]"*) ]]
then
warn \"$name\": Filenames containing [ or ] are not found on Solaris -- please do it manually
else
$FIND $GALLERY_VAR -type f -name "$name"
fi
}
# rename "./resizes/2012/2012-05-Test/2012-05-26 19.17.19.jpg" "2012-05-26 19_17_19.jpg"
function rename()
{
to_be_renamed=$1
new_name="$2"
new_path=$(dirname "$to_be_renamed")/$new_name
rename_file "$to_be_renamed" "$new_path"
}
# rename 111 "2012-05-26 19_17_19.jpg"
function rename_sql()
{
to_be_renamed_id=$1
new_name="$2"
QUERY="UPDATE items set name='$new_name' WHERE id=$to_be_renamed_id;"
update_db "$QUERY"
}
declare -i TEST_FAILED
function assert_mangle()
{
name="$1"
expected="$2"
actual=$(mangle_name "$name")
if [ ! "$actual" == "$expected" ]
then
warn FAILED for \"$name\": expected \"$expected\" but was \"$actual\"
TEST_FAILED=1
fi
}
function run_test()
{
TEST_FAILED=0
assert_mangle "test.gif" "test.gif"
assert_mangle "test..gif" "test_.gif"
assert_mangle "a.b.c.d.png" "a_b_c_d.png"
# Know failing tests
#assert_mangle "with trainling dot.png." "with trailing dot_png."
#assert_mangle "test" "expected"
return $TEST_FAILED
}
run_test || exit 1
[ -f moves.sh ] && rm moves.sh
[ -f updates.sql ] && rm updates.sql
sql "select id,name from items where name like '%.%.%' order by name;" | while read id file
do
if [[ $id =~ ^[0-9]+$ ]]; then
new_name=$(mangle_name "$file")
echo ""
echo "---------------"
echo \"$file\":
files_for_name "$file" | while read to_be_renamed
do
echo \"$file\": Rename \"$to_be_renamed\" to \"$new_name\"
rename "$to_be_renamed" "$new_name"
rename_sql $id "$new_name"
done
fi
done

Comments