public function save() { $form = $this->_get_import_form(); if ($form->validate()) { $embed_path = $form->configure_g2_import->embed_path->value; if (!is_file($embed_path) && file_exists("{$embed_path}/embed.php")) { $embed_path = "{$embed_path}/embed.php"; } if (g2_import::is_valid_embed_path($embed_path)) { message::success("Gallery 2 path saved."); module::set_var("g2_import", "embed_path", $embed_path); url::redirect("admin/g2_import"); } else { $form->configure_g2_import->embed_path->add_error("invalid", 1); } } $view = new Admin_View("admin.html"); $view->content = new View("admin_g2_import.html"); $view->content->form = $form; print $view; }
static function item_created($item) { g2_import::copy_matching_thumbnails_and_resizes($item); }
/** * Import a single photo or movie. */ static function import_item(&$queue) { $g2_item_id = array_shift($queue); if (self::map($g2_item_id)) { return t("Item with id: %id already imported, skipping", array("id" => $g2_item_id)); } try { self::$current_g2_item = $g2_item = g2(GalleryCoreApi::loadEntitiesById($g2_item_id)); $g2_path = g2($g2_item->fetchPath()); } catch (Exception $e) { return t("Failed to import Gallery 2 item with id: %id\n%exception", array("id" => $g2_item_id, "exception" => $e->__toString())); } $parent = ORM::factory("item", self::map($g2_item->getParentId())); $g2_type = $g2_item->getEntityType(); $corrupt = 0; if (!file_exists($g2_path)) { // If the Gallery 2 source image isn't available, this operation is going to fail. That can // happen in cases where there's corruption in the source Gallery 2. In that case, fall // back on using a broken image. It's important that we import *something* otherwise // anything that refers to this item in Gallery 2 will have a dangling pointer in Gallery 3 // // Note that this will change movies to be photos, if there's a broken movie. Hopefully // this case is rare enough that we don't need to take any heroic action here. g2_import::log(t("%path missing in import; replacing it with a placeholder", array("path" => $g2_path))); $g2_path = MODPATH . "g2_import/data/broken-image.gif"; $g2_type = "GalleryPhotoItem"; $corrupt = 1; } $message = array(); switch ($g2_type) { case "GalleryPhotoItem": if (!in_array($g2_item->getMimeType(), array("image/jpeg", "image/gif", "image/png"))) { Kohana::log("alert", "{$g2_path} is an unsupported image type; using a placeholder gif"); $message[] = t("'%path' is an unsupported image type, using a placeholder", array("path" => $g2_path)); $g2_path = MODPATH . "g2_import/data/broken-image.gif"; $corrupt = 1; } try { $item = photo::create($parent, $g2_path, $g2_item->getPathComponent(), self::_decode_html_special_chars($g2_item->getTitle()), self::_decode_html_special_chars(self::extract_description($g2_item)), self::map($g2_item->getOwnerId())); } catch (Exception $e) { Kohana::log("alert", "Corrupt image {$g2_path}\n" . $e->__toString()); $message[] = t("Corrupt image '%path'", array("path" => $g2_path)); $message[] = $e->__toString(); $corrupt = 1; } break; case "GalleryMovieItem": // @todo we should transcode other types into FLV if (in_array($g2_item->getMimeType(), array("video/mp4", "video/x-flv"))) { try { $item = movie::create($parent, $g2_path, $g2_item->getPathComponent(), self::_decode_html_special_chars($g2_item->getTitle()), self::_decode_html_special_chars(self::extract_description($g2_item)), self::map($g2_item->getOwnerId())); } catch (Exception $e) { Kohana::log("alert", "Corrupt movie {$g2_path}\n" . $e->__toString()); $message[] = t("Corrupt movie '%path'", array("path" => $g2_path)); $message[] = $e->__toString(); $corrupt = 1; } } else { Kohana::log("alert", "{$g2_path} is an unsupported movie type"); $message[] = t("'%path' is an unsupported movie type", array("path" => $g2_path)); $corrupt = 1; } break; default: // Ignore break; } if (!empty($item)) { self::import_keywords_as_tags($g2_item->getKeywords(), $item); } if (isset($item)) { self::set_map($g2_item_id, $item->id); $item->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_item_id)); $item->save(); } if ($corrupt) { $url_generator = $GLOBALS["gallery"]->getUrlGenerator(); // @todo we need a more persistent warning $g2_item_url = $url_generator->generateUrl(array("itemId" => $g2_item->getId())); // Why oh why did I ever approve the session id placeholder idea in G2? $g2_item_url = str_replace('&g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT', '', $g2_item_url); if (!empty($item)) { $message[] = t("<a href=\"%g2_url\">%title</a> from Gallery 2 could not be processed; " . "(imported as <a href=\"%g3_url\">%title</a>)", array("g2_url" => $g2_item_url, "g3_url" => $item->url(), "title" => $g2_item->getTitle())); } else { $message[] = t("<a href=\"%g2_url\">%title</a> from Gallery 2 could not be processed", array("g2_url" => $g2_item_url, "title" => $g2_item->getTitle())); } } self::$current_g2_item = null; return $message; }
static function import($task) { $start = microtime(true); g2_import::init(); $stats = $task->get("stats"); $done = $task->get("done"); $total = $task->get("total"); $completed = $task->get("completed"); $mode = $task->get("mode"); $queue = $task->get("queue"); if (!isset($mode)) { $stats = g2_import::stats(); $stats["items"] = $stats["photos"] + $stats["movies"]; unset($stats["photos"]); unset($stats["movies"]); $stats["highlights"] = $stats["albums"]; $task->set("stats", $stats); $task->set("total", $total = array_sum(array_values($stats))); $completed = 0; $mode = 0; $done = array(); foreach (array_keys($stats) as $key) { $done[$key] = 0; } $task->set("done", $done); $root_g2_id = g2(GalleryCoreApi::getDefaultAlbumId()); $root = ORM::factory("g2_map")->where("g2_id", "=", $root_g2_id)->find(); if (!$root->loaded()) { $root->g2_id = $root_g2_id; $root->g3_id = 1; $root->save(); } } $modes = array("groups", "users", "albums", "items", "comments", "tags", "highlights", "done"); while (!$task->done && microtime(true) - $start < 1.5) { if ($done[$modes[$mode]] == $stats[$modes[$mode]]) { // Nothing left to do for this mode. Advance. $mode++; $task->set("last_id", 0); $queue = array(); // Start the loop from the beginning again. This way if we get to a mode that requires no // actions (eg, if the G2 comments module isn't installed) we won't try to do any comments // queries.. in the next iteration we'll just skip over that mode. if ($modes[$mode] != "done") { continue; } } switch ($modes[$mode]) { case "groups": if (empty($queue)) { $task->set("queue", $queue = array_keys(g2(GalleryCoreApi::fetchGroupNames()))); } $log_message = g2_import::import_group($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing groups (%count of %total)", array("count" => $done["groups"] + 1, "total" => $stats["groups"])); break; case "users": if (empty($queue)) { $task->set("queue", $queue = array_keys(g2(GalleryCoreApi::fetchUsersForGroup(GROUP_EVERYBODY)))); } $log_message = g2_import::import_user($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing users (%count of %total)", array("count" => $done["users"] + 1, "total" => $stats["users"])); break; case "albums": if (empty($queue)) { $task->set("queue", $queue = g2(GalleryCoreApi::fetchAlbumTree())); } $log_message = g2_import::import_album($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing albums (%count of %total)", array("count" => $done["albums"] + 1, "total" => $stats["albums"])); break; case "items": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_item_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_item($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing photos (%count of %total)", array("count" => $done["items"] + 1, "total" => $stats["items"])); break; case "comments": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_comment_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_comment($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing comments (%count of %total)", array("count" => $done["comments"] + 1, "total" => $stats["comments"])); break; case "tags": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_tag_item_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_tags_for_item($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing tags (%count of %total)", array("count" => $done["tags"] + 1, "total" => $stats["tags"])); break; case "highlights": if (empty($queue)) { $task->set("queue", $queue = g2(GalleryCoreApi::fetchAlbumTree())); } $log_message = g2_import::set_album_highlight($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Album highlights (%count of %total)", array("count" => $done["tags"] + 1, "total" => $stats["albums"])); break; case "done": $task->status = t("Import complete"); $task->done = true; $task->state = "success"; break; } if (!$task->done) { $done[$modes[$mode]]++; $completed++; } } $task->percent_complete = 100 * ($completed / $total); $task->set("completed", $completed); $task->set("mode", $mode); $task->set("queue", $queue); $task->set("done", $done); }
<?php echo $form; ?> <?php if (g2_import::is_initialized()) { ?> <div id="gAdminG2ImportDetails"> <h2> <?php echo t("Import"); ?> </h2> <ul id="gMessage"> <li class="gSuccess"> <?php echo t("Gallery version %version detected", array("version" => g2_import::version())); ?> </li> <?php if ($g2_sizes["thumb"]["size"] && $thumb_size != $g2_sizes["thumb"]["size"]) { ?> <li class="gWarning"> <?php echo t("Your most common thumbnail size in Gallery 2 is %g2_pixels pixels, but your Gallery 3 thumbnail size is set to %g3_pixels pixels. <a href=\"%url\">Using the same value</a> will speed up your import.", array("g2_pixels" => $g2_sizes["thumb"]["size"], "g3_pixels" => $thumb_size, "url" => html::mark_clean(url::site("admin/theme_options")))); ?> </li> <?php } ?> <?php
/** * Import a single photo or movie. */ static function import_item(&$queue) { $g2_item_id = array_shift($queue); if (self::map($g2_item_id)) { return; } try { self::$current_g2_item = $g2_item = g2(GalleryCoreApi::loadEntitiesById($g2_item_id)); $g2_path = g2($g2_item->fetchPath()); } catch (Exception $e) { return t("Failed to import Gallery 2 item with id: %id\n%exception", array("id" => $g2_item_id, "exception" => (string) $e)); } $parent = ORM::factory("item", self::map($g2_item->getParentId())); $g2_type = $g2_item->getEntityType(); $corrupt = 0; if (!file_exists($g2_path)) { // If the Gallery 2 source image isn't available, this operation is going to fail. That can // happen in cases where there's corruption in the source Gallery 2. In that case, fall // back on using a broken image. It's important that we import *something* otherwise // anything that refers to this item in Gallery 2 will have a dangling pointer in Gallery 3 // // Note that this will change movies to be photos, if there's a broken movie. Hopefully // this case is rare enough that we don't need to take any heroic action here. g2_import::log(t("%path missing in import; replacing it with a placeholder", array("path" => $g2_path))); $g2_path = MODPATH . "g2_import/data/broken-image.gif"; $g2_type = "GalleryPhotoItem"; $corrupt = 1; } $messages = array(); switch ($g2_type) { case "GalleryPhotoItem": if (!in_array($g2_item->getMimeType(), array("image/jpeg", "image/gif", "image/png"))) { Kohana_Log::add("alert", "{$g2_path} is an unsupported image type; using a placeholder gif"); $messages[] = t("'%path' is an unsupported image type, using a placeholder", array("path" => $g2_path)); $g2_path = MODPATH . "g2_import/data/broken-image.gif"; $corrupt = 1; } try { $item = ORM::factory("item"); $item->type = "photo"; $item->parent_id = $parent->id; $item->set_data_file($g2_path); $item->name = $g2_item->getPathComponent(); $item->title = self::_decode_html_special_chars($g2_item->getTitle()); $item->title or $item->title = $item->name; $item->description = self::_decode_html_special_chars(self::extract_description($g2_item)); $item->owner_id = self::map($g2_item->getOwnerId()); $item->save(); } catch (Exception $e) { $exception_info = (string) new G2_Import_Exception(t("Corrupt image '%path'", array("path" => $g2_path)), $e, $messages); Kohana_Log::add("alert", "Corrupt image {$g2_path}\n" . $exception_info); $messages[] = $exception_info; $corrupt = 1; $item = null; } break; case "GalleryMovieItem": // @todo we should transcode other types into FLV if (in_array($g2_item->getMimeType(), array("video/mp4", "video/x-flv"))) { try { $item = ORM::factory("item"); $item->parent_id = $parent->id; $item->set_data_file($g2_path); $item->name = $g2_item->getPathComponent(); $item->title = self::_decode_html_special_chars($g2_item->getTitle()); $item->title or $item->title = $item->name; $item->description = self::_decode_html_special_chars(self::extract_description($g2_item)); $item->owner_id = self::map($g2_item->getOwnerId()); $item->save(); } catch (Exception $e) { $exception_info = (string) new G2_Import_Exception(t("Corrupt movie '%path'", array("path" => $g2_path)), $e, $messages); Kohana_Log::add("alert", "Corrupt movie {$g2_path}\n" . $exception_info); $messages[] = $exception_info; $corrupt = 1; $item = null; } } else { Kohana_Log::add("alert", "{$g2_path} is an unsupported movie type"); $messages[] = t("'%path' is an unsupported movie type", array("path" => $g2_path)); $corrupt = 1; } break; default: // Ignore break; } if (!empty($item)) { self::import_keywords_as_tags($g2_item->getKeywords(), $item); } $g2_item_url = self::g2_url(array("view" => "core.ShowItem", "itemId" => $g2_item->getId())); if (isset($item)) { try { $item->view_count = (int) g2(GalleryCoreApi::fetchItemViewCount($g2_item_id)); } catch (Exception $e) { $view_count = 1; } $item->save(); self::set_map($g2_item_id, $item->id, "item", $g2_item_url); self::set_map($g2_item_id, $item->id, "file", self::g2_url(array("view" => "core.DownloadItem", "itemId" => $g2_item_id))); $derivatives = g2(GalleryCoreApi::fetchDerivativesByItemIds(array($g2_item_id))); if (!empty($derivatives[$g2_item_id])) { foreach ($derivatives[$g2_item_id] as $derivative) { switch ($derivative->getDerivativeType()) { case DERIVATIVE_TYPE_IMAGE_THUMBNAIL: $resource_type = "thumbnail"; break; case DERIVATIVE_TYPE_IMAGE_RESIZE: $resource_type = "resize"; break; case DERIVATIVE_TYPE_IMAGE_PREFERRED: $resource_type = "full"; break; } self::set_map($derivative->getId(), $item->id, $resource_type, self::g2_url(array("view" => "core.DownloadItem", "itemId" => $derivative->getId()))); } } } if ($corrupt) { if (!empty($item)) { $title = $g2_item->getTitle(); $title or $title = $g2_item->getPathComponent(); $messages[] = t("<a href=\"%g2_url\">%title</a> from Gallery 2 could not be processed; " . "(imported as <a href=\"%g3_url\">%title</a>)", array("g2_url" => $g2_item_url, "g3_url" => $item->url(), "title" => $title)); } else { $messages[] = t("<a href=\"%g2_url\">%title</a> from Gallery 2 could not be processed", array("g2_url" => $g2_item_url, "title" => $g2_item->getTitle())); } } self::$current_g2_item = null; return $messages; }
/** * Import all the tags for a single item */ static function import_tags_for_item(&$queue) { GalleryCoreApi::requireOnce("modules/tags/classes/TagsHelper.class"); $g2_item_id = array_shift($queue); $g3_item = ORM::factory("item", self::map($g2_item_id)); try { $tag_names = array_values(g2(TagsHelper::getTagsByItemId($g2_item_id))); } catch (Exception $e) { g2_import::log("Failed to import tags for Gallery 2 item with id: %id", array("id" => $g2_item_id)); return; } foreach ($tag_names as $tag_name) { $tag = tag::add($g3_item, $tag_name); } // Tag operations are idempotent so we don't need to map them. Which is good because we don't // have an id for each individual tag mapping anyway so it'd be hard to set up the mapping. }
static function import($task) { g2_import::lower_error_reporting(); $start = microtime(true); g2_import::init(); $stats = $task->get("stats"); $done = $task->get("done"); $total = $task->get("total"); $completed = $task->get("completed"); $mode = $task->get("mode"); $queue = $task->get("queue"); if (!isset($mode)) { $stats = g2_import::g2_stats(); $stats["items"] = $stats["photos"] + $stats["movies"]; unset($stats["photos"]); unset($stats["movies"]); $stats["highlights"] = $stats["albums"]; $task->set("stats", $stats); $task->set("total", $total = array_sum(array_values($stats))); $completed = 0; $mode = 0; $done = array(); foreach (array_keys($stats) as $key) { $done[$key] = 0; } $task->set("done", $done); // Ensure G2 ACLs are compacted to speed up import. g2(GalleryCoreApi::compactAccessLists()); } $modes = array("groups", "users", "albums", "items", "comments", "tags", "highlights", "done"); while (!$task->done && microtime(true) - $start < 1.5) { if ($done[$modes[$mode]] == $stats[$modes[$mode]]) { // Nothing left to do for this mode. Advance. $mode++; $task->set("last_id", 0); $queue = array(); // Start the loop from the beginning again. This way if we get to a mode that requires no // actions (eg, if the G2 comments module isn't installed) we won't try to do any comments // queries.. in the next iteration we'll just skip over that mode. if ($modes[$mode] != "done") { continue; } } switch ($modes[$mode]) { case "groups": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_group_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_group($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing groups (%count of %total)", array("count" => $done["groups"] + 1, "total" => $stats["groups"])); break; case "users": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_user_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_user($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing users (%count of %total)", array("count" => $done["users"] + 1, "total" => $stats["users"])); break; case "albums": if (empty($queue)) { $g2_root_id = g2(GalleryCoreApi::getDefaultAlbumId()); $tree = g2(GalleryCoreApi::fetchAlbumTree()); $task->set("queue", $queue = array($g2_root_id => $tree)); // Update the root album to reflect the Gallery2 root album. $root_album = item::root(); g2_import::set_album_values($root_album, g2(GalleryCoreApi::loadEntitiesById($g2_root_id))); $root_album->save(); } $log_message = g2_import::import_album($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing albums (%count of %total)", array("count" => $done["albums"] + 1, "total" => $stats["albums"])); break; case "items": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_item_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_item($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing photos (%count of %total)", array("count" => $done["items"] + 1, "total" => $stats["items"])); break; case "comments": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_comment_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_comment($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing comments (%count of %total)", array("count" => $done["comments"] + 1, "total" => $stats["comments"])); break; case "tags": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_tag_item_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_tags_for_item($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing tags (%count of %total)", array("count" => $done["tags"] + 1, "total" => $stats["tags"])); break; case "highlights": if (empty($queue)) { $task->set("queue", $queue = g2(GalleryCoreApi::fetchAlbumTree())); } $log_message = g2_import::set_album_highlight($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Album highlights (%count of %total)", array("count" => $done["highlights"] + 1, "total" => $stats["highlights"])); break; case "done": $task->status = t("Import complete"); $task->done = true; $task->state = "success"; break; } if (!$task->done) { $done[$modes[$mode]]++; $completed++; } } $task->percent_complete = 100 * ($completed / $total); $task->set("completed", $completed); $task->set("mode", $mode); $task->set("queue", $queue); $task->set("done", $done); g2_import::restore_error_reporting(); }
public function save() { access::verify_csrf(); g2_import::lower_error_reporting(); $form = $this->_get_import_form(); if ($form->validate()) { $embed_path = $form->configure_g2_import->embed_path->value; if (!is_file($embed_path) && file_exists("{$embed_path}/embed.php")) { $embed_path = "{$embed_path}/embed.php"; } if (($g2_init_error = g2_import::is_valid_embed_path($embed_path)) == "ok") { message::success(t("Gallery 2 path saved")); module::set_var("g2_import", "embed_path", $embed_path); url::redirect("admin/g2_import"); } else { $form->configure_g2_import->embed_path->add_error($g2_init_error, 1); } } $view = new Admin_View("admin.html"); $view->content = new View("admin_g2_import.html"); $view->content->form = $form; g2_import::restore_error_reporting(); print $view; }
/** * Return a set of statistics about the number of users, groups, albums, photos, movies and * comments available for import from the Gallery 2 instance. */ static function stats() { global $gallery; GalleryCoreApi::requireOnce("modules/comment/classes/GalleryCommentHelper.class"); $root_album_id = g2(GalleryCoreApi::getDefaultAlbumId()); $stats["users"] = g2(GalleryCoreApi::fetchUserCount()); $stats["groups"] = g2(GalleryCoreApi::fetchGroupCount()); $stats["albums"] = g2(GalleryCoreApi::fetchItemIdCount("GalleryAlbumItem")); $stats["photos"] = g2(GalleryCoreApi::fetchItemIdCount("GalleryPhotoItem")); $stats["movies"] = g2(GalleryCoreApi::fetchItemIdCount("GalleryMovieItem")); if (g2_import::g2_module_active("comment") && module::is_active("comment")) { list(, $stats["comments"]) = g2(GalleryCommentHelper::fetchAllComments($root_album_id, 1)); } else { $stats["comments"] = 0; } if (g2_import::g2_module_active("tags") && module::is_active("tag")) { $result = g2($gallery->search("SELECT COUNT(DISTINCT([TagItemMap::itemId])) FROM [TagItemMap]"))->nextResult(); $stats["tags"] = $result[0]; } else { $stats["tags"] = 0; } return $stats; }
?> </h1> <p> <?php echo t("Import your Gallery 2 users, photos, movies, comments and tags into your new Gallery 3 installation."); ?> </p> <div class="g-block-content"> <?php echo $form; ?> </div> <div class="g-block-content"> <? if (g2_import::is_initialized()): ?> <div id="g-admin-g2-import-details"> <h2> <?php echo t("Import"); ?> </h2> <ul id="g-action-status" class="g-message-block"> <li class="g-success"> <?php echo t("Gallery version %version detected", array("version" => $version)); ?> </li> <? if ($g2_sizes["thumb"]["size"] && $thumb_size != $g2_sizes["thumb"]["size"]): ?> <li class="g-warning"> <?php echo t("Your most common thumbnail size in Gallery 2 is %g2_pixels pixels, but your Gallery 3 thumbnail size is set to %g3_pixels pixels. <a href=\"%url\">Using the same value</a> will speed up your import.", array("g2_pixels" => $g2_sizes["thumb"]["size"], "g3_pixels" => $thumb_size, "url" => html::mark_clean(url::site("admin/theme_options"))));