summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-04-18 04:27:04 +0000
committerBharat Mediratta <bharat@menalto.com>2009-04-18 04:27:04 +0000
commita3e8818449677d08444cd30613603440e6529c5a (patch)
tree7030d4825316819d3894570a7923cc9304844add
parent1a332009c7ac59984e627678ecc91dbaf267ae57 (diff)
Track what we've already imported in the G2_Map_Model and keep it
around in the task so that on subsequent import runs we don't try to reimport stuff we've already pulled in. This also gives us a mapping so that we will be able to translate old G2 urls into the hot new G3 urls.
-rw-r--r--modules/g2_import/helpers/g2_import.php58
-rw-r--r--modules/g2_import/helpers/g2_import_installer.php8
-rw-r--r--modules/g2_import/helpers/g2_import_task.php37
-rw-r--r--modules/g2_import/models/g2_map.php21
4 files changed, 99 insertions, 25 deletions
diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php
index bf7a1a8c..20be2107 100644
--- a/modules/g2_import/helpers/g2_import.php
+++ b/modules/g2_import/helpers/g2_import.php
@@ -96,9 +96,12 @@ class g2_import_Core {
return $stats;
}
- static function import_group($i) {
- $map = g2(GalleryCoreApi::fetchGroupNames(1, $i));
- $g2_group_id = current(array_keys($map));
+ static function import_group(&$queue, &$map) {
+ $g2_group_id = array_shift($queue);
+ if (array_key_exists($g2_group_id, $map)) {
+ return;
+ }
+
$g2_group = g2(GalleryCoreApi::loadEntitiesById($g2_group_id));
if ($g2_group->getGroupType() != GROUP_NORMAL) {
return;
@@ -106,15 +109,24 @@ class g2_import_Core {
try {
$group = group::create($g2_group->getGroupName());
+
+ $map[$g2_group->getId()] = $group->id;
+ $g2_map = ORM::factory("g2_map");
+ $g2_map->id = $group->id;
+ $g2_map->g2_id = $g2_group->getId();
+ $g2_map->save();
} 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));
- $g2_user_id = current(array_keys($map));
+ static function import_user(&$queue, &$map) {
+ $g2_user_id = array_shift($queue);
+ if (array_key_exists($g2_user_id, $map)) {
+ return;
+ }
+
if (g2(GalleryCoreApi::isAnonymousUser($g2_user_id))) {
return;
}
@@ -126,6 +138,13 @@ class g2_import_Core {
$user->email = $g2_user->getEmail();
$user->language = $g2_user->getLanguage();
$user->save();
+ $user_map[$g2_user->getId()] = $user->id;
+
+ $map[$g2_user->getId()] = $user->id;
+ $g2_map = ORM::factory("g2_map");
+ $g2_map->id = $user->id;
+ $g2_map->g2_id = $g2_user->getId();
+ $g2_map->save();
// @todo put the user into the appropriate groups
} catch (Exception $e) {
@@ -134,32 +153,41 @@ class g2_import_Core {
}
}
- static function import_album(&$queue, &$album_map) {
+ static function import_album(&$queue, &$map) {
// 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.
- // Grab the current album off the queue and enqueue its children at the end of the line
- list($g2_id, $children) = each($queue);
- unset($queue[$g2_id]);
+ // 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 (array_key_exists($g2_album_id, $map)) {
+ return;
+ }
+
// Load the G2 album item, and figure out its parent in G3.
- $g2_album = g2(GalleryCoreApi::loadEntitiesById($g2_id));
+ $g2_album = g2(GalleryCoreApi::loadEntitiesById($g2_album_id));
if ($g2_album->getParentId() == null) {
return;
}
- $g3_parent_album = ORM::factory("item", $album_map[$g2_album->getParentId()]);
- $g3_album = album::create(
- $g3_parent_album,
+ $parent_album = ORM::factory("item", $map[$g2_album->getParentId()]);
+ $album = album::create(
+ $parent_album,
$g2_album->getPathComponent(),
$g2_album->getTitle(),
$g2_album->getDescription());
- $album_map[$g2_album->getId()] = $g3_album->id;
+
+ $map[$g2_album->getId()] = $album->id;
+ $g2_map = ORM::factory("g2_map");
+ $g2_map->id = $album->id;
+ $g2_map->g2_id = $g2_album->getId();
+ $g2_map->save();
// @todo import owners
// @todo figure out how to import summary vs. description
diff --git a/modules/g2_import/helpers/g2_import_installer.php b/modules/g2_import/helpers/g2_import_installer.php
index db25e3a7..22aa76c4 100644
--- a/modules/g2_import/helpers/g2_import_installer.php
+++ b/modules/g2_import/helpers/g2_import_installer.php
@@ -19,8 +19,16 @@
*/
class g2_import_installer {
static function install() {
+ $db = Database::instance();
$version = module::get_version("g2_import");
if ($version == 0) {
+ $db->query("CREATE TABLE IF NOT EXISTS {g2_maps} (
+ `id` int(9) NOT NULL,
+ `g2_id` int(9) NOT NULL,
+ PRIMARY KEY (`id`),
+ KEY (`g2_id`))
+ ENGINE=InnoDB DEFAULT CHARSET=utf8;");
+
module::set_version("g2_import", 1);
}
}
diff --git a/modules/g2_import/helpers/g2_import_task.php b/modules/g2_import/helpers/g2_import_task.php
index 964150be..3715e8a1 100644
--- a/modules/g2_import/helpers/g2_import_task.php
+++ b/modules/g2_import/helpers/g2_import_task.php
@@ -36,14 +36,29 @@ class g2_import_task_Core {
$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");
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;
+
+ $root_g2_id = g2(GalleryCoreApi::getDefaultAlbumId());
+ $root = ORM::factory("g2_map")->where("g2_id", $root_g2_id)->find();
+ if (!$root->loaded) {
+ $root->id = 1;
+ $root->g2_id = $root_g2_id;
+ $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");
@@ -55,13 +70,20 @@ class g2_import_task_Core {
switch($modes[$mode]) {
case "groups":
- g2_import::import_group($i);
+ if (!$i) {
+ $task->set("queue", $queue = array_keys(g2(GalleryCoreApi::fetchGroupNames())));
+ }
+ g2_import::import_group($queue, $map);
$task->status = t(
"Importing groups %count / %total", array("count" => $i, "total" => $stats["groups"]));
break;
case "users":
- g2_import::import_user($i);
+ if (!$i) {
+ $task->set(
+ "queue", $queue = array_keys(g2(GalleryCoreApi::fetchUsersForGroup(GROUP_EVERYBODY))));
+ }
+ g2_import::import_user($queue, $map);
$task->status = t(
"Importing users %count / %total", array("count" => $i, "total" => $stats["users"]));
break;
@@ -69,16 +91,9 @@ class g2_import_task_Core {
case "albums":
if (!$i) {
$task->set("queue", $queue = g2(GalleryCoreApi::fetchAlbumTree()));
- $task->set(
- "album_map", $album_map = array(g2(GalleryCoreApi::getDefaultAlbumId()) => 1));
- } else {
- $queue = $task->get("queue");
- $album_map = $task->get("album_map");
}
-
- g2_import::import_album($queue, $album_map);
+ g2_import::import_album($queue, $map);
$task->set("queue", $queue);
- $task->set("album_map", $album_map);
$task->status = t(
"Importing albums %count / %total", array("count" => $i, "total" => $stats["albums"]));
break;
@@ -110,5 +125,7 @@ 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/models/g2_map.php b/modules/g2_import/models/g2_map.php
new file mode 100644
index 00000000..a88a1445
--- /dev/null
+++ b/modules/g2_import/models/g2_map.php
@@ -0,0 +1,21 @@
+<?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_Map_Model extends ORM {
+}