Mercurial > SimpleWebPresenter
comparison sitemap.php @ 30:647b72603b7d
Script to autogenerate a sitemap.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Sun, 30 Sep 2012 03:39:32 +0200 |
| parents | |
| children | f37be50a70cf |
comparison
equal
deleted
inserted
replaced
| 29:394b5df43d1a | 30:647b72603b7d |
|---|---|
| 1 <? | |
| 2 header('Content-type: application/xml'); | |
| 3 print '<?xml version="1.0" encoding="UTF-8"?>'; | |
| 4 print "\n"; | |
| 5 ?> | |
| 6 <urlset | |
| 7 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" | |
| 8 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 9 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 | |
| 10 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> | |
| 11 <? | |
| 12 include_once('php/Options.inc'); | |
| 13 | |
| 14 function startswith($haystack, $needle) { | |
| 15 return strpos($haystack, $needle) === 0; | |
| 16 } | |
| 17 | |
| 18 function endsWith($haystack, $needle) { | |
| 19 $l = strlen($haystack) - strlen($needle); | |
| 20 return strrpos($haystack, $needle) === $l; | |
| 21 } | |
| 22 | |
| 23 function getHeaders($url) | |
| 24 { | |
| 25 $response = http_head($url, array("timeout"=>1), $info); | |
| 26 $headers = array(); | |
| 27 $str = explode("\n", $response); | |
| 28 foreach($str as $kv) { | |
| 29 $p = strpos($kv, ":"); | |
| 30 if ($p) { | |
| 31 $key = substr($kv, 0, $p); | |
| 32 $value = trim(substr($kv, $p + 1)); | |
| 33 $headers[$key] = $value; | |
| 34 } | |
| 35 } | |
| 36 return $headers; | |
| 37 } | |
| 38 | |
| 39 function opttostring($opts) | |
| 40 { | |
| 41 $str = ''; | |
| 42 foreach (array_keys($opts) as $key) { | |
| 43 $value = $opts[$key]; | |
| 44 if ($str) { | |
| 45 $str .= "&${key}=${value}"; | |
| 46 } | |
| 47 else { | |
| 48 $str = "?${key}=${value}"; | |
| 49 } | |
| 50 } | |
| 51 return $str; | |
| 52 } | |
| 53 | |
| 54 | |
| 55 $master = new DOMDocument(); | |
| 56 $master->load("master.xml"); | |
| 57 | |
| 58 $options = new Options($master); | |
| 59 | |
| 60 $base = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; | |
| 61 $base = substr($base, 0, strrpos($base, '/')); | |
| 62 | |
| 63 $acceptedLanguages = $options->getAcceptedLanguages(); | |
| 64 | |
| 65 foreach($options->getAcceptedLanguages() as $lang) { | |
| 66 if ($handle = opendir("${lang}")) { | |
| 67 while (false !== ($entry = readdir($handle))) { | |
| 68 if (endsWith($entry, '.xml')) { | |
| 69 $fentry = "${lang}/${entry}"; | |
| 70 $doc = new DOMDocument(); | |
| 71 | |
| 72 if (file_exists($fentry)) { | |
| 73 $doc->load($fentry); | |
| 74 | |
| 75 $opts = array(); | |
| 76 if ($acceptedLanguages->length > 1) { | |
| 77 $opts['lang'] = $lang; | |
| 78 } | |
| 79 | |
| 80 $toplevel = $doc->getElementsByTagName("toplevel"); | |
| 81 | |
| 82 if($toplevel->length) { | |
| 83 $name = substr($entry, 0, -4); | |
| 84 | |
| 85 if ($name != $options->getInputDefault('name')) { | |
| 86 $opts['name'] = $name; | |
| 87 } | |
| 88 | |
| 89 $optstring = opttostring($opts); | |
| 90 | |
| 91 $location = "${base}/${optstring}"; | |
| 92 $headers = getHeaders($location); | |
| 93 | |
| 94 $location = htmlentities($location); | |
| 95 | |
| 96 $lastmod = $headers["Last-Modified"]; | |
| 97 $lastmod = strtotime($lastmod); | |
| 98 $lastmod = date(DateTime::W3C, $lastmod); | |
| 99 | |
| 100 print "<url>\n"; | |
| 101 print "<loc>${location}</loc>\n"; | |
| 102 print "<lastmod>${lastmod}</lastmod>\n"; | |
| 103 print "</url>\n"; | |
| 104 } | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 closedir($handle); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 ?> | |
| 113 </urlset> |
