xref: /web-master/scripts/update-backend (revision 54328e85)
1#!/usr/local/bin/php -q
2<?php /* vim: set noet ts=2 sw=2 ft=php: : */
3
4// We protect the master server with this token, so
5// only who knows the token can fetch data from the server
6$token = getenv("TOKEN");
7if (!$token && file_exists(".token")) $token = file_get_contents(".token");
8if (!$token) die("you have to set the TOKEN environment variable");
9$token = rawurlencode($token);
10
11// We need the root path for phpweb to write the data there
12if (!$argv[1]) die("usage: $argv[0] directory\n");
13$root = $argv[1];
14
15// This script may run for a long time,
16// due to remote data fetching
17set_time_limit(30 * 60);
18
19// Get list of upcoming events
20fetch_into_file("https://master.php.net/fetch/events.php?token=$token",
21                "$root/backend/events.csv");
22
23// Pregenerate event listing sidebar for homepage
24include "event_listing";
25// If we are in the first days of the month then only list current month
26$months = (date('j') < 10) ? 1:2;
27pregenerate_events("$root/backend/events.csv", "$root/include/pregen-events.inc", $months);
28
29
30// Run ip-to-country fetch code
31include "ip-to-country";
32fetch_ip_to_country($root);
33
34include "pregen_news";
35pregen_atom("$root/archive/archive.xml", "$root/feed.atom", "$root/include/pregen-news.inc");
36include "rss_parser";
37legacy_rss("$root/feed.atom", "$root/news.rss", "$root/conferences/news.rss");
38
39// Pregenerate conference teaser
40include "conference_teaser";
41pregenerate_conf_teaser("$root/feed.atom", "$root/include/pregen-confs.inc");
42
43// Pregenerate elephpant image pool from flickr.
44include "pregen_flickr";
45$flickr_api_key = getenv("TOKEN_FLICKR");
46if (!$flickr_api_key && file_exists(".token_flickr")) $flickr_api_key = file_get_contents(".token_flickr");
47if (!$flickr_api_key) die("you have to set the TOKEN_FLICKR environment variable or create a .token_flickr file");
48pregen_flickr(
49    $flickr_api_key,
50    $root . '/images/elephpants',
51    100
52);
53
54include 'usergroups_update_infos';
55usergroups_update_infos($root);
56
57// Fetch data into a temporary file first, and then
58// overwrite the real file with the new data
59function fetch_into_file($url, $file)
60{
61    $SSL_fopen = false;
62    if(in_array('https', stream_get_wrappers())) {
63        $SSL_fopen = true;
64    }
65
66    // Open URL for reading
67    if($SSL_fopen) {
68        $source = @fopen($url, "r");
69        if (!$source) {
70            return;
71        }
72    } else {
73        $source = popen("curl -s '$url'", 'r');
74    }
75
76    // Open temporary file for writing
77    $dest = @fopen("$file~", "w");
78    if (!$dest) {
79        echo "failed to open '$file~' for writing\n";
80        return;
81    }
82
83    // Read until $source provides data, and write
84    // out the chunk to the output file if possible
85    while (!feof($source)) {
86        $chunk = fread($source, 4096);
87        if (fwrite($dest, $chunk) < 0) {
88            fclose($source);
89            fclose($dest);
90            unlink("$file~");
91            echo "failed writing to '$file~'\n";
92            return;
93        }
94    }
95    fclose($source);
96    fclose($dest);
97
98    // If we don't have new data, delete file
99    if (!@filesize("$file~")) {
100        echo "'$file~' was empty, skipping\n";
101        unlink("$file~");
102        return;
103    }
104
105    // Replace real file with temporary file
106    return rename("$file~", $file);
107}
108