Files
archived-web-master/scripts/update-backend
Sobak 54328e85c2 Remove mirrors related code (in rather non-risky, not so intrusive way)
I tried to keep backwards compatibility where it seemed to make sense
(e.g. the API endpoint) or the script that might still be called
somehow. Hopefully I didn't break anything but if I did feel free
to point it at me and sorry in advance :/
2020-04-18 23:21:37 +02:00

108 lines
3.2 KiB
PHP
Executable File

#!/usr/local/bin/php -q
<?php /* vim: set noet ts=2 sw=2 ft=php: : */
// We protect the master server with this token, so
// only who knows the token can fetch data from the server
$token = getenv("TOKEN");
if (!$token && file_exists(".token")) $token = file_get_contents(".token");
if (!$token) die("you have to set the TOKEN environment variable");
$token = rawurlencode($token);
// We need the root path for phpweb to write the data there
if (!$argv[1]) die("usage: $argv[0] directory\n");
$root = $argv[1];
// This script may run for a long time,
// due to remote data fetching
set_time_limit(30 * 60);
// Get list of upcoming events
fetch_into_file("https://master.php.net/fetch/events.php?token=$token",
"$root/backend/events.csv");
// Pregenerate event listing sidebar for homepage
include "event_listing";
// If we are in the first days of the month then only list current month
$months = (date('j') < 10) ? 1:2;
pregenerate_events("$root/backend/events.csv", "$root/include/pregen-events.inc", $months);
// Run ip-to-country fetch code
include "ip-to-country";
fetch_ip_to_country($root);
include "pregen_news";
pregen_atom("$root/archive/archive.xml", "$root/feed.atom", "$root/include/pregen-news.inc");
include "rss_parser";
legacy_rss("$root/feed.atom", "$root/news.rss", "$root/conferences/news.rss");
// Pregenerate conference teaser
include "conference_teaser";
pregenerate_conf_teaser("$root/feed.atom", "$root/include/pregen-confs.inc");
// Pregenerate elephpant image pool from flickr.
include "pregen_flickr";
$flickr_api_key = getenv("TOKEN_FLICKR");
if (!$flickr_api_key && file_exists(".token_flickr")) $flickr_api_key = file_get_contents(".token_flickr");
if (!$flickr_api_key) die("you have to set the TOKEN_FLICKR environment variable or create a .token_flickr file");
pregen_flickr(
$flickr_api_key,
$root . '/images/elephpants',
100
);
include 'usergroups_update_infos';
usergroups_update_infos($root);
// Fetch data into a temporary file first, and then
// overwrite the real file with the new data
function fetch_into_file($url, $file)
{
$SSL_fopen = false;
if(in_array('https', stream_get_wrappers())) {
$SSL_fopen = true;
}
// Open URL for reading
if($SSL_fopen) {
$source = @fopen($url, "r");
if (!$source) {
return;
}
} else {
$source = popen("curl -s '$url'", 'r');
}
// Open temporary file for writing
$dest = @fopen("$file~", "w");
if (!$dest) {
echo "failed to open '$file~' for writing\n";
return;
}
// Read until $source provides data, and write
// out the chunk to the output file if possible
while (!feof($source)) {
$chunk = fread($source, 4096);
if (fwrite($dest, $chunk) < 0) {
fclose($source);
fclose($dest);
unlink("$file~");
echo "failed writing to '$file~'\n";
return;
}
}
fclose($source);
fclose($dest);
// If we don't have new data, delete file
if (!@filesize("$file~")) {
echo "'$file~' was empty, skipping\n";
unlink("$file~");
return;
}
// Replace real file with temporary file
return rename("$file~", $file);
}