Commit 6a579bd2 authored by andrei.dincu's avatar andrei.dincu

Issue #2249723 by andrei.dincu: xmlsitemap-regenerate drush command 90% works

parent d05275ba
......@@ -6,7 +6,6 @@
*
* @ingroup xmlsitemap
*/
use Drupal\Core\Language\LanguageInterface;
use Drupal\xmlsitemap\XmlSitemapInterface;
use Drupal\xmlsitemap\Entity\XmlSitemap;
......@@ -56,9 +55,8 @@ function _xmlsitemap_regenerate_before() {
if (\Drupal::config('xmlsitemap.settings')->get('developer_mode')) {
watchdog('xmlsitemap', 'Starting XML sitemap generation. Memory usage: @memory-peak.', array(
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
),
WATCHDOG_DEBUG
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
), WATCHDOG_DEBUG
);
}
}
......@@ -140,7 +138,7 @@ function xmlsitemap_generate_page(XmlSitemapInterface $sitemap, $page) {
return $writer->getSitemapElementCount();
}
function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer, $chunk) {
function xmlsitemap_generate_chunk(XmlSitemapInterface $sitemap, XMLSitemapWriter $writer, $chunk) {
$lastmod_format = \Drupal::config('xmlsitemap.settings')->get('lastmod_format');
$url_options = $sitemap->uri['options'];
......@@ -256,7 +254,7 @@ function xmlsitemap_regenerate_batch(array $smids = array()) {
$smids[] = $sitemap->id();
}
}
$t = 't';
$batch = array(
'operations' => array(),
......@@ -289,7 +287,8 @@ function xmlsitemap_regenerate_batch(array $smids = array()) {
*/
function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
if (!isset($context['sandbox']['sitemap'])) {
$sitemap = \Drupal::entityManager()->getStorage('xmlsitemap')->load($smid);
//$sitemap = \Drupal::entityManager()->getStorage('xmlsitemap')->load($smid);
$sitemap = xmlsitemap_sitemap_load($smid);
$context['sandbox']['sitemap'] = $sitemap;
$context['sandbox']['sitemap']->setChunks(1);
$context['sandbox']['sitemap']->setLinks(0);
......@@ -299,7 +298,6 @@ function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
xmlsitemap_check_directory($context['sandbox']['sitemap']);
xmlsitemap_clear_directory($context['sandbox']['sitemap']);
}
$sitemap = &$context['sandbox']['sitemap'];
$links = xmlsitemap_generate_page($sitemap, $sitemap->getChunks());
//$context['message'] = t('Now generating %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap->uri['options'] + array('query' => array('page' => $sitemap->getChunks())))));
......@@ -333,9 +331,9 @@ function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
*/
function xmlsitemap_regenerate_batch_generate_index($smid, array &$context) {
$sitemap = xmlsitemap_sitemap_load($smid);
if ($sitemap->getChunks() > 1) {
if ($sitemap != NULL && $sitemap->getChunks() > 1) {
xmlsitemap_generate_index($sitemap);
//$context['message'] = t('Now generating sitemap index %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap->uri['options'])));
$context['message'] = t('Now generating sitemap index %sitemap-url.', array('%sitemap-url' => url('sitemap.xml', $sitemap->uri['options'])));
}
}
......@@ -347,13 +345,10 @@ function xmlsitemap_regenerate_batch_finished($success, $results, $operations, $
\Drupal::config('xmlsitemap.settings')->set('generated_last', REQUEST_TIME);
drupal_set_message(t('The sitemaps were regenerated.'));
// Show a watchdog message that the sitemap was regenerated.
watchdog('xmlsitemap',
'Finished XML sitemap generation in @elapsed. Memory usage: @memory-peak.',
array(
'@elapsed' => $elapsed,
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
),
WATCHDOG_NOTICE
watchdog('xmlsitemap', 'Finished XML sitemap generation in @elapsed. Memory usage: @memory-peak.', array(
'@elapsed' => $elapsed,
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
), WATCHDOG_NOTICE
);
}
else {
......@@ -399,7 +394,7 @@ function xmlsitemap_rebuild_batch(array $entities, $save_custom = FALSE) {
*/
function xmlsitemap_batch_variable_set(array $variables) {
foreach ($variables as $variable => $value) {
\Drupal::config('xmlsitemap.settings')->set($variable,$value);
\Drupal::config('xmlsitemap.settings')->set($variable, $value);
}
}
......
......@@ -371,7 +371,6 @@ function xmlsitemap_sitemap_load_multiple($smids = array(), array $conditions =
}
$sitemaps = $query->execute()->fetchAllAssoc('smid');
*/
$sitemaps = array();
foreach ($sitemaps as $sitemap) {
$sitemaps[$sitemap->id()]->setContext(unserialize($sitemap->getContext()));
$sitemaps[$sitemap->id()]->uri = xmlsitemap_sitemap_uri($sitemap);
......@@ -408,14 +407,15 @@ function xmlsitemap_sitemap_load_by_context(array $context = NULL) {
* @todo Save the sitemap's URL as a column?
*/
function xmlsitemap_sitemap_save(XmlSitemapInterface $sitemap) {
if (!isset($sitemap->getContext())) {
$context = $sitemap->getContext();
if (!isset($context) || !$context) {
$sitemap->setContext(array());
}
// Make sure context is sorted before saving the hash.
//$sitemap->is_new = empty($sitemap->getId());
//$sitemap->old_smid = $sitemap->is_new ? NULL : $sitemap->smid;
$sitemap->setOriginalId($sitemap->isNew()? NULL : $sitemap->getId());
$sitemap->setOriginalId($sitemap->isNew() ? NULL : $sitemap->getId());
//$sitemap->smid = xmlsitemap_sitemap_get_context_hash($sitemap->context);
$sitemap->setId(xmlsitemap_sitemap_get_context_hash($sitemap->getContext()));
// If the context was changed, we need to perform additional actions.
......@@ -426,25 +426,10 @@ function xmlsitemap_sitemap_save(XmlSitemapInterface $sitemap) {
$new_dir = xmlsitemap_get_directory($sitemap);
xmlsitemap_directory_move($old_dir, $new_dir);
// Change the smid field so drupal_write_record() does not fail.
/*db_update('xmlsitemap_sitemap')
->fields(array('smid' => $sitemap->smid))
->condition('smid', $sitemap->old_smid)
->execute();
*/
// Mark the sitemaps as needing regeneration.
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE);
}
$sitemap->save();
/*
if ($sitemap->is_new) {
drupal_write_record('xmlsitemap_sitemap', $sitemap);
module_invoke_all('xmlsitemap_sitemap_insert', $sitemap);
}
else {
drupal_write_record('xmlsitemap_sitemap', $sitemap, array('smid'));
module_invoke_all('xmlsitemap_sitemap_update', $sitemap);
}*/
return $sitemap;
}
......@@ -499,13 +484,13 @@ function xmlsitemap_sitemap_get_file(stdClass $sitemap, $chunk = 'index') {
* @param $sitemap
* The XML sitemap array.
*/
function xmlsitemap_sitemap_get_max_filesize(stdClass $sitemap) {
function xmlsitemap_sitemap_get_max_filesize(XmlSitemapInterface $sitemap) {
$dir = xmlsitemap_get_directory($sitemap);
$sitemap->max_filesize = 0;
$sitemap->setMaxFileSize(0);
foreach (file_scan_directory($dir, '/\.xml$/') as $file) {
$sitemap->max_filesize = max($sitemap->max_filesize, filesize($file->uri));
$sitemap->setMaxFileSize(max($sitemap->getMaxFileSize(), filesize($file->uri)));
}
return $sitemap->max_filesize;
return $sitemap->getMaxFileSize();
}
function xmlsitemap_sitemap_get_context_hash(array &$context) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment