-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-sync.php
More file actions
executable file
·138 lines (119 loc) · 3.82 KB
/
script-sync.php
File metadata and controls
executable file
·138 lines (119 loc) · 3.82 KB
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
#!/usr/bin/php -n -d error_reporting=-1 -d display_errors=1 -d memory_limit=1G
<?php
define('TF', 1);
require_once(realpath(dirname(__FILE__)) . '/utils.php');
ini_set('error_reporting', -1);
ini_set('display_errors', 1);
foreach(glob(realpath(dirname(__FILE__)) . '/configs/*.ini') as $configFile)
{
$config = parse_ini_file($configFile);
if (!hasFeature($config, 'scriptsync'))
{
printf("scriptsync not enabled for config: %s\n", $configFile);
continue;
}
if (!isset($config['scriptPath']))
{
printf("scriptPath not set for config: %s\n", $configFile);
continue;
}
processUpdate($config);
printf("Finished writing scripts to %s at %s\n", $config['scriptPath'], date('c'));
}
die("All done\n");
function deleteScriptFiles($scriptPath, $scriptId, $formId = null)
{
$candidates = glob(sprintf("%s/*/%s*", $scriptPath, $scriptId));
foreach ($candidates as $candidate)
{
unlink($candidate);
}
}
function processUpdate($config)
{
extract($config);
if (!file_exists($scriptPath))
{
$res = mkdir($scriptPath, 0777, true);
if (!$res)
{
printf("Unable to create script path\n");
return;
}
}
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$sequenceFile = sprintf('%s/%s_to_scriptsync_%s', $sequencePath, $dbName, md5($dbHost.$backupPath));
$since = "0";
$pending = 1;
if (file_exists($sequenceFile))
{
printf("Using sequence file: %s\n", $sequenceFile);
$lastSince = file_get_contents($sequenceFile);
$since = !empty($lastSince) ? $lastSince : $since;
}
$dbRootUrl = sprintf('%s/%s', $dbHost, $dbName);
$dbDetails = json_decode(file_get_contents($dbRootUrl, false, stream_context_create($arrContextOptions))) or die("Unable to connect to CouchDB\n");
while ($pending > 0)
{
$url = sprintf('%s/%s/_changes?include_docs=true&limit=10&since=%s&attachments=false', $dbHost, $dbName, $since);
printf("Using URL %s\n\n", $url);
$data = file_get_contents($url, false, stream_context_create($arrContextOptions)) or die("Unable to connect to CouchDB\n");
$response = json_decode($data);
$changes = $response->results;
$lastSeq = $response->last_seq;
$pending = isset($response->pending) ? $response->pending : $dbDetails->update_seq - $lastSeq;
$since = $lastSeq;
if ($pending == -1)
{
}
foreach ($changes as $change)
{
if (isset($change->deleted))
{
printf("Change %s is deleted, deleting doc.\n", $change->id);
deleteScriptFiles($scriptPath, $change->id);
continue;
}
printf("Found Record %s\n", $change->id);
if (!isset($change->doc->type))
{
printf("Record missing type, skipping.\n");
continue;
}
switch($change->doc->type)
{
case 'TFField':
if ($change->doc->fieldType != 'script')
{
continue;
}
$targetFilename = getScriptPathForRecord($scriptPath, $change->doc->form, $change->doc->_id, $change->doc->name);
printf("Target Filename: '%s'\n", $targetFilename);
if (!file_exists(dirname($targetFilename)))
{
mkdir(dirname($targetFilename), 0777, true);
}
deleteScriptFiles($scriptPath, $change->doc->_id);
file_put_contents($targetFilename, $change->doc->script);
break;
case 'TFScript':
$targetFilename = getScriptPathForRecord($scriptPath, $change->doc->form, $change->doc->_id, $change->doc->name);
printf("Target Filename: '%s'\n", getScriptPathForRecord($scriptPath, $change->doc->form, $change->doc->_id, $change->doc->name));
if (!file_exists(dirname($targetFilename)))
{
mkdir(dirname($targetFilename), 0777, true);
}
deleteScriptFiles($scriptPath, $change->doc->_id);
file_put_contents($targetFilename, $change->doc->code);
break;
}
}
printf("Pending: %d; Last Seq: %s; Seq File: %s\n", $pending, $lastSeq, $sequenceFile);
file_put_contents($sequenceFile, $lastSeq);
}
}