bjornbjorn / snippetssync.ee2_addon

Save snippets as files during development
http://wedoaddons.com
21 stars 8 forks source link

A way to clean the filesystem of orphaned snippets/global vars. #4

Open jannisg opened 12 years ago

jannisg commented 12 years ago

Hi Bjørn,

I am working on a way for snippets that have been deleted (via the admin or direct DB access) to be purged from the filesystem but so far it's proving to be rather difficult because the sessionstart hook seems to fire (logically so)_ before the actual modification of the DB is taking place, so at runtime the DB doesn't yet reflect the changes of the delete action the user triggered.

Have you got any ideas on which hook I might be able to use that can be used in snippetssync and will run after any DB modifications?

My idea is documented in this feature branch here: jannisg:origin/feature/delete-files

Would love to hear your thoughts on this.

bjornbjorn commented 12 years ago

Hmm, not sure but maybe "sessions_end" would give different results? I don't think there's a global db hook of any sort. But anyway, this is tricky, cause how will you know if the user deleted the snippet or if it hasn't been added yet?

One option could be to do this via JavaScript, there is the cp_js_end hook: http://expressionengine.com/user_guide/development/extension_hooks/cp/javascript/index.html

With it you could add another event listener to the "Delete" link on the snippets page and let the user know that "This snippet is also saved as a file on the filesystem - do you want to delete this as well?" .. if they don't it will just be resynced like ususal so to delete the snippet they'd have to say yes.

Looking at some code to see how others use specific hooks always helps, to see how people use "cp_js_end" check out devot-ee:

http://devot-ee.com/hooks/ee/cp_js_end

jannisg commented 12 years ago

Yes, that's exactly the difficulty I was running into as well, that there was no way of knowing whether a snippet has not yet been added to the DB or whether it was forcefully deleted.

Having said that, I did find that the $_GET and $_REQUEST array both (naturally) have hooks we could use for this provided the snippet was deleted via the control panel and not via direct DB manipulation.

$_REQUEST['M'] == "snippets_delete" and $_REQUEST['delete_confirm'] is only available after user action in the CP and the latter of course only in their delete confirmation request.

My thought was that using those two (plus the two global var counterparts of course) in an if statement around the sync_all function (so it does not fire when something is being deleted) as well as providing a separate on_session_end function to then run the filesystem_purge() in which we could clean up the filesystem when both the $_REQUEST vars are set and true.

Actually… I just spotted that as part of that delete confirmation request is also the id of the snippet/global var that has just been deleted which we could use to reverse engineer the filename off of and then just delete the specific file belonging to the deleted snippet/global var. That could work without any additional hooks I think.

Perhaps though having a separate Accessory pane at the bottom of the CP for this might be a cleaner more decisive UI, two buttons in the pane, one purges filesystem and one to sync all manually?

What do you think? I will have a quick play around with the id idea I mentioned but perhaps the accessory route is an better one?

jannisg commented 12 years ago

Hmm… I was hoping by putting the #4 into pull request it would attach it to our previous issue #4 comment thread… obviously I was mistaken.

A possible solution is now listed here as issue #5 : https://github.com/bjornbjorn/snippetssync.ee2_addon/pull/5

Apologies for the issue spamming.