diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..dd41fdc --- /dev/null +++ b/ChangeLog @@ -0,0 +1,16 @@ +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. + +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 + +Project was forked from: https://github.com/import/sitemap-php 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/README.md b/README.md index 399ca1a..3e1e335 100644 --- a/README.md +++ b/README.md @@ -1,62 +1,107 @@ -**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 ? ----------- +# 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)_ -How to use ----------- +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. + + +## 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. - include 'Sitemap.php'; - $sitemap = new Sitemap('http://example.com'); +```php + include 'src/SitemapPHP/Sitemap.php'; +``` + + +### 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 + @@ -94,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). diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2d24aa9 --- /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": "https://github.com/evert/sitemap-php/", + "license": "MIT", + "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/" } + } +} diff --git a/Sitemap.php b/src/SitemapPHP/Sitemap.php similarity index 86% rename from Sitemap.php rename to src/SitemapPHP/Sitemap.php index a15e78b..02b745a 100644 --- a/Sitemap.php +++ b/src/SitemapPHP/Sitemap.php @@ -1,5 +1,7 @@ * @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 { /** * - * @var XMLWriter + * @var \XMLWriter */ private $writer; private $domain; @@ -61,7 +64,7 @@ private function getDomain() { /** * Returns XMLWriter object instance * - * @return XMLWriter + * @return \XMLWriter */ private function getWriter() { return $this->writer; @@ -70,9 +73,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; } @@ -155,7 +158,7 @@ private function incCurrentSitemap() { * */ private function startSitemap() { - $this->setWriter(new XMLWriter()); + $this->setWriter(new \XMLWriter()); if ($this->getCurrentSitemap()) { $this->getWriter()->openURI($this->getPath() . $this->getFilename() . self::SEPERATOR . $this->getCurrentSitemap() . self::EXT); } else { @@ -171,14 +174,14 @@ 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) { if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) { - if ($this->getWriter() instanceof XMLWriter) { + if ($this->getWriter() instanceof \XMLWriter) { $this->endSitemap(); } $this->startSitemap(); @@ -187,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) @@ -231,7 +235,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);