aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Import_progress.php
blob: d9b13e8a88eb1b36c241a6790d3dec5b4e5b4d13 (plain) (blame)
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
<?php
namespace Zotlabs\Module;

use App;
use Zotlabs\Lib\PConfig;
use Zotlabs\Daemon\Master;

class Import_progress extends \Zotlabs\Web\Controller {

	function post() {

		if(! local_channel())
			return;

	}

	function get() {

		if(! local_channel()) {
			return;
		}

		nav_set_selected('Channel Import');

		$channel = App::get_channel();
		$import_host = PConfig::Get(local_channel(), 'import', 'host');

		// items
		$c = PConfig::Get(local_channel(), 'import', 'content_progress');

		if ($c) {
			$total_cpages = floor(intval($c['items_total']) / intval($c['items_page']));
			if(!$total_cpages) {
				$total_cpages = 1; // because of floor
			}

			$cpage = $c['last_page'] + 1; // because page count start at 0

			$cprogress = intval(floor((intval($cpage) * 100) / $total_cpages));
			$ccompleted_str = t('Item sync completed!');

			if(argv(1) === 'resume_itemsync' && $cprogress < 100) {
				Master::Summon($c['next_cmd']);
				goaway('/import_progress');
			}
		}
		else {
			if(argv(1) === 'resume_itemsync' && $import_host) {
				$alive = probe_api_path($import_host);
				if ($alive) {
					$parsed = parse_url($alive);
					unset($parsed['path']);

					$hz_server = unparse_url($parsed);
					$since = datetime_convert(date_default_timezone_get(), date_default_timezone_get(), '0001-01-01 00:00');
					$until = datetime_convert(date_default_timezone_get(), date_default_timezone_get(), 'now + 1 day');
					$page = 0;
					Master::Summon(['Content_importer', sprintf('%d', $page), $since, $until, $channel['channel_address'], urlencode($hz_server)]);
					goaway('/import_progress');
				}
				else {
					notice(t('Import host does not seem to be online or compatible') . EOL);
				}
			}

			$cprogress = 'waiting to start...';

			if (PConfig::Get(local_channel(), 'import', 'content_completed')) {
				// There was nothing todo. Fake 100% and mention that there were no files found
				$cprogress = 100;
			}

			$ccompleted_str = t('Item sync completed but no items were found!');
		}

		$cprogress_str = ((intval($cprogress)) ? $cprogress . '%' : $cprogress);

		// files
		$f = PConfig::Get(local_channel(), 'import', 'files_progress');

		if ($f) {
			$total_fpages = floor(intval($f['files_total']) / intval($f['files_page']));
			if(!$total_fpages) {
				$total_fpages = 1;
			}

			$fpage = $f['last_page'] + 1;

			$fprogress = intval(floor((intval($fpage) * 100) / $total_fpages));
			$fcompleted_str = t('File sync completed!');

			if(argv(1) === 'resume_filesync' && $fprogress < 100) {
				Master::Summon($f['next_cmd']);
				goaway('/import_progress');
			}
		}
		else {
			if(argv(1) === 'resume_filesync' && $import_host) {
				$alive = probe_api_path($import_host);
				if ($alive) {
					$parsed = parse_url($alive);
					unset($parsed['path']);

					$hz_server = unparse_url($parsed);
					$page = 0;

					Master::Summon(['File_importer', sprintf('%d', $page), $channel['channel_address'], urlencode($hz_server)]);
					goaway('/import_progress');
				}
				else {
					notice(t('Import host does not seem to be online or compatible') . EOL);
				}
			}

			$fprogress = 'waiting to start...';

			if (PConfig::Get(local_channel(), 'import', 'files_completed')) {
				// There was nothing todo. Fake 100% and mention that there were no files found
				$fprogress = 100;
			}

			$fcompleted_str = t('File sync completed but no files were found!');
		}

		$fprogress_str = ((intval($fprogress)) ? $fprogress . '%' : $fprogress);

		if(is_ajax()) {
			$ret = [
				'cprogress' => $cprogress,
				'fprogress' => $fprogress
			];

			json_return_and_die($ret);
		}

		$o = replace_macros(get_markup_template("import_progress.tpl"), [
			'$chtitle_str' => t('Channel clone status'),
			'$ctitle_str' => t('Item sync status'),
			'$ftitle_str' => t('File sync status'),
			'$cprogress_str' => $cprogress_str,
			'$cprogress' => intval($cprogress),
			'$fprogress_str' => $fprogress_str,
			'$fprogress' => intval($fprogress),
			'$fcompleted_str' => $fcompleted_str,
			'$ccompleted_str' => $ccompleted_str,
			'$chcompleted_str' => t('Channel cloning completed!'),
			'$resume_str' => t('Resume'),
			'$resume_helper_str' => t('Only resume if sync stalled!')
		]);

		return $o;
	}

}