From a7f345476f80cde43589069825a256534de7745c Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 15 Apr 2009 07:23:22 +0000 Subject: Very early round of Gallery 2 import code. It can import users (with the wrong password) and import groups, with a basic UI. Needs a ton more work. --- modules/g2_import/helpers/g2_import.php | 134 ++++++++++++++++++++++ modules/g2_import/helpers/g2_import_installer.php | 31 +++++ modules/g2_import/helpers/g2_import_menu.php | 29 +++++ modules/g2_import/helpers/g2_import_task.php | 102 ++++++++++++++++ 4 files changed, 296 insertions(+) create mode 100644 modules/g2_import/helpers/g2_import.php create mode 100644 modules/g2_import/helpers/g2_import_installer.php create mode 100644 modules/g2_import/helpers/g2_import_menu.php create mode 100644 modules/g2_import/helpers/g2_import_task.php (limited to 'modules/g2_import/helpers') diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php new file mode 100644 index 00000000..13a9666c --- /dev/null +++ b/modules/g2_import/helpers/g2_import.php @@ -0,0 +1,134 @@ +setActiveUser($admin); + + return true; + } + + static function version() { + $core = g2(GalleryCoreApi::loadPlugin("module", "core")); + $versions = $core->getInstalledVersions(); + return $versions["gallery"]; + } + + static function stats() { + 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")); + list (, $stats["comments"]) = g2(GalleryCommentHelper::fetchAllComments($root_album_id, 1)); + return $stats; + } + + static function import_group($i) { + $map = g2(GalleryCoreApi::fetchGroupNames(1, $i - 1)); + $g2_group_id = current(array_keys($map)); + $g2_group = g2(GalleryCoreApi::loadEntitiesById($g2_group_id)); + if ($g2_group->getGroupType() != GROUP_NORMAL) { + return; + } + + try { + $group = group::create($g2_group->getGroupName()); + } catch (Exception $e) { + // @todo For now we assume this is a "duplicate group" exception + // which we will ignore. + } + } + + static function import_user($i) { + $map = g2(GalleryCoreApi::fetchUsersForGroup(GROUP_EVERYBODY, 1, $i - 1)); + $g2_user_id = current(array_keys($map)); + if (g2(GalleryCoreApi::isAnonymousUser($g2_user_id))) { + return; + } + + $g2_user = g2(GalleryCoreApi::loadEntitiesById($g2_user_id)); + try { + $user = user::create($g2_user->getUserName(), $g2_user->getFullName(), ""); + $user->hashed_password = $g2_user->getHashedPassword(); + $user->email = $g2_user->getEmail(); + $user->language = $g2_user->getLanguage(); + $user->save(); + } catch (Exception $e) { + // @todo For now we assume this is a "duplicate user" exception + // which we will ignore. + } + } +} diff --git a/modules/g2_import/helpers/g2_import_installer.php b/modules/g2_import/helpers/g2_import_installer.php new file mode 100644 index 00000000..db25e3a7 --- /dev/null +++ b/modules/g2_import/helpers/g2_import_installer.php @@ -0,0 +1,31 @@ +get("content_menu") + ->append(Menu::factory("link") + ->id("g2_import") + ->label(t("Gallery 2 Import")) + ->url(url::site("admin/g2_import"))); + } +} diff --git a/modules/g2_import/helpers/g2_import_task.php b/modules/g2_import/helpers/g2_import_task.php new file mode 100644 index 00000000..1ddc95d6 --- /dev/null +++ b/modules/g2_import/helpers/g2_import_task.php @@ -0,0 +1,102 @@ +callback("g2_import_task::import") + ->name(t("Import from Gallery 2")) + ->description( + t("Gallery %version detected", array("version" => g2_import::version()))) + ->severity(log::SUCCESS)); + } + } + + static function import($task) { + $start = microtime(true); + g2_import::init(); + $stats = $task->get("stats"); + $total = $task->get("total"); + $completed = $task->get("completed"); + $i = $task->get("i"); + $mode = $task->get("mode"); + if (!isset($mode)) { + $task->set("stats", $stats = g2_import::stats()); + $task->set("total", $total = array_sum(array_values($stats))); + $completed = 0; + $i = 0; + $mode = 0; + } + + $modes = array("groups", "users", "albums", "photos", "comments", "done"); + while (!$task->done && microtime(true) - $start < 1) { + if ($i >= $stats[$modes[$mode]]) { + $i = 0; + $mode++; + } + $i++; + + switch($modes[$mode]) { + case "groups": + g2_import::import_group($i); + $task->status = t( + "Importing groups %count / %total", array("count" => $i, "total" => $stats["groups"])); + break; + + case "users": + g2_import::import_user($i); + $task->status = t( + "Importing users %count / %total", array("count" => $i, "total" => $stats["users"])); + break; + + case "albums": + $task->status = t( + "Importing albums %count / %total", array("count" => $i, "total" => $stats["albums"])); + break; + + case "photos": + $task->status = t( + "Importing photos %count / %total", array("count" => $i, "total" => $stats["photos"])); + break; + + case "comments": + $task->status = t("Importing comments %count / %total", + array("count" => $i, "total" => $stats["comments"])); + break; + + case "done": + $task->status = t("Import complete"); + $task->done = true; + $task->state = "success"; + break; + } + + if (!$task->done) { + $completed++; + } + } + + $task->percent_complete = 100 * ($completed / $total); + $task->set("completed", $completed); + $task->set("mode", $mode); + $task->set("i", $i); + } +} -- cgit v1.2.3