/
compile.php
169 lines (142 loc) · 6.19 KB
/
compile.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
<?php
chdir(__DIR__);
$output = array();
$return_code = $return = 0;
$verbose = in_array('-V', $_SERVER['argv']);
$perform_simulations = in_array('-S', $_SERVER['argv']);
$archive = in_array('-A', $_SERVER['argv']);
$redirect = $verbose ? "" : " > /dev/null 2>&1 ";
function strip_decl($str)
{
return str_replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", '', $str);
}
// XML
$files[0] = strip_decl(file_get_contents("data/events.xml"));
$files[1] = strip_decl(file_get_contents("data/contents.xml"));
$files[2] = strip_decl(file_get_contents("data/appendices.xml"));
file_put_contents('tmp/cache.xml', "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>{$files[0]}{$files[1]}{$files[2]}</root>");
$start_time = microtime(true);
exec('saxonb-xslt -s:tmp/cache.xml -xsl:refs.xsl -o:tmp/refs -ext:on' . $redirect, $output, $return_code);
$return |= $return_code;
$refs = file('tmp/refs');
foreach($refs as $ref)
{
$ref = trim($ref);
if(strpos($ref, 'doi:') === 0)
{
$ref = substr($ref, strlen('doi:'));
$safedoi = str_replace(')', '_', str_replace('(', '_', str_replace('/', '_', $ref)));
$outfile = "tmp/ref_$safedoi.xml";
if(!file_exists($outfile))
{
usleep(100000); // wait 100 ms to avoid failed requests
exec("curl --location --header \"Accept: application/unixref+xml\" \"http://dx.doi.org/$ref\" -o \"$outfile\" " . $redirect, $output, $return_code);
// retry in case of failure :
if (!filesize($outfile))
exec("curl --location --header \"Accept: application/unixref+xml\" \"http://dx.doi.org/$ref\" -o \"$outfile\" " . $redirect, $output, $return_code);
$return |= $return_code;
}
}
else if(strpos($ref, 'isbn:') === 0)
{
$ref = substr($ref, strlen('isbn:'));
$isbn = $ref;
$outfile = "tmp/ref_$isbn.xml";
if(file_exists($outfile))
continue;
exec("basex -q 'let " . '$url' . " := \"https://www.googleapis.com/books/v1/volumes?q=isbn:$isbn\" return json-to-xml(fetch:text(" . '$url' . "))' > $outfile");
}
else if(strpos($ref, 'arxiv:') === 0)
{
$ref = substr($ref, strlen('arxiv:'));
$safearxiv = str_replace(')', '_', str_replace('(', '_', str_replace('/', '_', $ref)));
$outfile = "tmp/ref_$safearxiv.xml";
if(!file_exists($outfile))
{
file_put_contents($outfile, file_get_contents("http://export.arxiv.org/api/query?search_query=$ref&start=0&max_results=1;"));
$return |= $return_code;
}
}
}
echo "REFS generation completed (" . round(microtime(true) - $start_time, 4) . " s)\n";
$start_time = microtime(true);
exec('saxonb-xslt -s:tmp/cache.xml -xsl:layout.xsl -o:index.html -ext:on' . $redirect, $output, $return_code);
$return |= $return_code;
echo "HTML generation completed (" . round(microtime(true) - $start_time, 4) . " s)\n";
$start_time = microtime(true);
exec('saxonb-xslt -s:tmp/cache.xml -xsl:graph.xsl -o:graph.html -ext:on' . $redirect, $output, $return_code);
$return |= $return_code;
echo "graph generation completed (" . round(microtime(true) - $start_time, 4) . " s)\n";
// simulations
if($perform_simulations)
{
$start_time = microtime(true);
$simulations = glob('simulations/*', GLOB_ONLYDIR);
foreach($simulations as $simulation)
{
if(!file_exists("$simulation/do.sh"))
{
if($verbose) echo "Skipped $simulation due to missing do.sh file\n";
continue;
}
exec("tar -zcvf $simulation.tar.gz $simulation");
if($verbose) echo "Running simulation $simulation...\n";
chdir($simulation);
chmod('do.sh', 0755);
exec('./do.sh' . $redirect, $output, $return_code);
$return |= $return_code;
chdir('../..');
}
echo "simulations execution completed (" . round(microtime(true) - $start_time, 4) . " s)\n";
}
// gnuplot
$start_time = microtime(true);
function translate_latex($gnuplot)
{
$symbols = array("\alpha" => "{/Symbol a}",
"\beta" => "{/Symbol b}",
"\delta" => "{/Symbol d}",
"\Delta" => "{/Symbol D}",
"\gamma" => "{/Symbol g}",
"\omega" => "{/Symbol o}",
"\Omega" => "{/Symbol O}",
"\phi" => "{/Symbol p}",
"\tau" => "{/Symbol t}");
foreach($symbols as $tex => $svg) $gnuplot = str_replace($tex, $svg, $gnuplot);
return $gnuplot;
}
chdir('plots/');
$plots = glob("*.gnuplot");
foreach($plots as $plot)
{
$plot = preg_replace('/\\.(gnuplot)/', '', $plot);
file_put_contents("tmp", "set term svg enhanced dynamic dashed font 'DejaVuSerif,14'; set out '../images/$plot.svg'; \n" . translate_latex(file_get_contents("$plot.gnuplot")));
exec('gnuplot tmp' . $redirect, $output, $return_code);
file_put_contents("tmp", "set term epslatex; set out '../images/$plot.tex'; \n" . file_get_contents("$plot.gnuplot"));
exec('gnuplot tmp' . $redirect, $output, $return_code);
$return |= $return_code;
}
if(is_file('tmp')) unlink('tmp');
echo "plot generation completed (" . round(microtime(true) - $start_time, 4) . " s)\n";
chdir('..');
if(in_array('-B', $_SERVER['argv']))
{
$start_time = microtime(true);
exec('saxonb-xslt -s:tmp/cache.xml -xsl:booklet.xsl -o:booklet/booklet.tex -ext:on' . $redirect, $output, $return_code);
$return |= $return_code;
exec('cd booklet; pdflatex --shell-escape -interaction=nonstopmode booklet.tex', $output, $return_code);
exec('cd booklet; bibtex booklet', $output, $return_code);
exec('cd booklet; pdflatex --shell-escape -interaction=nonstopmode booklet.tex', $output, $return_code);
exec('cd booklet; pdflatex --shell-escape -interaction=nonstopmode booklet.tex', $output, $return_code);
echo "booklet generation completed (" . round(microtime(true) - $start_time, 4) . " s)\n";
}
@unlink('tmp/cache.xml');
@unlink('tmp/refs');
if($archive)
{
@unlink('archive.tar.gz');
exec("tar -zcvf ../archive.tar.gz . --exclude='*~' --exclude='.git' --exclude='tmp' --exclude='data'");
rename('../archive.tar.gz', 'archive.tar.gz');
}
echo "done.";
exit((int)$return);