changeset 745:262983fbc2ac feature-annex

Clean-up code to remove orphaned blogs and articles
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 06 Jun 2020 16:33:23 +0900
parents 52e74eb05b7e
children f215488bdc8d f40036e2598b
files s4-funcs.sh
diffstat 1 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/s4-funcs.sh	Sat Jun 06 16:32:45 2020 +0900
+++ b/s4-funcs.sh	Sat Jun 06 16:33:23 2020 +0900
@@ -3678,6 +3678,27 @@
   [ "$ddd" ] && err "----- `gdate +%FT%T.%3N` ------------666666"
 }
 
+clean-orphaned() {
+  # This shoud be done by foreign_key rules, but some db lack them
+  query<<-EOF
+	-- Find blogs that have no parent
+	CREATE TEMPORARY VIEW IF NOT EXISTS orphanedblog AS
+	SELECT blog.id,val FROM blog JOIN blog_s bs
+	       ON blog.id=bs.id AND key='owner'
+            WHERE val NOT IN (SELECT gname FROM grp)
+              AND val NOT IN (SELECT name FROM user);
+	-- Remove them
+	DELETE FROM blog WHERE id IN (SELECT id FROM orphanedblog);
+
+	-- Find articles that have no parent blog
+	CREATE TEMPORARY VIEW IF NOT EXISTS orphanedarticle AS
+	SELECT id FROM article
+	WHERE  blogid NOT IN (SELECT id FROM blog);
+
+	-- Remove them
+	DELETE FROM article WHERE id IN (SELECT id FROM orphanedarticle);
+	EOF
+}
 par2table() (
   # copy current parameters of par into destination table
   # $1=definition-file
@@ -3732,6 +3753,9 @@
     if [ x"$rm" = x"yes" ]; then
       if [ x"$rm$cfm" = x"yesyes" ]; then
 	query "delete from $tbl where rowid=$rowid;"
+	if [ x"$tbl" = x"grp" -o x"$tbl" = x"blog" ]; then
+	  clean-orphaned
+	fi
 	return 4
       else
 	echo "消去確認のチェックがないので消さなかったの..." | html p

yatex.org