diff --git a/lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php new file mode 100644 index 0000000..d576c4f --- /dev/null +++ b/lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php @@ -0,0 +1,69 @@ + + */ +class BaseGenerator +{ + /** + * @var string + */ + protected $hostUrl; + + /** + * @var SimpleXMLElement + */ + protected $xml; + + /** + * BaseGenerator constructor. + */ + public function __construct() + { + $this->hostUrl = "https://" . Config::getSystemConfig()->get("general")->get("domain"); + $this->newXmlDocument(); + } + + protected function newXmlDocument() + { + $this->xml = new SimpleXMLElement(''); + } + + + public function generateXml() + { + // Override in subclass + } + + /** + * Format a given date. + * + * @param $date + * @return string + */ + protected function getDateFormat($date) + { + return gmdate(DATE_ATOM, $date); + } +} diff --git a/lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/SitemapDocumentsGenerator.php similarity index 68% rename from lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php rename to lib/Byng/Pimcore/Sitemap/Generator/SitemapDocumentsGenerator.php index 9824f3c..ebf460b 100644 --- a/lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php +++ b/lib/Byng/Pimcore/Sitemap/Generator/SitemapDocumentsGenerator.php @@ -15,29 +15,21 @@ namespace Byng\Pimcore\Sitemap\Generator; -use Pimcore\Config; use Pimcore\Model\Document; +use Byng\Pimcore\Sitemap\Generator\BaseGenerator; use Byng\Pimcore\Sitemap\Gateway\DocumentGateway; -use Byng\Pimcore\Sitemap\Notifier\GoogleNotifier; use SimpleXMLElement; +use Pimcore\View\Helper\Url; + + /** * Sitemap Generator * * @author Ioannis Giakoumidis */ -final class SitemapGenerator +final class SitemapDocumentsGenerator extends BaseGenerator { - /** - * @var string - */ - private $hostUrl; - - /** - * @var SimpleXMLElement - */ - private $xml; - /** * @var DocumentGateway */ @@ -49,20 +41,17 @@ final class SitemapGenerator */ public function __construct() { - $this->hostUrl = Config::getSystemConfig()->get("general")->get("domain"); + parent::__construct(); $this->documentGateway = new DocumentGateway(); + } + protected function newXmlDocument() { $this->xml = new SimpleXMLElement( '' . '' ); } - /** - * Generates the sitemap.xml file - * - * @return void - */ public function generateXml() { // Get all the root elements with parentId '1' @@ -72,11 +61,8 @@ public function generateXml() $this->addUrlChild($rootDocument); $this->listAllChildren($rootDocument); } - $this->xml->asXML(PIMCORE_DOCUMENT_ROOT . "/sitemap.xml"); + $this->xml->asXML(PIMCORE_DOCUMENT_ROOT . "/sitemap-documents.xml"); - if (Config::getSystemConfig()->get("general")->get("environment") === "production") { - $this->notifySearchEngines(); - } } /** @@ -113,31 +99,4 @@ private function addUrlChild(Document $document) $url->addChild('lastmod', $this->getDateFormat($document->getModificationDate())); } } - - /** - * Format a given date. - * - * @param $date - * @return string - */ - private function getDateFormat($date) - { - return gmdate(DATE_ATOM, $date); - } - - /** - * Notify search engines about the sitemap update. - * - * @return void - */ - private function notifySearchEngines() - { - $googleNotifier = new GoogleNotifier(); - - if ($googleNotifier->notify()) { - echo "Google has been notified \n"; - } else { - echo "Google has not been notified \n"; - } - } } diff --git a/lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php new file mode 100644 index 0000000..17c09e0 --- /dev/null +++ b/lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php @@ -0,0 +1,111 @@ + + */ +final class SitemapIndexGenerator extends BaseGenerator +{ + /** + * @var SitemapDocumentsGenerator + */ + private $sitemapDocumentsGenerator; + + /** + * @var SitemapObjectsGenerator + */ + private $sitemapObjectsGenerator; + + /** + * SitemapIndexGenerator constructor. + */ + public function __construct() + { + parent::__construct(); + $this->sitemapDocumentsGenerator = new SitemapDocumentsGenerator(); + $this->sitemapObjectsGenerator = new SitemapObjectsGenerator(); + } + + protected function newXmlDocument() { + $this->xml = new SimpleXMLElement( + '' + . '' + ); + } + + public function generateXml() + { + $this->sitemapDocumentsGenerator->generateXml(); + $this->sitemapObjectsGenerator->generateXml(); + $this->generateIndexXml(); + + if (Config::getSystemConfig()->get("general")->get("environment") === "production") { + $this->notifySearchEngines(); + } + } + + public function generateIndexXml() + { + $lastMod = new \DateTime(); + + $url = $this->xml->addChild("sitemap"); + $url->addChild('loc', $this->hostUrl . "/sitemap-index.xml"); + $url->addChild('lastmod', $this->getDateFormat($lastMod->getTimestamp())); + + $url = $this->xml->addChild("sitemap"); + $url->addChild('loc', $this->hostUrl . "/sitemap-documents.xml"); + $url->addChild('lastmod', $this->getDateFormat($lastMod->getTimestamp())); + + + if (defined("SITEMAP_OBJECTS")) { + foreach (SITEMAP_OBJECTS as $name => $route) { + $url = $this->xml->addChild("sitemap"); + $lowercaseName = strtolower($name); + $url->addChild('loc', $this->hostUrl . "/sitemap-{$lowercaseName}s.xml"); + $url->addChild('lastmod', $this->getDateFormat($lastMod->getTimestamp())); + } + } + $this->xml->asXML(PIMCORE_DOCUMENT_ROOT . "/sitemap.xml"); + $this->xml->asXML(PIMCORE_DOCUMENT_ROOT . "/sitemap-index.xml"); + + } + + /** + * Notify search engines about the sitemap update. + * + * @return void + */ + protected function notifySearchEngines() + { + $googleNotifier = new GoogleNotifier(); + + if ($googleNotifier->notify()) { + echo "Google has been notified \n"; + } else { + echo "Google has not been notified \n"; + } + } +} diff --git a/lib/Byng/Pimcore/Sitemap/Generator/SitemapObjectsGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/SitemapObjectsGenerator.php new file mode 100644 index 0000000..2dbe805 --- /dev/null +++ b/lib/Byng/Pimcore/Sitemap/Generator/SitemapObjectsGenerator.php @@ -0,0 +1,68 @@ + + */ +final class SitemapObjectsGenerator extends BaseGenerator +{ + protected function newXmlDocument() { + $this->xml = new SimpleXMLElement( + '' + . '' + ); + } + + public function generateXml() + { + if (defined("SITEMAP_OBJECTS")) { + foreach (SITEMAP_OBJECTS as $name => $route) { + $this->newXmlDocument(); + $objectClass = "\Pimcore\Model\Object\\{$name}"; + $objects = $objectClass::getList(); + foreach ($objects as $object) { + $this->addUrlChild($object, $route); + } + $lowercaseName = strtolower($name); + $this->xml->asXML(PIMCORE_DOCUMENT_ROOT . "/sitemap-{$lowercaseName}s.xml"); + } + } + } + + private function addUrlChild($object, $route) + { + if (!$object->getProperty("sitemap_exclude")) { + + $url = $this->xml->addChild("url"); + + $urlHelper = new Url(); + $route = $urlHelper->url(['key' => $object->getKey()], $route, true); + echo $this->hostUrl . $route . "\n"; + $url->addChild('loc', $this->hostUrl . $route); + $url->addChild('lastmod', $this->getDateFormat($object->getModificationDate())); + } + } +} diff --git a/lib/Byng/Pimcore/Sitemap/SitemapPlugin.php b/lib/Byng/Pimcore/Sitemap/SitemapPlugin.php index 851c804..9dbd9ed 100644 --- a/lib/Byng/Pimcore/Sitemap/SitemapPlugin.php +++ b/lib/Byng/Pimcore/Sitemap/SitemapPlugin.php @@ -19,7 +19,7 @@ use Pimcore\Model\Property\Predefined as PredefinedProperty; use Pimcore\Model\Schedule\Manager\Procedural as ProceduralScheduleManager; use Pimcore\Model\Schedule\Maintenance\Job as MaintenanceJob; -use Byng\Pimcore\Sitemap\Generator\SitemapGenerator; +use Byng\Pimcore\Sitemap\Generator\SitemapIndexGenerator; /** * Sitemap Plugin @@ -42,7 +42,7 @@ public function init() $target = $event->getTarget(); $target->registerJob(new MaintenanceJob( self::MAINTENANCE_JOB_GENERATE_SITEMAP, - new SitemapGenerator(), + new SitemapIndexGenerator(), "generateXml" )); });