Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add object sitemaps #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
69 changes: 69 additions & 0 deletions lib/Byng/Pimcore/Sitemap/Generator/BaseGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* This file is part of the pimcore-sitemap-plugin package.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Byng\Pimcore\Sitemap\Generator;

use Pimcore\Config;
use Byng\Pimcore\Sitemap\Generator\SitemapGenerator;
use SimpleXMLElement;

/**
* Sitemap Generator
*
* @author Ioannis Giakoumidis <[email protected]>
*/
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('<?xml version="1.0" encoding="UTF-8"?>');
}


public function generateXml()
{
// Override in subclass
}

/**
* Format a given date.
*
* @param $date
* @return string
*/
protected function getDateFormat($date)
{
return gmdate(DATE_ATOM, $date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
final class SitemapGenerator
final class SitemapDocumentsGenerator extends BaseGenerator
{
/**
* @var string
*/
private $hostUrl;

/**
* @var SimpleXMLElement
*/
private $xml;

/**
* @var DocumentGateway
*/
Expand All @@ -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(
'<?xml version="1.0" encoding="UTF-8"?>'
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'
);
}

/**
* Generates the sitemap.xml file
*
* @return void
*/
public function generateXml()
{
// Get all the root elements with parentId '1'
Expand All @@ -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();
}
}

/**
Expand Down Expand Up @@ -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";
}
}
}
111 changes: 111 additions & 0 deletions lib/Byng/Pimcore/Sitemap/Generator/SitemapIndexGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/**
* This file is part of the pimcore-sitemap-plugin package.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Byng\Pimcore\Sitemap\Generator;

use Pimcore\Config;
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;

/**
* Sitemap Generator
*
* @author Ioannis Giakoumidis <[email protected]>
*/
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(
'<?xml version="1.0" encoding="UTF-8"?>'
. '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>'
);
}

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";
}
}
}
68 changes: 68 additions & 0 deletions lib/Byng/Pimcore/Sitemap/Generator/SitemapObjectsGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* This file is part of the pimcore-sitemap-plugin package.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Byng\Pimcore\Sitemap\Generator;

use Pimcore\Model\Document;
use Byng\Pimcore\Sitemap\Generator\BaseGenerator;
use SimpleXMLElement;

use Pimcore\View\Helper\Url;


/**
* Sitemap Generator
*
* @author Ioannis Giakoumidis <[email protected]>
*/
final class SitemapObjectsGenerator extends BaseGenerator
{
protected function newXmlDocument() {
$this->xml = new SimpleXMLElement(
'<?xml version="1.0" encoding="UTF-8"?>'
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'
);
}

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()));
}
}
}
4 changes: 2 additions & 2 deletions lib/Byng/Pimcore/Sitemap/SitemapPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,7 +42,7 @@ public function init()
$target = $event->getTarget();
$target->registerJob(new MaintenanceJob(
self::MAINTENANCE_JOB_GENERATE_SITEMAP,
new SitemapGenerator(),
new SitemapIndexGenerator(),
"generateXml"
));
});
Expand Down