/** * Import a single comment. */ static function import_comment(&$queue) { $g2_comment_id = array_shift($queue); try { $g2_comment = g2(GalleryCoreApi::loadEntitiesById($g2_comment_id)); } catch (Exception $e) { return t("Failed to import Gallery 2 comment with id: %id\\%exception", array("id" => $g2_comment_id, "exception" => $e->__toString())); } $text = $g2_comment->getSubject(); if ($text) { $text .= " "; } $text .= $g2_comment->getComment(); // Just import the fields we know about. Do this outside of the comment API for now so that // 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->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(p::purify($comment->text)), 50))); }
function getG2Tree(&$xmap, &$parent, $params, &$items, &$urlGenerator) { if (!$items) { return null; } $xmap->changeLevel(1); $media = array(); foreach ($items as $itemId) { // Fetch the details for this item list($ret, $entity) = GalleryCoreApi::loadEntitiesById($itemId); if ($ret) { // error, skip and continue, catch this error in next component version continue; } $node = new stdClass(); $node->id = $entity->getId(); $node->uid = $parent->uid . 'a' . $entity->getId(); $node->name = $entity->getTitle(); $node->pid = $entity->getParentId(); $node->modified = $entity->getModificationTimestamp(); $node->link = $urlGenerator->generateUrl(array('view' => 'core.ShowItem', 'itemId' => $node->id), array('forceSessionId' => false, 'forceFullUrl' => false)); // Fix for the navigator view if ($xmap->view == 'navigator') { $node->link = str_replace('/administrator/index.php', '', $node->link); } // If it is an album if ($entity->getCanContainChildren()) { $node->priority = $params['cat_priority']; $node->changefreq = $params['cat_changefreq']; $node->expandible = true; // Get all child items contained in this album and add them to the tree list($ret, $childIds) = GalleryCoreApi::fetchChildItemIdsWithPermission($node->id, 'core.view'); if ($ret) { // error, skip and continue, catch this error in next component version continue; } if ($xmap->printNode($node) !== false) { xmap_com_g2bridge::getG2Tree($xmap, $parent, $params, $childIds, $urlGenerator); } } elseif ($params['include_items']) { $node->priority = $params['item_priority']; $node->changefreq = $params['item_changefreq']; $node->uid = $parent->uid . 'p' . $entity->getId(); $node->expandible = false; $media[] = $node; } } foreach ($media as $pic) { $xmap->printNode($pic); } $xmap->changeLevel(-1); }
/** * Import a single comment. */ static function import_comment(&$queue) { $g2_comment_id = array_shift($queue); try { $g2_comment = g2(GalleryCoreApi::loadEntitiesById($g2_comment_id)); } catch (Exception $e) { g2_import::log("Failed to import Gallery 2 comment with id: %id", array("id" => $g2_comment_id)); return; } $text = $g2_comment->getSubject(); if ($text) { $text .= " "; } $text .= $g2_comment->getComment(); // Just import the fields we know about. Do this outside of the comment API for now so that // 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->text = $text; $comment->state = "published"; $comment->server_http_host = $g2_comment->getHost(); $comment->save(); self::map($g2_comment->getId(), $comment->id); }
/** * Import a single comment. */ static function import_comment(&$queue) { $g2_comment_id = array_shift($queue); try { $g2_comment = g2(GalleryCoreApi::loadEntitiesById($g2_comment_id)); } catch (Exception $e) { return t("Failed to load Gallery 2 comment with id: %id\\%exception", 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 .= " "; } $text .= $g2_comment->getComment(); // Just import the fields we know about. Do this outside of the comment API for now so that // we don't trigger spam filtering events $comment = ORM::factory("comment"); $comment->author_id = self::map($g2_comment->getCommenterId()); $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(); try { $comment->save(); } catch (Exception $e) { return (string) new G2_Import_Exception(t("Failed to import comment with id: %id.", array("id" => $g2_comment_id)), $e); } }
/** * Generates album hierarchy as d.add entites of dtree * * @param int $current_album id of current album * @param int $parent node of the parent album */ function g2ic_make_html_album_tree_branches($current_album, $parent, &$node) { global $g2ic_options; list($error, $items) = GalleryCoreApi::loadEntitiesById(array($current_album)); if (!$error) { foreach ($items as $item) { $album_title = $item->getTitle(); $album_title = preg_replace("/(\n|\r)/", " ", $album_title); // Bug #44 if (empty($album_title)) { $album_title = $item->getPathComponent(); } } $html = ' d.add(' . $node . ',' . $parent . ',"' . $album_title . '","' . '?current_album=' . $current_album . '&sortby=' . $g2ic_options['sortby'] . '&images_per_page=' . $g2ic_options['images_per_page'] . '");' . "\n"; } list($error, $sub_albums) = GalleryCoreApi::fetchAlbumTree($current_album, 1); $albums = array_keys($sub_albums); if (count($albums) > 0) { $parent = $node; foreach ($albums as $album) { $node++; $html .= g2ic_make_html_album_tree_branches($album, $parent, $node); } } return $html; }
/** * Import a single comment. */ static function import_comment(&$queue) { $g2_comment_id = array_shift($queue); try { $g2_comment = g2(GalleryCoreApi::loadEntitiesById($g2_comment_id)); } catch (Exception $e) { return t("Failed to load Gallery 2 comment with id: %id\\%exception", array("id" => $g2_comment_id, "exception" => (string) $e)); } if ($id = self::map($g2_comment->getId())) { if (ORM::factory("comment", $id)->loaded()) { // Already imported and still exists return; } // This comment was already imported, but now it no longer exists. Import it again, per // ticket #1736. } $item_id = self::map($g2_comment->getParentId()); if (empty($item_id)) { // Item was not mapped. return; } $text = join("\n", array($g2_comment->getSubject(), $g2_comment->getComment())); $text = html_entity_decode($text); // Just import the fields we know about. Do this outside of the comment API for now so that // we don't trigger spam filtering events $comment = ORM::factory("comment"); $comment->author_id = self::map($g2_comment->getCommenterId()); $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->guest_email = "*****@*****.**"; } $comment->item_id = $item_id; $comment->text = self::_transform_bbcode($text); $comment->state = "published"; $comment->server_http_host = $g2_comment->getHost(); try { $comment->save(); } catch (Exception $e) { return (string) new G2_Import_Exception(t("Failed to import comment with id: %id.", array("id" => $g2_comment_id)), $e); } self::set_map($g2_comment->getId(), $comment->id, "comment"); // Backdate the creation date. We can't do this at creation time because // Comment_Model::save() will override it. Leave the updated date alone // so that if the comments get marked as spam, they don't immediately get // flushed (see ticket #1736) db::update("comments")->set("created", $g2_comment->getDate())->where("id", "=", $comment->id)->execute(); }
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(); }
/** * Returns the fully-qualified URL for the given image resource * * @param int $elementId * @return string */ function getElementUrl($elementId) { global $mosConfig_live_site; $path = $mosConfig_live_site . GalleryService::G2ALBUM_BASE_URL; $parents = GalleryCoreApi::fetchParentSequence($elementId); if (isset($parents[1])) { foreach ($parents[1] as $nodeId) { $node = GalleryCoreApi::loadEntitiesById($nodeId); $comp = $node[1]->getPathComponent(); if ($comp != '') { $path .= "/" . $comp; } } } $self = GalleryCoreApi::loadEntitiesById($elementId); $path .= "/" . $self[1]->getPathComponent(); return $path; }
/** * Find admin user and set as active user * @param bool $fallback (optional) whether we should try to fall back if the * API to load the admin user object fails * @return GalleryStatus a status code */ function selectAdminUser($fallback = false) { global $gallery; list($ret, $siteAdminGroupId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); if ($ret) { return $ret; } list($ret, $adminUserInfo) = GalleryCoreApi::fetchUsersForGroup($siteAdminGroupId, 1); if ($ret) { return $ret; } if (empty($adminUserInfo)) { return GalleryCoreApi::error(ERROR_MISSING_VALUE); } /* Fetch the first admin from list */ list($userId, $userName) = each($adminUserInfo); list($ret, $adminUser) = GalleryCoreApi::loadEntitiesById($userId, 'GalleryUser'); if ($ret) { if ($fallback) { /* Initialize a GalleryUser with the id of a real admin */ $gallery->debug('Unable to load admin user. Using in-memory user object as fallback'); GalleryCoreApi::requireOnce('modules/core/classes/GalleryUser.class'); $adminUser = new GalleryUser(); $adminUser->setId((int) $userId); $adminUser->setUserName($userName); } else { return $ret; } } $gallery->setActiveUser($adminUser); $session =& $gallery->getSession(); $session->put('isUpgrade', true); return null; }