If you use flag with session api, when the session expire the flag is deleted and all the associated data (fields).

This is a huge problem because you are deleting data. Specially if field are associated with the flag.

We lost thousands of thousands of records without knowing it. Now I will have to go throw all the backup and "merge" them...

The problem is with this hook on flag.module

/**
 * Implements hook_session_api_cleanup().
 *
 * Clear out anonymous user flaggings during Session API cleanup.
 */
function flag_session_api_cleanup($arg = 'run') {
  // Session API 1.1 version:
  if ($arg == 'run') {
    $query = db_select('flagging', 'fc');
    $query->leftJoin('session_api', 's', 'fc.sid = s.sid');
    $result = $query
      ->fields('fc', array('sid'))
      ->condition('fc.sid', 0, '<>')
      ->isNull('s.sid')
      ->execute();
    foreach ($result as $row) {
      db_delete('flagging')
        ->condition('sid', $row->sid)
        ->execute();
    }
  }
  // Session API 1.2+ version.
  elseif (is_array($arg)) {
    $outdated_sids = $arg;
    db_delete('flagging')->condition('sid', $outdated_sids, 'IN')->execute();
  }
}

Comments

gagarine’s picture

Title: Fag lose data after session expire » Flag lose data after session expire

flag... not fag... hum.

gagarine’s picture

The same is true for user cancellation. flag_user_cancel should not delete flag. #2489992: Do not delete flaggings on account cancelation

gagarine’s picture

Issue summary: View changes
gagarine’s picture

Title: Flag lose data after session expire » Flag delete data when session expire
joachim’s picture

Git blame (with some digging) tells me this was added in commit:

commit 38fcc3a6316634e92a806147939be43b00940898
Author: Nathan Haug <quicksketch@35821.no-reply.drupal.org>
Date:   Wed Oct 28 00:01:32 2009 +0000

    #271582 by chirale, ultimike, and quicksketch: Anonymous flagging, with support for page cache. w00t!

Presumably, there was a reason for doing this flagging deletion -- that issue should be investigated to see if there's anything that would help understand the rationale.

> We should also avoid deleting data on user cancel. From drupalapi "the module should either do nothing, unpublish content, or anonymize content" -> not delete!

That's debatable, but if you want to file a new issue for that debate, then go ahead :) My point of view is that flaggings aren't really content, they're more seen as user data. But I'd be interested to see what the wider opinion is on this.

At any rate, I really don't think this is behaviour we can change at this point in the cycle...

gagarine’s picture

Issue summary: View changes

I created an other issue for the user cancellation #2489992: Do not delete flaggings on account cancelation.

I think the reason is a wrong assumption than flag don't contain any information if the user doesn't exist anymore. I agree a lot of use case don't care but this is not true at least because:
- flag are fieldable and can contain a lot more than just a link between a user and a content
- keeping flag count is not enough for a lot of use case (content recommendation, statistics, ...) where you want to keep a relation between multiple flag.

We can provide an option to "cleanup" flag when session expire (off by default, eventually on by default for older install).

lightsurge’s picture

I was using this for a "waiting list", unfortunately. Now that these waiting list entries have been quietly removed over time, I also either face having to merge tables from backup, or leave people waiting for a long time.

It would be really useful to know if there would be anything unexpected happen by simply removing this cleanup function in the interim.

lightsurge’s picture

So this cleanup function kicks in when the session api cron job deletes sessions. Session api deletes sessions older than the expiry date of the session cookie, set in admin/config/development/session-api

So I increased the cookie expiry to a year for now.

