Commit 55c67ba1 authored by Pawel G's avatar Pawel G

Making drush use the batch process correctly.

parent 8caa994d
...@@ -82,7 +82,7 @@ function simple_sitemap_schema() { ...@@ -82,7 +82,7 @@ function simple_sitemap_schema() {
*/ */
function simple_sitemap_install() { function simple_sitemap_install() {
$sitemap = new Simplesitemap; $sitemap = new Simplesitemap;
$sitemap->generate_sitemap('drush'); $sitemap->generate_sitemap('cron');
$base_url = $GLOBALS['base_url']; $base_url = $GLOBALS['base_url'];
drupal_set_message(t("You can now include content into the <a href='@sitemap_url' target='_blank'>XML sitemap</a> by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url' target='_blank'>content type</a>, <a href='@taxonomy_vocabulary_url' target='_blank'>taxonomy vocabulary</a>, <a href='@menu_url' target='_blank'>menu</a> and <a href='@user_url' target='_blank'>user</a> pages.)<br/>Custom links can be added on <a href='@config_url' target='_blank'>this configuration page</a>.", array('@sitemap_url' => "$base_url/sitemap.xml", '@content_type_url' => "$base_url/admin/structure/types", '@taxonomy_vocabulary_url' => "$base_url/admin/structure/taxonomy", '@menu_url' => "$base_url/admin/structure/menu", '@user_url' => "$base_url/admin/config/people/accounts", '@config_url' => "$base_url/admin/config/search/simplesitemap/custom"))); drupal_set_message(t("You can now include content into the <a href='@sitemap_url' target='_blank'>XML sitemap</a> by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url' target='_blank'>content type</a>, <a href='@taxonomy_vocabulary_url' target='_blank'>taxonomy vocabulary</a>, <a href='@menu_url' target='_blank'>menu</a> and <a href='@user_url' target='_blank'>user</a> pages.)<br/>Custom links can be added on <a href='@config_url' target='_blank'>this configuration page</a>.", array('@sitemap_url' => "$base_url/sitemap.xml", '@content_type_url' => "$base_url/admin/structure/types", '@taxonomy_vocabulary_url' => "$base_url/admin/structure/taxonomy", '@menu_url' => "$base_url/admin/structure/menu", '@user_url' => "$base_url/admin/config/people/accounts", '@config_url' => "$base_url/admin/config/search/simplesitemap/custom")));
} }
......
...@@ -67,7 +67,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) { ...@@ -67,7 +67,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
if (isset($form['actions']['submit']['#submit'])) { if (isset($form['actions']['submit']['#submit'])) {
$form['actions']['submit']['#submit'][] = 'simple_sitemap_entity_form_submit'; $form['actions']['submit']['#submit'][] = 'simple_sitemap_entity_form_submit';
} }
else { // Fix for account page which rendered other submit handlers not usable. else { // Fix for account page rendering other submit handlers not usable.
$form['#submit'][] = 'simple_sitemap_entity_form_submit'; $form['#submit'][] = 'simple_sitemap_entity_form_submit';
} }
} }
...@@ -128,7 +128,7 @@ function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) { ...@@ -128,7 +128,7 @@ function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
unset($entity_types[$bundle_entity_type_id][$bundle]); unset($entity_types[$bundle_entity_type_id][$bundle]);
$sitemap->save_config('entity_types', $entity_types); $sitemap->save_config('entity_types', $entity_types);
drupal_set_message(t("You may want to <a href='@url'>regenerate</a> your XML sitemap now. drupal_set_message(t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.
Otherwise it will be rebuilt on the next cron run.", Otherwise it will be rebuilt on the next cron run.", //todo: First check if cron generation is enabled.
array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'))); array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap')));
} }
} }
...@@ -20,6 +20,7 @@ class Batch { ...@@ -20,6 +20,7 @@ class Batch {
const PATH_DOES_NOT_EXIST_OR_NO_ACCESS = "The path @faulty_path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users."; const PATH_DOES_NOT_EXIST_OR_NO_ACCESS = "The path @faulty_path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users.";
const ANONYMOUS_USER_ID = 0; const ANONYMOUS_USER_ID = 0;
function __construct($from = 'form') { function __construct($from = 'form') {
$this->batch = array( $this->batch = array(
'title' => t('Generating XML sitemap'), 'title' => t('Generating XML sitemap'),
...@@ -35,6 +36,7 @@ class Batch { ...@@ -35,6 +36,7 @@ class Batch {
'batch_process_limit' => $config['batch_process_limit'], 'batch_process_limit' => $config['batch_process_limit'],
'max_links' => $config['max_links'], 'max_links' => $config['max_links'],
'remove_duplicates' => $config['remove_duplicates'], 'remove_duplicates' => $config['remove_duplicates'],
'anonymous_user_account' => User::load(self::ANONYMOUS_USER_ID),
); );
} }
...@@ -44,11 +46,14 @@ class Batch { ...@@ -44,11 +46,14 @@ class Batch {
case 'form': case 'form':
break; break;
case 'drush': case 'drush':
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
drush_backend_batch_process();
break;
case 'cron': case 'cron':
$this->batch =& batch_get(); $this->batch =& batch_get();
$this->batch['progressive'] = FALSE; $this->batch['progressive'] = FALSE;
//drush_backend_batch_process(); // Seems to be working, but process does not stop. batch_process();
batch_process(); // Works, but less fancy.
break; break;
} }
} }
...@@ -174,7 +179,7 @@ class Batch { ...@@ -174,7 +179,7 @@ class Batch {
$url_object = Url::fromRoute($route_name, $route_parameters, $options); $url_object = Url::fromRoute($route_name, $route_parameters, $options);
$access = self::access($url_object, User::load(self::ANONYMOUS_USER_ID)); $access = self::access($url_object, $batch_info['anonymous_user_account']);
if (!$access) { if (!$access) {
continue; continue;
} }
...@@ -213,19 +218,19 @@ class Batch { ...@@ -213,19 +218,19 @@ class Batch {
if ($context['sandbox']['progress'] != $context['sandbox']['max']) { if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
// Adding processing message after finishing every part of the batch. // Adding processing message after finishing every part of the batch.
if (isset($context['results'][key($context['results'])]['path'])) { if (!empty($context['results'][key($context['results'])]['path'])) {
$last_path = HTML::escape($context['results'][key($context['results'])]['path']); $last_path = HTML::escape($context['results'][key($context['results'])]['path']);
$context['message'] = t("Processing path @current out of @max: @path", array( $context['message'] = t("Processing path @current out of @max: @path", array(
'@current' => $context['sandbox']['progress'], '@current' => $context['sandbox']['progress'],
'@max' => $context['sandbox']['max'], '@max' => $context['sandbox']['max'],
'@path' => $last_path, '@path' => $last_path,
)); ));
switch($batch_info['from']) { // switch($batch_info['from']) { //todo: add shell output
case 'drush': // case 'drush':
print $context['message'] . "\r\n"; // print $context['message'] . "\r\n";
break; // break;
default: // default:
} // }
} }
} }
...@@ -262,13 +267,13 @@ class Batch { ...@@ -262,13 +267,13 @@ class Batch {
foreach($custom_paths as $i => $custom_path) { foreach($custom_paths as $i => $custom_path) {
$user_input = $custom_path['path'][0] === '/' ? $custom_path['path'] : '/' . $custom_path['path']; $user_input = $custom_path['path'][0] === '/' ? $custom_path['path'] : '/' . $custom_path['path'];
if (!\Drupal::service('path.validator')->isValid($custom_path['path'])) { //todo: change to different function, as this also checks if current user has access. The user however varies depending if process started from the web interface or via cron/drush. if (!\Drupal::service('path.validator')->isValid($custom_path['path'])) { //todo: Change to different function, as this also checks if current user has access. The user however varies depending if process was started from the web interface or via cron/drush.
self::register_error(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS, array('@faulty_path' => $custom_path['path']), 'warning'); self::register_error(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS, array('@faulty_path' => $custom_path['path']), 'warning');
continue; continue;
} }
$url_object = Url::fromUserInput($user_input, $options); $url_object = Url::fromUserInput($user_input, $options);
$access = self::access($url_object, User::load(self::ANONYMOUS_USER_ID)); $access = self::access($url_object, $batch_info['anonymous_user_account']);
if (!$access) { if (!$access) {
continue; continue;
} }
......
...@@ -42,7 +42,7 @@ class Simplesitemap { ...@@ -42,7 +42,7 @@ class Simplesitemap {
$plugins = $manager->getDefinitions(); $plugins = $manager->getDefinitions();
// Go through simple_sitemap plugins and check if one of them declares usage // Go through simple_sitemap plugins and check if one of them declares usage
// of this particular form. If that's the case, get entity type id of the // of this particular form. If that's the case, get the entity type id of the
// plugin definition and assume the bundle to be of the same name as the // plugin definition and assume the bundle to be of the same name as the
// entity type id. // entity type id.
foreach($plugins as $plugin) { foreach($plugins as $plugin) {
......
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