Example #1
0
 /**
  * Import a single album.
  */
 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
     // the time we get to the child.
     // Dequeue the current album and enqueue its children
     list($g2_album_id, $children) = each($queue);
     unset($queue[$g2_album_id]);
     foreach ($children as $key => $value) {
         $queue[$key] = $value;
     }
     if (self::map($g2_album_id)) {
         return;
     }
     try {
         // Load the G2 album item, and figure out its parent in G3.
         $g2_album = g2(GalleryCoreApi::loadEntitiesById($g2_album_id));
     } catch (Exception $e) {
         return t("Failed to load Gallery 2 album with id: %id\n%exception", array("id" => $g2_album_id, "exception" => (string) $e));
     }
     if ($g2_album->getParentId() == null) {
         $album = item::root();
     } else {
         $parent_album = ORM::factory("item", self::map($g2_album->getParentId()));
         $album = ORM::factory("item");
         $album->type = "album";
         $album->parent_id = self::map($g2_album->getParentId());
         g2_import::set_album_values($album, $g2_album);
         try {
             $album->save();
         } catch (Exception $e) {
             throw new G2_Import_Exception(t("Failed to import Gallery 2 album with id %id and name %name.", array("id" => $g2_album_id, "name" => $album->name)), $e);
         }
         self::import_keywords_as_tags($g2_album->getKeywords(), $album);
     }
     self::set_map($g2_album_id, $album->id, "album", self::g2_url(array("view" => "core.ShowItem", "itemId" => $g2_album->getId())));
     self::_import_permissions($g2_album, $album);
 }
Example #2
0
 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();
 }