/** * Constructors and XmlSitemapWriter object. * * @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap * The sitemap array. * @param string $page * The current page of the sitemap being generated. */ public function __construct(XmlSitemapInterface $sitemap, $page) { $this->sitemap = $sitemap; $this->sitemap_page = $page; $this->uri = xmlsitemap_sitemap_get_file($sitemap, $page); $this->openUri($this->uri); }
/** * Provides the sitemap in XML format. * * @throws NotFoundHttpException * * @return \Symfony\Component\HttpFoundation\Response * The sitemap in XML format or plain text if xmlsitemap_developer_mode flag * is set. */ public function renderSitemapXml() { $sitemap = $this->entityManager->getStorage('xmlsitemap')->loadByContext(); if (!$sitemap) { throw new NotFoundHttpException(); } $chunk = xmlsitemap_get_current_chunk($sitemap); $file = xmlsitemap_sitemap_get_file($sitemap, $chunk); // Provide debugging information if enabled. if ($this->state->get('xmlsitemap_developer_mode')) { $module_path = drupal_get_path('module', 'xmlsitemap'); $template = $this->twig->loadTemplate($module_path . '/templates/sitemap-developer-mode.html.twig'); $elements = array('current_context' => print_r($context, TRUE), 'sitemap' => print_r($sitemap, TRUE), 'chunk' => $chunk, 'cache_file_location' => $file, 'cache_file_exists' => file_exists($file) ? 'Yes' : 'No'); return new Response($template->render($elements)); } $response = new Response(); return xmlsitemap_output_file($response, $file); }
/** * {@inheritdoc} */ public function regenerateBatchGenerate($smid, array &$context) { if (!isset($context['sandbox']['sitemap'])) { $sitemap = xmlsitemap_sitemap_load($smid); $context['sandbox']['sitemap'] = $sitemap; $context['sandbox']['sitemap']->setChunks(1); $context['sandbox']['sitemap']->setLinks(0); $context['sandbox']['max'] = XMLSITEMAP_MAX_SITEMAP_LINKS; // Clear the cache directory for this sitemap before generating any files. xmlsitemap_check_directory($context['sandbox']['sitemap']); xmlsitemap_clear_directory($context['sandbox']['sitemap']); } $sitemap =& $context['sandbox']['sitemap']; $links = $this->generatePage($sitemap, $sitemap->getChunks()); $context['message'] = t('Now generating %sitemap-url.', array('%sitemap-url' => Url::fromRoute('xmlsitemap.sitemap_xml', [], $sitemap->uri['options'] + array('query' => array('page' => $sitemap->getChunks())))->toString())); if ($links) { $sitemap->setLinks($sitemap->getLinks() + $links); $sitemap->setChunks($sitemap->getChunks() + 1); } else { // Cleanup the 'extra' empty file. $file = xmlsitemap_sitemap_get_file($sitemap, $sitemap->getChunks()); if (file_exists($file) && $sitemap->getChunks() > 1) { file_unmanaged_delete($file); } $sitemap->setChunks($sitemap->getChunks() - 1); // Save the updated chunks and links values. $context['sandbox']['max'] = $sitemap->getChunks(); $sitemap->setUpdated(REQUEST_TIME); xmlsitemap_sitemap_get_max_filesize($sitemap); xmlsitemap_sitemap_save($sitemap); } if ($sitemap->getChunks() != $context['sandbox']['max']) { $context['finished'] = $sitemap->getChunks() / $context['sandbox']['max']; } }