]> git.openstreetmap.org Git - chef.git/blob - cookbooks/planet/templates/default/planet-mirror-redirect-update.erb
<item>s must be inside <channel> for RSS
[chef.git] / cookbooks / planet / templates / default / planet-mirror-redirect-update.erb
1 #!/usr/bin/php
2 <?php
3 /*
4         DO NOT EDIT - This file is being maintained by Chef
5
6         planet-mirror-redirect
7         Check if planet file exists on mirror and link if not yet linked
8         Modifies .htaccess
9 */
10 $_YEAR = date('Y');
11
12 $_MIRROR = 'https://ftp5.gwdg.de/pub/misc/openstreetmap/planet.openstreetmap.org/';
13
14 $_FOLDERS = [
15               "planet_bz2" => [
16                           'FOLDER'     => '<%= node[:planet][:dump][:xml_directory] %>/'.$_YEAR.'/',
17                           'REGEX'      => '/^(planet|changesets|discussions)\-\d{6}(\-nolt)?\.osm\.(bz2|gz)$/',
18                           'DIR_PREFIX' => 'planet/'.$_YEAR.'/'
19                           ],
20
21               "history_bz2" => [
22                           'FOLDER' => '<%= node[:planet][:dump][:xml_history_directory] %>/'.$_YEAR.'/',
23                           'REGEX'  => '/^(history)\-\d{6}(\-nolt)?\.osm\.(bz2|gz)$/',
24                           'DIR_PREFIX' => 'planet/full-history/'.$_YEAR.'/'
25                           ],
26               "planet_pbf" => [
27                           'FOLDER'     => '<%= node[:planet][:dump][:pbf_directory] %>/',
28                           'REGEX'      => '/^(planet)\-\d{6}(\-nolt)?\.osm\.pbf$/',
29                           'DIR_PREFIX' => 'pbf/'
30                           ],
31
32               "history_pbf" => [
33                           'FOLDER' => '<%= node[:planet][:dump][:pbf_history_directory] %>/',
34                           'REGEX'  => '/^(history)\-\d{6}(\-nolt)?\.osm\.pbf$/',
35                           'DIR_PREFIX' => 'pbf/full-history/'
36                           ]
37             ];
38
39 $_PLANET_HTACCESS = realpath('<%= node[:planet][:dump][:xml_directory] %>/..').'/.htaccess';
40
41 function _MIRROR_FILE_SIZE($url) {
42         $ch = @curl_init();
43         curl_setopt($ch, CURLOPT_URL, $url);
44         curl_setopt($ch, CURLOPT_NOBODY, TRUE);
45         curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
46         $curl_response = @curl_exec($ch);
47         $curl_result = curl_getinfo($ch);
48         if ($curl_result['http_code']!='200') return FALSE;
49         return ($curl_result['download_content_length']);
50 }
51
52 if (!is_writable($_PLANET_HTACCESS)) die('File '.$_PLANET_HTACCESS.' is not writable by current user.'."\n");
53
54 $htaccess_contents = file_get_contents($_PLANET_HTACCESS);
55 $htaccess_handle = fopen($_PLANET_HTACCESS, 'a');
56
57 foreach ($_FOLDERS as $FOLDER) {
58
59   $_PLANET_FOLDER = $FOLDER["FOLDER"];
60   $_PLANET_REGEX  = $FOLDER["REGEX"];
61
62   if (!(is_dir($_PLANET_FOLDER))) {
63     continue;
64   }
65
66   if ($dh = opendir($_PLANET_FOLDER)) {
67     while (($file = readdir($dh)) !== false ) {
68       if (preg_match($_PLANET_REGEX, $file)) {
69         $file_slashed = str_replace(array('.','-'), array('\.','\-'), $FOLDER['DIR_PREFIX'].$file);
70         if (strpos($htaccess_contents, $file_slashed) === false) {
71           $file_size = filesize($_PLANET_FOLDER.$file);
72           sleep(rand(2,5));
73           $file_mirror_size = _MIRROR_FILE_SIZE($_MIRROR.$FOLDER['DIR_PREFIX'].$file);
74           if ($file_size==$file_mirror_size) {
75             echo 'Adding: '.$file."\n";
76             fwrite($htaccess_handle, 'RewriteRule'."\t". '^('.$file_slashed.')$'."\t".$_MIRROR.'$1'."\t".'[R,L]'."\n");
77           }
78         }
79       }
80     }
81     closedir($dh);
82   }
83 }
84
85 fclose($htaccess_handle);
86