-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmake_offline_archive.php
executable file
·135 lines (112 loc) · 4.36 KB
/
make_offline_archive.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
#!/usr/bin/php
<?php
// this is meant to be used from the command line, not a webpage, but
// it's written in PHP so we can reuse config.php and stuff.
chdir(dirname(__FILE__)); // just in case.
require_once('config.php');
// !!! FIXME: put this in a common.php file or something.
$supported_formats = [
'md' => 'gfm',
'mediawiki' => 'mediawiki'
];
$max_children = 4;
$num_children = 0;
function cook_tree_for_offline_html($srcdir, $dstdir)
{
global $supported_formats, $max_children, $num_children;
//print("cooktree: '$srcdir' -> '$dstdir'\n");
$dirp = opendir($srcdir);
if ($dirp === false) {
return;
}
while (($dent = readdir($dirp)) !== false) {
if (substr($dent, 0, 1) == '.') { continue; } // skip ".", "..", and metadata.
//print("cookdent: '$dent'\n");
$src = "$srcdir/$dent";
$dst = "$dstdir/$dent";
if (is_dir($src)) {
mkdir($dst);
cook_tree_for_offline_html($src, $dst);
} else {
$ext = strrchr($dent, '.');
if ($ext !== false) {
$ext = substr($ext, 1);
//print("cookext: '$ext'\n");
if (isset($supported_formats[$ext])) {
$from_format = $supported_formats[$ext];
$page = preg_replace('/\..*?$/', '', $dst);
$page = preg_replace('/^.*\//', '', $page);
$dst = preg_replace('/\..*?$/', '.html', $dst);
// split this across CPU cores.
while ($num_children >= $max_children) {
pcntl_waitpid(-1, $status); // wait for any child to end.
$num_children--;
}
// launch another!
$pid = pcntl_fork();
if ($pid == -1) {
print("FAILED TO FORK!\n");
} else if ($pid == 0) { // child process.
pcntl_exec('/usr/bin/pandoc', [ '--metadata', "pagetitle=$page", '--embed-resources', '--standalone', '-f', $from_format, '-t', 'html', '--css=static_files/ghwikipp.css', '--css=static_files/pandoc.css', '--lua-filter=./pandoc-filter-offline.lua', '-o', $dst, $src ]);
exit(1);
} else { // parent process.
$num_children++;
//print("$page\n");
}
}
}
}
}
closedir($dirp);
}
// Mainline!
date_default_timezone_set('UTC');
$outputname = $wiki_offline_basename;
@mkdir('static_files/offline'); // just in case.
$tmpdir = $offline_data;
$esctmpdir = escapeshellarg($tmpdir);
system("rm -rf $esctmpdir");
if (!mkdir($tmpdir)) {
print("Failed to mkdir '$tmpdir'\n");
exit(1);
}
$tmpoutdir = "$tmpdir/$outputname";
$esctmpoutdir = escapeshellarg($tmpoutdir);
if (!mkdir($tmpoutdir)) {
print("Failed to mkdir '$tmpoutdir'\n");
exit(1);
}
$tmprawdir = "$tmpdir/raw";
$escrawdir = escapeshellarg($raw_data);
if (!mkdir($tmprawdir)) {
print("Failed to mkdir '$tmprawdir'\n");
exit(1);
}
// copy all the raw data elsewhere, so we don't hold the lock longer than
// necessary. We should probably use rsync to speed this up more. :)
$git_repo_lock_fp = fopen($repo_lock_fname, 'c+');
if ($git_repo_lock_fp === false) {
print("Failed to obtain Git repo lock. Please try again later.\n");
exit(1);
} else if (flock($git_repo_lock_fp, LOCK_EX) === false) {
print("Exclusive flock of Git repo lock failed. Please try again later."); // uh...?
exit(1);
}
$esctmprawdir = escapeshellarg($tmprawdir);
system("cp -R $escrawdir/* $esctmprawdir/");
flock($git_repo_lock_fp, LOCK_UN);
fclose($git_repo_lock_fp);
unset($git_repo_lock_fp);
// okay, now we can operate on this data.
cook_tree_for_offline_html($tmprawdir, $tmpoutdir);
while ($num_children > 0) {
pcntl_waitpid(-1, $status); // wait for any child to end.
$num_children--;
}
if (file_exists("$tmpoutdir/FrontPage.html")) {
copy("$tmpoutdir/FrontPage.html", "$tmpoutdir/index.html"); // make a copy, not a rename, in case something references FrontPage explicitly.
}
$zipfname = "$outputname.zip";
$esczipfname = escapeshellarg($zipfname);
$escoutputname = escapeshellarg($outputname);
system("cd $esctmpdir && zip -9rq $esczipfname $escoutputname && mv $esczipfname ../static_files/offline/");