summaryrefslogtreecommitdiff
path: root/modules/g2_import/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'modules/g2_import/helpers')
-rw-r--r--modules/g2_import/helpers/g2_import.php231
-rw-r--r--modules/g2_import/helpers/g2_import_task.php22
2 files changed, 201 insertions, 52 deletions
diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php
index 0fcc0539..faf08291 100644
--- a/modules/g2_import/helpers/g2_import.php
+++ b/modules/g2_import/helpers/g2_import.php
@@ -304,7 +304,15 @@ class g2_import_Core {
if ($user) {
$message = t("Loaded existing user: '%name'.", array("name" => $user->name));
} else {
- $user = identity::create_user($g2_user->getUsername(), $g2_user->getfullname(), "");
+ $user = identity::create_user($g2_user->getUsername(), $g2_user->getfullname(),
+ // Note: The API expects a password in cleartext.
+ // Just use the hashed password as an unpredictable
+ // value here. The user will have to reset the password.
+ $g2_user->getHashedPassword(), $g2_user->getEmail());
+ if (class_exists("User_Model") && $user instanceof User_Model) {
+ // This will work if G2's password is a PasswordHash password as well.
+ $user->hashed_password = $g2_user->getHashedPassword();
+ }
$message = t("Created user: '%name'.", array("name" => $user->name));
}
@@ -357,45 +365,54 @@ class g2_import_Core {
}
if ($g2_album->getParentId() == null) {
- return t("Skipping Gallery 2 root album");
- }
- $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());
- $album->name = $g2_album->getPathComponent();
- $album->title = self::_decode_html_special_chars($g2_album->getTitle());
- $album->description = self::_decode_html_special_chars(self::extract_description($g2_album));
- $album->owner_id = self::map($g2_album->getOwnerId());
- $album->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_album_id));
- $album->created = $g2_album->getCreationTimestamp();
-
- $order_map = array(
- "originationTimestamp" => "captured",
- "creationTimestamp" => "created",
- "description" => "description",
- "modificationTimestamp" => "updated",
- "orderWeight" => "weight",
- "pathComponent" => "name",
- "summary" => "description",
- "title" => "title",
- "viewCount" => "view_count");
- $direction_map = array(
- ORDER_ASCENDING => "asc",
- ORDER_DESCENDING => "desc");
- if (array_key_exists($g2_order = $g2_album->getOrderBy(), $order_map)) {
- $album->sort_column = $order_map[$g2_order];
- $album->sort_order = $direction_map[$g2_album->getOrderDirection()];
- }
- $album->save();
-
- self::import_keywords_as_tags($g2_album->getKeywords(), $album);
+ $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());
+ $album->name = $g2_album->getPathComponent();
+ $album->title = self::_decode_html_special_chars($g2_album->getTitle());
+ $album->title or $album->title = $album->name;
+ $album->description = self::_decode_html_special_chars(self::extract_description($g2_album));
+ $album->owner_id = self::map($g2_album->getOwnerId());
+ $album->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_album_id));
+ $album->created = $g2_album->getCreationTimestamp();
+
+ $order_map = array(
+ "originationTimestamp" => "captured",
+ "creationTimestamp" => "created",
+ "description" => "description",
+ "modificationTimestamp" => "updated",
+ "orderWeight" => "weight",
+ "pathComponent" => "name",
+ "summary" => "description",
+ "title" => "title",
+ "viewCount" => "view_count");
+ $direction_map = array(
+ ORDER_ASCENDING => "asc",
+ ORDER_DESCENDING => "desc");
+ // Only consider G2's first sort order
+ $g2_order = explode("|", $g2_album->getOrderBy() . "");
+ $g2_order = $g2_order[0];
+ $g2_order_direction = explode("|", $g2_album->getOrderDirection() . "");
+ $g2_order_direction = $g2_order_direction[0];
+ if (array_key_exists($g2_order, $order_map)) {
+ $album->sort_column = $order_map[$g2_order];
+ $album->sort_order = $direction_map[$g2_order_direction];
+ }
+ $album->save();
+
+ 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);
}
/**
@@ -488,6 +505,7 @@ class g2_import_Core {
$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();
@@ -509,6 +527,7 @@ class g2_import_Core {
$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());
} catch (Exception $e) {
@@ -541,6 +560,10 @@ class g2_import_Core {
$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) {
@@ -586,6 +609,125 @@ class g2_import_Core {
array("&", "\"", "<", ">"), $value);
}
+ private static $_permission_map = array(
+ "core.view" => "view",
+ "core.viewSource" => "view_full",
+ "core.edit" => "edit",
+ "core.addDataItem" => "add",
+ "core.addAlbumItem" => "add");
+
+ /**
+ * Imports G2 permissions, mapping G2's permission model to G3's
+ * much simplified permissions.
+ *
+ * - Ignores user permissions, G3 only supports group permissions.
+ * - Ignores item permissions, G3 only supports album permissions.
+ *
+ * G2 permission -> G3 permission
+ * ---------------------------------
+ * core.view view
+ * core.viewSource view_full
+ * core.edit edit
+ * core.addDataItem add
+ * core.addAlbumItem add
+ * core.viewResizes <ignored>
+ * core.delete <ignored>
+ * comment.* <ignored>
+ */
+ private static function _import_permissions($g2_album, $g3_album) {
+ // No need to do anything if this album has the same G2 ACL as its parent.
+ if ($g2_album->getParentId() != null &&
+ g2(GalleryCoreApi::fetchAccessListId($g2_album->getId())) ==
+ g2(GalleryCoreApi::fetchAccessListId($g2_album->getParentId()))) {
+ return;
+ }
+
+ $granted_permissions = self::_map_permissions($g2_album->getId());
+
+ if ($g2_album->getParentId() == null) {
+ // Compare to current permissions, and change them if necessary.
+ $g3_parent_album = item::root();
+ } else {
+ $g3_parent_album = $g3_album->parent();
+ }
+ $granted_parent_permissions = array();
+ $perm_ids = array_unique(array_values(self::$_permission_map));
+ foreach (identity::groups() as $group) {
+ $granted_parent_permissions[$group->id] = array();
+ foreach ($perm_ids as $perm_id) {
+ if (access::group_can($group, $perm_id, $g3_parent_album)) {
+ $granted_parent_permissions[$group->id][$perm_id] = 1;
+ }
+ }
+ }
+
+ // Note: Only registering permissions if they're not the same as
+ // the inherited ones.
+ foreach ($granted_permissions as $group_id => $permissions) {
+ if (!isset($granted_parent_permissions[$group_id])) {
+ foreach (array_keys($permissions) as $perm_id) {
+ access::allow(identity::lookup_group($group_id), $perm_id, $g3_album);
+ }
+ } else if ($permissions != $granted_parent_permissions[$group_id]) {
+ $parent_permissions = $granted_parent_permissions[$group_id];
+ // @todo Probably worth caching the group instances.
+ $group = identity::lookup_group($group_id);
+ // Note: Cannot use array_diff_key.
+ foreach (array_keys($permissions) as $perm_id) {
+ if (!isset($parent_permissions[$perm_id])) {
+ access::allow($group, $perm_id, $g3_album);
+ }
+ }
+ foreach (array_keys($parent_permissions) as $perm_id) {
+ if (!isset($permissions[$perm_id])) {
+ access::deny($group, $perm_id, $g3_album);
+ }
+ }
+ }
+ }
+
+ foreach ($granted_parent_permissions as $group_id => $parent_permissions) {
+ if (isset($granted_permissions[$group_id])) {
+ continue; // handled above
+ }
+ $group = identity::lookup_group($group_id);
+ foreach (array_keys($parent_permissions) as $perm_id) {
+ access::deny($group, $perm_id, $g3_album);
+ }
+ }
+ }
+
+ /**
+ * Loads all the granted group G2 permissions for a specific
+ * album and returns an array with G3 groups ids and G3 permission ids.
+ */
+ private static function _map_permissions($g2_album_id) {
+ $g2_permissions = g2(GalleryCoreApi::fetchAllPermissionsForItem($g2_album_id));
+ $permissions = array();
+ foreach ($g2_permissions as $entry) {
+ // @todo Do something about user permissions? E.g. map G2's user albums
+ // to a user-specific group in G3?
+ if (!isset($entry["groupId"])) {
+ continue;
+ }
+ $g2_permission_id = $entry["permission"];
+ if (!isset(self::$_permission_map[$g2_permission_id])) {
+ continue;
+ }
+ $group_id = self::map($entry["groupId"]);
+ if ($group_id == null) {
+ // E.g. the G2 admin group isn't mapped.
+ continue;
+ }
+ $permission_id = self::$_permission_map[$g2_permission_id];
+ if (!isset($permissions[$group_id])) {
+ $permissions[$group_id] = array();
+ }
+ $permissions[$group_id][$permission_id] = 1;
+ }
+ return $permissions;
+ }
+
/**
* Import a single comment.
*/
@@ -599,6 +741,12 @@ class g2_import_Core {
array("id" => $g2_comment_id, "exception" => (string)$e));
}
+ $item_id = self::map($g2_comment->getParentId());
+ if (empty($item_id)) {
+ // Item was not mapped.
+ return;
+ }
+
$text = $g2_comment->getSubject();
if ($text) {
$text .= " ";
@@ -609,18 +757,17 @@ class g2_import_Core {
// we don't trigger spam filtering events
$comment = ORM::factory("comment");
$comment->author_id = self::map($g2_comment->getCommenterId());
- $comment->guest_name = $g2_comment->getAuthor();
- $comment->item_id = self::map($g2_comment->getParentId());
+ $comment->guest_name = "";
+ if ($comment->author_id == identity::guest()->id) {
+ $comment->guest_name = $g2_comment->getAuthor();
+ $comment->guest_name or $comment->guest_name = (string) t("Anonymous coward");
+ }
+ $comment->item_id = $item_id;
$comment->text = self::_transform_bbcode($text);
$comment->state = "published";
$comment->server_http_host = $g2_comment->getHost();
$comment->created = $g2_comment->getDate();
$comment->save();
-
- self::map($g2_comment->getId(), $comment->id);
- return t("Imported comment '%comment' for item with id: %id",
- array("id" => $comment->item_id,
- "comment" => text::limit_words(nl2br(html::purify($comment->text)), 50)));
}
/**
diff --git a/modules/g2_import/helpers/g2_import_task.php b/modules/g2_import/helpers/g2_import_task.php
index e0212b33..b1179e22 100644
--- a/modules/g2_import/helpers/g2_import_task.php
+++ b/modules/g2_import/helpers/g2_import_task.php
@@ -19,14 +19,19 @@
*/
class g2_import_task_Core {
static function available_tasks() {
+ $version = '';
g2_import::lower_error_reporting();
if (g2_import::is_configured()) {
g2_import::init();
+ // Guard from common case where the import has been
+ // completed and the original files have been removed.
+ if (class_exists("GalleryCoreApi")) {
+ $version = g2_import::version();
+ }
}
- $version = g2_import::version();
g2_import::restore_error_reporting();
- if (class_exists("GalleryCoreApi")) {
+ if (g2_import::is_initialized()) {
return array(Task_Definition::factory()
->callback("g2_import_task::import")
->name(t("Import from Gallery 2"))
@@ -68,13 +73,8 @@ class g2_import_task_Core {
}
$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();
- }
+ // Ensure G2 ACLs are compacted to speed up import.
+ g2(GalleryCoreApi::compactAccessLists());
}
$modes = array("groups", "users", "albums", "items", "comments", "tags", "highlights", "done");
@@ -123,7 +123,9 @@ class g2_import_task_Core {
case "albums":
if (empty($queue)) {
- $task->set("queue", $queue = g2(GalleryCoreApi::fetchAlbumTree()));
+ $g2_root_id = g2(GalleryCoreApi::getDefaultAlbumId());
+ $tree = g2(GalleryCoreApi::fetchAlbumTree());
+ $task->set("queue", $queue = array($g2_root_id => $tree));
}
$log_message = g2_import::import_album($queue);
if ($log_message) {