From 590a4677f34f866911fa6c0902130f8dbea7421a Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 15 Feb 2009 08:08:22 +0000 Subject: Tweak EXIF extraction code to be more robust. * Create Exif_Record_Model to track whether we've scanned the EXIF data for this photo or not. This allows us to track photos that don't have EXIF data (and won't have any Exif_Keys) * Blow away old Exif_Keys when extracting, else we hit unique index constraints. * exif::_get_stats() -- before it was running the task forever --- modules/exif/helpers/exif.php | 17 ++++++++++---- modules/exif/helpers/exif_event.php | 3 +++ modules/exif/helpers/exif_installer.php | 6 +++++ modules/exif/helpers/exif_task.php | 39 ++++++++++++++++++++------------- 4 files changed, 46 insertions(+), 19 deletions(-) (limited to 'modules/exif/helpers') diff --git a/modules/exif/helpers/exif.php b/modules/exif/helpers/exif.php index ee128353..55877140 100644 --- a/modules/exif/helpers/exif.php +++ b/modules/exif/helpers/exif.php @@ -22,10 +22,12 @@ * This is the API for handling exif data. */ class exif_Core { - + protected static $exif_keys; - + public static function extract($item) { + ORM::factory("exif_key")->where("item_id", $item->id)->delete_all(); + // Only try to extract EXIF from photos if ($item->is_photo() && $item->mime_type == "image/jpeg") { require_once(MODPATH . "exif/lib/exif.php"); @@ -57,6 +59,13 @@ class exif_Core { } } } + + $record = ORM::factory("exif_record")->where("item_id", $item->id)->find(); + if (!$record->loaded) { + $record->item_id = $item->id; + } + $record->dirty = 0; + $record->save(); } public static function get($item, $summary=true) { @@ -71,10 +80,10 @@ class exif_Core { foreach ($keys as $key) { $exif[] = array("caption" => $definitions[$key->name][2], "value" => $key->value); } - + return $exif; } - + private static function _keys() { if (!isset(self::$exif_keys)) { diff --git a/modules/exif/helpers/exif_event.php b/modules/exif/helpers/exif_event.php index ac627b11..bc34d63d 100644 --- a/modules/exif/helpers/exif_event.php +++ b/modules/exif/helpers/exif_event.php @@ -26,5 +26,8 @@ class exif_event_Core { ORM::factory("exif_key") ->where("item_id", $item->id) ->delete_all(); + ORM::factory("exif_info") + ->where("item_id", $item->id) + ->delete_all(); } } diff --git a/modules/exif/helpers/exif_installer.php b/modules/exif/helpers/exif_installer.php index a008581d..791cd4a6 100644 --- a/modules/exif/helpers/exif_installer.php +++ b/modules/exif/helpers/exif_installer.php @@ -32,6 +32,12 @@ class exif_installer { PRIMARY KEY (`id`), UNIQUE KEY(`item_id`, `summary`, `name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE IF NOT EXISTS `exif_records` ( + `id` int(9) NOT NULL auto_increment, + `item_id` int(9) NOT NULL, + `dirty` BOOLEAN default 1, + PRIMARY KEY (`id`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); module::set_version("exif", 1); } } diff --git a/modules/exif/helpers/exif_task.php b/modules/exif/helpers/exif_task.php index f430d185..82d78f4c 100644 --- a/modules/exif/helpers/exif_task.php +++ b/modules/exif/helpers/exif_task.php @@ -19,12 +19,28 @@ */ class exif_task_Core { static function available_tasks() { + $db = Database::instance(); + + // Delete extra exif_records + $db->query( + "DELETE `exif_records`.* FROM `exif_records` " . + "LEFT JOIN `items` ON (`exif_records`.`item_id` = `items`.`id`) " . + "WHERE `items`.`id` IS NULL"); + + // Insert missing exif_records + $db->query( + "INSERT INTO `exif_records`(`item_id`) (" . + " SELECT `items`.`id` FROM `items` " . + " LEFT JOIN `exif_records` ON (`exif_records`.`item_id` = `items`.`id`) " . + " WHERE `exif_records`.`id` IS NULL)"); + list ($remaining, $total, $percent) = self::_get_stats(); return array(Task_Definition::factory() ->callback("exif_task::extract_exif") ->name(t("Extract EXIF data")) - ->description($remaining ? - sprintf(t("EXIF data is available for %d%% of the images"), $percent) + ->description($remaining + ? t("%percent% of your photos need to be scanned for EXIF data", + array("percent" => $percent)) : t("EXIF data is up-to-date")) ->severity($remaining ? log::WARNING : log::SUCCESS)); } @@ -33,9 +49,8 @@ class exif_task_Core { $completed = $task->get("completed", 0); $work = ORM::factory("item") - ->join("exif_keys", "items.id", "item_id", "LEFT") - ->where("items.type", "photo") - ->where("exif_keys.id", null) + ->join("exif_records", "items.id", "exif_records.item_id") + ->where("exif_records.dirty", 1) ->find(); exif::extract($work); $completed++; @@ -55,15 +70,9 @@ class exif_task_Core { } private static function _get_stats() { - $exif_count = ORM::factory("exif_key") - ->select("DISTINCT item_id") - ->find_all() - ->count(); - - $items_count = ORM::factory("item") - ->where("type", "photo") - ->count_all(); - return array($exif_count - $items_count, $items_count, - round(100 * ($exif_count / $items_count))); + $missing_exif = ORM::factory("exif_record")->where("dirty", 1)->count_all(); + $total_items = ORM::factory("item")->count_all(); + return array($missing_exif, $total_items, + round(100 * (($total_items - $missing_exif) / $total_items))); } } -- cgit v1.2.3