sitemap can be generated according to a specific language and/or specific context (catalog, content)
This commit is contained in:
@@ -13,11 +13,13 @@
|
|||||||
|
|
||||||
namespace Front\Controller;
|
namespace Front\Controller;
|
||||||
|
|
||||||
|
use Doctrine\Common\Cache\FilesystemCache;
|
||||||
use Thelia\Controller\Front\BaseFrontController;
|
use Thelia\Controller\Front\BaseFrontController;
|
||||||
use Thelia\Core\HttpFoundation\Request;
|
use Thelia\Core\HttpFoundation\Request;
|
||||||
use Thelia\Core\HttpFoundation\Response;
|
use Thelia\Core\HttpFoundation\Response;
|
||||||
use Thelia\Log\Tlog;
|
use Thelia\Log\Tlog;
|
||||||
use Thelia\Model\ConfigQuery;
|
use Thelia\Model\ConfigQuery;
|
||||||
|
use Thelia\Model\LangQuery;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller uses to generate sitemap.xml
|
* Controller uses to generate sitemap.xml
|
||||||
@@ -34,12 +36,12 @@ class SitemapController extends BaseFrontController {
|
|||||||
/**
|
/**
|
||||||
* Folder name for sitemap cache
|
* Folder name for sitemap cache
|
||||||
*/
|
*/
|
||||||
const SITEMAP_DIR = "sitemap";
|
const SITEMAP_CACHE_DIR = "sitemap";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Folder name for sitemap cache
|
* Key prefix for sitemap cache
|
||||||
*/
|
*/
|
||||||
const SITEMAP_FILE = "sitemap";
|
const SITEMAP_CACHE_KEY = "sitemap";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Response
|
* @return Response
|
||||||
@@ -49,36 +51,48 @@ class SitemapController extends BaseFrontController {
|
|||||||
|
|
||||||
/** @var Request $request */
|
/** @var Request $request */
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
$flush = $request->query->get("flush", "");
|
|
||||||
$expire = ConfigQuery::read("sitemap_ttl", '7200');
|
|
||||||
|
|
||||||
// check if sitemap already in cache
|
// the locale : fr, en,
|
||||||
$cacheDir = $this->getCacheDir();
|
$lang = $request->query->get("lang", "");
|
||||||
$cacheFileURL = $cacheDir . self::SITEMAP_FILE . '.xml';
|
if ("" !== $lang) {
|
||||||
$expire = intval($expire) ?: 7200;
|
if (! $this->checkLang($lang)){
|
||||||
$cacheContent = null;
|
$this->pageNotFound();
|
||||||
|
|
||||||
if (!($this->checkAdmin() && "" !== $flush)){
|
|
||||||
try {
|
|
||||||
$cacheContent = $this->getCache($cacheFileURL, $expire);
|
|
||||||
} catch (\RuntimeException $ex) {
|
|
||||||
// Problem loading cache, permission errors ?
|
|
||||||
Tlog::getInstance()->addAlert($ex->getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// specific content : product, category, cms
|
||||||
|
$context = $request->query->get("context", "");
|
||||||
|
if (! in_array($context, array("", "catalog", "content")) ){
|
||||||
|
$this->pageNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
if (null === $cacheContent){
|
$flush = $request->query->get("flush", "");
|
||||||
|
|
||||||
|
// check if sitemap already in cache
|
||||||
|
$cacheContent = false;
|
||||||
|
|
||||||
|
$cacheDir = $this->getCacheDir();
|
||||||
|
$cacheKey = self::SITEMAP_CACHE_KEY . $lang . $context;
|
||||||
|
$cacheExpire = intval(ConfigQuery::read("sitemap_ttl", '7200')) ?: 7200;
|
||||||
|
|
||||||
|
$cacheDriver = new FilesystemCache($cacheDir);
|
||||||
|
if (!($this->checkAdmin() && "" !== $flush)){
|
||||||
|
$cacheContent = $cacheDriver->fetch($cacheKey);
|
||||||
|
} else {
|
||||||
|
$cacheDriver->delete($cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if not in cache
|
||||||
|
if (false === $cacheContent){
|
||||||
// render the view
|
// render the view
|
||||||
$cacheContent = $this->renderRaw("sitemap");
|
$cacheContent = $this->renderRaw(
|
||||||
|
"sitemap",
|
||||||
|
array(
|
||||||
|
"_lang_" => $lang,
|
||||||
|
"_context_" => $context
|
||||||
|
)
|
||||||
|
);
|
||||||
// save cache
|
// save cache
|
||||||
try {
|
$cacheDriver->save($cacheKey, $cacheContent, $cacheExpire);
|
||||||
$this->setCache($cacheFileURL, $cacheContent);
|
|
||||||
} catch (\RuntimeException $ex) {
|
|
||||||
// Problem loading cache, permission errors ?
|
|
||||||
Tlog::getInstance()->addAlert($ex->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = new Response();
|
$response = new Response();
|
||||||
@@ -88,70 +102,45 @@ class SitemapController extends BaseFrontController {
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the cache directory for sitemap
|
||||||
|
*
|
||||||
|
* @return mixed|string
|
||||||
|
*/
|
||||||
|
protected function getCacheDir()
|
||||||
|
{
|
||||||
|
$cacheDir = $this->container->getParameter("kernel.cache_dir");
|
||||||
|
$cacheDir = rtrim($cacheDir, '/');
|
||||||
|
$cacheDir .= '/' . self::SITEMAP_CACHE_DIR . '/';
|
||||||
|
|
||||||
|
return $cacheDir;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if current user has ADMIN role
|
* Check if current user has ADMIN role
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function checkAdmin(){
|
protected function checkAdmin(){
|
||||||
return $this->getSecurityContext()->isGranted(array("ADMIN"), array(), array(), array());
|
return $this->getSecurityContext()->hasAdminUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the content of the file if it exists and not expired?
|
|
||||||
*
|
|
||||||
* @param $fileURL path to the file
|
|
||||||
* @param $expire TTL for the file
|
|
||||||
* @return null|string The content of the file if it exists and not expired
|
|
||||||
* @throws \RuntimeException
|
|
||||||
*/
|
|
||||||
protected function getCache($fileURL, $expire)
|
|
||||||
{
|
|
||||||
$content = null;
|
|
||||||
if (is_file($fileURL)){
|
|
||||||
$mtime = filemtime($fileURL);
|
|
||||||
if ($mtime + $expire < time()){
|
|
||||||
if (! @unlink($fileURL)){
|
|
||||||
throw new \RuntimeException(sprintf("Failed to remove %s file in cache directory", $fileURL));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$content = file_get_contents($fileURL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save content in the file specified by `$fileURL`
|
* Check if a lang is used
|
||||||
*
|
*
|
||||||
* @param $fileURL the path to the file
|
* @param $lang The lang code. e.g.: fr
|
||||||
* @param $content the content of the file
|
* @return bool true if the language is used, otherwise false
|
||||||
* @throws \RuntimeException
|
|
||||||
*/
|
*/
|
||||||
protected function setCache($fileURL, $content)
|
private function checkLang($lang)
|
||||||
{
|
{
|
||||||
if (! @file_put_contents($fileURL, $content)){
|
// load locals
|
||||||
throw new \RuntimeException(sprintf("Failed to save %s file in cache directory", $fileURL));
|
$lang = LangQuery::create()
|
||||||
}
|
->findOneByCode($lang);
|
||||||
}
|
|
||||||
|
return (null !== $lang);
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the cache dir used for sitemaps
|
|
||||||
*
|
|
||||||
* @return string the path to the cache dir
|
|
||||||
* @throws \RuntimeException
|
|
||||||
*/
|
|
||||||
protected function getCacheDir()
|
|
||||||
{
|
|
||||||
$cacheDir = $this->container->getParameter("kernel.cache_dir");
|
|
||||||
$cacheDir = rtrim($cacheDir, '/');
|
|
||||||
$cacheDir .= '/' . self::SITEMAP_DIR . '/';
|
|
||||||
if (! is_dir($cacheDir)){
|
|
||||||
if (! @mkdir($cacheDir, 0777, true)) {
|
|
||||||
throw new \RuntimeException(sprintf("Failed to create %s dir in cache directory", self::SITEMAP_DIR));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $cacheDir;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,47 +1,71 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
generated on : {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}
|
||||||
|
{if $_lang_ != "" }lang : {$_lang_}{/if}
|
||||||
|
{if $_context_ != "" }context : {$_context_}{/if}
|
||||||
|
-->
|
||||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
<url>
|
<url>
|
||||||
<loc>{url path="/"}</loc>
|
<loc>{url path="/"}</loc>
|
||||||
</url>
|
{*
|
||||||
<!-- categories -->
|
You can also set priority and changefreq
|
||||||
{loop type="lang" name="category_lang"}
|
<priority>0.8</priority>
|
||||||
{loop type="category" name="category" lang="$ID"}
|
<changefreq>weekly</changefreq>
|
||||||
<url>
|
*}
|
||||||
<loc>{$URL}</loc>
|
</url>
|
||||||
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
|
|
||||||
{*
|
{if $_context_ == "" || $_context_ == "catalog" }
|
||||||
You can also set priority and changefreq
|
|
||||||
<priority>0.8</priority>
|
<!-- categories -->
|
||||||
<changefreq>weekly</changefreq>
|
{loop type="lang" name="category_lang"}
|
||||||
*}
|
{if $_lang_ == "" || $_lang_ == $CODE }
|
||||||
</url>
|
{loop type="category" name="category" lang="$ID"}
|
||||||
{/loop}
|
<url>
|
||||||
{/loop}
|
<loc>{$URL}</loc>
|
||||||
<!-- products -->
|
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
|
||||||
{loop type="lang" name="product_lang"}
|
</url>
|
||||||
{loop type="product" name="product" lang="$ID"}
|
{/loop}
|
||||||
<url>
|
{/if}
|
||||||
<loc>{$URL}</loc>
|
{/loop}
|
||||||
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
|
|
||||||
</url>
|
<!-- products -->
|
||||||
{/loop}
|
{loop type="lang" name="product_lang"}
|
||||||
{/loop}
|
{if $_lang_ == "" || $_lang_ == $CODE }
|
||||||
<!-- folders -->
|
{loop type="product" name="product" lang="$ID"}
|
||||||
{loop type="lang" name="folder_lang"}
|
<url>
|
||||||
{loop type="folder" name="folder" lang="$ID"}
|
<loc>{$URL}</loc>
|
||||||
<url>
|
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
|
||||||
<loc>{$URL}</loc>
|
</url>
|
||||||
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
|
{/loop}
|
||||||
</url>
|
{/if}
|
||||||
{/loop}
|
{/loop}
|
||||||
{/loop}
|
|
||||||
<!-- contents -->
|
{/if}
|
||||||
{loop type="lang" name="content_lang"}
|
|
||||||
{loop type="content" name="content" lang="$ID"}
|
{if $_context_ == "" || $_context_ == "content" }
|
||||||
<url>
|
<!-- folders -->
|
||||||
<loc>{$URL}</loc>
|
{loop type="lang" name="folder_lang"}
|
||||||
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
|
{if $_lang_ == "" || $_lang_ == $CODE }
|
||||||
</url>
|
{loop type="folder" name="folder" lang="$ID"}
|
||||||
{/loop}
|
<url>
|
||||||
{/loop}
|
<loc>{$URL}</loc>
|
||||||
|
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
|
||||||
|
</url>
|
||||||
|
{/loop}
|
||||||
|
{/if}
|
||||||
|
{/loop}
|
||||||
|
|
||||||
|
<!-- contents -->
|
||||||
|
{loop type="lang" name="content_lang"}
|
||||||
|
{if $_lang_ == "" || $_lang_ == $CODE }
|
||||||
|
{loop type="content" name="content" lang="$ID"}
|
||||||
|
<url>
|
||||||
|
<loc>{$URL}</loc>
|
||||||
|
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
|
||||||
|
</url>
|
||||||
|
{/loop}
|
||||||
|
{/if}
|
||||||
|
{/loop}
|
||||||
|
{/if}
|
||||||
|
|
||||||
</urlset>
|
</urlset>
|
||||||
Reference in New Issue
Block a user