forked from FlominatorTM/WikiTree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatfeed.php
More file actions
289 lines (244 loc) · 7.29 KB
/
Copy pathcatfeed.php
File metadata and controls
289 lines (244 loc) · 7.29 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
$is_debug = isset($_REQUEST['debug']);
$cat = str_replace('Category:', '', $_REQUEST['cat']);
if(!$is_debug)
{
header("Content-Type: application/rss+xml");
header('Content-Disposition: inline;Filename=' . urlencode($cat).".xml");
echo('<?xml version="1.0" encoding="UTF-8"?>');
}
print_debug("debugging enabled");
$limit = 10; //currently does nothing
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url_here = $protocol . $_SERVER['HTTP_HOST'] . htmlspecialchars($_SERVER['REQUEST_URI'], ENT_XML1); ;
$depth = 9;
if (isset($_REQUEST['depth']) && $_REQUEST['depth'] != "")
{
$depth = 0 + $_REQUEST['depth'];
}
if(!check_has_any_data($cat, $depth))
{
get_current_content($cat, $depth);
}
else if(has_new_data_available($cat, $depth))
{
// echo "new data";
$file_time_before = filemtime(current_file($cat, $depth));
$prev_filling = get_previous_content($cat, $depth);
get_current_content($cat, $depth);
compare_and_dump_contents($cat, $depth, $prev_filling, $file_time_before);
}
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="<?php echo $url_here; ?>" rel="self" type="application/rss+xml" />
<description></description>
<language>en</language>
<pubDate><?php echo(date("r", filemtime(current_file($cat, $depth)))) ?></pubDate>
<title>Changes to Category:<?php echo $cat; ?></title>
<link><?php echo "https://www.wikitree.com/wiki/Category:" . str_replace(' ', '_', $cat) ?></link>
<?php
build_feed($cat, $depth, $limit);
function escape_cat($cat)
{
return str_replace(' ', '_', urldecode($cat));
}
function cat_dir($cat, $depth)
{
$dir = "/sftp/straub620/www/catdata/" . escape_cat($cat);
print_debug("depth=$depth");
if($depth != 9 && $depth != "9")
{
$dir = $dir . '_' . $depth;
}
if(!is_dir($dir))
{
mkdir ($dir, 0777);
}
return $dir."/";
}
function current_file($cat, $depth)
{
return cat_dir($cat, $depth) . "current.txt";
}
function date_file($cat, $depth)
{
return cat_dir($cat, $depth) . "date.txt";
}
function check_has_any_data($cat, $depth)
{
return is_file(current_file($cat, $depth));
}
function get_current_content($cat, $depth)
{
print_debug("entering get_current_content");
$url = "https://plus.wikitree.com/function/WTWebProfileSearch/Flo_CatFeed.csv?Query=subcat$depth=\"". urlencode($cat)."\"&MaxProfiles=15000&Format=CSV";
// echo $url."<br>";
$csv_page = file_get_contents($url);
$current_file = current_file($cat, $depth);
$arbitrary_byte_number = 100;
if(strlen($csv_page)< $arbitrary_byte_number)
{
print_debug("csv_page is shorter than " + $arbitrary_byte_number + " bytes");
return;
}
print_debug("length csv_page=" . strlen($csv_page));
$lines = [];
$csv_lines = explode("\n", $csv_page);
$i=-1;
foreach($csv_lines as $csv_line)
{
if(!stristr($csv_line, "User ID"))
{
$line_parts = explode(";", $csv_line);
if(is_numeric($line_parts[0]))
{
$i++;
$lines[$i] = trim($csv_line);
}
else
{
$lines[$i].="|" . trim($csv_line);
}
}
}
file_put_contents($current_file, implode("\n", $lines));
chmod($current_file, 0777);
$date_file = date_file($cat, $depth);
file_put_contents($date_file, get_wiki_tree_plus_date());
chmod($date_file, 0777);
}
function get_wiki_tree_plus_date()
{
$page = "https://plus.wikitree.com/DataDates.json";
$json = json_decode(file_get_contents($page));
return $json->categoriesDate;
}
function has_new_data_available($cat, $depth)
{
print_debug("entering has_new_data_available($cat, $depth)");
$plus_cat_date = get_wiki_tree_plus_date();
$list_date = file_get_contents(date_file($cat, $depth));
print_debug("plus_cat_date=$plus_cat_date");
print_debug("list_date=$list_date");
if ($plus_cat_date == "" || $list_date == "")
{
print_debug("returning because of empty date");
return false;
}
//list_date has to be older, so difference can mean only "new data"
$lists_differ = $plus_cat_date != $list_date;
print_debug("lists_differ=$lists_differ");
return $lists_differ;
}
function get_previous_content($cat, $depth)
{
return file_get_contents(current_file($cat, $depth));
}
function compare_and_dump_contents($cat, $depth, $old, $file_time_before)
{
$current_file = current_file($cat, $depth);
$new = file_get_contents($current_file);
print_debug("length current_file content=" . strlen($new));
print_debug("previous file content=" . strlen($old));
$new_rows = explode("\n", $new);
$old_rows = explode("\n", $old);
$additions = get_missing_rows($old, $new_rows);
$removals = get_missing_rows($new, $old_rows);
$list_date = file_get_contents(date_file($cat));
$dir = cat_dir($cat);
if(count($additions) > 0 || count($removals) > 0)
{
file_put_contents($dir . $list_date . "+.csv", implode("\n", $additions));
file_put_contents($dir . $list_date . "-.csv", implode("\n", $removals));
}
else
{
touch($current_file, $file_time_before);
}
}
function get_missing_rows($old, $new_rows)
{
$additions = array();
foreach($new_rows as $new_row)
{
$row_parts = explode(";", $new_row);
{
if(is_numeric($row_parts[0]) && !stristr($old, $row_parts[0]))
{
$additions[] = $new_row;
//todo: do something with line_wraps
}
}
}
return $additions;
}
function build_feed($cat, $depth, $limit)
{
// echo "building";
$dir = cat_dir($cat, $depth);
$files = scandir($dir, SCANDIR_SORT_ASCENDING);
if(count($files)==4) //only . .. date.txt and current.txt
{
$current_file_time = filemtime(current_file($cat, $depth));
echo " <item>\n";
echo " <title>Tracking of content started</title>\n";
// echo " <link>$link</link>\n";
echo " <guid>https://www.wikitree.com/wiki/Category:" . urlencode(str_replace(' ', '_', $cat)) . '#' . "$current_file_time</guid>\n";
echo " <description>No changes so far, please be patient for a few days</description>\n";
echo " <pubDate>" . date("r", $current_file_time) . "</pubDate>\n";
echo " </item>\n";
}
else
{
for($i=0;$i<count($files);$i++)
{
if(stristr($files[$i], '-.csv'))
{
$path = $dir . $files[$i];
$current_file_time = filemtime($path);
echo " <item>\n";
echo " <title>Category changes</title>\n";
// echo " <link>$link</link>\n";
echo " <guid>https://www.wikitree.com/wiki/Category:" . urlencode(str_replace(' ', '_', $cat)) . '#' . "$current_file_time</guid>\n";
echo " <description><![CDATA[";
$removals = file_get_contents($path);
$add_file = str_replace('-.csv', '+.csv', $path);
$additions = file_get_contents($add_file);
echo "Additions:\n";
// print_debug("additions:" . $additions);
print_profile_lines(explode("\n", $additions));
echo "Removals:\n";
// print_debug("removals:" . $removals);
print_profile_lines(explode("\n", $removals));
echo " ]]></description>\n";
echo " <pubDate>" . date("r", $current_file_time) . "</pubDate>\n";
echo " </item>\n";
}
}
}
}
function print_profile_lines($rows)
{
echo "<ul>";
foreach($rows as $row)
{
// print_debug("row: $row");
if(strlen($row)>1)
{
$cols = explode(';', $row);
echo "<li>$cols[3]: https://www.wikitree.com/wiki/$cols[1]</li>";
}
}
echo "</ul>";
}
function print_debug($line)
{
global $is_debug;
if($is_debug)
{
echo $line . "<br>";
}
}
?></channel>
</rss>