s4

annotate s4-world.sh @ 826:f2d69fb3e038

Link to group list in other world with kwd
author HIROSE Yuuji <yuuji@gentei.org>
date Sun, 21 Jun 2020 10:08:12 +0900
parents e9817a71f332
children 97be0474f268
rev   line source
yuuji@734 1 #!/bin/sh
yuuji@734 2
yuuji@817 3 autoremovestop=2
yuuji@817 4
yuuji@791 5 # Setup variables for running time
yuuji@791 6 if $isCGI; then
yuuji@791 7 case "$S4WORLDLIST" in
yuuji@791 8 *:*:*)
yuuji@791 9 worldlistfile=cache/worldlist
yuuji@797 10 worldgrpfile=cache/worldgrps
yuuji@791 11 worldoptionfile=cache/worldoption
yuuji@791 12 worldnamefile=cache/worldname
yuuji@791 13 if [ ! -e $worldlistfile -o $worldlistfile -ot s4-config.sh \
yuuji@791 14 -o ! -e $worldoptionfile -o $worldoptionfile -ot s4-config.sh \
yuuji@794 15 -o s4-world.sh -nt $worldoptionfile ]
yuuji@791 16 then
yuuji@791 17 echo S4MASTERURL=$S4MASTERURL >> tmp/debug.out
yuuji@791 18 cat <<-EOF > $worldlistfile
yuuji@797 19 <div><table>
yuuji@791 20 <tr><th>World List</th></tr>
yuuji@794 21 <tr><td title="Base World$nl拠点となるWorldです">&rArr;
yuuji@794 22 <a href="${S4MASTERURL:-$URL}">Base</a></td></tr>
yuuji@791 23 EOF
yuuji@791 24 true > $worldoptionfile
yuuji@791 25 for i in $S4WORLDLIST; do
yuuji@791 26 echo $i | {
yuuji@791 27 IFS=: read name short d
yuuji@791 28 cgi="s4-world-$short$cgiext"
yuuji@791 29 conf="s4-config-$short.sh"
yuuji@791 30 cat<<-EOF >>$worldlistfile
yuuji@791 31 <tr><td title="$d">&rArr; <a href="$cgi">$name</a></td></tr>
yuuji@791 32 EOF
yuuji@791 33 cat<<-EOF >>$worldoptionfile
yuuji@791 34 <option title="$d" value="$conf">$name</option>
yuuji@791 35 EOF
yuuji@791 36 echo "$name" > $worldnamefile.$short
yuuji@791 37 }
yuuji@791 38 done
yuuji@791 39 if [ -s "$worldoptionfile" ]; then
yuuji@791 40 echo "<option value=\"s4-config.sh\">Base</option>" >> $worldoptionfile
yuuji@791 41 echo "</table>$nl</div>" >> $worldlistfile
yuuji@797 42 sed 's/href="\([^>]*\)"/href="\1?grps"/' $worldlistfile \
yuuji@797 43 > $worldgrpfile
yuuji@791 44 else
yuuji@791 45 true > $worldoptionfile; true > $worldlistfile # Remove contents
yuuji@791 46 rm -f ${worldnamefile}.*
yuuji@791 47 fi
yuuji@791 48 fi
yuuji@791 49 if [ -s "$worldlistfile" ]; then
yuuji@797 50 S4WORLDS="▼spaste(\`$worldlistfile')"
yuuji@791 51 S4WORLDNAME=${S4WORLD:+`cat $worldnamefile.$S4WORLD`}
yuuji@797 52 S4WORLDGRPS="▼spaste(\`$worldgrpfile')"
yuuji@791 53 fi
yuuji@791 54 ;;
yuuji@791 55 esac
yuuji@791 56 fi
yuuji@791 57
yuuji@734 58
yuuji@817 59 syncaccount_1() {
yuuji@817 60
yuuji@817 61 n_m=`query "SELECT printf('%d:%d',\
yuuji@817 62 (SELECT count(*) FROM m.user), \
yuuji@817 63 (SELECT count(*) FROM user));"`
yuuji@817 64 n=${n_m%:*}; m=${n_m#*:} # n:m
yuuji@817 65 if [ -z "$n" -o "$n" -lt 2 ]; then
yuuji@817 66 err "Skipping account sync because m.user cannot be reached[n=$n]."
yuuji@817 67 return -3
yuuji@794 68 fi
yuuji@817 69 if [ -z "$forceusersync" -a ! -f db/forceusersync ]; then
yuuji@817 70 if [ $((m-n)) -le $((autoremovestop+0)) ]; then
yuuji@817 71 # If if-condition evaluation fails, fall through to else-caluse
yuuji@817 72 rm -f db/forceusersync
yuuji@817 73 else
yuuji@817 74 err "More than $autoremovestop users vanished($((m-n)))."
yuuji@817 75 err "Automatic removal canceled. If you want to sync user accounts"
yuuji@817 76 err "forcibly, touch db/forceusersync file each time of world update."
yuuji@817 77 return -4
yuuji@817 78 fi
yuuji@817 79 fi
yuuji@817 80 err "`gdate +%S.%3N` Starting account synchronization[$$]"
yuuji@742 81
yuuji@817 82 ## num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db <<EOF
yuuji@817 83 num=$(query <<EOF
yuuji@734 84 BEGIN;
yuuji@734 85 DElETE FROM main.user WHERE rowid NOT IN (SELECT rowid FROM m.user);
yuuji@734 86 INSERT INTO main.user(rowid, name)
yuuji@734 87 SELECT rowid, name FROM m.user
yuuji@734 88 WHERE m.user.rowid NOT IN (SELECT rowid FROM user);
yuuji@734 89 UPDATE user SET name = (SELECT name FROM m.user WHERE main.user.rowid=m.user.rowid);
yuuji@742 90 DELETE FROM main.user_s WHERE rowid NOT IN (SELECT rowid FROM m.user_s);
yuuji@742 91 REPLACE INTO main.user_s(rowid, name, key, type, val, bin)
yuuji@734 92 SELECT rowid,* FROM m.user_s;
yuuji@742 93 DELETE FROM main.user_m WHERE rowid NOT IN (SELECT rowid FROM m.user_m);
yuuji@742 94 REPLACE INTO main.user_m(rowid, name, key, type, val, bin)
yuuji@734 95 SELECT rowid,* FROM m.user_m
yuuji@820 96 WHERE user_m.name != m.user_m.name
yuuji@820 97 OR user_m.key != m.user_m.key
yuuji@820 98 OR user_m.type != m.user_m.type
yuuji@820 99 OR user_m.val != m.user_m.val
yuuji@820 100 OR length(user_m.bin)!= length(m.user_m.bin)
yuuji@820 101 ;
yuuji@734 102 END;
yuuji@734 103
yuuji@734 104 /* Compare user tables */
yuuji@734 105 WITH master AS (
yuuji@734 106 SELECT p.rowid,* FROM m.user p
yuuji@734 107 NATURAL LEFT JOIN m.user_s
yuuji@734 108 NATURAL LEFT JOIN m.user_m
yuuji@743 109 ), thisworld AS (
yuuji@734 110 SELECT p.rowid,* FROM user p
yuuji@734 111 NATURAL LEFT JOIN user_s
yuuji@734 112 NATURAL LEFT JOIN user_m
yuuji@734 113 ), m_a AS (
yuuji@743 114 SELECT * FROM master EXCEPT SELECT * FROM thisworld
yuuji@734 115 ), a_m AS (
yuuji@743 116 SELECT * FROM thisworld EXCEPT SELECT * FROM master
yuuji@734 117 ) SELECT (SELECT count(*) FROM m_a) + (SELECT count(*) FROM a_m);
yuuji@734 118 EOF
yuuji@817 119 )
yuuji@817 120 if [ -n "$num" -a "$num" -eq 0 ]; then
yuuji@817 121 err "`gdate +%S.%3N` Account synchronization[$$] done in difference $num"
yuuji@817 122 echo "`date '+%F %T'`: Sync done by process $$" >> $syncflag
yuuji@817 123 else
yuuji@817 124 err "Account synch[$$] failed or bailed with num=[$num]"
yuuji@817 125 fi
yuuji@817 126 return $num
yuuji@817 127 }
yuuji@817 128
yuuji@817 129 syncaccount() {
yuuji@817 130 forceusersync=$1
yuuji@817 131 err "db=$db mas=$S4MASTERDB sessdb=$sessdb"
yuuji@817 132 # If in parent world, no need to do rest of jobs
yuuji@817 133 if [ -z "$S4MASTERDB" -o ! -s "$S4MASTERDB" ]; then
yuuji@817 134 return
yuuji@817 135 fi
yuuji@817 136 # Confim child
yuuji@817 137 if [ "$db" -ef "$S4MASTERDB" ]; then
yuuji@817 138 return # Points to the same file
yuuji@817 139 fi
yuuji@817 140 # File based sync check precedes to DB count check for performance issue
yuuji@817 141 syncflag=${db%.*}.synctime
yuuji@817 142 runflag=${db%.*}.run
yuuji@817 143 userupdateflag=`dirname $S4MASTERDB`/`basename $userupdateflag`
yuuji@817 144 test ! -e "$userupdateflag" && return
yuuji@817 145 [ -z "$forceusersync" -a "$syncflag" -nt "$userupdateflag" ] && return
yuuji@817 146 if [ -s "$runflag" ]; then
yuuji@817 147 limit=`cat $runflag|tr -c -d 0-9`
yuuji@817 148 if [ -n "$limit" -a "$limit" -gt `date +%s` ]; then
yuuji@817 149 err "World $S4WORLD account sync withholded by process $$"
yuuji@817 150 return # Running sync by other process not leaching timeout
yuuji@817 151 fi
yuuji@817 152 fi
yuuji@817 153 echo $((`date +%s` + 10)) > $runflag # Setting running flag by timeout 10s
yuuji@817 154 query<<-EOF
yuuji@817 155 .bail ON
yuuji@817 156 ATTACH DATABASE "$S4MASTERDB" AS m;
yuuji@817 157 EOF
yuuji@817 158 syncaccount_1
yuuji@817 159 rc=$?
yuuji@817 160 test -e "$runflag" && rm -f "$runflag"
yuuji@817 161 query<<-EOF
yuuji@817 162 DETACH DATABASE m;
yuuji@817 163 .bail OFF
yuuji@817 164 EOF
yuuji@817 165 return $rc
yuuji@817 166 }
yuuji@825 167
yuuji@825 168 getworldDB() { # $1=conf
yuuji@825 169 (unset DB; . "$1"; echo ${DB:-$defaultdb})
yuuji@825 170 }
yuuji@825 171
yuuji@825 172 worldnameDBlist() {
yuuji@825 173 echo Base:Base:`getworldDB ./s4-config.sh`
yuuji@825 174 for w in $S4WORLDLIST; do
yuuji@825 175 case "$w" in
yuuji@825 176 *:*:*:*) continue ;;
yuuji@825 177 *:*:*) w=${w%:*}; name=${w%:*}; world=${w#*:} ;;
yuuji@825 178 *) continue ;;
yuuji@825 179 esac
yuuji@825 180 d=`getworldDB "./s4-config-$world.sh"`
yuuji@825 181 test -s "$d" && echo ${world}:${name}:$d
yuuji@825 182 done
yuuji@825 183 }
yuuji@825 184
yuuji@825 185 grepgrpworld() (
yuuji@825 186 world=$1; wname=$2; kwd=$3; db=$4 cond=$5
yuuji@825 187 case $world in
yuuji@825 188 Base) cgi=`basename ${S4MASTERURL:-$URL}` ;;
yuuji@825 189 *) cgi=s4-world-$world$cgiext ;;
yuuji@825 190 esac
yuuji@825 191 query <<-EOF |
yuuji@825 192 ATTACH DATABASE "$db" AS td;
yuuji@825 193 SELECT DISTINCT td.grp.rowid,hex(gname)
yuuji@825 194 FROM td.grp NATURAL JOIN td.grp_mem
yuuji@825 195 WHERE $cond;
yuuji@825 196 DETACH DATABASE td;
yuuji@825 197 EOF
yuuji@825 198 while IFS='|' read rowid hgname; do
yuuji@825 199 # echo rowid=$rowid - `echo "$hgname"|unhexize|htmlescape`
yuuji@825 200 htmlgn=`echo $hgname|unhexize|htmlescape`
yuuji@825 201 printf '<a href="%s?grp+%d">%s</a> ' "$cgi" "$rowid" "$htmlgn"
yuuji@825 202 done | {
yuuji@825 203 read ans
yuuji@825 204 if [ -n "$ans" ]; then
yuuji@825 205 w=`echo $wname|htmlescape`
yuuji@826 206 u="<a href=\"$cgi?grps\"><span class=\"pre\">$w</span></a>"
yuuji@826 207 s="(<a href=\"$cgi?stage=grps&kwd=$kwd\">絞込</a>)"
yuuji@825 208 cat<<-EOF
yuuji@825 209 <tr>
yuuji@826 210 <tr><td>$u $s</td>
yuuji@825 211 <td>$ans</td>
yuuji@825 212 </dl>
yuuji@825 213 EOF
yuuji@825 214 fi
yuuji@825 215 }
yuuji@825 216 )
yuuji@825 217
yuuji@825 218 peekgrpworlds() (
yuuji@825 219 # $1=(kwd=Pattern|mem=User)
yuuji@825 220 arg=${1#*=}
yuuji@825 221 case $1 in
yuuji@825 222 kwd=*) cond="gname LIKE '%$arg%'"
yuuji@825 223 guide="`echo $arg|htmlescape` を含むグループは別世界にもあります" ;;
yuuji@825 224 mem=*) cond="user = '$arg'"
yuuji@825 225 guide="別世界の所属グループ";;
yuuji@825 226 esac
yuuji@825 227 for wd in `worldnameDBlist`; do
yuuji@825 228 world=${wd%%:*}; wd=${wd#*:}
yuuji@825 229 worldname=${wd%:*}
yuuji@825 230 d=${wd#*:}
yuuji@825 231 if [ ! $db -ef $d -a -s $d ]; then
yuuji@826 232 grepgrpworld "$world" "$worldname" "$arg" "$d" "$cond"
yuuji@825 233 fi
yuuji@825 234 done | {
yuuji@825 235 result=`cat`
yuuji@825 236 if [ -n "$result" ]; then
yuuji@825 237 echo "<h2>$guide</h2>"
yuuji@825 238 echo "<table class=\"b\">$nl $result$nl</table>"
yuuji@825 239 fi
yuuji@825 240 }
yuuji@825 241 )