/vendor/roumen/sitemap/src/Roumen/Sitemap/Sitemap.php

https://bitbucket.org/chillidee/myplaymate.com.au · PHP · 301 lines · 179 code · 47 blank · 75 comment · 32 complexity · d720a9493da2bb82499a062288ef9913 MD5 · raw file

  1. <?php
  2. namespace Roumen\Sitemap;
  3. /**
  4. * Sitemap class for laravel-sitemap package.
  5. *
  6. * @author Roumen Damianoff <roumen@dawebs.com>
  7. * @version 2.4.17
  8. * @link http://roumen.it/projects/laravel-sitemap
  9. * @license http://opensource.org/licenses/mit-license.php MIT License
  10. */
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\Config;
  13. use Illuminate\Support\Facades\File;
  14. use Illuminate\Support\Facades\Response;
  15. use Illuminate\Support\Facades\View;
  16. class Sitemap
  17. {
  18. /**
  19. * Model instance
  20. * @var Model $model
  21. */
  22. public $model = null;
  23. /**
  24. * Using constructor we populate our model from configuration file
  25. * @param array $config
  26. */
  27. public function __construct(array $config)
  28. {
  29. $this->model = new Model($config);
  30. }
  31. /**
  32. * Set cache options
  33. *
  34. * @param string $key
  35. * @param Carbon|Datetime|int $duration
  36. * @param boolean $useCache
  37. */
  38. public function setCache($key = null, $duration = null, $useCache = true)
  39. {
  40. $this->model->setUseCache($useCache);
  41. if ($key !== null)
  42. {
  43. $this->model->setCacheKey($key);
  44. }
  45. if ($duration !== null)
  46. {
  47. $this->model->setCacheDuration($duration);
  48. }
  49. }
  50. /**
  51. * Add new sitemap item to $items array
  52. *
  53. * @param string $loc
  54. * @param string $lastmod
  55. * @param string $priority
  56. * @param string $freq
  57. * @param array $images
  58. * @param string $title
  59. * @param array $translations
  60. * @param array $googlenews
  61. *
  62. * @return void
  63. */
  64. public function add($loc, $lastmod = null, $priority = null, $freq = null, $images = array(), $title = null, $translations = array(), $videos = array(), $googlenews = array())
  65. {
  66. if ($this->model->getEscaping())
  67. {
  68. $loc = htmlentities($loc, ENT_XML1);
  69. if ($title != null) htmlentities($title, ENT_XML1);
  70. if ($images)
  71. {
  72. foreach ($images as $k => $image)
  73. {
  74. foreach ($image as $key => $value)
  75. {
  76. $images[$k][$key] = htmlentities($value, ENT_XML1);
  77. }
  78. }
  79. }
  80. if ($translations)
  81. {
  82. foreach($translations as $k => $translation)
  83. {
  84. foreach ($translation as $key => $value)
  85. {
  86. $translations[$k][$key] = htmlentities($value, ENT_XML1);
  87. }
  88. }
  89. }
  90. if ($videos)
  91. {
  92. foreach ($videos as $k => $video)
  93. {
  94. if ($video['title']) $videos[$k]['title'] = htmlentities($video['title'], ENT_XML1);
  95. if ($video['description']) $videos[$k]['description'] = htmlentities($video['description'], ENT_XML1);
  96. }
  97. }
  98. if ($googlenews)
  99. {
  100. if (isset($googlenews['sitename'])) $googlenews['sitename'] = htmlentities($googlenews['sitename'], ENT_XML1);
  101. }
  102. }
  103. $googlenews['sitename'] = isset($googlenews['sitename']) ? $googlenews['sitename'] : '';
  104. $googlenews['language'] = isset($googlenews['language']) ? $googlenews['language'] : 'en';
  105. $googlenews['pubication_date'] = isset($googlenews['pubication_date']) ? $googlenews['pubication_date'] : date('Y-m-d H:i:s');
  106. $this->model->setItems(
  107. array(
  108. 'loc' => $loc,
  109. 'lastmod' => $lastmod,
  110. 'priority' => $priority,
  111. 'freq' => $freq,
  112. 'images' => $images,
  113. 'title' => $title,
  114. 'translations' => $translations,
  115. 'videos' => $videos,
  116. 'googlenews' => $googlenews
  117. )
  118. );
  119. }
  120. /**
  121. * Add new sitemap to $sitemaps array
  122. *
  123. * @param string $loc
  124. * @param string $lastmod
  125. *
  126. * @return void
  127. */
  128. public function addSitemap($loc, $lastmod = null)
  129. {
  130. $this->model->setSitemaps(
  131. array(
  132. 'loc' => $loc,
  133. 'lastmod' => $lastmod
  134. )
  135. );
  136. }
  137. /**
  138. * Returns document with all sitemap items from $items array
  139. *
  140. * @param string $format (options: xml, html, txt, ror-rss, ror-rdf, google-news)
  141. *
  142. * @return View
  143. */
  144. public function render($format = 'xml')
  145. {
  146. $data = $this->generate($format);
  147. if($format=='html')
  148. {
  149. return $data['content'];
  150. }
  151. return Response::make($data['content'], 200, $data['headers']);
  152. }
  153. /**
  154. * Generates document with all sitemap items from $items array
  155. *
  156. * @param string $format (options: xml, html, txt, ror-rss, ror-rdf, sitemapindex, google-news)
  157. *
  158. * @return array
  159. */
  160. public function generate($format = 'xml')
  161. {
  162. if ($this->isCached())
  163. {
  164. ($format == 'sitemapindex') ? $this->model->sitemaps = Cache::get($this->model->getCacheKey()) : $this->model->items = Cache::get($this->model->getCacheKey());
  165. } elseif ($this->model->getUseCache())
  166. {
  167. ($format == 'sitemapindex') ? Cache::put($this->model->getCacheKey(), $this->model->getSitemaps(), $this->model->getCacheDuration()) : Cache::put($this->model->getCacheKey(), $this->model->getItems(), $this->model->getCacheDuration());
  168. }
  169. if (!$this->model->getLink())
  170. {
  171. $this->model->setLink(Config::get('app.url'));
  172. }
  173. if (!$this->model->getTitle())
  174. {
  175. $this->model->setTitle(('Sitemap for ' . $this->model->getLink()));
  176. }
  177. $channel = array(
  178. 'title' => $this->model->getTitle(),
  179. 'link' => $this->model->getLink()
  180. );
  181. switch ($format)
  182. {
  183. case 'ror-rss':
  184. return array('content' => View::make('sitemap::ror-rss', array('items' => $this->model->getItems(), 'channel' => $channel))->render(), 'headers' => array('Content-type' => 'text/rss+xml; charset=utf-8'));
  185. case 'ror-rdf':
  186. return array('content' => View::make('sitemap::ror-rdf', array('items' => $this->model->getItems(), 'channel' => $channel))->render(), 'headers' => array('Content-type' => 'text/rdf+xml; charset=utf-8'));
  187. case 'html':
  188. return array('content' => View::make('sitemap::html', array('items' => $this->model->getItems(), 'channel' => $channel))->render(), 'headers' => array('Content-type' => 'text/html'));
  189. case 'txt':
  190. return array('content' => View::make('sitemap::txt', array('items' => $this->model->getItems()))->render(), 'headers' => array('Content-type' => 'text/plain'));
  191. case 'sitemapindex':
  192. return array('content' => View::make('sitemap::sitemapindex', array('sitemaps' => $this->model->getSitemaps()))->render(), 'headers' => array('Content-type' => 'text/xml; charset=utf-8'));
  193. default:
  194. return array('content' => View::make('sitemap::xml', array('items' => $this->model->getItems()))->render(), 'headers' => array('Content-type' => 'text/xml; charset=utf-8'));
  195. }
  196. }
  197. /**
  198. * Generate sitemap and store it to a file
  199. *
  200. * @param string $format (options: xml, html, txt, ror-rss, ror-rdf, sitemapindex, google-news)
  201. * @param string $filename (without file extension, may be a path like 'sitemaps/sitemap1' but must exist)
  202. *
  203. * @return void
  204. */
  205. public function store($format = 'xml', $filename = 'sitemap')
  206. {
  207. // check if this sitemap have more than 50000 elements
  208. if (count($this->model->getItems()) > 50000)
  209. {
  210. foreach (array_chunk($this->model->getItems(), 50000) as $key => $item)
  211. {
  212. $this->model->items = $item;
  213. $this->store('xml', $filename . '-' . $key);
  214. $this->addSitemap(url($filename . '-' . $key . '.xml'));
  215. }
  216. $data = $this->generate('sitemapindex');
  217. }
  218. else
  219. {
  220. $data = $this->generate($format);
  221. }
  222. if ($format == 'ror-rss' || $format == 'ror-rdf' || $format == 'sitemapindex' || $format == 'google-news')
  223. {
  224. $format = 'xml';
  225. }
  226. $file = public_path() . DIRECTORY_SEPARATOR . $filename . '.' . $format;
  227. // must return something
  228. if (File::put($file, $data['content']))
  229. {
  230. return "Success! Your sitemap file is created.";
  231. } else
  232. {
  233. return "Error! Your sitemap file is NOT created.";
  234. }
  235. // clear
  236. ($format == 'sitemapindex') ? $this->model->sitemaps = array() : $this->model->items = array();
  237. }
  238. /**
  239. * Check if content is cached
  240. *
  241. * @return bool
  242. */
  243. public function isCached()
  244. {
  245. if ($this->model->getUseCache())
  246. {
  247. if (Cache::has($this->model->getCacheKey()))
  248. {
  249. return true;
  250. }
  251. }
  252. return false;
  253. }
  254. }