This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- render protection (don't render sitemap with >50000 elements) - option to force using a size limits for the number of elements in a sitemap in the store() method, instead of default splitting of bigger sitemaps to smaller sitemaps with sitemapindex - memory leak fix (caching for file generation, clear unneeded resources) - fixed wrong file extension of html/txt generated files
- Loading branch information
Roumen Damianoff
committed
Dec 17, 2015
1 parent
248fb45
commit 637e094
Showing
1 changed file
with
66 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,25 +4,29 @@ | |
* Sitemap class for laravel-sitemap package. | ||
* | ||
* @author Roumen Damianoff <[email protected]> | ||
* @version 2.5.6 | ||
* @version 2.5.8 | ||
* @link http://roumen.it/projects/laravel-sitemap | ||
* @license http://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Facades\Response; | ||
use Illuminate\Support\Facades\View; | ||
|
||
|
||
class Sitemap | ||
{ | ||
|
||
/** | ||
* Model instance | ||
* | ||
* @var Model $model | ||
*/ | ||
public $model = null; | ||
|
||
|
||
/** | ||
* Using constructor we populate our model from configuration file | ||
* | ||
|
@@ -33,6 +37,7 @@ public function __construct(array $config) | |
$this->model = new Model($config); | ||
} | ||
|
||
|
||
/** | ||
* Set cache options | ||
* | ||
|
@@ -55,6 +60,26 @@ public function setCache($key = null, $duration = null, $useCache = true) | |
} | ||
} | ||
|
||
|
||
/** | ||
* Checks if content is cached | ||
* | ||
* @return bool | ||
*/ | ||
public function isCached() | ||
{ | ||
if ($this->model->getUseCache()) | ||
{ | ||
if (Cache::has($this->model->getCacheKey())) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
||
/** | ||
* Add new sitemap item to $items array | ||
* | ||
|
@@ -133,6 +158,7 @@ public function add($loc, $lastmod = null, $priority = null, $freq = null, $imag | |
]); | ||
} | ||
|
||
|
||
/** | ||
* Add new sitemap to $sitemaps array | ||
* | ||
|
@@ -149,6 +175,7 @@ public function addSitemap($loc, $lastmod = null) | |
]); | ||
} | ||
|
||
|
||
/** | ||
* Returns document with all sitemap items from $items array | ||
* | ||
|
@@ -168,6 +195,7 @@ public function render($format = 'xml') | |
return Response::make($data['content'], 200, $data['headers']); | ||
} | ||
|
||
|
||
/** | ||
* Generates document with all sitemap items from $items array | ||
* | ||
|
@@ -177,10 +205,17 @@ public function render($format = 'xml') | |
*/ | ||
public function generate($format = 'xml') | ||
{ | ||
// don't render (cache) more than 50000 elements in a single sitemap | ||
if (count($this->model->getItems()) > 50000) | ||
{ | ||
// get only most recent 50000 | ||
$this->model->limitSize(); | ||
} | ||
|
||
// check if caching is enabled, there is a cached content and its duration isn't expired | ||
if ($this->isCached()) | ||
{ | ||
($format == 'sitemapindex') ? $this->model->sitemaps = Cache::get($this->model->getCacheKey()) : $this->model->items = Cache::get($this->model->getCacheKey()); | ||
($format == 'sitemapindex') ? $this->model->resetSitemaps(Cache::get($this->model->getCacheKey())) : $this->model->resetItems(Cache::get($this->model->getCacheKey())); | ||
} | ||
elseif ($this->model->getUseCache()) | ||
{ | ||
|
@@ -219,6 +254,7 @@ public function generate($format = 'xml') | |
} | ||
} | ||
|
||
|
||
/** | ||
* Generate sitemap and store it to a file | ||
* | ||
|
@@ -229,7 +265,7 @@ public function generate($format = 'xml') | |
*/ | ||
public function store($format = 'xml', $filename = 'sitemap') | ||
{ | ||
// turn off caching | ||
// turn off caching for this method | ||
$this->model->setUseCache(false); | ||
|
||
// use correct file extension | ||
|
@@ -238,14 +274,25 @@ public function store($format = 'xml', $filename = 'sitemap') | |
// check if this sitemap have more than 50000 elements | ||
if (count($this->model->getItems()) > 50000) | ||
{ | ||
foreach (array_chunk($this->model->getItems(), 50000) as $key => $item) | ||
// check if limiting size of items array is enabled | ||
if (!$this->model->getUseLimitSize()) | ||
{ | ||
$this->model->items = $item; | ||
$this->store($format, $filename . '-' . $key); | ||
$this->addSitemap(url($filename . '-' . $key . '.' . $fe)); | ||
} | ||
// use sitemapindex and generate partial sitemaps | ||
foreach (array_chunk($this->model->getItems(), 50000) as $key => $item) | ||
{ | ||
$this->model->resetItems($item); | ||
$this->store($format, $filename . '-' . $key); | ||
$this->addSitemap(url($filename . '-' . $key . '.' . $fe)); | ||
} | ||
|
||
$data = $this->generate('sitemapindex'); | ||
$data = $this->generate('sitemapindex'); | ||
} | ||
else | ||
{ | ||
// reset items and use only most recent 50000 items | ||
$this->model->limitSize(); | ||
$data = $this->generate($format); | ||
} | ||
} | ||
else | ||
{ | ||
|
@@ -264,26 +311,17 @@ public function store($format = 'xml', $filename = 'sitemap') | |
return "Error! Your sitemap file is NOT created."; | ||
} | ||
|
||
// clear | ||
($format == 'sitemapindex') ? $this->model->sitemaps = [] : $this->model->items = []; | ||
} | ||
|
||
/** | ||
* Check if content is cached | ||
* | ||
* @return bool | ||
*/ | ||
public function isCached() | ||
{ | ||
if ($this->model->getUseCache()) | ||
// clear memory | ||
if ($format == 'sitemapindex') | ||
{ | ||
if (Cache::has($this->model->getCacheKey())) | ||
{ | ||
return true; | ||
} | ||
$this->model->resetSitemaps(); | ||
$this->model->resetItems(); | ||
} | ||
else | ||
{ | ||
$this->model->resetItems(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} | ||
|
||
} |