Mercurial > SimpleWebPresenter
comparison Sitemap.inc @ 73:947d53f95ccd
Refactor Sitemap into a separate class.
Catch all exceptions in index.php and send a 500 error if nothing else
catches it.
Check response status before submitting to sitemap.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Fri, 12 Oct 2012 01:06:32 +0200 |
| parents | |
| children | 1d5166aba2c5 |
comparison
equal
deleted
inserted
replaced
| 72:7bc88fe62101 | 73:947d53f95ccd |
|---|---|
| 1 <? | |
| 2 include_once 'ScriptIncludeCache.inc'; | |
| 3 | |
| 4 /// @cond | |
| 5 $baseDir = dirname(__FILE__); | |
| 6 $cache = ScriptIncludeCache::instance(__FILE__); | |
| 7 $cache->includeOnce('common-functions.inc', $baseDir); | |
| 8 $cache->includeOnce('Options.inc', dirname(__FILE__)); | |
| 9 /// @endcond | |
| 10 | |
| 11 class Sitemap | |
| 12 { | |
| 13 private $master; | |
| 14 private $options; | |
| 15 | |
| 16 function __construct($path) { | |
| 17 $this->master = new DOMDocument(); | |
| 18 $this->master->load($path); | |
| 19 | |
| 20 $this->options = new Options($this->master); | |
| 21 } | |
| 22 | |
| 23 function genPage() { | |
| 24 /// The final output variable | |
| 25 $out = '<?xml version="1.0" encoding="UTF-8"?>'; | |
| 26 $out .= "\n"; | |
| 27 $out .= '<urlset | |
| 28 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" | |
| 29 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 30 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 | |
| 31 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> | |
| 32 '; | |
| 33 | |
| 34 $base = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; | |
| 35 $base = substr($base, 0, strrpos($base, '/')); | |
| 36 | |
| 37 $acceptedLanguages = $this->options->getAcceptedLanguages(); | |
| 38 | |
| 39 foreach($this->options->getAcceptedLanguages() as $lang) { | |
| 40 if ($handle = opendir("${lang}")) { | |
| 41 while (false !== ($entry = readdir($handle))) { | |
| 42 if (endsWith($entry, '.xml')) { | |
| 43 $fentry = "${lang}/${entry}"; | |
| 44 $doc = new DOMDocument(); | |
| 45 | |
| 46 if (file_exists($fentry)) { | |
| 47 $doc->load($fentry); | |
| 48 | |
| 49 $opts = array(); | |
| 50 if (count($acceptedLanguages) > 1) { | |
| 51 $opts['lang'] = $lang; | |
| 52 } | |
| 53 | |
| 54 $toplevel = $doc->getElementsByTagName("toplevel"); | |
| 55 | |
| 56 if($toplevel->length) { | |
| 57 $name = substr($entry, 0, -4); | |
| 58 | |
| 59 if ($name != $this->options->getInputDefault('name')) { | |
| 60 $opts['name'] = $name; | |
| 61 } | |
| 62 | |
| 63 $optstring = opttostring($opts); | |
| 64 | |
| 65 $location = "${base}/${optstring}"; | |
| 66 $headers = getHeaders($location); | |
| 67 | |
| 68 $location = htmlentities($location); | |
| 69 | |
| 70 $lastmod = $headers["Last-Modified"]; | |
| 71 | |
| 72 $n = StatusCodes::codeFromHeader($headers['']); | |
| 73 | |
| 74 if ($n == StatusCodes::HTTP_OK) { | |
| 75 $lastmod = strtotime($lastmod); | |
| 76 $lastmod = date(DateTime::W3C, $lastmod); | |
| 77 | |
| 78 $out .= "<url>\n"; | |
| 79 $out .= "<loc>${location}</loc>\n"; | |
| 80 $out .= "<lastmod>${lastmod}</lastmod>\n"; | |
| 81 $out .= "</url>\n"; | |
| 82 } | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 } | |
| 87 closedir($handle); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 $out .= '</urlset>'; | |
| 92 | |
| 93 header('Content-type: application/xml'); | |
| 94 print $out; | |
| 95 } | |
| 96 } | |
| 97 ?> |
