-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.php
189 lines (157 loc) · 6.59 KB
/
worker.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
186
187
188
189
<?php
session_start(['cookie_lifetime' => 86400]);
// what this file does: get data, saves an rmd file, send to R renderer, return rendered files and messages
include "getnewid.php";
// control vars
$sysMsg = "";
$errorMsg = "";
$createdFileNames = [];
$testingLevel = 0 ; // 0 = not testing, 1 = some test output, 2 = more text output
$runR = true;
$platform = "aws"; // aws, xampp (local)
$startTime = 0;
$dontTakeTheseExtensions = ['tex', 'rmd', 'log'];
// reporting
error_reporting(E_ALL & ~E_NOTICE);
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
global $errorMsg;
//throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
$errorMsg .= "<p>Error ($errline) $errstr</p>\n";
}
set_error_handler("exception_error_handler");
if ( $testingLevel > 1 ) {
$time = date('Y-m-d H:i:s');
$sysMsg .= "<p>Timestamp: $time</p>\n";
}
// set initial timestamp
$date = new DateTime();
$startTime = $date->getTimestamp();
// get folder info
$isDirSet = false;
if ( ! isset ( $_SESSION_['folder_id'] ) ) {
$idErr = GetNewId();
if ( strlen($idErr) > 0 ) {
$errorMsg .= $idErr;
} else {
$isDirSet = true;
}
}
if ( ! $isDirSet ) {
$errorMsg .= "<p>Error creating new id.</p>\n";
} else if ( $testingLevel > 1 ) {
$sysMsg .= "<p>(main) Folder ID: " . $_SESSION['folder_id'] . "</p>\n";
}
$haveData = false;
if ( isset($_POST['rmd_name']) && ! empty($_POST['rmd_name']) && isset($_POST['rmd_text']) && ! empty($_POST['rmd_text']) ) {
$haveData = true;
}
if ( $testingLevel > 1 ) {
$sysMsg .= "<p>Starting process. data: $haveData. dir: $isDirSet</p>\n";
}
if ( $haveData && $isDirSet ) {
// gather info and save to the rmd file
$name = $_POST['rmd_name'];
$text = $_POST['rmd_text'];
if ( isset($_POST['formats']) && ! empty($_POST['formats']) ) {
$formats = $_POST['formats'];
} else {
$formats = "";
}
if ( $_SESSION['folder_id'] > 0 )
{
$id = $_SESSION['folder_id'];
$fileName = $_POST['rmd_name'];
$fileName = str_replace(" ", "", $fileName);
$fileFormatsInput = trim($formats);
$fileFormats = explode(" ", $fileFormatsInput);
$dir = "./output/$id";
// wipe out the old files
if ( file_exists($dir) ) {
foreach ( $fileFormats as $thisFormat) {
$fullFileName = "$dir/$fileName.$thisFormat";
if ( file_exists($fullFileName)) {
unlink($fullFileName);
}
}
rmdir($dir);
}
// write input data to file
$fileContents = $_POST['rmd_text'];
//$fileContents = mb_convert($fileContents, 'UTF-8', 'auto'); // probably don't use these, as encoding already utf8 text can mangle it
//$fileContents = utf8_encode($fileContents);
$file = "$dir/$fileName.rmd";
mkdir($dir);
file_put_contents($file, $fileContents);
if ( $testingLevel > 1 ) {
$sysMsg .= "<p>RMD file location: $dir</p>\n";
}
// hack: session_start isn't working (probably a php.ini issue with session.save_path permission), which means all files are in different folders.
// Let's group them up before we submit. Then R will catch every .bib in the folder :)
if ( isset($_POST['bib_paths']) ) {
foreach ( $_POST['bib_paths'] as $thisBibPath ) {
$oldBib = str_replace("../", "output/", $thisBibPath);
$newBib = "./output/" . $id . "/" . basename($thisBibPath);
$copyWorked = copy($oldBib, $newBib);
if ( $testingLevel > 1 && ! $copyWorked) {
$err = error_get_last();
$sysMsg .= "<p>copied Path [$oldBib] to [$newBib]. Worked: [$copyWorked]</p>\n";
$sysMsg .= "<p>Last err: " . $err['message'] . " line " . $err['line'] . "</p>\n";
$sysMsg .= "<p>file existed in the first place? [" . file_exists($oldBib) . "]</p>\n";
}
}
}
// run renderer to generate files
if ( $runR ) {
// args for R file
$args = "";
$argLocalPath = "\\output\\$id\\$fileName.rmd\"";
if ( $platform == "aws" ) {
$argLocalPath = "/output/$id/$fileName.rmd\"";
}
$args .= "\"" . dirname(__FILE__) . $argLocalPath; // the file path + name
$args .= " id=\"$id\"";
$args .= " formats=\"$fileFormatsInput\"";
if ( isset($_POST['csl_path'] ) ) {
$args .= " " . basename($_POST['csl_path']);
}
$rScript = "C:\\Program Files\\R\\R-3.5.0\\bin\\Rscript.exe"; // xampp localhost default
$rFile = "C:\\xampp\\htdocs\\AROW\\R\\RMDrender.R";
if ( $platform == "aws" ) {
$rScript = "/usr/lib/R/bin/Rscript"; // aws linux
$rFile = "/var/www/html/R/RMDrender.R";
}
$execCommand = "\"$rScript\" \"$rFile\" $args";
if ( $testingLevel > 1 ) {
$sysMsg .= "<p>Exec'ing: $execCommand</p>\n";
}
exec($execCommand, $output, $return);
if ( isset($output) ) {
if ( $testingLevel > 1 ) {
$sysMsg .= "<div class='card p-4'><h4>Output from R</h4>" . print_r($output, true) . "</div>\n";
}
//$sysMsg .= "<div>" . serialize($output) . "</div>\n";
// we regex through the garbage output to get our filenames.
// it's wrapped in FILENAMESHERE|actualfiles|FILENAMESTHERE
//$pattern = '/\/output\/\d+\/([^"]+)"/';
$createdFileNames = [];
$pattern = '/HERESAFILE\|[^\|]+\/([^\|\/]+)\|/';
preg_match_all($pattern, serialize($output), $match);
if ( count($match) > 1 ) {
$createdFileNames = $match[1];
}
//$sysMsg .= "<p>createdFileNames: " . print_r(implode(', ',$createdFileNames), true) . "</p>\n";
} else {
$sysMsg .= "<h3>R failed to run.</h3>\n";
$sysMsg .= "<div>" . print_r($output, true) . "</div>\n";
}
}
} else {
$sysMsg .= "<p>Unable to process data. Folder id: " . $_SESSION['folder_id'] . "</p>\n";
}
}
if ( $testingLevel > 1 ) {
$sysMsg .= "<p>Processing complete. " . implode($fileFormats, ", ") . "</p>\n";
}
$outputData = array("error" => $errorMsg, "ID" => $id, "created_filenames" => $createdFileNames, "message" => $sysMsg);
echo json_encode($outputData);
?>