Skip to content

Commit

Permalink
CjWPublishTools: sitemaps controller, basic caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jan committed Aug 28, 2015
1 parent beebfee commit a58927e
Showing 1 changed file with 48 additions and 46 deletions.
94 changes: 48 additions & 46 deletions Controller/SitemapsController.php
Expand Up @@ -5,72 +5,74 @@
use eZ\Bundle\EzPublishCoreBundle\Controller;
use Symfony\Component\HttpFoundation\Response;

// todo: get available languages, config item for include types, caching etc.
// todo: get available languages, config item for include types

class SitemapsController extends Controller
{
public function sitemapAction()
{
$ttl = 3600 * 12;
$urls = array();
$hostname = $this->getRequest()->getHost();
$ttl = 3600 * 6;

$publishToolsService = $this->get( 'cjw_publishtools.service.functions' );
$response = new Response;
$response->headers->set( 'Content-Type', 'text/xml' );
$response->setPublic();
$response->setSharedMaxAge( $ttl );

$rootLocationId = $this->getConfigResolver()->getParameter('content.tree_root.location_id');
$siteaccessName = $this->container->get('ezpublish.siteaccess')->name;
$cacheFile = $this->container->getParameter( 'kernel.cache_dir' ) . '/sitemap_'.$siteaccessName.'.xml';

$listLocations = $publishToolsService->fetchLocationListArr(
array( $rootLocationId ), array( 'depth' => 10,
'limit' => 25000,
// 'include' => array( 'article' ),
'main_location_only' => true,
'datamap' => false )
);
$cacheFileMtime = 0;
if ( file_exists( $cacheFile ) )
{
$cacheFileMtime = stat( $cacheFile )['mtime'];
}

foreach( $listLocations[$rootLocationId]['children'] as $location )
if ( $cacheFileMtime < ( time() - $ttl ) )
{
$loc = $this->generateUrl( $location );
$lastmod = date( 'c', $location->contentInfo->modificationDate->getTimestamp() );
$urls = array();
$hostname = $this->getRequest()->getHost();

$urls[] = array( 'loc' => $loc,
'lastmod' => $lastmod );
}
$publishToolsService = $this->get( 'cjw_publishtools.service.functions' );

$rootLocationId = $this->getConfigResolver()->getParameter('content.tree_root.location_id');

$listLocations = $publishToolsService->fetchLocationListArr(
array( $rootLocationId ), array( 'depth' => 10,
'limit' => 25000,
// 'include' => array( 'article' ),
'main_location_only' => true,
'datamap' => false )
);

// https://doc.ez.no/display/EZP/Persistence+cache
// http://blogs.arondor.com/en/layout/set/print/Content-management/%28tag%29/Cache
/* INIT Filesystem Cache */
/*
$pool = $this->container->get( 'ezpublish.cache_pool' );
$item = $pool->getItem( 'sitemapxml', $rootLocationId, $this->getRequest()->getScheme(), null );
$data = $item->get();
*/
/* GENERATE Cache Block */
/*
if($item->isMiss())
foreach( $listLocations[$rootLocationId]['children'] as $location )
{
$loc = $this->generateUrl( $location );
$lastmod = date( 'c', $location->contentInfo->modificationDate->getTimestamp() );

$urls[] = array( 'loc' => $loc, 'lastmod' => $lastmod );
}

$sitemapXmlResponse = $this->render(
'CjwPublishToolsBundle::sitemap.xml.twig',
array( 'urls' => $urls, 'hostname' => 'https://'.$hostname ),
$response
);

file_put_contents( $cacheFile, $sitemapXmlResponse->getContent() );
}
else
{
// RenderView without cache headers
$data = $this->renderView( 'CjwPublishToolsBundle::sitemap.xml.twig',
array( 'urls' => $urls, 'hostname' => $hostname ) );
// Store data in cache
$item->set( $data, $TTL );
$sitemapXmlResponse = $response->setContent( file_get_contents( $cacheFile ) );
}
*/
$response = new Response;
$response->setPublic();
$response->setSharedMaxAge( $ttl );
// $response->headers->set( 'X-Location-Id', $rootLocationId );

return $this->render(
'CjwPublishToolsBundle::sitemap.xml.twig',
array( 'urls' => $urls, 'hostname' => $hostname ),
$response
);
return $sitemapXmlResponse;
}

public function robotsAction()
{
return $this->render(
'CjwPublishToolsBundle::robots.txt.twig',
array( 'hostname' => $this->getRequest()->getHost() )
array( 'hostname' => 'https://'.$this->getRequest()->getHost() )
);
}
}

0 comments on commit a58927e

Please sign in to comment.