We are using VBO on Drupal.org to mass delete nodes created by specific user. It turns out no watchdog entry is being created on such bulk deletions, therefore we can't find out who deleted something if there was a mistake.

Steps to reproduce:
1. Bulk delete nodes.
2. Go to watchdog and check if any new message appeared.

We can provide D.o dev site for testing.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojanz’s picture

Category: Bug report » Support request
Status: Active » Fixed

VBO doesn't log to watchdog any kind of operation.
It also has no way of knowing all of the ids that were processed, since there could theoretically be 100k.
I suggest implementing hook_entity_delete / hook_node_delete and logging from there.

tvn’s picture

Thanks! Sorry for useless issue. :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Steven Jones’s picture

Status: Closed (fixed) » Active

Sorry for re-opening, but I just want to clarify why VBO doesn't/can't log what it's doing.

If we look at the specific VBO action we're talking about here, delete:

http://cgit.drupalcode.org/views_bulk_operations/tree/actions/delete.act...

Then we can see that of course, VBO does know the ID that's being processed, because it doesn't just delete a random entity.

Would you accept a patch that added configurable (on/off) logging to the delete action (at least) so that VBO logs in a similar way to deleting an entity through other admin UIs?
Otherwise, we'll have to write our own action that's basically the same, but logs.

bojanz’s picture

Well yes, I meant logging all ids at once.
VBO is frequently used to delete thousands of entities, having thousands of matching new entries in watchdog sounds very impractical.
I would accept such a patch, if the option is disabled by default.

Steven Jones’s picture

While I agree about the thousands of extra log messages, VBO is often used to replace an admin UI where people will only be deleting 10s of entities at a time.

I'll look into providing a patch.

Thanks!

Steven Jones’s picture

Category: Support request » Feature request
Status: Active » Needs review
FileSize
2.08 KB

Okay, here's an initial patch, to get some feedback on the approach etc.

This adds optional logging to just the delete_item action.

bojanz’s picture

Status: Needs review » Needs work
+  // We'll need the entity around if we want to log the deletion later.
+  if (!empty($context['settings']['log'])) {
+    $entity = entity_load_single($context['entity_type'], $entity_id);
+  }

We already have the entity, why are we loading it again?

+    // Log an appropriate message for this entity type.
+    switch ($context['entity_type']) {
+      case 'node':
+        watchdog('content', '@type: deleted %title.', array('@type' => $entity->type, '%title' => $entity->title));
+        break;
+
+      case 'taxonomy_term':
+        watchdog('taxonomy', 'Deleted term %name.', array('%name' => $entity->name), WATCHDOG_NOTICE);
+        break;
+
+      case 'user':
+        watchdog('user', 'Deleted user: %name %email.', array('%name' => $entity->name, '%email' => '<' . $entity->mail . '>'), WATCHDOG_NOTICE);
+        break;
+
+      default:
+        watchdog('entity', 'Deleted entity: type: %type, id: %id.', array('%type' => $context['entity_type'], '%id' => $entity_id));
+        break;
+
+    }

No need to switch, we know the entity_type and we can use entity_label() to get the label of the entity. "%name %email" for users is the only special case here, which we might want to keep (or fix so that the default entity label looks like that)

Steven Jones’s picture

Assigned: Unassigned » Steven Jones

Oh yeah, didn't notice that we did already have the entity around.

As for the different messages, I was making it so they match the relevant modules log messages when bulk deleting in them, hence the switch.

I'll work on the improvements, and provide a new patch.

Steven Jones’s picture

Status: Needs work » Needs review
FileSize
2 KB

Here's an improved patch, we don't re-load the entity, we use entity_label and I've added a little more documentation to explain why we use the switch on the format of log messages.

klidifia’s picture

This is good, I don't think it matters if there's thousands of logs, could be important! Just a question about your watchdog calls though:

