2009-04-15 07:23:22 +00:00
|
|
|
<?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));
|
2009-05-05 03:56:42 +00:00
|
|
|
} else {
|
|
|
|
|
return array();
|
2009-04-15 07:23:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function import($task) {
|
|
|
|
|
$start = microtime(true);
|
|
|
|
|
g2_import::init();
|
2009-04-21 04:21:34 +00:00
|
|
|
|
2009-04-15 07:23:22 +00:00
|
|
|
$stats = $task->get("stats");
|
|
|
|
|
$total = $task->get("total");
|
|
|
|
|
$completed = $task->get("completed");
|
|
|
|
|
$i = $task->get("i");
|
|
|
|
|
$mode = $task->get("mode");
|
2009-04-18 04:27:04 +00:00
|
|
|
$queue = $task->get("queue");
|
2009-04-15 07:23:22 +00:00
|
|
|
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;
|
2009-04-18 04:27:04 +00:00
|
|
|
|
|
|
|
|
$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;
|
2009-04-18 23:10:57 +00:00
|
|
|
$root->g3_id = 1;
|
2009-04-18 04:27:04 +00:00
|
|
|
$root->save();
|
|
|
|
|
}
|
2009-04-15 07:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$modes = array("groups", "users", "albums", "photos", "comments", "done");
|
|
|
|
|
while (!$task->done && microtime(true) - $start < 1) {
|
2009-04-17 09:05:12 +00:00
|
|
|
if ($i >= ($stats[$modes[$mode]] - 1)) {
|
2009-04-15 07:23:22 +00:00
|
|
|
$i = 0;
|
|
|
|
|
$mode++;
|
2009-04-21 04:21:34 +00:00
|
|
|
$queue = array();
|
2009-04-15 07:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch($modes[$mode]) {
|
|
|
|
|
case "groups":
|
2009-04-18 04:27:04 +00:00
|
|
|
if (!$i) {
|
|
|
|
|
$task->set("queue", $queue = array_keys(g2(GalleryCoreApi::fetchGroupNames())));
|
|
|
|
|
}
|
2009-04-21 04:21:34 +00:00
|
|
|
g2_import::import_group($queue);
|
2009-04-15 07:23:22 +00:00
|
|
|
$task->status = t(
|
|
|
|
|
"Importing groups %count / %total", array("count" => $i, "total" => $stats["groups"]));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "users":
|
2009-04-18 04:27:04 +00:00
|
|
|
if (!$i) {
|
|
|
|
|
$task->set(
|
|
|
|
|
"queue", $queue = array_keys(g2(GalleryCoreApi::fetchUsersForGroup(GROUP_EVERYBODY))));
|
|
|
|
|
}
|
2009-04-21 04:21:34 +00:00
|
|
|
g2_import::import_user($queue);
|
2009-04-15 07:23:22 +00:00
|
|
|
$task->status = t(
|
|
|
|
|
"Importing users %count / %total", array("count" => $i, "total" => $stats["users"]));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "albums":
|
2009-04-17 09:05:12 +00:00
|
|
|
if (!$i) {
|
|
|
|
|
$task->set("queue", $queue = g2(GalleryCoreApi::fetchAlbumTree()));
|
|
|
|
|
}
|
2009-04-21 04:21:34 +00:00
|
|
|
g2_import::import_album($queue);
|
2009-04-17 09:05:12 +00:00
|
|
|
$task->set("queue", $queue);
|
2009-04-15 07:23:22 +00:00
|
|
|
$task->status = t(
|
|
|
|
|
"Importing albums %count / %total", array("count" => $i, "total" => $stats["albums"]));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "photos":
|
2009-04-21 04:21:34 +00:00
|
|
|
if (empty($queue)) {
|
|
|
|
|
$task->set("queue", $queue = g2_import::get_item_ids($task->get("last_id", 0)));
|
|
|
|
|
$task->set("last_id", end($queue));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g2_import::import_item($queue);
|
2009-04-15 07:23:22 +00:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-17 09:05:12 +00:00
|
|
|
$i++;
|
2009-04-15 07:23:22 +00:00
|
|
|
if (!$task->done) {
|
|
|
|
|
$completed++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$task->percent_complete = 100 * ($completed / $total);
|
|
|
|
|
$task->set("completed", $completed);
|
|
|
|
|
$task->set("mode", $mode);
|
|
|
|
|
$task->set("i", $i);
|
2009-04-18 04:27:04 +00:00
|
|
|
$task->set("queue", $queue);
|
2009-04-15 07:23:22 +00:00
|
|
|
}
|
|
|
|
|
}
|