diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-04-21 04:21:34 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-04-21 04:21:34 +0000 |
commit | 201e8cdf7046937eef0e53542d777bf67c496054 (patch) | |
tree | defa5d3aa5f241e60d9e8e4f3b62c5f26ad6165f | |
parent | e9867fb97cfcf1c5ae98f3ce6c8a0c6791b018ca (diff) |
Highlight: we can now import photos and movies.
Notes:
* Don't store the g2->g3 map in the task; it gets too large and
overflows the context column.
* Import album sort orders
* Try to treat duplication in the summary and description fields
sensibly.
* Move the g2 import config page under the Settings menu
* Clean up the settings page slightly and send users to the
maintenance page to do the actual import (not an optimal UI, but it
works).
-rw-r--r-- | modules/g2_import/helpers/g2_import.php | 160 | ||||
-rw-r--r-- | modules/g2_import/helpers/g2_import_menu.php | 2 | ||||
-rw-r--r-- | modules/g2_import/helpers/g2_import_task.php | 21 | ||||
-rw-r--r-- | modules/g2_import/views/admin_g2_import.html.php | 7 |
4 files changed, 140 insertions, 50 deletions
diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php index b0b91bfb..d0b11e25 100644 --- a/modules/g2_import/helpers/g2_import.php +++ b/modules/g2_import/helpers/g2_import.php @@ -32,6 +32,7 @@ function g2() { class g2_import_Core { public static $init = false; + public static $map = array(); static function is_configured() { return module::get_var("g2_import", "embed_path"); @@ -96,9 +97,9 @@ class g2_import_Core { return $stats; } - static function import_group(&$queue, &$map) { + static function import_group(&$queue) { $g2_group_id = array_shift($queue); - if (array_key_exists($g2_group_id, $map)) { + if (self::map($g2_group_id)) { return; } @@ -120,20 +121,17 @@ class g2_import_Core { break; } - $map[$g2_group->getId()] = $group->id; - $g2_map = ORM::factory("g2_map"); - $g2_map->g3_id = $group->id; - $g2_map->g2_id = $g2_group->getId(); - $g2_map->save(); + self::set_map($g2_group->getId(), $group->id); } - static function import_user(&$queue, &$map) { + static function import_user(&$queue) { $g2_user_id = array_shift($queue); - if (array_key_exists($g2_user_id, $map)) { + if (self::map($g2_user_id)) { return; } if (g2(GalleryCoreApi::isAnonymousUser($g2_user_id))) { + self::set_map($g2_user_id, user::guest()->id); return; } @@ -156,19 +154,15 @@ class g2_import_Core { if ($g2_group_id == $g2_admin_group_id) { $user->admin = true; } else { - $user->add(ORM::factory("group", $map[$g2_group_id])); + $user->add(ORM::factory("group", self::map($g2_group_id))); } } $user->save(); - $map[$g2_user->getId()] = $user->id; - $g2_map = ORM::factory("g2_map"); - $g2_map->g3_id = $user->id; - $g2_map->g2_id = $g2_user->getId(); - $g2_map->save(); + self::set_map($g2_user->getId(), $user->id); } - static function import_album(&$queue, &$map) { + static function import_album(&$queue) { // The queue is a set of nested associative arrays where the key is the album id and the // value is an array of similar arrays. We'll do a breadth first tree traversal using the // queue to keep our state. Doing it breadth first means that the parent will be created by @@ -181,7 +175,7 @@ class g2_import_Core { $queue[$key] = $value; } - if (array_key_exists($g2_album_id, $map)) { + if (self::map($g2_album_id)) { return; } @@ -190,11 +184,90 @@ class g2_import_Core { if ($g2_album->getParentId() == null) { return; } - $parent_album = ORM::factory("item", $map[$g2_album->getParentId()]); + $parent_album = ORM::factory("item", self::map($g2_album->getParentId())); + + $album = album::create( + $parent_album, + $g2_album->getPathComponent(), + $g2_album->getTitle(), + self::extract_description($g2_album), + self::map($g2_album->getOwnerId())); + + $album->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_album_id)); + $album->created = $g2_album->getCreationTimestamp(); + + // @todo supported "keywords", "originationTimestamp", and "random" sort orders. + $order_map = array( + "creationTimestamp" => "created", + "description" => "description", + "modificationTimestamp" => "updated", + "orderWeight" => "weight", + "pathComponent" => "name", + "summary" => "description", + "title" => "title", + "viewCount" => "view_count"); + $direction_map = array( + ORDER_ASCENDING => "asc", + ORDER_DESCENDING => "desc"); + if (array_key_exists($g2_order = $g2_album->getOrderBy(), $order_map)) { + $album->sort_column = $order_map[$g2_order]; + $album->sort_order = $direction_map[$g2_album->getOrderDirection()]; + } + $album->save(); + + self::set_map($g2_album_id, $album->id); + + // @todo import origination timestamp + // @todo import keywords as tags + } + + static function import_item(&$queue) { + $g2_item_id = array_shift($queue); + + if (self::map($g2_item_id)) { + return; + } + + $g2_item = g2(GalleryCoreApi::loadEntitiesById($g2_item_id)); + $parent = ORM::factory("item", self::map($g2_item->getParentId())); + switch ($g2_item->getEntityType()) { + case "GalleryPhotoItem": + $item = photo::create( + $parent, + g2($g2_item->fetchPath()), + $g2_item->getPathComponent(), + $g2_item->getTitle(), + self::extract_description($g2_item), + self::map($g2_item->getOwnerId())); + break; + case "GalleryMovieItem": + // @todo we should transcode other types into FLV + if (in_array($g2_item->getMimeType(), array("video/mp4", "video/x-flv"))) { + $item = movie::create( + $parent, + g2($g2_item->fetchPath()), + $g2_item->getPathComponent(), + $g2_item->getTitle(), + self::extract_description($g2_item), + self::map($g2_item->getOwnerId())); + } + break; + + default: + // Ignore + break; + } + + if (isset($item)) { + self::set_map($g2_item_id, $item->id); + } + } + + static function extract_description($g2_item) { // If the summary is a subset of the description just import the description, else import both. - $g2_summary = $g2_album->getSummary(); - $g2_description = $g2_album->getDescription(); + $g2_summary = $g2_item->getSummary(); + $g2_description = $g2_item->getDescription(); if (!$g2_summary || $g2_summary == $g2_description || strstr($g2_description, $g2_summary) !== false) { @@ -202,26 +275,41 @@ class g2_import_Core { } else { $description = $g2_summary . " " . $g2_description; } + return $description; + } - $album = album::create( - $parent_album, - $g2_album->getPathComponent(), - $g2_album->getTitle(), - $description, - $map[$g2_album->getOwnerId()]); + static function get_item_ids($min_id) { + global $gallery; + + $ids = array(); + $results = g2($gallery->search( + "SELECT [GalleryItem::id] " . + "FROM [GalleryEntity], [GalleryItem] " . + "WHERE [GalleryEntity::id] = [GalleryItem::id] " . + "AND [GalleryEntity::entityType] IN ('GalleryPhotoItem', 'GalleryMovieItem') " . + "AND [GalleryItem::id] > ? " . + "ORDER BY [GalleryItem::id] ASC", + array($min_id), + array("limit" => array("count" => 100)))); + while ($result = $results->nextResult()) { + $ids[] = $result[0]; + } + return $ids; + } - $album->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_album_id)); - $album->created = $g2_album->getCreationTimestamp(); - $album->save(); + static function map($g2_id) { + if (!array_key_exists($g2_id, self::$map)) { + $g2_map = ORM::factory("g2_map")->where("g2_id", $g2_id)->find(); + self::$map[$g2_id] = $g2_map->loaded ? $g2_map->g3_id : null; + } + + return self::$map[$g2_id]; + } - $map[$g2_album->getId()] = $album->id; + static function set_map($g2_id, $g3_id) { $g2_map = ORM::factory("g2_map"); - $g2_map->g3_id = $album->id; - $g2_map->g2_id = $g2_album->getId(); + $g2_map->g3_id = $g3_id; + $g2_map->g2_id = $g2_id; $g2_map->save(); - - // @todo import origination timestamp - // @todo import sort order - // @todo import keywords as tags } } diff --git a/modules/g2_import/helpers/g2_import_menu.php b/modules/g2_import/helpers/g2_import_menu.php index 3eff2714..3163e04e 100644 --- a/modules/g2_import/helpers/g2_import_menu.php +++ b/modules/g2_import/helpers/g2_import_menu.php @@ -20,7 +20,7 @@ class g2_import_menu_Core { static function admin($menu, $theme) { $menu - ->get("content_menu") + ->get("settings_menu") ->append(Menu::factory("link") ->id("g2_import") ->label(t("Gallery 2 Import")) diff --git a/modules/g2_import/helpers/g2_import_task.php b/modules/g2_import/helpers/g2_import_task.php index fea949e5..45f0fe52 100644 --- a/modules/g2_import/helpers/g2_import_task.php +++ b/modules/g2_import/helpers/g2_import_task.php @@ -33,10 +33,10 @@ class g2_import_task_Core { static function import($task) { $start = microtime(true); g2_import::init(); + $stats = $task->get("stats"); $total = $task->get("total"); $completed = $task->get("completed"); - $map = $task->get("map"); $i = $task->get("i"); $mode = $task->get("mode"); $queue = $task->get("queue"); @@ -54,11 +54,6 @@ class g2_import_task_Core { $root->g3_id = 1; $root->save(); } - - $map = array(); - foreach (ORM::factory("g2_map")->find_all() as $row) { - $map[$row->g2_id] = $row->id; - } } $modes = array("groups", "users", "albums", "photos", "comments", "done"); @@ -66,6 +61,7 @@ class g2_import_task_Core { if ($i >= ($stats[$modes[$mode]] - 1)) { $i = 0; $mode++; + $queue = array(); } switch($modes[$mode]) { @@ -73,7 +69,7 @@ class g2_import_task_Core { if (!$i) { $task->set("queue", $queue = array_keys(g2(GalleryCoreApi::fetchGroupNames()))); } - g2_import::import_group($queue, $map); + g2_import::import_group($queue); $task->status = t( "Importing groups %count / %total", array("count" => $i, "total" => $stats["groups"])); break; @@ -83,7 +79,7 @@ class g2_import_task_Core { $task->set( "queue", $queue = array_keys(g2(GalleryCoreApi::fetchUsersForGroup(GROUP_EVERYBODY)))); } - g2_import::import_user($queue, $map); + g2_import::import_user($queue); $task->status = t( "Importing users %count / %total", array("count" => $i, "total" => $stats["users"])); break; @@ -92,13 +88,19 @@ class g2_import_task_Core { if (!$i) { $task->set("queue", $queue = g2(GalleryCoreApi::fetchAlbumTree())); } - g2_import::import_album($queue, $map); + g2_import::import_album($queue); $task->set("queue", $queue); $task->status = t( "Importing albums %count / %total", array("count" => $i, "total" => $stats["albums"])); break; case "photos": + if (empty($queue)) { + $task->set("queue", $queue = g2_import::get_item_ids($task->get("last_id", 0))); + $task->set("last_id", end($queue)); + } + + g2_import::import_item($queue); $task->status = t( "Importing photos %count / %total", array("count" => $i, "total" => $stats["photos"])); break; @@ -125,7 +127,6 @@ class g2_import_task_Core { $task->set("completed", $completed); $task->set("mode", $mode); $task->set("i", $i); - $task->set("map", $map); $task->set("queue", $queue); } } diff --git a/modules/g2_import/views/admin_g2_import.html.php b/modules/g2_import/views/admin_g2_import.html.php index 9b61b584..8b116ccd 100644 --- a/modules/g2_import/views/admin_g2_import.html.php +++ b/modules/g2_import/views/admin_g2_import.html.php @@ -12,9 +12,10 @@ <div id="gAdminG2Import"> <h1> <?= t("Import") ?> </h1> <div class="gSuccess"> - <?= t("We've detected Gallery version: %version", array("version" => g2_import::version())) ?> + <?= t("Gallery version %version detected", array("version" => g2_import::version())) ?> </div> - <a href="<?= url::site("admin/maintenance/start/g2_import::import?csrf=$csrf") ?>" - class="gPanelLink"><?= t("Begin Import") ?></a> + + <?= t("You can perform an import on the <a href=\"%url\">maintenance page</a>", + array("url" => url::site("admin/maintenance"))) ?> </div> <? endif ?> |