summaryrefslogtreecommitdiff
path: root/modules/g2_import/helpers/g2_import_task.php
blob: 1ddc95d6a60aec00154991b2b6cd74a7547cb496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);
  }
}