/
functions.php
207 lines (160 loc) · 5.04 KB
/
functions.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
declare(strict_types=1);
/**
* Repeatedly calls a callable until it's time to stop
*
* @param callable $callable - the thing to run
* @param int $secondsBetweenRuns - the minimum time between runs
* @param int $sleepTime - the time to sleep between runs
* @param int $maxRunTime - the max time to run for, before returning
*/
function continuallyExecuteCallable($callable, int $secondsBetweenRuns, int $sleepTime, int $maxRunTime)
{
$startTime = microtime(true);
$lastRuntime = 0;
$finished = false;
echo "starting continuallyExecuteCallable \n";
while ($finished === false) {
set_time_limit(120);
$shouldRunThisLoop = false;
if ($secondsBetweenRuns === 0) {
$shouldRunThisLoop = true;
}
else if ((microtime(true) - $lastRuntime) > $secondsBetweenRuns) {
$shouldRunThisLoop = true;
}
if ($shouldRunThisLoop === true) {
$callable();
$lastRuntime = microtime(true);
}
if (checkSignalsForExit()) {
break;
}
if ($sleepTime > 0) {
sleep($sleepTime);
}
if ((microtime(true) - $startTime) > $maxRunTime) {
echo "Reach maxRunTime - finished = true\n";
$finished = true;
}
}
echo "Finishing continuallyExecuteCallable\n";
}
/**
* Self-contained monitoring system for system signals
* returns true if a 'graceful exit' like signal is received.
*
* We don't listen for SIGKILL as that needs to be an immediate exit,
* which PHP already provides.
* @return bool
*/
function checkSignalsForExit()
{
static $initialised = false;
static $needToExit = false;
$fnSignalHandler = function ($signalNumber) use (&$needToExit) {
$needToExit = true;
};
if ($initialised === false) {
pcntl_signal(SIGINT, $fnSignalHandler, false);
pcntl_signal(SIGQUIT, $fnSignalHandler, false);
pcntl_signal(SIGTERM, $fnSignalHandler, false);
pcntl_signal(SIGHUP, $fnSignalHandler, false);
pcntl_signal(SIGUSR1, $fnSignalHandler, false);
$initialised = true;
}
pcntl_signal_dispatch();
return $needToExit;
}
/**
* @param $content
* @return string|null
*/
function docs_get_xml_id($content)
{
// All xmlid
$match = array();
if (preg_match_all('/xml:id=("|\')(.*?)("|\')/', $content, $match)) {
// we can only cope with one id
foreach ($match[2] as $id){
return $id;
}
return implode('|', $match[2]);
}
return null;
}
/**
* @param $filename
* @return array
*/
function renderSingleFileForManual($filename)
{
$fullpathOfSourceFile = ROOT_PATH . '/docs/en/' . $filename;
echo "renderSingleFileForManual " . $fullpathOfSourceFile . "\n";
$contents = file_get_contents($fullpathOfSourceFile);
if ($contents === false) {
return [null, "failed to read file $filename"];
}
$xml_id = docs_get_xml_id($contents);
if ($xml_id === null) {
chdir(ROOT_PATH);
return [null, "Failed to find xml_id for file $filename."];
}
$srcCommand = <<< TEXT
php ../doc-base/configure.php \
--generate='$fullpathOfSourceFile' \
--with-partial="$xml_id"
TEXT;
echo "About to run configure to src -> xml\n";
chdir(ROOT_PATH . '/docs/en');
exec($srcCommand, $output, $return_value);
if ($return_value !== 0) {
chdir(ROOT_PATH);
return [null, "Failed to run [$srcCommand]"];
}
$filename = '.manual.' . $xml_id . '.xml';
$tmpDirNameInOutputTemp = hash('sha256', (string)time());
$tmpDirName = ROOT_PATH . '/output_temp/' . $tmpDirNameInOutputTemp;
$created = @mkdir($tmpDirName, 0755, true);
$fullpath = ROOT_PATH . "/docs/doc-base/" . $filename;
$phdPath = ROOT_PATH . '/phd/render.php';
chdir(ROOT_PATH);
$phdCommand = <<< TEXT
php $phdPath \
--docbook $fullpath \
--memoryindex \
--package PHP \
--format php \
--output ./output_temp/$tmpDirNameInOutputTemp
TEXT;
echo "about to render xml -> docbook\n";
chdir(ROOT_PATH);
exec($phdCommand, $output, $return_value);
// echo "return value is [$return_value]\n";
// echo "output is:\n";
// var_dump($output);
$source = $tmpDirName . '/php-web/' . $xml_id . '.php';
$destination = ROOT_PATH . '/output/php-web/' . $xml_id . '.php';
$copied = @copy(
$source,
$destination
);
// @TODO - delete temporary directory here.
if ($copied) {
// echo "File should be updated.\n";
return [$xml_id, null];
}
return [null, "Failed to copy updated file to where it needs to be.\n"];
// echo "source was: $source \n";
// echo "Destination was: $destination \n";
}
function doesFileNeedCompiling($filename)
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (strcasecmp($ext, 'xml') === 0) {
// echo "File $filename is an xml file - interesting!\n";
return true;
}
// echo "File $filename is not xml file - BORING!\n";
return false;
}