public function action_index() { if (!file_exists(DOCROOT . $this->sitemap_directory_base)) { throw new HTTP_Exception_404(); } $this->site_code = ORM::factory('site', $this->request->site_id)->code; $this->sitemap_directory = $this->sitemap_directory_base . DIRECTORY_SEPARATOR . $this->site_code; $this->response->headers('Content-Type', 'text/xml')->headers('cache-control', 'max-age=0, must-revalidate, public')->headers('expires', gmdate('D, d M Y H:i:s', time()) . ' GMT'); try { $dir = new DirectoryIterator(DOCROOT . $this->sitemap_directory); $xml = new DOMDocument('1.0', Kohana::$charset); $xml->formatOutput = TRUE; $root = $xml->createElement('sitemapindex'); $root->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); $xml->appendChild($root); foreach ($dir as $fileinfo) { if ($fileinfo->isDot() or $fileinfo->isDir()) { continue; } $_file_path = str_replace(DOCROOT, '', $fileinfo->getPathName()); $_file_url = $this->domain . '/' . str_replace(DIRECTORY_SEPARATOR, '/', $_file_path); $sitemap = $xml->createElement('sitemap'); $root->appendChild($sitemap); $sitemap->appendChild(new DOMElement('loc', $_file_url)); $_last_mod = Sitemap::date_format($fileinfo->getCTime()); $sitemap->appendChild(new DOMElement('lastmod', $_last_mod)); } } catch (Exception $e) { echo Debug::vars($e->getMessage()); die; } echo $xml->saveXML(); }
/** * @test * @group sitemap * @dataProvider provider_date_format */ public function test_date_format($date, $expected) { if (!$expected) { try { Sitemap::date_format($date); } catch (InvalidArgumentException $e) { return; } $this->fail('The InvalidArgumentException was not raised'); } $return = Sitemap::date_format($date); $this->assertSame($expected, $return); }
/** * The date of last modification of the file. * @param integer $lastmod Unix timestamp */ public function set_last_mod($lastmod) { $this->attributes['lastmod'] = Sitemap::date_format($lastmod); return $this; }
/** * @test * @group sitemap * @dataProvider provider_create * @covers Sitemap_News::create * @param <type> $pub * @param <type> $lang * @param <type> $access * @param <type> $genre * @param <type> $date * @param <type> $title * @param <type> $tags */ public function test_create($pub, $lang, $access, $genre, $date, $title, $tags) { $instance = new Sitemap_News(); $instance->set_publication($pub)->set_lang($lang)->set_access($access)->set_genres($genre)->set_publication_date($date)->set_title($title)->set_keywords($tags); $return = $instance->create(); $xml = simplexml_import_dom($return); $this->assertEquals($pub, (string) $xml->{'news:publication'}->{'news:name'}); $this->assertEquals($lang, (string) $xml->{'news:publication'}->{'news:lang'}); $this->assertEquals($access, (string) $xml->{'news:access'}); $this->assertEquals(implode(',', $genre), (string) $xml->{'news:genres'}); $this->assertEquals(Sitemap::date_format($date), (string) $xml->{'news:publication_date'}); $this->assertEquals($title, (string) $xml->{'news:title'}); $this->assertEquals(implode(',', $tags), (string) $xml->{'news:keywords'}); }
/** * @test * @group sitemap * @dataProvider provider_create * @param string $location * @param integer $lastmod * @param string $change_frequency * @param integer|float $priority */ public function test_create($location, $lastmod, $change_frequency, $priority) { $instance = new Sitemap_URL(); $instance->set_loc($location)->set_last_mod($lastmod)->set_change_frequency($change_frequency)->set_priority($priority); $return = $instance->create(); // This solution allows me to see failure results displayed in the // CLI runner. Using assertTag or assertSelectEquals only gives me a boolean // value back and makes it very hard to track down errors. $xml = simplexml_import_dom($return); $this->assertEquals(Sitemap::encode($location), (string) $xml->loc); $this->assertEquals(Sitemap::date_format($lastmod), (string) $xml->lastmod); $this->assertEquals($change_frequency, (string) $xml->changefreq); $this->assertEquals($priority, (string) $xml->priority); }
/** * @param integer $date Article publication date in unixtimestamp format */ public function set_publication_date($date) { $this->_attributes['publication_date'] = Sitemap::date_format($date); return $this; }