watchdog('content', '@type: deleted %title.', array('@type' => $entity->type, '%title' => $entity->title));
watchdog('taxonomy', 'Deleted term %name.', array('%name' => $entity->name), WATCHDOG_NOTICE);
watchdog('user', 'Deleted user: %name %email.', array('%name' => $entity->name, '%email' => '<' . $entity->mail . '>'), WATCHDOG_NOTICE);
watchdog('entity', 'Deleted @type %label.', array('@type' => $context['entity_type'], '%label' => entity_label($context['entity_type'], $entity)));

Is there a reason the "severity" is set only on the taxonomy and user ones? WATCHDOG_NOTICE is the default.

Would it be possible to add in a "link" for them, something like: l('view', 'node/' . $entity->entity_id));

Steven Jones’s picture

@klidifia I just copied the relevant lines from the core modules that do that logging, so I'm just copying what core does. Adding a link would be bit redundant because the item has been deleted, so the link would be an immediate 404.

klidifia’s picture

Good point. What about the configurability of this. Is it something that perhaps the administrator of the site can set rather than the person performing the action deciding whether to log it.

The issue I am trying to solve here along the lines of the initial one is if someone deletes some stuff using VBO by accident. (Or malicious - in which case they would choose not to log it).

Steven Jones’s picture

Assigned: Steven Jones » Unassigned
Peter Majmesku’s picture

How about this issue now? The last post is 1 year old and no logging for deletion of nodes is an important aspect from my point of view. Will the suggested code land in the release?

Steven Jones’s picture

@jepSter have you had chance to review the patch in #10? Does it work for you?

Peter Majmesku’s picture

FileSize
148.08 KB

@Steven Jones:
Sorry, but I cannot bring it to work.

What have I tried? I've applied the patch, flushed the cache. Afterwards I was searching for the settings form, to enable your functionality. But I couldn't find it. The code for the settings form inside your patch is as follows (it would be good, if it had a comment about the path to the settings form):

/**
 * Admin settings form for the delete_item bulk operation.
 */
function views_bulk_operations_delete_item_views_bulk_operations_form($settings) {
  $form = array();

  $form['log'] = array(
    '#type' => 'checkbox',
    '#title' => t('Log deletions'),
    '#description' => t('Log individual deletions to the watchdog. Deleting large amounts of entities will generate large amounts of log messages.'),
    '#default_value' => !empty($settings['log']),
  );

  return $form;
}

Since it seems to be a "action", I was searching for the actions settings form for it. But I couldn't find anything about it:
Screenshot

Steven Jones’s picture

@jepSter ah, that's the core 'actions' UI.

You want to edit the view that you're adding Views Bulk Operations to, and where you can select the VBO actions available, you should be able to configure this one.

Peter Majmesku’s picture

FileSize
185.18 KB

Thanks for your fast reply.

Where can I find the setting inside views? I was searching for any "field" but there is no setting for it. Here's the screenshot from the default administration page for nodes:
View

Steven Jones’s picture

@jepSter if you click on 'Bulk operations: Content' isn't there some config options in there?

Peter Majmesku’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
129.47 KB
195 KB

@Steven Jones: Thank you! I can confirm that your patch works. I hope that your patch will go into the admin views module code with some description. So that people don't have to google around. ;)

See below the screenshots from my test:
Config

Log

Changed state to "Reviewd & tested by the community".

Steven Jones’s picture

@jepSter thank you for the review! Glad you found the patch useful in the end.

Peter Majmesku’s picture

You're welcome! :)

Will anybody apply the patch into the VBO module?

bojanz’s picture

Status: Reviewed & tested by the community » Fixed

Tweaked the look of the setting, and committed. Thanks!

  • bojanz committed f65fb0f on 7.x-3.x authored by Steven Jones
    Issue #2282079 by Steven Jones: No watchdog entry created on bulk node...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Peter Majmesku’s picture

Wouldn't it make sense to enable this setting by default if watchdog (dblog module) is switched on? I have no idea, why this behavior should be an option.

Peter Majmesku’s picture

Status: Closed (fixed) » Active
bojanz’s picture

Status: Active » Closed (fixed)

See #5

Peter Majmesku’s picture

@bojanz: Ah, good point with the mass-deletion.

I've opened an issue at https://www.drupal.org/node/2638108 for a feature improvement plan to discuss.