summaryrefslogtreecommitdiff
path: root/modules/g2_import/helpers/g2_import_task.php
blob: b1179e22b54d540157d5d4b2c2fdfbc1c0628f55 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2009 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() {
    $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();
      }
    }
    g2_import::restore_error_reporting();

    if (g2_import::is_initialized()) {
      return array(Task_Definition::factory()
                   ->callback("g2_import_task::import")
                   ->name(t("Import from Gallery 2"))
                   ->description(
                     t("Gallery %version detected", array("version" => $version)))
                   ->severity(log::SUCCESS));
    }

    return array();
  }

  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::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 = array_keys(g2(GalleryCoreApi::fetchGroupNames())));
        }
        $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 = array_keys(g2(GalleryCoreApi::fetchUsersForGroup(GROUP_EVERYBODY))));
        }
        $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));
        }
        $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();
  }
}