-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
185 lines (130 loc) · 5.86 KB
/
index.php
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
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
set_time_limit(0);
date_default_timezone_set('CET');
ob_implicit_flush(true);
ob_end_flush();
require_once "config.php";
include_once("alignment.php");
function getFilesWithDate($dir) {
$files = scandir($dir);
foreach ($files as $f) {
if (($f != "." && $f != ".." && !is_dir($dir."/".$f) && preg_match("~\.json$~", $f) === 1)) {
$files_current[$f] = filemtime($dir."/".$f);
}
}
return $files_current;
}
//If there is no repository yet, get it.
if (!is_dir($conf["git"]["localPath"])) {
$cmd = $conf["git"]["exec"]." clone ".$conf["git"]["repository"]." ".$conf["git"]["localPath"];
exec($cmd);
$files_current = array();
} else {
//Get the current last edit dates of all *.json files
$files_current = getFilesWithDate($conf["git"]["localPath"]);
}
//Get the current last edit dates of all *.json files
//$files_current = json_decode(file_get_contents(__DIR__."/file-index.json"));
//Pull the updates
exec($conf["git"]["exec"]." -C ".$conf["git"]["localPath"]." remote update");
exec($conf["git"]["exec"]." -C ".$conf["git"]["localPath"]." pull");
$files_new = getFilesWithDate($conf["git"]["localPath"]);
//queue Index
if (!file_exists(__DIR__."/queue-index.json")) {
file_put_contents(__DIR__."/queue-index.json", json_encode(array()));
}
$queueIndex = json_decode(file_get_contents(__DIR__."/queue-index.json"),true);
foreach ($files_new as $k => $fd) {
if ((array_key_exists($k,$files_current) === false) || ($fd>$files_current[$k])) {
$tmpFile = json_decode(file_get_contents($conf["git"]["localPath"]."/".$k),true);
echo "<pre>";
foreach ($tmpFile as $speech) {
if (!$queueIndex[$k][$speech["media"]["sourcePage"]]) {
$queueIndex[$k][$speech["media"]["sourcePage"]] = "todo";
}
}
}
}
/*
* TODO: Cleaner-Job, find out schedule
* Massive performance improvement if we do it.
* Todo: Garbagecollector for temp files which aeneas needs for alignment
*
foreach ($queueIndex as $tmpK=>$file) {
$tmpCnt = 0;
$tmpLength = count($file);
print_r($tmpLength);
foreach ($file as $k=>$v) {
if ($v=="done") {
$tmpCnt++;
}
}
echo $tmpLength;
echo "<br><br>";
if ($tmpCnt == $tmpLength) {
unset($queueIndex[$tmpK]);
}
}
*/
file_put_contents(__DIR__."/queue-index.json",json_encode($queueIndex,JSON_PRETTY_PRINT));
foreach ($queueIndex as $filename=>$speechSourcePages) {
echo "filename:";
print_r($filename);
echo "file:";
print_r($speechSourcePages);
$inputJson = json_decode(file_get_contents($conf["git"]["localPath"]."/".$filename),true);
foreach ($speechSourcePages as $sourcePage=>$status) {
if ($status == "todo") {
foreach ($inputJson as $speechKey => $speech) {
if ($speech["media"]["sourcePage"] == $sourcePage) {
//This is the right speech to send to aeneas due to the todo queue
/*
* TODO: Check which type is proceedings, for now we take [0]
*
foreach ($speech as $textContents) {
foreach ($textContents as $textContent) {
if ()
}
}
*/
if (isset($speech["textContents"]) && count($speech["textContents"]) != 0) {
$mediaFileURI = ($speech["media"]["audioFileURI"]) ? $speech["media"]["audioFileURI"] : $speech["media"]["videoFileURI"];
$textObject = json_encode($speech["textContents"][0], true);
$tempFilenameForXMP = preg_replace("~\.~","-",microtime(true))."_".$speech["textContents"][0]["type"].".xml";
$tempFilePathForXMP = $conf["dir"]["input"]."/".$tempFilenameForXMP;
$alignmentInputXML = textObjectToAlignmentInput($textObject, $mediaFileURI);
file_put_contents($tempFilePathForXMP, $alignmentInputXML);
try {
$response = forceAlignXMLData($tempFilenameForXMP,$conf["sleep"]);
if ($response["success"] == "true") {
//TODO: aufräumen und files updaten (queue und merged.json)
$alignmentOutput = file_get_contents($response["filename"]);
$queueIndex[$filename][$sourcePage] = "done";
$inputJson[$speechKey]["textContents"][0] = mergeAlignmentOutputWithTextObject($alignmentOutput,$textObject);
$inputJson[$speechKey]["media"]["aligned"] = 1;
} else {
$queueIndex[$filename][$sourcePage] = "error";
logMessage("Error processing file: Custom error\n");
}
} catch (Exception $e) {
logMessage("Error processing file:\n".$e->getFile()."\n"."Line: ".$e->getFile()."\n".$e->getMessage()."\n");
$error = true;
}
if ($error) {
logMessage("Error: Indexing queue could not be completed\n");
} else {
logMessage("\nSuccess: finished processing queue\n\n");
}
}
}
}
}
}
if (!is_dir($conf["dir"]["output-json"])) {
mkdir($conf["dir"]["output-json"]);
}
file_put_contents($conf["dir"]["output-json"]."/".$filename, json_encode($inputJson,JSON_PRETTY_PRINT));
file_put_contents(__DIR__."/queue-index.json",json_encode($queueIndex,JSON_PRETTY_PRINT));
}
?>