Mercurial > SimpleWebPresenter
view Sitemap.inc @ 96:c7de7a4641d7
Generate humanreadable urls for sitemap.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 18 Oct 2012 21:48:58 +0200 |
| parents | 8a9bfbe220ca |
| children | f2d52fed708c |
line wrap: on
line source
<? include_once 'ScriptIncludeCache.inc'; /// @cond $baseDir = dirname(__FILE__); $cache = ScriptIncludeCache::instance(__FILE__); $cache->includeOnce('Http.inc', $baseDir); $cache->includeOnce('Page.inc', $baseDir); $cache->includeOnce('common-functions.inc', $baseDir); /// @endcond /** * Functionality for generating a sitemap */ class Sitemap extends Page { private $master; private $options; /** * Constructs a sitemap object from a master document * * @param $path location of master document */ function __construct($path) { $this->master = new DOMDocument(); $this->master->load($path); $this->options = new Options($this->master); } function cacheCheck() { return false; } function mayValidate() { return false; } function generateContent() { /// The final output variable $out = '<?xml version="1.0" encoding="UTF-8"?>'; $out .= "\n"; $out .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> '; $base = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; $l = strrpos($base, '/'); if ($l) $base = substr($base, 0, $l); $acceptedLanguages = $this->options->getAcceptedLanguages(); foreach($this->options->getAcceptedLanguages() as $lang) { if ($handle = opendir(basePath() . "/${lang}")) { while (false !== ($entry = readdir($handle))) { if (endsWith($entry, '.xml')) { $fentry = basepath() . "/${lang}/${entry}"; $doc = new DOMDocument(); if (file_exists($fentry)) { $doc->load($fentry); $opts = array(); if (count($acceptedLanguages) > 1) { $opts['lang'] = $lang; } $toplevel = $doc->getElementsByTagName("toplevel"); if($toplevel->length) { $name = substr($entry, 0, -4); if ($name != $this->options->getInputDefault('name')) { $opts['name'] = $name; } $optstring = genUrl($opts, array(), array('lang', 'name')); $location = "${base}${optstring}/"; $headers = Http::getHeaders($location, 5); $location = htmlentities($location); $lastmod = $headers["Last-Modified"]; $n = StatusCodes::codeFromHeader($headers['']); if ($n == StatusCodes::HTTP_OK) { $lastmod = strtotime($lastmod); $lastmod = date(DateTime::W3C, $lastmod); $out .= "<url>\n"; $out .= "<loc>${location}</loc>\n"; $out .= "<lastmod>${lastmod}</lastmod>\n"; $out .= "</url>\n"; } } } } } closedir($handle); } } $out .= '</urlset>'; $res = new PageContent($out); $res->setHeader('Content-type', 'application/xml'); return $res; } } ?>
