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です">⇒
|
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">⇒ <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@885
|
80 err "`gdate +%S.%3N` Starting account synchronization"
|
yuuji@887
|
81 SYNC_M=1
|
yuuji@742
|
82
|
yuuji@835
|
83 prevsync=`tail -1 $syncflag|colrm 20` # 2020-06-21 12:30:00 = 19cols
|
yuuji@835
|
84 syncall=${db%.*}.syncall
|
yuuji@835
|
85 err syncallfile=$syncall
|
yuuji@835
|
86 if [ -e $syncall ]; then
|
yuuji@835
|
87 rm -f "$syncall"
|
yuuji@835
|
88 err "Force update user_m for all users"
|
yuuji@835
|
89 else
|
yuuji@835
|
90 case "$prevsync" in
|
yuuji@835
|
91 [2-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]\ [012][0-9]:??:??)
|
yuuji@881
|
92 SYNCCOND="WHERE name in (SELECT name FROM m.user_s WHERE key='profupdate' AND val > '$prevsync')"
|
yuuji@880
|
93 err "prevsync=$prevsync"
|
yuuji@880
|
94 limited=`query "SELECT DISTINCT name FROM m.user_s $SYNCCOND;"`
|
yuuji@880
|
95 if [ -n "$limited" ]; then
|
yuuji@880
|
96 err synccond limited to "$limited"
|
yuuji@880
|
97 else
|
yuuji@880
|
98 err synccond canceled for user_m
|
yuuji@880
|
99 SYNCCOND="WHERE NULL"
|
yuuji@887
|
100 SYNC_M=
|
yuuji@880
|
101 fi
|
yuuji@835
|
102 err "Touch $syncall (owner=`id -un`) to update all user_m."
|
yuuji@835
|
103 ;;
|
yuuji@835
|
104 *)
|
yuuji@835
|
105 echo arere ;;
|
yuuji@835
|
106 esac
|
yuuji@835
|
107 fi
|
yuuji@817
|
108 ## num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db <<EOF
|
yuuji@903
|
109 result=$(query <<EOF
|
yuuji@734
|
110 BEGIN;
|
yuuji@734
|
111 DElETE FROM main.user WHERE rowid NOT IN (SELECT rowid FROM m.user);
|
yuuji@734
|
112 INSERT INTO main.user(rowid, name)
|
yuuji@734
|
113 SELECT rowid, name FROM m.user
|
yuuji@734
|
114 WHERE m.user.rowid NOT IN (SELECT rowid FROM user);
|
yuuji@904
|
115 /* PrimaryKey:name cannot be changed, but ensure the integrity of user */
|
yuuji@904
|
116 UPDATE user SET name = (SELECT name FROM m.user WHERE main.user.rowid=m.user.rowid) WHERE name != (SELECT name from m.user WHERE main.user.rowid=m.user.rowid);
|
yuuji@742
|
117 DELETE FROM main.user_s WHERE rowid NOT IN (SELECT rowid FROM m.user_s);
|
yuuji@742
|
118 REPLACE INTO main.user_s(rowid, name, key, type, val, bin)
|
yuuji@734
|
119 SELECT rowid,* FROM m.user_s;
|
yuuji@887
|
120 ${SYNC_M:+
|
yuuji@742
|
121 DELETE FROM main.user_m WHERE rowid NOT IN (SELECT rowid FROM m.user_m);
|
yuuji@742
|
122 REPLACE INTO main.user_m(rowid, name, key, type, val, bin)
|
yuuji@835
|
123 SELECT rowid,* FROM m.user_m $SYNCCOND
|
yuuji@834
|
124 EXCEPT
|
yuuji@835
|
125 SELECT rowid,* FROM main.user_m $SYNCCOND
|
yuuji@887
|
126 ;} /* SYNC_M closed here */
|
yuuji@903
|
127 SELECT 'OK';
|
yuuji@734
|
128 END;
|
yuuji@903
|
129 EOF
|
yuuji@903
|
130 )
|
yuuji@903
|
131 err "`gdate +%S.%3N` Update account tables finished with result=[$result]"
|
yuuji@903
|
132 test x"$result" = x"OK" && num=$(query <<EOF
|
yuuji@734
|
133 /* Compare user tables */
|
yuuji@734
|
134 WITH master AS (
|
yuuji@734
|
135 SELECT p.rowid,* FROM m.user p
|
yuuji@887
|
136 NATURAL LEFT JOIN m.user_s
|
yuuji@887
|
137 ${SYNC_M:+ NATURAL LEFT JOIN m.user_m}
|
yuuji@903
|
138 $SYNCCOND
|
yuuji@743
|
139 ), thisworld AS (
|
yuuji@734
|
140 SELECT p.rowid,* FROM user p
|
yuuji@887
|
141 NATURAL LEFT JOIN user_s
|
yuuji@887
|
142 ${SYNC_M:+ NATURAL LEFT JOIN user_m}
|
yuuji@903
|
143 $SYNCCOND
|
yuuji@734
|
144 ), m_a AS (
|
yuuji@743
|
145 SELECT * FROM master EXCEPT SELECT * FROM thisworld
|
yuuji@734
|
146 ), a_m AS (
|
yuuji@743
|
147 SELECT * FROM thisworld EXCEPT SELECT * FROM master
|
yuuji@734
|
148 ) SELECT (SELECT count(*) FROM m_a) + (SELECT count(*) FROM a_m);
|
yuuji@734
|
149 EOF
|
yuuji@817
|
150 )
|
yuuji@817
|
151 if [ -n "$num" -a "$num" -eq 0 ]; then
|
yuuji@885
|
152 err "`gdate +%S.%3N` Account synchronization done in difference $num"
|
yuuji@817
|
153 echo "`date '+%F %T'`: Sync done by process $$" >> $syncflag
|
yuuji@817
|
154 else
|
yuuji@885
|
155 err "Account synch failed or bailed with num=[$num]"
|
yuuji@817
|
156 fi
|
yuuji@817
|
157 return $num
|
yuuji@817
|
158 }
|
yuuji@817
|
159
|
yuuji@817
|
160 syncaccount() {
|
yuuji@817
|
161 forceusersync=$1
|
yuuji@859
|
162 ## err "db=$db mas=$S4MASTERDB sessdb=$sessdb"
|
yuuji@817
|
163 # If in parent world, no need to do rest of jobs
|
yuuji@817
|
164 if [ -z "$S4MASTERDB" -o ! -s "$S4MASTERDB" ]; then
|
yuuji@817
|
165 return
|
yuuji@817
|
166 fi
|
yuuji@817
|
167 # Confim child
|
yuuji@817
|
168 if [ "$db" -ef "$S4MASTERDB" ]; then
|
yuuji@817
|
169 return # Points to the same file
|
yuuji@817
|
170 fi
|
yuuji@817
|
171 # File based sync check precedes to DB count check for performance issue
|
yuuji@817
|
172 syncflag=${db%.*}.synctime
|
yuuji@817
|
173 runflag=${db%.*}.run
|
yuuji@817
|
174 userupdateflag=`dirname $S4MASTERDB`/`basename $userupdateflag`
|
yuuji@817
|
175 test ! -e "$userupdateflag" && return
|
yuuji@817
|
176 [ -z "$forceusersync" -a "$syncflag" -nt "$userupdateflag" ] && return
|
yuuji@817
|
177 if [ -s "$runflag" ]; then
|
yuuji@817
|
178 limit=`cat $runflag|tr -c -d 0-9`
|
yuuji@892
|
179 err syncaccount: limit=$limit
|
yuuji@817
|
180 if [ -n "$limit" -a "$limit" -gt `date +%s` ]; then
|
yuuji@817
|
181 err "World $S4WORLD account sync withholded by process $$"
|
yuuji@817
|
182 return # Running sync by other process not leaching timeout
|
yuuji@817
|
183 fi
|
yuuji@817
|
184 fi
|
yuuji@817
|
185 echo $((`date +%s` + 10)) > $runflag # Setting running flag by timeout 10s
|
yuuji@892
|
186 err syncaccount: setrunflag[$runflag]=`cat $runflag`
|
yuuji@817
|
187 query<<-EOF
|
yuuji@817
|
188 .bail ON
|
yuuji@817
|
189 ATTACH DATABASE "$S4MASTERDB" AS m;
|
yuuji@817
|
190 EOF
|
yuuji@817
|
191 syncaccount_1
|
yuuji@817
|
192 rc=$?
|
yuuji@817
|
193 test -e "$runflag" && rm -f "$runflag"
|
yuuji@817
|
194 query<<-EOF
|
yuuji@817
|
195 DETACH DATABASE m;
|
yuuji@817
|
196 .bail OFF
|
yuuji@817
|
197 EOF
|
yuuji@817
|
198 return $rc
|
yuuji@817
|
199 }
|
yuuji@825
|
200
|
yuuji@825
|
201 getworldDB() { # $1=conf
|
yuuji@825
|
202 (unset DB; . "$1"; echo ${DB:-$defaultdb})
|
yuuji@825
|
203 }
|
yuuji@825
|
204
|
yuuji@825
|
205 worldnameDBlist() {
|
yuuji@825
|
206 echo Base:Base:`getworldDB ./s4-config.sh`
|
yuuji@825
|
207 for w in $S4WORLDLIST; do
|
yuuji@825
|
208 case "$w" in
|
yuuji@825
|
209 *:*:*:*) continue ;;
|
yuuji@825
|
210 *:*:*) w=${w%:*}; name=${w%:*}; world=${w#*:} ;;
|
yuuji@825
|
211 *) continue ;;
|
yuuji@825
|
212 esac
|
yuuji@825
|
213 d=`getworldDB "./s4-config-$world.sh"`
|
yuuji@825
|
214 test -s "$d" && echo ${world}:${name}:$d
|
yuuji@825
|
215 done
|
yuuji@825
|
216 }
|
yuuji@825
|
217
|
yuuji@828
|
218 grepgrpworld() {
|
yuuji@828
|
219 world=$1; wname=$2; exp=$3; tdb=$4
|
yuuji@825
|
220 case $world in
|
yuuji@827
|
221 Base) cgi=${S4MASTERURL:-$URL} ;;
|
yuuji@825
|
222 *) cgi=s4-world-$world$cgiext ;;
|
yuuji@825
|
223 esac
|
yuuji@827
|
224 case $exp in
|
yuuji@828
|
225 mem:*)
|
yuuji@828
|
226 arg=${exp#*:}
|
yuuji@828
|
227 cond="user = '$arg'"
|
yuuji@828
|
228 guide="「${S4WORLDNAME:-Base}」以外の世界の所属グループ"
|
yuuji@828
|
229 s="(<a href=\"$cgi?stage=grps&kwd=mem:$arg\">所属絞</a>)" ;;
|
yuuji@828
|
230 *)
|
yuuji@828
|
231 arg=`sqlquote "%$3%"`
|
yuuji@828
|
232 cond="gname LIKE $arg"
|
yuuji@828
|
233 guide="$exp を含むグループは別世界にもあります"
|
yuuji@828
|
234 s="(<a href=\"$cgi?stage=grps&kwd=$exp\">絞込</a>)" ;;
|
yuuji@827
|
235 esac
|
yuuji@825
|
236 query <<-EOF |
|
yuuji@828
|
237 ATTACH DATABASE "$tdb" AS td;
|
yuuji@825
|
238 SELECT DISTINCT td.grp.rowid,hex(gname)
|
yuuji@825
|
239 FROM td.grp NATURAL JOIN td.grp_mem
|
yuuji@825
|
240 WHERE $cond;
|
yuuji@825
|
241 DETACH DATABASE td;
|
yuuji@825
|
242 EOF
|
yuuji@825
|
243 while IFS='|' read rowid hgname; do
|
yuuji@825
|
244 # echo rowid=$rowid - `echo "$hgname"|unhexize|htmlescape`
|
yuuji@825
|
245 htmlgn=`echo $hgname|unhexize|htmlescape`
|
yuuji@825
|
246 printf '<a href="%s?grp+%d">%s</a> ' "$cgi" "$rowid" "$htmlgn"
|
yuuji@825
|
247 done | {
|
yuuji@825
|
248 read ans
|
yuuji@825
|
249 if [ -n "$ans" ]; then
|
yuuji@825
|
250 w=`echo $wname|htmlescape`
|
yuuji@826
|
251 u="<a href=\"$cgi?grps\"><span class=\"pre\">$w</span></a>"
|
yuuji@825
|
252 cat<<-EOF
|
yuuji@828
|
253 GUIDE:<h2>`echo "$guide"|htmlescape`</h2>
|
yuuji@825
|
254 <tr>
|
yuuji@826
|
255 <tr><td>$u $s</td>
|
yuuji@825
|
256 <td>$ans</td>
|
yuuji@825
|
257 </dl>
|
yuuji@825
|
258 EOF
|
yuuji@825
|
259 fi
|
yuuji@825
|
260 }
|
yuuji@828
|
261 }
|
yuuji@825
|
262
|
yuuji@825
|
263 peekgrpworlds() (
|
yuuji@828
|
264 # $1=(Pattern|mem:User)
|
yuuji@828
|
265 # err "pgw-1=[$1]"
|
yuuji@825
|
266 for wd in `worldnameDBlist`; do
|
yuuji@825
|
267 world=${wd%%:*}; wd=${wd#*:}
|
yuuji@825
|
268 worldname=${wd%:*}
|
yuuji@825
|
269 d=${wd#*:}
|
yuuji@825
|
270 if [ ! $db -ef $d -a -s $d ]; then
|
yuuji@827
|
271 grepgrpworld "$world" "$worldname" "$1" "$d"
|
yuuji@825
|
272 fi
|
yuuji@825
|
273 done | {
|
yuuji@825
|
274 result=`cat`
|
yuuji@825
|
275 if [ -n "$result" ]; then
|
yuuji@828
|
276 cat<<-EOF
|
yuuji@828
|
277 `echo "$result"|sed 's/^GUIDE://;2q'`
|
yuuji@828
|
278 <table class="b">
|
yuuji@828
|
279 `echo "$result"|grep -v '^GUIDE:'`
|
yuuji@828
|
280 </table>
|
yuuji@828
|
281 EOF
|
yuuji@825
|
282 fi
|
yuuji@825
|
283 }
|
yuuji@825
|
284 )
|