From 12fbcc997689eaea652dff95518f50bd46a53d90 Mon Sep 17 00:00:00 2001 From: Joe Connor Date: Sat, 8 Jul 2017 22:59:47 +0800 Subject: [PATCH 1/3] Add object sitemaps --- .../Sitemap/Generator/SitemapGenerator.php | 94 +++++++++++++++++-- 1 file changed, 86 insertions(+), 8 deletions(-) diff --git a/lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php index 9824f3c..f521d46 100644 --- a/lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php +++ b/lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php @@ -21,6 +21,9 @@ use Byng\Pimcore\Sitemap\Notifier\GoogleNotifier; use SimpleXMLElement; +use Pimcore\View\Helper\Url; + + /** * Sitemap Generator * @@ -52,33 +55,95 @@ public function __construct() $this->hostUrl = Config::getSystemConfig()->get("general")->get("domain"); $this->documentGateway = new DocumentGateway(); + $this->newXml(); + } + + private function newXml() { $this->xml = new SimpleXMLElement( '' . '' ); } + private function newIndexXml() { + $this->xml = new SimpleXMLElement( + '' + . '' + ); + } + + public function generateXml() + { + $this->generateDocumentsXml(); + $this->generateObjectsXml(); + $this->generateIndexXml(); + + if (Config::getSystemConfig()->get("general")->get("environment") === "production") { + $this->notifySearchEngines(); + } + } + /** - * Generates the sitemap.xml file + * Generates the sitemap-documents.xml file * * @return void */ - public function generateXml() + public function generateDocumentsXml() { // Get all the root elements with parentId '1' $rootDocuments = $this->documentGateway->getChildren(1); foreach ($rootDocuments as $rootDocument) { - $this->addUrlChild($rootDocument); + $this->addDocumentUrlChild($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(); + } + + public function generateObjectsXml() + { + if (defined("SITEMAP_OBJECTS")) { + foreach (SITEMAP_OBJECTS as $name => $route) { + $this->newXml(); + $objectClass = "\Pimcore\Model\Object\\{$name}"; + $objects = $objectClass::getList(); + foreach ($objects as $object) { + $this->addObjectUrlChild($object, $route); + } + $lowercaseName = strtolower($name); + $this->xml->asXML(PIMCORE_DOCUMENT_ROOT . "/sitemap-{$lowercaseName}s.xml"); + } } } + public function generateIndexXml() + { + $this->newIndexXml(); + $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"); + + } + /** * Finds all the children of a document recursively * @@ -90,7 +155,7 @@ private function listAllChildren(Document $document) $children = $this->documentGateway->getChildren($document->getId()); foreach ($children as $child) { - $this->addUrlChild($child); + $this->addDocumentUrlChild($child); $this->listAllChildren($child); } } @@ -101,7 +166,7 @@ private function listAllChildren(Document $document) * @param Document $document * @return void */ - private function addUrlChild(Document $document) + private function addDocumentUrlChild(Document $document) { if ( $document instanceof Document\Page && @@ -114,6 +179,19 @@ private function addUrlChild(Document $document) } } + private function addObjectUrlChild($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())); + // } + } /** * Format a given date. * From 8fa888d830746bb625247883e0706e446634d98a Mon Sep 17 00:00:00 2001 From: Joe Connor Date: Mon, 10 Jul 2017 15:33:34 +0800 Subject: [PATCH 2/3] Add ObjectSitemaps --- .../Sitemap/Generator/BaseGenerator.php | 69 ++++++ .../Generator/SitemapDocumentsGenerator.php | 102 ++++++++ .../Sitemap/Generator/SitemapGenerator.php | 221 ------------------ .../Generator/SitemapIndexGenerator.php | 97 ++++++++ .../Generator/SitemapObjectsGenerator.php | 68 ++++++ lib/Byng/Pimcore/Sitemap/SitemapPlugin.php | 4 +- 6 files changed, 338 insertions(+), 223 deletions(-) create mode 100644 lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php create mode 100644 lib/Byng/Pimcore/Sitemap/Generator/SitemapDocumentsGenerator.php delete mode 100644 lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php create mode 100644 lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php create mode 100644 lib/Byng/Pimcore/Sitemap/Generator/SitemapObjectsGenerator.php diff --git a/lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php new file mode 100644 index 0000000..1d46323 --- /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 = 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/SitemapDocumentsGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/SitemapDocumentsGenerator.php new file mode 100644 index 0000000..ebf460b --- /dev/null +++ b/lib/Byng/Pimcore/Sitemap/Generator/SitemapDocumentsGenerator.php @@ -0,0 +1,102 @@ + + */ +final class SitemapDocumentsGenerator extends BaseGenerator +{ + /** + * @var DocumentGateway + */ + private $documentGateway; + + + /** + * SitemapGenerator constructor. + */ + public function __construct() + { + parent::__construct(); + $this->documentGateway = new DocumentGateway(); + } + + protected function newXmlDocument() { + $this->xml = new SimpleXMLElement( + '' + . '' + ); + } + + public function generateXml() + { + // Get all the root elements with parentId '1' + $rootDocuments = $this->documentGateway->getChildren(1); + + foreach ($rootDocuments as $rootDocument) { + $this->addUrlChild($rootDocument); + $this->listAllChildren($rootDocument); + } + $this->xml->asXML(PIMCORE_DOCUMENT_ROOT . "/sitemap-documents.xml"); + + } + + /** + * Finds all the children of a document recursively + * + * @param Document $document + * @return void + */ + private function listAllChildren(Document $document) + { + $children = $this->documentGateway->getChildren($document->getId()); + + foreach ($children as $child) { + $this->addUrlChild($child); + $this->listAllChildren($child); + } + } + + /** + * Adds a url child in the xml file. + * + * @param Document $document + * @return void + */ + private function addUrlChild(Document $document) + { + if ( + $document instanceof Document\Page && + !$document->getProperty("sitemap_exclude") + ) { + echo $this->hostUrl . $document->getFullPath() . "\n"; + $url = $this->xml->addChild("url"); + $url->addChild('loc', $this->hostUrl . $document->getFullPath()); + $url->addChild('lastmod', $this->getDateFormat($document->getModificationDate())); + } + } +} diff --git a/lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php deleted file mode 100644 index f521d46..0000000 --- a/lib/Byng/Pimcore/Sitemap/Generator/SitemapGenerator.php +++ /dev/null @@ -1,221 +0,0 @@ - - */ -final class SitemapGenerator -{ - /** - * @var string - */ - private $hostUrl; - - /** - * @var SimpleXMLElement - */ - private $xml; - - /** - * @var DocumentGateway - */ - private $documentGateway; - - - /** - * SitemapGenerator constructor. - */ - public function __construct() - { - $this->hostUrl = Config::getSystemConfig()->get("general")->get("domain"); - $this->documentGateway = new DocumentGateway(); - - $this->newXml(); - } - - private function newXml() { - $this->xml = new SimpleXMLElement( - '' - . '' - ); - } - - private function newIndexXml() { - $this->xml = new SimpleXMLElement( - '' - . '' - ); - } - - public function generateXml() - { - $this->generateDocumentsXml(); - $this->generateObjectsXml(); - $this->generateIndexXml(); - - if (Config::getSystemConfig()->get("general")->get("environment") === "production") { - $this->notifySearchEngines(); - } - } - - /** - * Generates the sitemap-documents.xml file - * - * @return void - */ - public function generateDocumentsXml() - { - // Get all the root elements with parentId '1' - $rootDocuments = $this->documentGateway->getChildren(1); - - foreach ($rootDocuments as $rootDocument) { - $this->addDocumentUrlChild($rootDocument); - $this->listAllChildren($rootDocument); - } - $this->xml->asXML(PIMCORE_DOCUMENT_ROOT . "/sitemap-documents.xml"); - - } - - public function generateObjectsXml() - { - if (defined("SITEMAP_OBJECTS")) { - foreach (SITEMAP_OBJECTS as $name => $route) { - $this->newXml(); - $objectClass = "\Pimcore\Model\Object\\{$name}"; - $objects = $objectClass::getList(); - foreach ($objects as $object) { - $this->addObjectUrlChild($object, $route); - } - $lowercaseName = strtolower($name); - $this->xml->asXML(PIMCORE_DOCUMENT_ROOT . "/sitemap-{$lowercaseName}s.xml"); - } - } - } - - public function generateIndexXml() - { - $this->newIndexXml(); - $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"); - - } - - /** - * Finds all the children of a document recursively - * - * @param Document $document - * @return void - */ - private function listAllChildren(Document $document) - { - $children = $this->documentGateway->getChildren($document->getId()); - - foreach ($children as $child) { - $this->addDocumentUrlChild($child); - $this->listAllChildren($child); - } - } - - /** - * Adds a url child in the xml file. - * - * @param Document $document - * @return void - */ - private function addDocumentUrlChild(Document $document) - { - if ( - $document instanceof Document\Page && - !$document->getProperty("sitemap_exclude") - ) { - echo $this->hostUrl . $document->getFullPath() . "\n"; - $url = $this->xml->addChild("url"); - $url->addChild('loc', $this->hostUrl . $document->getFullPath()); - $url->addChild('lastmod', $this->getDateFormat($document->getModificationDate())); - } - } - - private function addObjectUrlChild($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())); - // } - } - /** - * 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..f4eb7c6 --- /dev/null +++ b/lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php @@ -0,0 +1,97 @@ + + */ +final class SitemapIndexGenerator extends BaseGenerator +{ + /** + * @var DocumentGateway + */ + private $documentGateway; + + + protected function newXmlDocument() { + $this->xml = new SimpleXMLElement( + '' + . '' + ); + } + + public function generateXml() + { + $this->sitemapGenerator->generateDocumentsXml(); + $this->sitemapGenerator->generateObjectsXml(); + $this->generateIndexXml(); + + if (Config::getSystemConfig()->get("general")->get("environment") === "production") { + $this->notifySearchEngines(); + } + } + + public function generateIndexXml() + { + $this->newIndexXml(); + $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" )); }); From 62b6841523a8686a83905b686c9b5d98ea707d98 Mon Sep 17 00:00:00 2001 From: Joe Connor Date: Mon, 10 Jul 2017 15:35:39 +0800 Subject: [PATCH 3/3] Refactor into proper methods --- .../Sitemap/Generator/BaseGenerator.php | 2 +- .../Generator/SitemapIndexGenerator.php | 28 ++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php index 1d46323..d576c4f 100644 --- a/lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php +++ b/lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php @@ -41,7 +41,7 @@ class BaseGenerator */ public function __construct() { - $this->hostUrl = Config::getSystemConfig()->get("general")->get("domain"); + $this->hostUrl = "https://" . Config::getSystemConfig()->get("general")->get("domain"); $this->newXmlDocument(); } diff --git a/lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php b/lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php index f4eb7c6..17c09e0 100644 --- a/lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php +++ b/lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php @@ -16,8 +16,9 @@ namespace Byng\Pimcore\Sitemap\Generator; use Pimcore\Config; -use Byng\Pimcore\Sitemap\Notifier\BaseGenerator; -use Byng\Pimcore\Sitemap\Generator\SitemapGenerator; +use Byng\Pimcore\Sitemap\Generator\BaseGenerator; +use Byng\Pimcore\Sitemap\Generator\SitemapDocumentsGenerator; +use Byng\Pimcore\Sitemap\Generator\SitemapObjectsGenerator; use Byng\Pimcore\Sitemap\Notifier\GoogleNotifier; use SimpleXMLElement; @@ -29,10 +30,24 @@ final class SitemapIndexGenerator extends BaseGenerator { /** - * @var DocumentGateway + * @var SitemapDocumentsGenerator */ - private $documentGateway; + 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( @@ -43,8 +58,8 @@ protected function newXmlDocument() { public function generateXml() { - $this->sitemapGenerator->generateDocumentsXml(); - $this->sitemapGenerator->generateObjectsXml(); + $this->sitemapDocumentsGenerator->generateXml(); + $this->sitemapObjectsGenerator->generateXml(); $this->generateIndexXml(); if (Config::getSystemConfig()->get("general")->get("environment") === "production") { @@ -54,7 +69,6 @@ public function generateXml() public function generateIndexXml() { - $this->newIndexXml(); $lastMod = new \DateTime(); $url = $this->xml->addChild("sitemap");