comparison sitemap.php @ 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 dd4ddedca4c5
children 1d5166aba2c5
comparison
equal deleted inserted replaced
72:7bc88fe62101 73:947d53f95ccd
1 <? 1 <?
2 /** 2 /**
3 * @file 3 * @file
4 * Generates a sitemap 4 * Generates a sitemap
5 * @todo reuse functionality from InputParser
6 */ 5 */
7 6
8 /// The final output variable
9 $out = '<?xml version="1.0" encoding="UTF-8"?>';
10 $out .= "\n";
11 $out .= '<urlset
12 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
13 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
14 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
15 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
16 ';
17 include_once 'ScriptIncludeCache.inc'; 7 include_once 'ScriptIncludeCache.inc';
18 8
19 /// @cond 9 /// @cond
20 $baseDir = dirname(__FILE__); 10 $baseDir = dirname(__FILE__);
21 $cache = ScriptIncludeCache::instance(__FILE__); 11 $cache = ScriptIncludeCache::instance(__FILE__);
22 $cache->includeOnce('common-functions.inc', $baseDir); 12 $cache->includeOnce('Sitemap.inc', dirname(__FILE__));
23 $cache->includeOnce('Options.inc', dirname(__FILE__));
24 /// @endcond 13 /// @endcond
25 14
26 $master = new DOMDocument(); 15 $sitemap = new Sitemap("master.xml");
27 $master->load("master.xml"); 16 $sitemap->genPage();
28
29 $options = new Options($master);
30
31 $base = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
32 $base = substr($base, 0, strrpos($base, '/'));
33
34 $acceptedLanguages = $options->getAcceptedLanguages();
35
36 foreach($options->getAcceptedLanguages() as $lang) {
37 if ($handle = opendir("${lang}")) {
38 while (false !== ($entry = readdir($handle))) {
39 if (endsWith($entry, '.xml')) {
40 $fentry = "${lang}/${entry}";
41 $doc = new DOMDocument();
42
43 if (file_exists($fentry)) {
44 $doc->load($fentry);
45
46 $opts = array();
47 if (count($acceptedLanguages) > 1) {
48 $opts['lang'] = $lang;
49 }
50
51 $toplevel = $doc->getElementsByTagName("toplevel");
52
53 if($toplevel->length) {
54 $name = substr($entry, 0, -4);
55
56 if ($name != $options->getInputDefault('name')) {
57 $opts['name'] = $name;
58 }
59
60 $optstring = opttostring($opts);
61
62 $location = "${base}/${optstring}";
63 $headers = getHeaders($location);
64
65 $location = htmlentities($location);
66
67 $lastmod = $headers["Last-Modified"];
68 $lastmod = strtotime($lastmod);
69 $lastmod = date(DateTime::W3C, $lastmod);
70
71 $out .= "<url>\n";
72 $out .= "<loc>${location}</loc>\n";
73 $out .= "<lastmod>${lastmod}</lastmod>\n";
74 $out .= "</url>\n";
75 }
76 }
77 }
78 }
79 closedir($handle);
80 }
81 }
82
83 $out .= '</urlset>';
84
85 header('Content-type: application/xml');
86 print $out;
87 ?> 17 ?>