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
Comment #1
gagarine commentedflag... not fag... hum.
Comment #2
gagarine commentedThe same is true for user cancellation. flag_user_cancel should not delete flag. #2489992: Do not delete flaggings on account cancelation
Comment #3
gagarine commentedComment #4
gagarine commentedComment #5
joachim commentedGit blame (with some digging) tells me this was added in commit:
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...
Comment #6
gagarine commentedI 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).
Comment #7
lightsurge commentedI 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.
Comment #8
lightsurge commentedSo 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)
Comment #9
indigoxela commented@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?
Comment #10
indigoxela commentedSo 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?
Comment #11
indigoxela commentedAs 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?
Comment #12
joachim commented> 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.
Comment #13
indigoxela commented@joachim many thanks for your hint
Attached is a patch, which brings an additional setting as described in #10.
Comment #14
indigoxela commentedOops, forgot to set the issue status and affected version setting needs an update, too.
Comment #15
indigoxela commentedI 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).
Comment #16
joachim commentedQuick eyeball review:
TRUE rather than 1. Also no need for ===. We control this value; what else is it going to contain?
Don't use abbreviations in names.
Would be better to have something less technical in the UI text here.
Why the weight?
Full sentence in comments.
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.
Comment #17
indigoxela commentedI 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.
Comment #18
indigoxela commentedNew 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.
Comment #19
indigoxela commentedTwo years later... Is anyone willing to test/review this patch for a critical bug?
Comment #20
ivnishDrupal 7 is EOL. Issue will be closed, but patches are still here