]> git.openstreetmap.org Git - chef.git/blob - cookbooks/planet/templates/default/planet-mirror-redirect-update.erb
Convert nginx_site to an LWRP
[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($_PLANET_FOLDER.'../..').'/.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                                         //PHP IS BRAINDEAD filesize borked >4GB
38                                         //$file_size = $file_stats['size'];
39                                         $file_size = trim(`stat -c%s $_PLANET_FOLDER$file`);
40                                         sleep(rand(2,5));
41                                         $file_mirror_size = _MIRROR_FILE_SIZE($_MIRROR.$file);
42                                         if ($file_size==$file_mirror_size) {
43                                                         echo 'Adding: '.$file."\n";
44                                                         fwrite($htaccess_handle,        'RewriteRule'."\t".
45                                                                                         '^('.$file_slashed.')$'."\t".
46                                                                                         'http://ftp.heanet.ie/mirrors/openstreetmap.org/$1'."\t".
47                                                                                         '[R,L]'."\n");
48                                         }
49                                 }
50                         }
51                 }
52                 closedir($dh);
53                 fclose($htaccess_handle);
54         }
55 }