From b0d671389258244315baeb990f02875a397156aa Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Wed, 25 Jan 2012 15:23:58 +0100 Subject: [PATCH 01/16] Using SitemapPHP namespace, and renamed to correct directory --- src/SitemapPHP/Sitemap.php | 247 +++++++++++++++++++++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 src/SitemapPHP/Sitemap.php diff --git a/src/SitemapPHP/Sitemap.php b/src/SitemapPHP/Sitemap.php new file mode 100644 index 0000000..b638fa3 --- /dev/null +++ b/src/SitemapPHP/Sitemap.php @@ -0,0 +1,247 @@ + + * @copyright 2009-2011 Osman Üngür + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @version Version @package_version@ + * @since Class available since Version 1.0.0 + * @link http://github.com/osmanungur/sitemap-php + */ +class Sitemap { + + /** + * + * @var XMLWriter + */ + private $writer; + private $domain; + private $path; + private $filename = 'sitemap'; + private $current_item = 0; + private $current_sitemap = 0; + + const EXT = '.xml'; + const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9'; + const DEFAULT_PRIORITY = 0.5; + const ITEM_PER_SITEMAP = 50000; + const SEPERATOR = '-'; + const INDEX_SUFFIX = 'index'; + + /** + * + * @param string $domain + */ + public function __construct($domain) { + $this->setDomain($domain); + } + + /** + * Sets root path of the website, starting with http:// or https:// + * + * @param string $domain + */ + public function setDomain($domain) { + $this->domain = $domain; + return $this; + } + + /** + * Returns root path of the website + * + * @return string + */ + private function getDomain() { + return $this->domain; + } + + /** + * Returns XMLWriter object instance + * + * @return XMLWriter + */ + private function getWriter() { + return $this->writer; + } + + /** + * Assigns XMLWriter object instance + * + * @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 + * + * @return int + */ + 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 + * + * @return int + */ + 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()); + $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT); + $this->getWriter()->startDocument('1.0', 'UTF-8'); + $this->getWriter()->setIndent(true); + $this->getWriter()->startElement('urlset'); + $this->getWriter()->writeAttribute('xmlns', self::SCHEMA); + } + + /** + * Adds an item to sitemap + * + * @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) { + if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) { + if ($this->getWriter() instanceof XMLWriter) { + $this->endSitemap(); + } + $this->startSitemap(); + $this->incCurrentSitemap(); + } + $this->incCurrentItem(); + $this->getWriter()->startElement('url'); + $this->getWriter()->writeElement('loc', $this->getDomain() . $loc); + $this->getWriter()->writeElement('priority', $priority); + 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 + * + * @param string $date Unix timestamp or any English textual datetime description + * @return string Year-Month-Day formatted date. + */ + private function getLastModifiedDate($date) { + if (ctype_digit($date)) { + return date('Y-m-d', $date); + } else { + $date = strtotime($date); + return date('Y-m-d', $date); + } + } + + /** + * Finalizes tags of sitemap XML document. + * + */ + private function endSitemap() { + $this->getWriter()->endElement(); + $this->getWriter()->endDocument(); + } + + /** + * Writes Google sitemap index for generated sitemap files + * + * @param string $loc Accessible URL path of sitemaps + * @param string|int $lastmod The date of last modification of sitemap. Unix timestamp or any English textual datetime description. + */ + public function createSitemapIndex($loc, $lastmod = 'Today') { + $this->endSitemap(); + $indexwriter = new XMLWriter(); + $indexwriter->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . self::INDEX_SUFFIX . self::EXT); + $indexwriter->startDocument('1.0', 'UTF-8'); + $indexwriter->setIndent(true); + $indexwriter->startElement('sitemapindex'); + $indexwriter->writeAttribute('xmlns', self::SCHEMA); + for ($index = 0; $index < $this->getCurrentSitemap(); $index++) { + $indexwriter->startElement('sitemap'); + $indexwriter->writeElement('loc', $loc . $this->getFilename() . self::SEPERATOR . $index . self::EXT); + $indexwriter->writeElement('lastmod', $this->getLastModifiedDate($lastmod)); + $indexwriter->endElement(); + } + $indexwriter->endElement(); + $indexwriter->endDocument(); + } + +} From 3426ad6d1a8cdc5bba98df2843cccc93da399154 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Wed, 25 Jan 2012 15:24:36 +0100 Subject: [PATCH 02/16] Added composer.phar document --- Sitemap.php | 245 -------------------------------------------------- composer.json | 24 +++++ 2 files changed, 24 insertions(+), 245 deletions(-) delete mode 100644 Sitemap.php create mode 100644 composer.json diff --git a/Sitemap.php b/Sitemap.php deleted file mode 100644 index e7ad9fd..0000000 --- a/Sitemap.php +++ /dev/null @@ -1,245 +0,0 @@ - - * @copyright 2009-2011 Osman Üngür - * @license http://www.opensource.org/licenses/bsd-license.php BSD License - * @version Version @package_version@ - * @since Class available since Version 1.0.0 - * @link http://github.com/osmanungur/sitemap-php - */ -class Sitemap { - - /** - * - * @var XMLWriter - */ - private $writer; - private $domain; - private $path; - private $filename = 'sitemap'; - private $current_item = 0; - private $current_sitemap = 0; - - const EXT = '.xml'; - const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9'; - const DEFAULT_PRIORITY = 0.5; - const ITEM_PER_SITEMAP = 50000; - const SEPERATOR = '-'; - const INDEX_SUFFIX = 'index'; - - /** - * - * @param string $domain - */ - public function __construct($domain) { - $this->setDomain($domain); - } - - /** - * Sets root path of the website, starting with http:// or https:// - * - * @param string $domain - */ - public function setDomain($domain) { - $this->domain = $domain; - return $this; - } - - /** - * Returns root path of the website - * - * @return string - */ - private function getDomain() { - return $this->domain; - } - - /** - * Returns XMLWriter object instance - * - * @return XMLWriter - */ - private function getWriter() { - return $this->writer; - } - - /** - * Assigns XMLWriter object instance - * - * @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 - * - * @return int - */ - 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 - * - * @return int - */ - 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()); - $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT); - $this->getWriter()->startDocument('1.0', 'UTF-8'); - $this->getWriter()->setIndent(true); - $this->getWriter()->startElement('urlset'); - $this->getWriter()->writeAttribute('xmlns', self::SCHEMA); - } - - /** - * Adds an item to sitemap - * - * @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) { - if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) { - if ($this->getWriter() instanceof XMLWriter) { - $this->endSitemap(); - } - $this->startSitemap(); - $this->incCurrentSitemap(); - } - $this->incCurrentItem(); - $this->getWriter()->startElement('url'); - $this->getWriter()->writeElement('loc', $this->getDomain() . $loc); - $this->getWriter()->writeElement('priority', $priority); - 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 - * - * @param string $date Unix timestamp or any English textual datetime description - * @return string Year-Month-Day formatted date. - */ - private function getLastModifiedDate($date) { - if (ctype_digit($date)) { - return date('Y-m-d', $date); - } else { - $date = strtotime($date); - return date('Y-m-d', $date); - } - } - - /** - * Finalizes tags of sitemap XML document. - * - */ - private function endSitemap() { - $this->getWriter()->endElement(); - $this->getWriter()->endDocument(); - } - - /** - * Writes Google sitemap index for generated sitemap files - * - * @param string $loc Accessible URL path of sitemaps - * @param string|int $lastmod The date of last modification of sitemap. Unix timestamp or any English textual datetime description. - */ - public function createSitemapIndex($loc, $lastmod = 'Today') { - $this->endSitemap(); - $indexwriter = new XMLWriter(); - $indexwriter->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . self::INDEX_SUFFIX . self::EXT); - $indexwriter->startDocument('1.0', 'UTF-8'); - $indexwriter->setIndent(true); - $indexwriter->startElement('sitemapindex'); - $indexwriter->writeAttribute('xmlns', self::SCHEMA); - for ($index = 0; $index < $this->getCurrentSitemap(); $index++) { - $indexwriter->startElement('sitemap'); - $indexwriter->writeElement('loc', $loc . $this->getFilename() . self::SEPERATOR . $index . self::EXT); - $indexwriter->writeElement('lastmod', $this->getLastModifiedDate($lastmod)); - $indexwriter->endElement(); - } - $indexwriter->endElement(); - $indexwriter->endDocument(); - } - -} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..624b0c8 --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "evert/sitemap-php", + "type": "library", + "description": "Lightweight library for generating Google sitemap XML files", + "keywords": ["Sitemap"], + "homepage": "http://code.google.com/p/sabredav/", + "license": "BSD License", + "authors": [ + { + "name": "Osman Üngür", + "email": "osmanungur@gmail.com" + }, + { + "name": "Evert Pot", + "email": "evert@rooftopsolutions.nl" + } + ], + "require": { + "php": ">=5.3.0" + }, + "autoload": { + "psr-0": { "SitemapPHP": "src/" } + } +} From 6eb578506641c7fd60bb5e329af5f1ad58415a53 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Wed, 25 Jan 2012 15:26:56 +0100 Subject: [PATCH 03/16] Updated readme --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 87c9aa4..08b4ca8 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,17 @@ What is sitemap-php ? Fast and lightweight class for generating Google sitemap XML files and index of sitemap files. Written on PHP and uses XMLWriter extension (wrapper for libxml xmlWriter API) for creating XML files. XMLWriter extension is enabled by default in PHP 5 >= 5.1.2. If you having more than 50000 url, it splits items to seperated files. _(In benchmarks, 1.000.000 url was generating in 8 seconds)_ +This is a slightly modified version of the original. The Sitemap class is now added to a 'SitemapPHP' namespace, and a composer document has been added. + How to use ---------- Include Sitemap.php file to your PHP document and call Sitemap class with your base domain. - include 'Sitemap.php'; + include 'src/SitemapPHP/Sitemap.php'; + + use SitemapPHP\Sitemap; + $sitemap = new Sitemap('http://example.com'); Now, we need to define path for saving XML files. This can be relative like `xmls` or absolute `/path/to/your/folder` and *must be a writable folder*. In default it uses same folder with your script. @@ -91,4 +96,4 @@ sitemap-index.xml -You need to submit sitemap-index.xml to Google Sitemaps. \ No newline at end of file +You need to submit sitemap-index.xml to Google Sitemaps. From 507d379cfbe6e78b9d952700c97d992907b092a0 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Wed, 25 Jan 2012 16:24:58 +0100 Subject: [PATCH 04/16] Fixing reference to XMLWriter (global object) --- src/SitemapPHP/Sitemap.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/SitemapPHP/Sitemap.php b/src/SitemapPHP/Sitemap.php index b638fa3..2705ee0 100644 --- a/src/SitemapPHP/Sitemap.php +++ b/src/SitemapPHP/Sitemap.php @@ -19,7 +19,7 @@ class Sitemap { /** * - * @var XMLWriter + * @var \XMLWriter */ private $writer; private $domain; @@ -65,7 +65,7 @@ private function getDomain() { /** * Returns XMLWriter object instance * - * @return XMLWriter + * @return \XMLWriter */ private function getWriter() { return $this->writer; @@ -74,9 +74,9 @@ private function getWriter() { /** * Assigns XMLWriter object instance * - * @param XMLWriter $writer + * @param \XMLWriter $writer */ - private function setWriter(XMLWriter $writer) { + private function setWriter(\XMLWriter $writer) { $this->writer = $writer; } @@ -159,7 +159,7 @@ private function incCurrentSitemap() { * */ private function startSitemap() { - $this->setWriter(new XMLWriter()); + $this->setWriter(new \XMLWriter()); $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT); $this->getWriter()->startDocument('1.0', 'UTF-8'); $this->getWriter()->setIndent(true); @@ -178,7 +178,7 @@ private function startSitemap() { */ public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL) { if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) { - if ($this->getWriter() instanceof XMLWriter) { + if ($this->getWriter() instanceof \XMLWriter) { $this->endSitemap(); } $this->startSitemap(); @@ -228,7 +228,7 @@ private function endSitemap() { */ public function createSitemapIndex($loc, $lastmod = 'Today') { $this->endSitemap(); - $indexwriter = new XMLWriter(); + $indexwriter = new \XMLWriter(); $indexwriter->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . self::INDEX_SUFFIX . self::EXT); $indexwriter->startDocument('1.0', 'UTF-8'); $indexwriter->setIndent(true); From 56e8ed18ddf834ebd61afb90f714fba0a1273996 Mon Sep 17 00:00:00 2001 From: Philipp Scheit Date: Thu, 18 Apr 2013 11:43:54 +0300 Subject: [PATCH 05/16] enable to end sitemap with no added items --- src/SitemapPHP/Sitemap.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/SitemapPHP/Sitemap.php b/src/SitemapPHP/Sitemap.php index 2705ee0..0314d32 100644 --- a/src/SitemapPHP/Sitemap.php +++ b/src/SitemapPHP/Sitemap.php @@ -216,8 +216,10 @@ private function getLastModifiedDate($date) { * */ private function endSitemap() { - $this->getWriter()->endElement(); - $this->getWriter()->endDocument(); + if (isset($this->writer)) { + $this->getWriter()->endElement(); + $this->getWriter()->endDocument(); + } } /** From f560716fc52592aa9bc314e05a8405ea654234c7 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Tue, 23 Apr 2013 13:35:15 +0100 Subject: [PATCH 06/16] Added a changelog --- ChangeLog | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 ChangeLog diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..d616c75 --- /dev/null +++ b/ChangeLog @@ -0,0 +1,4 @@ +0.0.1-alpha (2012-01-25) + * First release + +Project was forked from: https://github.com/import/sitemap-php From fe6e2e2e1cc204c9fa241994747c57356abfe9b3 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Tue, 23 Apr 2013 13:37:24 +0100 Subject: [PATCH 07/16] Updated changelog --- ChangeLog | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ChangeLog b/ChangeLog index d616c75..88752d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +1.0.0-stable (2013-04-23) + * First stable release. + * Fixed: Issue #1: Writing empty sitemaps. + 0.0.1-alpha (2012-01-25) * First release From 1bc93ad7a24e9f84b6f162cdebf891349944908f Mon Sep 17 00:00:00 2001 From: Mike Lay Date: Mon, 2 Dec 2013 22:43:16 -0800 Subject: [PATCH 08/16] Change First Sitemap File to sitemap.xml Change first sitemap file from sitemap-1.xml to sitemap.xml This is for the benefit of sites that have less than 50,000 pages and will only have one sitemap file Conflicts: src/SitemapPHP/Sitemap.php --- src/SitemapPHP/Sitemap.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/SitemapPHP/Sitemap.php b/src/SitemapPHP/Sitemap.php index 0314d32..6409459 100644 --- a/src/SitemapPHP/Sitemap.php +++ b/src/SitemapPHP/Sitemap.php @@ -160,7 +160,11 @@ private function incCurrentSitemap() { */ private function startSitemap() { $this->setWriter(new \XMLWriter()); - $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT); + if ($this->getCurrentSitemap()) { + $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT); + } else { + $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::EXT); + } $this->getWriter()->startDocument('1.0', 'UTF-8'); $this->getWriter()->setIndent(true); $this->getWriter()->startElement('urlset'); @@ -238,7 +242,7 @@ public function createSitemapIndex($loc, $lastmod = 'Today') { $indexwriter->writeAttribute('xmlns', self::SCHEMA); for ($index = 0; $index < $this->getCurrentSitemap(); $index++) { $indexwriter->startElement('sitemap'); - $indexwriter->writeElement('loc', $loc . $this->getFilename() . self::SEPERATOR . $index . self::EXT); + $indexwriter->writeElement('loc', $loc . $this->getFilename() . ($index ? self::SEPERATOR . $index : '') . self::EXT); $indexwriter->writeElement('lastmod', $this->getLastModifiedDate($lastmod)); $indexwriter->endElement(); } From b042bc7bdd30b2b8e63eaabe153beb44c1ba9266 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Tue, 3 Dec 2013 09:45:45 -0500 Subject: [PATCH 09/16] Updated changelog. --- ChangeLog | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ChangeLog b/ChangeLog index 88752d7..3e0a407 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +1.1.0-stable (2013-12-03) + * Changed: First sitemap is now called sitemap.xml, rather than + sitemap-1.xml. Patch by @mkly. + 1.0.0-stable (2013-04-23) * First stable release. * Fixed: Issue #1: Writing empty sitemaps. From bfac722f79d2be9db76bdc14150ca157b8102f9e Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Fri, 17 Apr 2015 17:40:23 -0400 Subject: [PATCH 10/16] Removed statement from readme that does not apply here. --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 317fb50..08b4ca8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -**For the 90's people, i'm keeping this repository as 5.2 compatible. If you need PSR-0 and Composer compatible version, [here is a fork that maintained by Evert Pot](https://github.com/evert/sitemap-php).** - What is sitemap-php ? ---------- From 13f9d8f0fe9c746e4cba9bc56c406563cd405b4b Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Fri, 17 Apr 2015 17:43:32 -0400 Subject: [PATCH 11/16] Added my own copyright, and changed link to my repository. --- LICENSE | 1 + src/SitemapPHP/Sitemap.php | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 4adb768..6a99090 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ The MIT License (MIT) Copyright (c) 2009-2015 Osman Üngür +Copyright (c) 2012-2015 Evert Pot (http://evertpot.com/) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/SitemapPHP/Sitemap.php b/src/SitemapPHP/Sitemap.php index 7d480e0..1c49693 100644 --- a/src/SitemapPHP/Sitemap.php +++ b/src/SitemapPHP/Sitemap.php @@ -10,8 +10,9 @@ * @package Sitemap * @author Osman Üngür * @copyright 2009-2015 Osman Üngür + * @copyright 2012-2015 Evert Pot (http://evertpot.com/) * @license http://opensource.org/licenses/MIT MIT License - * @link http://github.com/o/sitemap-php + * @link http://github.com/evert/sitemap-php */ class Sitemap { From a5a5a81dd12f3951b6de1fe8e95deaa370d97e7d Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Fri, 17 Apr 2015 17:45:22 -0400 Subject: [PATCH 12/16] Updated changelog. Prepare for release. --- ChangeLog | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ChangeLog b/ChangeLog index 3e0a407..dd41fdc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +1.2.0-stable (2015-04-17) + * Now licensed as MIT. + * Fixed: Fix fatal error on empty sitemap. Patch my @mkly. + 1.1.0-stable (2013-12-03) * Changed: First sitemap is now called sitemap.xml, rather than sitemap-1.xml. Patch by @mkly. From be07a062943401067bf0ba62710ca733c838bc24 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Fri, 17 Apr 2015 17:45:50 -0400 Subject: [PATCH 13/16] Updated license in composer.json. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 624b0c8..17b80c2 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "description": "Lightweight library for generating Google sitemap XML files", "keywords": ["Sitemap"], "homepage": "http://code.google.com/p/sabredav/", - "license": "BSD License", + "license": "MIT", "authors": [ { "name": "Osman Üngür", From fafc47423bd06ec494c763f57a5304e9b3e2d4d3 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Mon, 6 Jul 2015 14:20:42 -0400 Subject: [PATCH 14/16] Fixed homepage url. Fixes #8. Closes #9. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 17b80c2..2d24aa9 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "library", "description": "Lightweight library for generating Google sitemap XML files", "keywords": ["Sitemap"], - "homepage": "http://code.google.com/p/sabredav/", + "homepage": "https://github.com/evert/sitemap-php/", "license": "MIT", "authors": [ { From a5801ab6506997d3ee2dd266ec1571c4dc67dd67 Mon Sep 17 00:00:00 2001 From: userlond Date: Tue, 2 Feb 2016 08:51:00 +0700 Subject: [PATCH 15/16] Update Sitemap.php Make priority param in SitemapPHP/Sitemap.php::addItem optional --- src/SitemapPHP/Sitemap.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/SitemapPHP/Sitemap.php b/src/SitemapPHP/Sitemap.php index 1c49693..02b745a 100644 --- a/src/SitemapPHP/Sitemap.php +++ b/src/SitemapPHP/Sitemap.php @@ -174,9 +174,9 @@ private function startSitemap() { * Adds an item to sitemap * * @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. + * @param string|null $priority The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. + * @param string|null $changefreq How frequently the page is likely to change. Valid values are always, hourly, daily, weekly, monthly, yearly and never. + * @param string|int|null $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) { @@ -190,7 +190,8 @@ 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('priority', $priority); + if($priority !== null) + $this->getWriter()->writeElement('priority', $priority); if ($changefreq) $this->getWriter()->writeElement('changefreq', $changefreq); if ($lastmod) From ba480702edd432d822015d35f73e0f4a76940ba4 Mon Sep 17 00:00:00 2001 From: David Oti Date: Thu, 4 Apr 2019 01:40:10 +0100 Subject: [PATCH 16/16] Update README.md --- README.md | 73 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 08b4ca8..3e1e335 100644 --- a/README.md +++ b/README.md @@ -1,65 +1,107 @@ -What is sitemap-php ? ----------- +# Sitemap-php Fast and lightweight class for generating Google sitemap XML files and index of sitemap files. Written on PHP and uses XMLWriter extension (wrapper for libxml xmlWriter API) for creating XML files. XMLWriter extension is enabled by default in PHP 5 >= 5.1.2. If you having more than 50000 url, it splits items to seperated files. _(In benchmarks, 1.000.000 url was generating in 8 seconds)_ This is a slightly modified version of the original. The Sitemap class is now added to a 'SitemapPHP' namespace, and a composer document has been added. -How to use ----------- + +## Requirements + +- PHP 5.1.2 and above + +## Steps: + +* [Installation](#installation) +* [Usage](#usage) +* [Maintainers](#maintainers) +* [License](#license) + + +### Installation + +**Composer** + +Run the following command to include this package via Composer + +```shell +composer require evert/sitemap-php +``` + +**Include** Include Sitemap.php file to your PHP document and call Sitemap class with your base domain. +```php include 'src/SitemapPHP/Sitemap.php'; - - use SitemapPHP\Sitemap; +``` - $sitemap = new Sitemap('http://example.com'); + +### Usage + +```php + use SitemapPHP\Sitemap; + + $sitemap = new Sitemap('http://example.com'); +``` Now, we need to define path for saving XML files. This can be relative like `xmls` or absolute `/path/to/your/folder` and *must be a writable folder*. In default it uses same folder with your script. +```php $sitemap->setPath('xmls/'); +``` Generated XML file names defaulted to `sitemap-*.xml`, you can customize prefix of filenames with `setFilename` method. +```php $sitemap->setFilename('customsitemap'); - +``` We'll add sitemap url's with `addItem` method. In this method, only first parameter (location) is required. +```php $sitemap->addItem('/', '1.0', 'daily', 'Today'); $sitemap->addItem('/about', '0.8', 'monthly', 'Jun 25'); $sitemap->addItem('/contact', '0.6', 'yearly', '14-12-2009'); $sitemap->addItem('/otherpage'); +``` w/ method chaining. +```php $sitemap->addItem('/projects', '0.8')->addItem('/somepage')->addItem('/hiddenpage', '0.4', 'yearly', '01-01-2011')->addItem('/rss'); +``` from a sql result, or whatever. +```php $query = Doctrine_Query::create() ->select('p.created_at, p.slug') ->from('Posts p') ->orderBy('p.id DESC') ->useResultCache(true); $posts = $query->fetchArray(array(), Doctrine_Core::HYDRATE_ARRAY); - foreach ($posts as $post) { - $sitemap->addItem('/post/' . $post['slug'], '0.6', 'weekly', $post['created_at']); - } + foreach ($posts as $post) { + $sitemap->addItem('/post/' . $post['slug'], '0.6', 'weekly', $post['created_at']); + } +``` If you need to change domain for sitemap instance, you can override it via `setDomain` method. +```php $sitemap->setDomain('http://blog.example.com'); +``` Finally we create index for sitemap files. This method also closes tags of latest generated xml file. +```php $sitemap->createSitemapIndex('http://example.com/sitemap/', 'Today'); +``` When you run your script, it generates and saves XML files to given path. sitemap-0.xml + @@ -97,3 +139,12 @@ sitemap-index.xml You need to submit sitemap-index.xml to Google Sitemaps. + + +### Maintainers + +This package is maintained by [Evert Pot](https://github.com/evert), [David Oti](http://github.com/davmixcool), [Osman Ungur](https://github.com/o), [Mike Lay](https://github.com/mkly), [Userlond](https://github.com/userlond), [Philipp Scheit](https://github.com/pscheit), and you! + +### License + +This package is licensed under the [MIT license](https://github.com/evert/sitemap-php/blob/master/LICENSE).