diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-04-15 07:23:22 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-04-15 07:23:22 +0000 |
commit | a7f345476f80cde43589069825a256534de7745c (patch) | |
tree | d90dfe15263af64b9194e5dcef8810c40b75765e | |
parent | 8c1d1a9d0df5e1501d8beccbadeaf93c95313a62 (diff) |
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.
-rw-r--r-- | modules/g2_import/controllers/admin_g2_import.php | 62 | ||||
-rw-r--r-- | modules/g2_import/helpers/g2_import.php | 134 | ||||
-rw-r--r-- | modules/g2_import/helpers/g2_import_installer.php | 31 | ||||
-rw-r--r-- | modules/g2_import/helpers/g2_import_menu.php | 29 | ||||
-rw-r--r-- | modules/g2_import/helpers/g2_import_task.php | 102 | ||||
-rw-r--r-- | modules/g2_import/module.info | 3 | ||||
-rw-r--r-- | modules/g2_import/views/admin_g2_import.html.php | 20 |
7 files changed, 381 insertions, 0 deletions
diff --git a/modules/g2_import/controllers/admin_g2_import.php b/modules/g2_import/controllers/admin_g2_import.php new file mode 100644 index 00000000..6197240b --- /dev/null +++ b/modules/g2_import/controllers/admin_g2_import.php @@ -0,0 +1,62 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Admin_g2_import_Controller extends Admin_Controller { + public function index() { + if (g2_import::is_configured()) { + g2_import::init(); + } + + $view = new Admin_View("admin.html"); + $view->content = new View("admin_g2_import.html"); + $view->content->form = $this->_get_import_form(); + print $view; + } + + public function save() { + $form = $this->_get_import_form(); + if ($form->validate()) { + $embed_path = $form->configure_g2_import->embed_path->value; + 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; + } + + private function _get_import_form() { + $form = new Forge( + "admin/g2_import/save", "", "post", array("id" => "gAdminConfigureG2ImportForm")); + $group = $form->group("configure_g2_import")->label(t("Configure Gallery 2 Import")); + $group->input("embed_path")->label(t("Filesystem path to your Gallery 2 embed.php file")) + ->value(module::get_var("g2_import", "embed_path", "")); + $group->embed_path->error_messages( + "invalid", t("The path you entered is not a Gallery 2 installation.")); + $group->submit("")->value(t("Save")); + return $form; + } +}
\ No newline at end of file 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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +function g2() { + $args = func_get_arg(0); + $ret = array_shift($args); + if ($ret) { + throw new Exception("@todo G2_FUNCTION_FAILED"); + } + if (count($args) == 1) { + return $args[0]; + } else { + return $args; + } +} + +class g2_import_Core { + public static $init = false; + + static function is_configured() { + return module::get_var("g2_import", "embed_path"); + } + + static function is_initialized() { + return g2_import::$init; + } + + static function init() { + if (g2_import::$init) { + return; + } + + $embed_path = module::get_var("g2_import", "embed_path"); + if (empty($embed_path)) { + throw new Exception("@todo G2_IMPORT_NOT_CONFIGURED"); + } + + g2_import::$init = g2_import::init_embed($embed_path); + } + + static function is_valid_embed_path($embed_path) { + return file_exists($embed_path) && g2_import::init_embed($embed_path); + } + + static function init_embed($embed_path) { + require($embed_path); + if (!class_exists("GalleryEmbed")) { + return false; + } + + $ret = GalleryEmbed::init(); + if ($ret) { + return false; + } + + $admin_group_id = g2(GalleryCoreApi::getPluginParameter("module", "core", "id.adminGroup")); + $admins = g2(GalleryCoreApi::fetchUsersForGroup($admin_group_id, 1)); + $admin_id = current(array_flip($admins)); + $admin = g2(GalleryCoreApi::loadEntitiesById($admin_id)); + $GLOBALS["gallery"]->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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class g2_import_installer { + static function install() { + $version = module::get_version("g2_import"); + if ($version == 0) { + module::set_version("g2_import", 1); + } + } + + static function uninstall() { + module::delete("g2_import"); + } +} diff --git a/modules/g2_import/helpers/g2_import_menu.php b/modules/g2_import/helpers/g2_import_menu.php new file mode 100644 index 00000000..3eff2714 --- /dev/null +++ b/modules/g2_import/helpers/g2_import_menu.php @@ -0,0 +1,29 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class g2_import_menu_Core { + static function admin($menu, $theme) { + $menu + ->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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class g2_import_task_Core { + static function available_tasks() { + if (g2_import::is_configured()) { + g2_import::init(); + return array(Task_Definition::factory() + ->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); + } +} diff --git a/modules/g2_import/module.info b/modules/g2_import/module.info new file mode 100644 index 00000000..9e04f49b --- /dev/null +++ b/modules/g2_import/module.info @@ -0,0 +1,3 @@ +name = Gallery2 Import +description = Import your Gallery 2 content into Gallery 3 +version = 1 diff --git a/modules/g2_import/views/admin_g2_import.html.php b/modules/g2_import/views/admin_g2_import.html.php new file mode 100644 index 00000000..9b61b584 --- /dev/null +++ b/modules/g2_import/views/admin_g2_import.html.php @@ -0,0 +1,20 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<div id="gAdminG2Config"> + <h1> <?= t("Gallery 2 Import") ?> </h1> + <p> + <?= t("Import your Gallery 2 users, photos, movies, comments and tags into your new Gallery 3 installation.") ?> + </p> + + <?= $form ?> +</div> + +<? if (g2_import::is_initialized()): ?> +<div id="gAdminG2Import"> + <h1> <?= t("Import") ?> </h1> + <div class="gSuccess"> + <?= t("We've detected Gallery version: %version", array("version" => g2_import::version())) ?> + </div> + <a href="<?= url::site("admin/maintenance/start/g2_import::import?csrf=$csrf") ?>" + class="gPanelLink"><?= t("Begin Import") ?></a> +</div> +<? endif ?> |