diff options
author | Malf Furious <m@lfurio.us> | 2017-04-14 01:39:24 -0400 |
---|---|---|
committer | Malf Furious <m@lfurio.us> | 2017-04-15 00:47:38 -0400 |
commit | e2d3d894f024169cd84c93913db88f3864954532 (patch) | |
tree | f574acf8afb32f2667ca47315e37464295c4d574 /app/cron.php | |
parent | 65a0097b7e4206dbd687d6d7d2d0fd29a3e3c799 (diff) | |
download | scrott-e2d3d894f024169cd84c93913db88f3864954532.tar.gz scrott-e2d3d894f024169cd84c93913db88f3864954532.zip |
Add database garbage collection
Diffstat (limited to '')
-rw-r--r-- | app/cron.php | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/app/cron.php b/app/cron.php new file mode 100644 index 0000000..abadeec --- /dev/null +++ b/app/cron.php @@ -0,0 +1,73 @@ +<?php + +/* + * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * For more information, please refer to UNLICENSE + */ + +require_once "class/database.class.php"; + +/* + * This file is intended to be run periodically by the local cron daemon + * and will perform tasks such as reacting to incoming emails and running + * garbage collection on the object database. + */ + +/* + * Remove all orphaned objects from Scrott's object database. These are + * any object with either a parent or owner set, which doesn't exist. + * Metadata tables (members, views, etc.) are also updated to drop all rows + * related to purged objects. + */ +function gc() : void +{ + $query = "CREATE TEMPORARY TABLE IF NOT EXISTS objtmp AS (SELECT * FROM objects)"; + database::query($query); + + /* cleanup base objects table */ + $query = "DELETE FROM objects WHERE parent != '' AND parent NOT IN (SELECT guid FROM objtmp)"; + database::query($query); + + $query = "DELETE FROM objects WHERE owner != '' AND owner NOT IN (SELECT guid FROM objtmp)"; + database::query($query); + + /* cleanup sub tables */ + $query = "DELETE FROM users WHERE guid NOT IN (SELECT guid FROM objects)"; + database::query($query); + + $query = "DELETE FROM groups WHERE guid NOT IN (SELECT guid FROM objects)"; + database::query($query); + + $query = "DELETE FROM pads WHERE guid NOT IN (SELECT guid FROM objects)"; + database::query($query); + + $query = "DELETE FROM stages WHERE guid NOT IN (SELECT guid FROM objects)"; + database::query($query); + + $query = "DELETE FROM issues WHERE guid NOT IN (SELECT guid FROM objects)"; + database::query($query); + + $query = "DELETE FROM mesgs WHERE guid NOT IN (SELECT guid FROM objects)"; + database::query($query); + + /* cleanup metadata tables */ + $query = "DELETE FROM members WHERE guid NOT IN (SELECT guid FROM objects) " . + "OR member NOT IN (SELECT guid FROM objects)"; + database::query($query); + + $query = "DELETE FROM views WHERE guid NOT IN (SELECT guid FROM objects) " . + "OR viewer NOT IN (SELECT guid FROM objects)"; + database::query($query); +} + +/* Run maintenance routines */ +gc(); + +?> |