]> git.openstreetmap.org Git - chef.git/blob - cookbooks/planet/templates/default/planet-mirror-redirect-update.erb
Use rsync preallocate files to avoid fragmentation
[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 $_PLANET_FOLDER = '<%= node[:planet][:dump][:xml_directory] %>/'.$_YEAR.'/';
12 $_PLANET_REGEX = "/^(planet|changesets)\-\d{6}(\-nolt)?\.osm\.(bz2|gz)$/";
13 $_MIRROR = 'http://ftp.heanet.ie/mirrors/openstreetmap.org/planet/'.$_YEAR.'/';
14 $_PLANET_HTACCESS = realpath('<%= node[:planet][:dump][:xml_directory] %>/..').'/.htaccess';
15
16 function _MIRROR_FILE_SIZE($url) {
17         $ch = @curl_init();
18         curl_setopt($ch, CURLOPT_URL, $url);
19         curl_setopt($ch, CURLOPT_NOBODY, TRUE);
20         curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
21         $curl_response = @curl_exec($ch);
22         $curl_result = curl_getinfo($ch);
23         if ($curl_result['http_code']!='200') return FALSE;
24         return ($curl_result['download_content_length']);
25 }                       
26
27 if (!is_writable($_PLANET_HTACCESS)) die('File '.$_PLANET_HTACCESS.' is not writable by current user.'."\n");
28
29 if (is_dir($_PLANET_FOLDER)) {
30         $htaccess_contents = file_get_contents($_PLANET_HTACCESS);
31         $htaccess_handle = fopen($_PLANET_HTACCESS, 'a');
32         if ($dh = opendir($_PLANET_FOLDER)) {
33                 while (($file = readdir($dh)) !== false ) {
34                         if (preg_match($_PLANET_REGEX,$file)) {
35                                 $file_slashed = 'planet/'.$_YEAR.'/'.str_replace(array('.','-'), array('\.','\-'), $file);
36                                 if (strpos($htaccess_contents,$file_slashed) === false) {
37                                         $file_size = filesize($_PLANET_FOLDER.$file);
38                                         sleep(rand(2,5));
39                                         $file_mirror_size = _MIRROR_FILE_SIZE($_MIRROR.$file);
40                                         if ($file_size==$file_mirror_size) {
41                                                         echo 'Adding: '.$file."\n";
42                                                         fwrite($htaccess_handle,        'RewriteRule'."\t".
43                                                                                         '^('.$file_slashed.')$'."\t".
44                                                                                         'http://ftp.heanet.ie/mirrors/openstreetmap.org/$1'."\t".
45                                                                                         '[R,L]'."\n");
46                                         }
47                                 }
48                         }
49                 }
50                 closedir($dh);
51                 fclose($htaccess_handle);
52         }
53 }