Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 27 additions & 35 deletions Sitemap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Sitemap
*
Expand All @@ -12,7 +11,6 @@
* @link http://github.com/o/sitemap-php
*/
class Sitemap {

/**
*
* @var XMLWriter
Expand All @@ -23,22 +21,22 @@ class Sitemap {
private $filename = 'sitemap';
private $current_item = 0;
private $current_sitemap = 0;

private $use_domain_url=false;
const EXT = '.xml';
const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9';
const IMAGE = 'http://www.google.com/schemas/sitemap-image/1.1';
const DEFAULT_PRIORITY = 0.5;
const ITEM_PER_SITEMAP = 50000;
const SEPERATOR = '-';
const INDEX_SUFFIX = 'index';

/**
*
* @param string $domain
*/
public function __construct($domain) {
public function __construct($domain,$use_domain_url=false) {
$this->use_domain_url = $use_domain_url;
$this->setDomain($domain);
}

/**
* Sets root path of the website, starting with http:// or https://
*
Expand All @@ -48,7 +46,6 @@ public function setDomain($domain) {
$this->domain = $domain;
return $this;
}

/**
* Returns root path of the website
*
Expand All @@ -57,7 +54,6 @@ public function setDomain($domain) {
private function getDomain() {
return $this->domain;
}

/**
* Returns XMLWriter object instance
*
Expand All @@ -66,56 +62,50 @@ private function getDomain() {
private function getWriter() {
return $this->writer;
}

/**
* Assigns XMLWriter object instance
*
* @param XMLWriter $writer
* @param XMLWriter $writer
*/
private function setWriter(XMLWriter $writer) {
$this->writer = $writer;
}

/**
* Returns path of sitemaps
*
*
* @return string
*/
private function getPath() {
return $this->path;
}

/**
* Sets paths of sitemaps
*
*
* @param string $path
* @return Sitemap
*/
public function setPath($path) {
$this->path = $path;
return $this;
}

/**
* Returns filename of sitemap file
*
*
* @return string
*/
private function getFilename() {
return $this->filename;
}

/**
* Sets filename of sitemap file
*
*
* @param string $filename
* @return Sitemap
*/
public function setFilename($filename) {
$this->filename = $filename;
return $this;
}

/**
* Returns current item count
*
Expand All @@ -124,15 +114,13 @@ public function setFilename($filename) {
private function getCurrentItem() {
return $this->current_item;
}

/**
* Increases item counter
*
*
*/
private function incCurrentItem() {
$this->current_item = $this->current_item + 1;
}

/**
* Returns current sitemap file count
*
Expand All @@ -141,18 +129,16 @@ private function incCurrentItem() {
private function getCurrentSitemap() {
return $this->current_sitemap;
}

/**
* Increases sitemap file count
*
*
*/
private function incCurrentSitemap() {
$this->current_sitemap = $this->current_sitemap + 1;
}

/**
* Prepares sitemap XML document
*
*
*/
private function startSitemap() {
$this->setWriter(new XMLWriter());
Expand All @@ -165,18 +151,18 @@ private function startSitemap() {
$this->getWriter()->setIndent(true);
$this->getWriter()->startElement('urlset');
$this->getWriter()->writeAttribute('xmlns', self::SCHEMA);
$this->getWriter()->writeAttribute('xmlns:image', self::IMAGE);
}

/**
* Adds an item to sitemap
*
* @param string $loc URL of the page. This value must be less than 2,048 characters.
* @param string $loc URL of the page. This value must be less than 2,048 characters.
* @param string $priority The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0.
* @param string $changefreq How frequently the page is likely to change. Valid values are always, hourly, daily, weekly, monthly, yearly and never.
* @param string|int $lastmod The date of last modification of url. Unix timestamp or any English textual datetime description.
* @return Sitemap
*/
public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL) {
public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL,$image=false) {
if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) {
if ($this->getWriter() instanceof XMLWriter) {
$this->endSitemap();
Expand All @@ -186,16 +172,25 @@ public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq =
}
$this->incCurrentItem();
$this->getWriter()->startElement('url');
$this->getWriter()->writeElement('loc', $this->getDomain() . $loc);
$this->getWriter()->writeElement('loc', ($this->use_domain_url?$this->getDomain() . $loc:$loc));
$this->getWriter()->writeElement('priority', $priority);
if($image)
{
$this->getWriter()->startElement('image:image');
$this->getWriter()->writeElement('image:loc',$image['loc']);
$this->getWriter()->writeElement('image:title',$image['title']);
$this->getWriter()->writeElement('image:caption','');
$this->getWriter()->endElement();
}


if ($changefreq)
$this->getWriter()->writeElement('changefreq', $changefreq);
if ($lastmod)
$this->getWriter()->writeElement('lastmod', $this->getLastModifiedDate($lastmod));
$this->getWriter()->endElement();
return $this;
}

/**
* Prepares given date for sitemap
*
Expand All @@ -210,7 +205,6 @@ private function getLastModifiedDate($date) {
return date('Y-m-d', $date);
}
}

/**
* Finalizes tags of sitemap XML document.
*
Expand All @@ -222,7 +216,6 @@ private function endSitemap() {
$this->getWriter()->endElement();
$this->getWriter()->endDocument();
}

/**
* Writes Google sitemap index for generated sitemap files
*
Expand All @@ -246,5 +239,4 @@ public function createSitemapIndex($loc, $lastmod = 'Today') {
$indexwriter->endElement();
$indexwriter->endDocument();
}

}
}