Exemplo n.º 1
0
 /**
  * Generate sitemap and store it to a file
  *
  * @param string $format (options: xml, html, txt, ror-rss, ror-rdf, sitemapindex, google-news)
  * @param string $filename (without file extension, may be a path like 'sitemaps/sitemap1' but must exist)
  *
  * @return void
  */
 public function store($format = 'xml', $filename = 'sitemap')
 {
     // turn off caching for this method
     $this->model->setUseCache(false);
     // use correct file extension
     $format == 'txt' || $format == 'html' ? $fe = $format : ($fe = 'xml');
     // check if this sitemap have more than 50000 elements (or 1000 if is google-news)
     if ($format != "google-news" && count($this->model->getItems()) > 50000 || $format == "google-news" && count($this->model->getItems()) > 1000) {
         $format != "google-news" ? $max = 50000 : ($max = 1000);
         // check if limiting size of items array is enabled
         if (!$this->model->getUseLimitSize()) {
             // use sitemapindex and generate partial sitemaps
             foreach (array_chunk($this->model->getItems(), $max) as $key => $item) {
                 $this->model->resetItems($item);
                 $this->store($format, $filename . '-' . $key);
                 $this->addSitemap(url($filename . '-' . $key . '.' . $fe));
             }
             $data = $this->generate('sitemapindex');
         } else {
             // reset items and use only most recent $max items
             $this->model->limitSize($max);
             $data = $this->generate($format);
         }
     } else {
         $data = $this->generate($format);
     }
     $file = public_path() . DIRECTORY_SEPARATOR . $filename . '.' . $fe;
     // must return something
     if (File::put($file, $data['content'])) {
         return "Success! Your sitemap file is created.";
     } else {
         return "Error! Your sitemap file is NOT created.";
     }
     // clear memory
     if ($format == 'sitemapindex') {
         $this->model->resetSitemaps();
         $this->model->resetItems();
     } else {
         $this->model->resetItems();
     }
 }
Exemplo n.º 2
0
 /**
  * Generate sitemap and store it to a file
  *
  * @param string $format (options: xml, html, txt, ror-rss, ror-rdf, sitemapindex, google-news)
  * @param string $filename (without file extension, may be a path like 'sitemaps/sitemap1' but must exist)
  * @param string $path (path to store sitemap like '/www/site/public')
  * @param string $style (path to custom xls style like '/styles/xsl/xml-sitemap.xsl')
  *
  * @return void
  */
 public function store($format = 'xml', $filename = 'sitemap', $path = null, $style = null)
 {
     // turn off caching for this method
     $this->model->setUseCache(false);
     // use correct file extension
     $format == 'txt' || $format == 'html' ? $fe = $format : ($fe = 'xml');
     // use custom size limit for sitemaps
     if ($this->model->getMaxSize() > 0 && count($this->model->getItems()) > $this->model->getMaxSize()) {
         if ($this->model->getUseLimitSize()) {
             // limit size
             $this->model->limitSize($this->model->getMaxSize());
             $data = $this->generate($format, $style);
         } else {
             // use sitemapindex and generate partial sitemaps
             foreach (array_chunk($this->model->getItems(), $this->model->getMaxSize()) as $key => $item) {
                 // reset current items
                 $this->model->resetItems($item);
                 // generate new partial sitemap
                 $this->store($format, $filename . '-' . $key, $path, $style);
                 // add sitemap to sitemapindex
                 if ($path != null) {
                     // if using custom path generate relative urls for sitemaps in the sitemapindex
                     $this->addSitemap($filename . '-' . $key . '.' . $fe);
                 } else {
                     // else generate full urls based on app's domain
                     $this->addSitemap(url($filename . '-' . $key . '.' . $fe));
                 }
             }
             $data = $this->generate('sitemapindex', $style);
         }
     } else {
         if ($format != "google-news" && count($this->model->getItems()) > 50000 || $format == "google-news" && count($this->model->getItems()) > 1000) {
             $format != "google-news" ? $max = 50000 : ($max = 1000);
             // check if limiting size of items array is enabled
             if (!$this->model->getUseLimitSize()) {
                 // use sitemapindex and generate partial sitemaps
                 foreach (array_chunk($this->model->getItems(), $max) as $key => $item) {
                     // reset current items
                     $this->model->resetItems($item);
                     // generate new partial sitemap
                     $this->store($format, $filename . '-' . $key, $path, $style);
                     // add sitemap to sitemapindex
                     if ($path != null) {
                         // if using custom path generate relative urls for sitemaps in the sitemapindex
                         $this->addSitemap($filename . '-' . $key . '.' . $fe);
                     } else {
                         // else generate full urls based on app's domain
                         $this->addSitemap(url($filename . '-' . $key . '.' . $fe));
                     }
                 }
                 $data = $this->generate('sitemapindex', $style);
             } else {
                 // reset items and use only most recent $max items
                 $this->model->limitSize($max);
                 $data = $this->generate($format, $style);
             }
         } else {
             $data = $this->generate($format, $style);
         }
     }
     // if custom path
     if ($path == null) {
         $file = public_path() . DIRECTORY_SEPARATOR . $filename . '.' . $fe;
     } else {
         $file = $path . DIRECTORY_SEPARATOR . $filename . '.' . $fe;
     }
     // must return something
     if (File::put($file, $data['content'])) {
         return "Success! Your sitemap file is created.";
     } else {
         return "Error! Your sitemap file is NOT created.";
     }
     // clear memory
     if ($format == 'sitemapindex') {
         $this->model->resetSitemaps();
         $this->model->resetItems();
     } else {
         $this->model->resetItems();
     }
 }