-
Notifications
You must be signed in to change notification settings - Fork 6
/
legacy_write_html.php
235 lines (190 loc) · 6.12 KB
/
legacy_write_html.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
require_once('config.php');
require_once('common.php');
log_info("Writing HTML files:\n");
$forum_url = rtrim($forum_url, '/');
$forum_dir = rtrim($forum_dir, '/');
$target_dir = rtrim($target_dir, '/');
$forum_dir_valid = TRUE;
if (!is_dir($forum_dir)) {
error_log("WARNING: forum directory is not valid: '$forum_dir'");
error_log(" Smilies and attachments will not be copied.");
$forum_dir_valid = FALSE;
}
if (!file_exists("./forum-data.json")) {
error_log("ERROR: forum data not available. Run this command first:");
error_log(" php extract");
exit(1);
}
mkdir_p($target_dir, 0755);
$total_forums = 0;
$total_topics = 0;
$total_attachments = 0;
$total_attachment_errors = 0;
function copy_attachments($posts, $topic_dir, $tid) {
global $forum_dir;
global $target_dir;
global $total_attachment_errors;
global $total_attachments;
// prevent copy warnings
$old_error_reporting = error_reporting(E_ERROR);
foreach ($posts as $post) {
$attachments = $post['attachments'];
if (!empty($attachments)) {
foreach ($attachments as $attachment) {
$id = $attachment['id'];
$physical_filename = $attachment['physical_filename'];
$real_filename = $attachment['real_filename'];
//$filetime => $attachment['filetime'];
$source = $forum_dir . "/files/" . $physical_filename;
$dest = $target_dir . '/' . $topic_dir . $real_filename;
if (copy($source, $dest) !== FALSE) {
log_info("*");
$total_attachments++;
} else {
error_log("\nERROR: cannot copy attachment '$source' to '$dest'");
error_log(" Attachment ID: $id / Topic ID: $tid");
$total_attachment_errors++;
}
}
}
}
error_reporting($old_error_reporting);
}
function generate_topics($extracted) {
global $forum_name, $forum_url;
global $archive_base_url;
global $total_topics;
global $forum_dir_valid;
$sitemap = array();
$topics = $extracted['topics'];
$forums = $extracted['forums'];
log_info("Writing topics...");
foreach ($topics as $tid => $topic) {
$fid = $topics[$tid]['fid'];
$var = array();
$var['forum_name'] = $forum_name;
if (!empty($forums[$fid]['title'])) {
$var['forum_title'] = $forums[$fid]['title'];
} else {
$var['forum_title'] = '(unknown forum)';
}
if (!empty($topics[$tid]['title'])) {
$topic_title = $topics[$tid]['title'];
} else {
$topic_title = '(unknown topic)';
}
$var['slug'] = slug($topic_title);
$var['title'] = get_topic_title_with_type($topic_title, $topics[$tid]['type']);
$var['tid'] = $tid;
$var['url'] = $forum_url . '/viewtopic.php?t=' . $tid;
$var['posts'] = array();
$var['lastmod'] = $topics[$tid]['lastmod'];
$var['posts'] = $topic['posts'];
// Generate a redirection page. We might not know the topic slug when
// linking. In such case we land in the slug-less page which is a redirect
// to the slugged URL, with content.
$content = template_get($var, 'topic-redirect.tpl.php');
write_content($fid . '/' . $tid . '/index.html', $content);
$post_rel_url = $fid . '/' . $tid . '/' . $var['slug'] . '/';
array_push($sitemap, array(
'loc' => $archive_base_url . $post_rel_url,
'lastmod' => $var['lastmod'],
));
$content = template_get($var, 'topic.tpl.php');
write_content($post_rel_url . '/index.html', $content);
log_info(" $tid");
if ($forum_dir_valid) {
// saving attachments
copy_attachments($topic['posts'], $post_rel_url, $tid);
}
$total_topics++;
}
log_info("\n");
log_info("Writing sitemap.xml ...");
$var = array(
'urlset' => $sitemap,
);
$content = template_get($var, 'sitemap.tpl.php');
write_content('sitemap.xml', $content);
log_info("done.\n");
}
function generate_forums($extracted) {
global $forum_name, $forum_description;
global $total_forums;
$forums = $extracted['forums'];
$topics = $extracted['topics'];
log_info("Loading forums...");
foreach ($forums as $fid => $forum) {
if (!empty($forums[$fid]['title'])) {
$forum_title = $forums[$fid]['title'];
} else {
$forum_title = '(unknown forum)';
}
$var = array(
'topics' => $topics,
'list' => $forums[$fid]['topics'],
'forum_name' => $forum_name,
'forum_title' => $forum_title,
'forum_description' => $forum_description
);
$content = template_get($var, 'forum.tpl.php');
write_content($fid . '/index.html', $content);
log_info(" $fid");
$total_forums++;
}
log_info("\n");
}
function generate_main($extracted) {
global $forum_name, $forum_description;
log_info("Creating index...");
// Content
$var = array(
'categories' => $extracted['categories'],
'forums' => $extracted['forums'],
'forum_name' => $forum_name,
'forum_description' => $forum_description
);
$content = template_get($var, 'main.tpl.php');
write_content('index.html', $content);
log_info("done\n");
}
function copy_smilies() {
global $target_dir;
global $forum_dir;
global $forum_dir_valid;
if ($forum_dir_valid) {
$source_dir = $forum_dir . "/images/smilies";
if (!is_dir($source_dir)) {
error_log("ERROR: forum smiley directory does not exist: $source_dir");
} else {
log_info("Copying smilies...");
$dest_dir = $target_dir . "/images";
mkdir_p($dest_dir);
recurse_copy($source_dir, $dest_dir . "/smilies");
log_info("done\n");
}
}
}
function copy_templates() {
global $target_dir;
log_info("Copying scripts and images...");
$source_dir = "./templates/res";
$dest_dir = $target_dir;
recurse_copy($source_dir, $dest_dir);
log_info("done\n");
}
log_info("Loading forum-data.json...");
$extracted = json_decode(file_get_contents("./forum-data.json"), true);
log_info("done.\n");
generate_main($extracted);
generate_forums($extracted);
generate_topics($extracted);
copy_templates();
copy_smilies();
log_info("\nStatistics:\n");
log_info("- forums: $total_forums\n");
log_info("- topics: $total_topics\n");
log_info("- attachments: $total_attachments ($total_attachment_errors failed)\n");
log_info("Successfully created forum archive in: $target_dir\n");
log_info("\n");