s4

view s4-world.sh @ 881:a240c8988c65

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