Mercurial > SimpleWebPresenter
comparison Sitemap.inc @ 98:f2d52fed708c
Split retrieval and production of urls.
Support for stylesheets in sitemap.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Fri, 19 Oct 2012 01:36:22 +0200 |
| parents | c7de7a4641d7 |
| children | d98e315308cd |
comparison
equal
deleted
inserted
replaced
| 97:ba11b1c1b881 | 98:f2d52fed708c |
|---|---|
| 6 $cache = ScriptIncludeCache::instance(__FILE__); | 6 $cache = ScriptIncludeCache::instance(__FILE__); |
| 7 $cache->includeOnce('Http.inc', $baseDir); | 7 $cache->includeOnce('Http.inc', $baseDir); |
| 8 $cache->includeOnce('Page.inc', $baseDir); | 8 $cache->includeOnce('Page.inc', $baseDir); |
| 9 $cache->includeOnce('common-functions.inc', $baseDir); | 9 $cache->includeOnce('common-functions.inc', $baseDir); |
| 10 /// @endcond | 10 /// @endcond |
| 11 | |
| 12 function cmp_length_lex($a, $b) | |
| 13 { | |
| 14 if ($a == $b) { | |
| 15 return 0; | |
| 16 } | |
| 17 $la = strlen($a); | |
| 18 $lb = strlen($b); | |
| 19 if ($la == $lb) { | |
| 20 return ($la < $lb) ? -1 : 1; | |
| 21 } | |
| 22 return ($a < $b) ? -1 : 1; | |
| 23 } | |
| 24 | |
| 11 | 25 |
| 12 /** | 26 /** |
| 13 * Functionality for generating a sitemap | 27 * Functionality for generating a sitemap |
| 14 */ | 28 */ |
| 15 class Sitemap extends Page | 29 class Sitemap extends Page |
| 41 | 55 |
| 42 function generateContent() { | 56 function generateContent() { |
| 43 /// The final output variable | 57 /// The final output variable |
| 44 $out = '<?xml version="1.0" encoding="UTF-8"?>'; | 58 $out = '<?xml version="1.0" encoding="UTF-8"?>'; |
| 45 $out .= "\n"; | 59 $out .= "\n"; |
| 60 $out .= '<?xml-stylesheet type="text/xsl" href="/css/gss.xsl"?>'; | |
| 61 $out .= "\n"; | |
| 46 $out .= '<urlset | 62 $out .= '<urlset |
| 47 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" | 63 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" |
| 48 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | 64 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 49 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 | 65 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 |
| 50 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> | 66 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> |
| 54 $l = strrpos($base, '/'); | 70 $l = strrpos($base, '/'); |
| 55 if ($l) | 71 if ($l) |
| 56 $base = substr($base, 0, $l); | 72 $base = substr($base, 0, $l); |
| 57 | 73 |
| 58 $acceptedLanguages = $this->options->getAcceptedLanguages(); | 74 $acceptedLanguages = $this->options->getAcceptedLanguages(); |
| 75 | |
| 76 $urls = array(); | |
| 59 | 77 |
| 60 foreach($this->options->getAcceptedLanguages() as $lang) { | 78 foreach($this->options->getAcceptedLanguages() as $lang) { |
| 61 if ($handle = opendir(basePath() . "/${lang}")) { | 79 if ($handle = opendir(basePath() . "/${lang}")) { |
| 62 while (false !== ($entry = readdir($handle))) { | 80 while (false !== ($entry = readdir($handle))) { |
| 63 if (endsWith($entry, '.xml')) { | 81 if (endsWith($entry, '.xml')) { |
| 83 | 101 |
| 84 $optstring = genUrl($opts, array(), array('lang', 'name')); | 102 $optstring = genUrl($opts, array(), array('lang', 'name')); |
| 85 | 103 |
| 86 $location = "${base}${optstring}/"; | 104 $location = "${base}${optstring}/"; |
| 87 | 105 |
| 88 $headers = Http::getHeaders($location, 5); | 106 array_push($urls, $location); |
| 89 | |
| 90 $location = htmlentities($location); | |
| 91 | |
| 92 $lastmod = $headers["Last-Modified"]; | |
| 93 | |
| 94 $n = StatusCodes::codeFromHeader($headers['']); | |
| 95 | |
| 96 if ($n == StatusCodes::HTTP_OK) { | |
| 97 $lastmod = strtotime($lastmod); | |
| 98 $lastmod = date(DateTime::W3C, $lastmod); | |
| 99 | |
| 100 $out .= "<url>\n"; | |
| 101 $out .= "<loc>${location}</loc>\n"; | |
| 102 $out .= "<lastmod>${lastmod}</lastmod>\n"; | |
| 103 $out .= "</url>\n"; | |
| 104 } | |
| 105 } | 107 } |
| 106 } | 108 } |
| 107 } | 109 } |
| 108 } | 110 } |
| 109 closedir($handle); | 111 closedir($handle); |
| 112 } | |
| 113 } | |
| 114 | |
| 115 usort($urls, cmp_length_lex); | |
| 116 | |
| 117 foreach($urls as $location) { | |
| 118 $headers = Http::getHeaders($location, 5); | |
| 119 | |
| 120 $location = htmlentities($location); | |
| 121 | |
| 122 $lastmod = $headers["Last-Modified"]; | |
| 123 | |
| 124 $n = StatusCodes::codeFromHeader($headers['']); | |
| 125 | |
| 126 if ($n == StatusCodes::HTTP_OK) { | |
| 127 $lastmod = strtotime($lastmod); | |
| 128 $lastmod = date(DateTime::W3C, $lastmod); | |
| 129 | |
| 130 $out .= "<url>\n"; | |
| 131 $out .= "<loc>${location}</loc>\n"; | |
| 132 $out .= "<lastmod>${lastmod}</lastmod>\n"; | |
| 133 $out .= "</url>\n"; | |
| 110 } | 134 } |
| 111 } | 135 } |
| 112 | 136 |
| 113 $out .= '</urlset>'; | 137 $out .= '</urlset>'; |
| 114 | 138 |
