summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-04-05 03:54:08 +0000
committerBharat Mediratta <bharat@menalto.com>2009-04-05 03:54:08 +0000
commit846fc51f612997efbee1e5a0d05b773b5a4c7f73 (patch)
tree620b6f7ef37fb408183b7e4f26ad386b7dc7d191
parent7d712ecd9629de7f15f79a175fd95866e75645ac (diff)
Optimization: add all exif/iptc keys in a single insert, instead of
many separate queries. In the process, rip out the summary column, we weren't using it. Clean up the test, and drop the exif_records database on uninstall.
-rw-r--r--modules/exif/controllers/exif.php4
-rw-r--r--modules/exif/helpers/exif.php40
-rw-r--r--modules/exif/helpers/exif_installer.php4
-rw-r--r--modules/exif/tests/Exif_Test.php17
4 files changed, 30 insertions, 35 deletions
diff --git a/modules/exif/controllers/exif.php b/modules/exif/controllers/exif.php
index d46d6083..70f82ad6 100644
--- a/modules/exif/controllers/exif.php
+++ b/modules/exif/controllers/exif.php
@@ -26,8 +26,8 @@ class Exif_Controller extends Controller {
access::required("view", $item);
$view = new View("exif_dialog.html");
- $view->details = exif::get($item, false);
-
+ $view->details = exif::get($item);
+
print $view;
}
}
diff --git a/modules/exif/helpers/exif.php b/modules/exif/helpers/exif.php
index 50a772b5..92b43df7 100644
--- a/modules/exif/helpers/exif.php
+++ b/modules/exif/helpers/exif.php
@@ -30,17 +30,17 @@ class exif_Core {
// Only try to extract EXIF from photos
if ($item->is_photo() && $item->mime_type == "image/jpeg") {
+ $db = Database::instance();
+ $data = array();
require_once(MODPATH . "exif/lib/exif.php");
$exif_raw = read_exif_data_raw($item->file_path(), false);
if (isset($exif_raw['ValidEXIFData'])) {
foreach(self::_keys() as $field => $exifvar) {
if (isset($exif_raw[$exifvar[0]][$exifvar[1]])) {
- $exif_key = ORM::factory("exif_key");
- $exif_key->item_id = $item->id;
- $exif_key->name = $field;
- $exif_key->value = $exif_raw[$exifvar[0]][$exifvar[1]];
- $exif_key->summary = $exifvar[3];
- $exif_key->save();
+ $data[] = sprintf(
+ "(%d, '%s', '%s')",
+ $item->id, $db->escape_str($field),
+ $db->escape_str($exif_raw[$exifvar[0]][$exifvar[1]]));
}
}
}
@@ -49,15 +49,19 @@ class exif_Core {
$iptc = iptcparse($info["APP13"]);
foreach (array("Keywords" => "2#025", "Caption" => "2#120") as $keyword => $iptc_key) {
if (!empty($iptc[$iptc_key])) {
- $exif_key = ORM::factory("exif_key");
- $exif_key->item_id = $item->id;
- $exif_key->name = $keyword;
- $exif_key->value = implode(" ", $iptc[$iptc_key]);
- $exif_key->summary = false;
- $exif_key->save();
+ $data[] = sprintf(
+ "(%d, '%s', '%s')",
+ $item->id, $db->escape_str($keyword),
+ $db->escape_str(implode(" ", $iptc[$iptc_key])));
}
}
}
+
+ // ORM and Database::insert() do not handle inserting multiple rows at once, so do it
+ // the hard way.
+ $query = "INSERT INTO {exif_keys} (`item_id`, `name`, `value`) VALUES" .
+ join(",", $data);
+ $db->query($query);
}
$record = ORM::factory("exif_record")->where("item_id", $item->id)->find();
@@ -68,14 +72,11 @@ class exif_Core {
$record->save();
}
- static function get($item, $summary=true) {
+ static function get($item) {
$exif = array();
- $exif_key = ORM::factory("exif_key")
- ->where("item_id", $item->id);
- if ($summary) {
- $exif_key->where("summary", $summary);
- }
- $keys = $exif_key->find_all();
+ $keys = ORM::factory("exif_key")
+ ->where("item_id", $item->id)
+ ->find_all();
$definitions = self::_keys();
foreach ($keys as $key) {
$exif[] = array("caption" => $definitions[$key->name][2], "value" => $key->value);
@@ -84,7 +85,6 @@ class exif_Core {
return $exif;
}
-
private static function _keys() {
if (!isset(self::$exif_keys)) {
self::$exif_keys = array(
diff --git a/modules/exif/helpers/exif_installer.php b/modules/exif/helpers/exif_installer.php
index 80990917..b0173e41 100644
--- a/modules/exif/helpers/exif_installer.php
+++ b/modules/exif/helpers/exif_installer.php
@@ -27,10 +27,9 @@ class exif_installer {
`id` int(9) NOT NULL auto_increment,
`item_id` int(9) NOT NULL,
`name` varchar(64) NOT NULL,
- `summary` boolean NOT NULL,
`value` varchar(1024) NOT NULL,
PRIMARY KEY (`id`),
- UNIQUE KEY(`item_id`, `summary`, `name`))
+ UNIQUE KEY(`item_id`, `name`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;");
$db->query("CREATE TABLE IF NOT EXISTS {exif_records} (
`id` int(9) NOT NULL auto_increment,
@@ -45,6 +44,7 @@ class exif_installer {
static function uninstall() {
$db = Database::instance();
$db->query("DROP TABLE IF EXISTS {exif_keys};");
+ $db->query("DROP TABLE IF EXISTS {exif_records};");
module::delete("exif");
}
}
diff --git a/modules/exif/tests/Exif_Test.php b/modules/exif/tests/Exif_Test.php
index 4ab1e21e..466a52bd 100644
--- a/modules/exif/tests/Exif_Test.php
+++ b/modules/exif/tests/Exif_Test.php
@@ -21,9 +21,9 @@ class Exif_Test extends Unit_Test_Case {
public function exif_extract_test() {
$rand = rand();
$root = ORM::factory("item", 1);
- $photo = photo::create($root, dirname(__FILE__) . "/data/image.jpg", "$rand.jpg", $rand, $rand);
-
- $exif = exif::get($photo);
+ $photo = photo::create(
+ $root, DOCROOT . "modules/exif/tests/data/image.jpg", "$rand.jpg", $rand, $rand);
+
$expected = array(
array("caption" => "Camera Maker", "value" => "Pentax Corporation"),
array("caption" => "Camera Model", "value" => "PENTAX K10D"),
@@ -36,12 +36,7 @@ class Exif_Test extends Unit_Test_Case {
array("caption" => "ISO", "value" => "6553700"),
array("caption" => "Metering Mode", "value" => "Multi-Segment"),
array("caption" => "Shutter Speed", "value" => "1/60 sec"),
- array("caption" => "Date/Time", "value" => "2008:03:17 17:41:25")
- );
- $this->assert_equal($expected, $exif);
-
- $exif = exif::get($photo, false);
- $expected = array_merge($expected, array(
+ array("caption" => "Date/Time", "value" => "2008:03:17 17:41:25"),
array("caption" => "Copyright", "value" => "(C) 2008 - T. Almdal"),
array("caption" => "Orientation", "value" => "1: Normal (0 deg)"),
array("caption" => "Resolution Unit", "value" => "Inch"),
@@ -50,7 +45,7 @@ class Exif_Test extends Unit_Test_Case {
array("caption" => "Brightness Value", "value" => "0"),
array("caption" => "Scene Type", "value" => "0"),
array("caption" => "Subject Distance", "value" => "0"),
- ));
- $this->assert_equal($expected, $exif);
+ );
+ $this->assert_equal($expected, exif::get($photo));
}
} \ No newline at end of file