The default seems to be 30 days (ouch, can't believe my data has only been lasting that long)

indigoxela’s picture

@lightsurge I just realized this behaviour with data loss on expired sessions.

Did you ever find a solution besides setting the session expiry time to a very long delay?

indigoxela’s picture

So what would be a clean solution for that problem?

There are use-cases, where the flagging (and its field data) should not vanish as soon as the session is expired.
But there also might be use-cases, where this is the required behavior.

Possibly a setting on the /admin/structure/flags/manage/[flag name] - along with the access settings a checkbox with something like that:

Should flagging data get deleted when Session API cleanup job is run on expired sessions (anonymous usage only).

Default behavior: "Yes" (as this is the default behavior now) - but a possibility to opt out and prevent data loss.

Or are there any smarter suggestions?

indigoxela’s picture

As there were no other suggestions, I tried to provide a patch.
Looked like an easy job at first.

Add the form definition to function flag_form, add the option to class flag_flag, but now I'm stuck and could need some guidance.

hook_session_api_cleanup() knows nothing about the flag and its settings. Only the $sid is provided. How to get the flag settings without killing performance?

joachim’s picture

> hook_session_api_cleanup() knows nothing about the flag and its settings. Only the $sid is provided. How to get the flag settings without killing performance?

> $query = db_select('flagging', 'fc');

You can get the flag ID in that query.

So first, load all flags, check which ones have a setting for clearing anonymous flags, and add an IN (ids) clause to the query.

indigoxela’s picture

@joachim many thanks for your hint

Attached is a patch, which brings an additional setting as described in #10.

indigoxela’s picture

Version: 7.x-3.6 » 7.x-3.x-dev
Status: Active » Needs review

Oops, forgot to set the issue status and affected version setting needs an update, too.

indigoxela’s picture

I am aware that not many people are following this issue, but it would be cool, if someone took the time to review the patch (#13).

joachim’s picture

Quick eyeball review:

  1. +++ b/flag.module
    @@ -1280,7 +1280,16 @@ function flag_session_api_cleanup($arg = 'run') {
    +      if ($flag->sess_api_cleanup === 1) {
    ...
    +      db_delete('flagging')->condition('sid', $outdated_sids, 'IN')->condition('fid', $cleanup_ids, 'IN')->execute();
    

    TRUE rather than 1. Also no need for ===. We control this value; what else is it going to contain?

  2. +++ b/includes/flag.admin.inc
    @@ -481,6 +481,16 @@ function flag_form($form, &$form_state, $flag) {
    +    $form['access']['sess_api_cleanup'] = array(
    

    Don't use abbreviations in names.

  3. +++ b/includes/flag.admin.inc
    @@ -481,6 +481,16 @@ function flag_form($form, &$form_state, $flag) {
    +      '#title' => t('Delete anonymous flaggings on Session API cleanup'),
    +      '#description' => t('Delete flagging data when anonymous session has expired (on Session API cron run).'),
    

    Would be better to have something less technical in the UI text here.

  4. +++ b/includes/flag.admin.inc
    @@ -481,6 +481,16 @@ function flag_form($form, &$form_state, $flag) {
    +      '#weight' => -1,
    

    Why the weight?

  5. +++ b/includes/flag/flag_flag.inc
    @@ -192,6 +192,8 @@ class flag_flag {
    +      // anon session cleanup
    

    Full sentence in comments.

  6. +++ b/includes/flag/flag_flag.inc
    @@ -192,6 +192,8 @@ class flag_flag {
    +      'sess_api_cleanup' => 1,
    

    TRUE, rather than 1.

Also, Flag D8 has the opposite behaviour -- it doesn't delete anon flaggings -- and there is as of recently an issue to introduce that behaviour.

So I'd like the two issues to coordinate a little -- the UI and setting name should match, for instance.

indigoxela’s picture

Also, Flag D8 has the opposite behaviour -- it doesn't delete anon flaggings -- and there is as of recently an issue to introduce that behaviour.

I agree, D7 and D8 versions shouldn't do stuff fundamentally different.

I chose to keep deletion as default behavior, as it was the default until now. Changing defaults is always a bit dangerous, but obviously there needs to be a change either in D7 version or in D8 version. Which one is preferred?

Btw, the weight of the form item should be self-explanatory, when viewing the admin ui with and without it.

I suppose, this is the related D8 issue: https://www.drupal.org/project/flag/issues/3018713
There's no discussion about ui text yet.

Regarding the wording changes: please suggest something less technical for the ui.

indigoxela’s picture

New patch after "quick eyeball review".

Please note: the logic is still the same - delete by default.

If the help text is still too technical - I'm open for suggestions.

Is this issue here blocked by the D8 version issue? In other words: do we have to wait until #3018713 is solved?
Hopefully not, because I don't see much activity there.

indigoxela’s picture

Two years later... Is anyone willing to test/review this patch for a critical bug?

ivnish’s picture

Status: Needs review » Closed (outdated)

Drupal 7 is EOL. Issue will be closed, but patches are still here