comparison s4-blog.sh @ 17:01f579d2c889

Rename to s4
author HIROSE Yuuji <yuuji@gentei.org>
date Tue, 21 Jul 2015 14:43:24 +0900
parents y4-blog.sh@636df1c1bdf2
children 7ae119a14613
comparison
equal deleted inserted replaced
16:636df1c1bdf2 17:01f579d2c889
1 #
2 type cgiinit >/dev/null 2>&1 || . ./s4-funcs.sh
3
4 blog_genform() {
5 #
6 t=$1
7 }
8
9 blog_writable() (
10 # $1=articleid $2=user
11 blogowner=`getvalbyid blog owner "$1"`
12 [ x"$blogowner" = x"$2" ] || isuser "$blogowner" || ismember "$2" "$blogowner"
13 )
14 blog_notify_reply() {
15 # $1=articleid $2=ReplyingUser $3=WrittenText
16 blogowner=`getvalbyid blog owner "$1"`
17 blogtitle=`getvalbyid blog title "$1"`
18 blogurl="$urlbase?replyblog+$1"
19 emails=`collectemail $blogowner`
20 smail "$emails" "書込通知 $urlbase"<<EOF
21 あなたの板に書き込みがありました。
22 場所: $blogurl
23 題目: $blogtitle
24 筆者: `gecos $2`
25 内容:
26 `echo "$3"|sed 's/^/> /'`
27 EOF
28 }
29 blog_showentry() {
30 # $1=table $2=rowid
31 if [ -n "$2" ]; then
32 if [ -n "$imgcached" ]; then
33 tmpd=$tmpdir/$imgcached/$thumbxy
34 else
35 tmpd=`mktempd`
36 tmpfiles=$tmpfiles" $tmpd"
37 fi
38 fi
39 tbl=${1%%[!A-Z0-9a-z_]*} rowid=${2%%[!A-Z0-9a-z_]*}
40 err rowid=$rowid, '$2'=$2
41 ts=${tbl}_s tm=${tbl}_m
42 at=article as=article_s am=article_m
43 serial=$(($(date +%s)-1420038000))s$$
44 # This function grasps blog entry definiton directly.
45 # blog: id
46 # blog_s: title,ctime,heading
47 # blog_m: *article
48
49 err "SELECT id from $tbl where rowid=$rowid"
50 id=`query "select id from $tbl where rowid=$rowid;"`
51 err id=$id
52 err "select val from $ts where key='title' and id='$id';"
53
54
55 #(1)Display root article
56 cat<<EOF
57 <form class="replyblog" action="$myname?replyblog+$rowid" method="POST" enctype="multipart/form-data">
58 <table class="bloghead">
59 EOF
60
61 href="<a href=\"?editheading+$rowid\"> 編集 </a>"
62 href2="<a href=\"?lshandout+$rowid\"> 提出状況 </a>"
63 href3="(<a href=\"?gethandout+$rowid\">ファイル取得</a>)"
64 cat<<EOF | sq -html $db \
65 | sed -e "s|\(<TR><TD>\),e,|\1 $href |" \
66 -e "s|,s,\(</TD>\)|$href2$href3\1|"
67 -- select val from $ts where key="title" and id="$id";
68 select
69 coalesce((select ",e," from blog where rowid=$rowid and author='$user'),'')
70 ||val||" "
71 ||case (select val from $ts where key="mode" and id="$id")
72 when 'report-closed' then "レポート提出用(自身のファイルのみ参照可),s,"
73 when 'report-open' then "レポート提出用,s,"
74 else ""
75 end
76 from $ts where key="ctime" and id="$id";
77 select val from $ts where key="heading" and id="$id";
78 EOF
79 cat<<EOF
80 </table>
81 <table class="blog_replies">
82 EOF
83
84 #(2)Display following articles
85 textform='<div><table class="b">
86 <tr><td><textarea name="text" cols="40" rows="4"></textarea></td></tr>
87 <tr><td>添付ファイル: <input type="file" name="image" multiple></td></tr>
88 </table>
89 <input type="submit" value="送信">
90 <input type="reset" value="リセット"></div>
91 '
92 ## 6/11の次: articleを出して行く
93 : <<EOF
94 シリアル:id:p:serial:
95 blogID:blogid:f:blog(id):
96 筆者:author:s:owner
97 時刻:ctime:s:stamp:
98 参照元:parent:s:parent:
99 パス:path:s:path:
100 本文:text:s:textarea:cols="60" rows="8"
101 画像:image:m:image:
102 stage:stage:x:hidden:value="replyblog"
103
104 article(id, blogid, author)
105 article_s: Visible = ctime, text Invisible = parent, path
106 article_m: image
107
108 article = hoge|1433812374x20849|yuuji@gentei.org
109 article_s = hoge|ctime|string|2015-06-13 12:27:34|
110 hoge|text|string|Shall we dance?|
111 EOF
112 ##
113 ##
114 err "select id from $at where blogid='$id';"
115 # arts=`sq $db "select a.rowid,a.id,
116 #-- coalesce(b.gecos, a.author)
117 # a.author from $at a
118 # LEFT JOIN (select name,val as gecos from user_s where key='gecos') b
119 # on a.author=b.name where blogid='$id'";`
120 arts=`query "select rowid,id,author from $at where blogid='$id';"`
121 err arts="[$arts]"
122 number=0
123 hrefhome0="<a href=\"?home"
124 for a in $arts; do
125 arid=${a%%|*} aid=`echo "$a"|cut -d'|' -f2` author=${a##*|} imgs=""
126 ### err a=$a, aid=$aid, author=$author
127 # name='' # Get gecos??
128 td=$(echo $tmpd/`echo $a|md5`)
129 val=`getvalbyid article image $arid $td`
130 err val="[$val]" and td as follows:
131
132 if true; then
133 if [ -n "$val" ]; then
134 hrfb="$myname?showattc+article_m"
135 imgs="<br>"$(echo "$val"\
136 |while read fn; do
137
138 #data=`percenthex $td/$fn`
139 #ct=`cat $td/$fn.content-type`
140 ri=`cat $td/$fn.rowid`
141 err fn=$fn ct=$ct ri=$ri; ls -lF $td/ 1>&3
142 #case $ct in
143 # [Ii]mage*) anch="<img src=\"data:$ct,$data\">$fn" ;;
144 # *) anch=$fn ;;
145 # esac
146 # Should not contain newlines for sed argument
147 # echo -n "<a href=\"$hrfb+$ri\">$anch</a>"
148 iconhref $td/$fn "$hrfb+$ri" "$fn" "$fn"
149 done)
150 fi
151 fi
152
153 ### number=$((number+1))
154 # reply="<input type=\"radio\" name=\"parent\" class=\"replybtn\" \
155 # value=\"$number\">"
156 getgecos="(select rowid from user where name='$author')||':'||\
157 coalesce((select val from user_s \
158 where name='$author' and key='gecos'), '$author')||',[/a],'"
159 href="$myname?editart+$arid+$rowid"
160 link="<a href=\"$href\">編集</a>"
161 cat<<EOF | sq -html $db \
162 | sed -e "s|,n,\([0-9]*\):|,n,$hrefhome0+\1\">|" \
163 -e 's|,\[/a\],|</a>|' \
164 -e 's/,n,/<br>/g' -e "s|,i,|$imgs|" \
165 -e "s|<TR>\(<TD>\)|<TR id=\"$aid\">\1|" \
166 -e "s|\(<TR.*>\)\(<TD>\),e,|\1\2$link|"
167 select
168 coalesce((select ",e," from article where id='$aid' and author='$user'),"")
169 ||
170 max(case key when 'ctime' then ",n,"||val||
171 ",n,"||$getgecos end) as TIME,
172 -- max(case key when 'parent' then val||"への返信" end) as REPLYTO,
173 max(case key when 'text' then val||",i," end) as TEXT
174 from article_s where id = '$aid'
175 group by id order by TIME;
176 EOF
177 done
178 echo "</table>"
179
180
181 blog_writable $rowid $user && cat<<EOF
182 <div class="blogcomment">
183 <p>コメント記入</p>
184 <input type="hidden" name="blogid" value="$id">
185 <input type="hidden" name="stage" value="replyblog">
186 <input type="hidden" name="serial" value="$serial">
187 $textform
188 </div>
189 EOF
190 echo "</form>"
191
192 # Record access log
193 acclog blog $rowid
194 }
195
196 lshandout() {
197 # $1=rowid of blog
198 time=`getvalbyid blog ctime $1|colrm 11`
199 owner=`getvalbyid blog owner $1`
200 title=`getvalbyid blog title $1`
201 ge=`gecos $owner`
202 lshandoutsub $owner "$@" \
203 |m4 -D_TITLE_="提出状況" \
204 -D_SUBTITLE_="$time [$title]@${ge:-$owner}" -D_DIARY_="" \
205 -D_FORM_="syscmd(cat)" -D_BLOGS_= -D_DUMPTABLE_= \
206 $layout/html.m4.html $layout/diary.m4.html
207 }
208 lshandoutsub() {
209 # $1=owner $2=rowid of blog
210 if isgroup $1; then
211 sample="(select user from grp_mem where gname='$1')"
212 else
213 sample="(select distinct author as user from arts)"
214 echo "<p>(集計は板への投稿者のみ)</p>"
215 fi
216 sql="with arts as (select id,author from article \
217 where blogid=(select id from blog where rowid=$2))\
218 select (select rowid from user where name=c0.user)||' '|| \
219 coalesce((select val from user_s where name=c0.user \
220 and key='gecos'),\
221 c0.user) as 'メンバー',\
222 sum(case when c1.key is not null then 1 else 0 end)\
223 as 'コメント記入',\
224 sum(case when c2.key is not null then 1 else 0 end)\
225 as 'ファイルの提出'\
226 from $sample c0 \
227 left join (select id,author from arts) a\
228 on c0.user=a.author\
229 left join (select id,key from article_s where key='text') c1\
230 on a.id=c1.id left join (select id,key from article_m ) c2\
231 on c1.id=c2.id group by c0.user order by c0.user;"
232 err ishandoutsub: sql="$sql"
233 echo '<table class="b td2r td3r">'
234 hrb="<a href=\"?home+"
235 echo "$sql" | sq -header -html $db \
236 | sed -e "s,\(<TR><TD>\)\([^ ]*\) \(.*\)</TD>,\1$hrb\2\">\3</TD>," -e 's,<TD>0</TD>,<TD class="warn">0</TD>,'
237 echo '</table>'
238 }
239 gethandout() {
240 # $1=rowid of blog
241 i=0
242 bd=$tmpd/archive.$$
243 mkdir $bd
244 query "select m.rowid,author,m.val from article a join article_m m\
245 on a.id=m.id where blogid=(select id from blog where rowid=$1)\
246 and m.key in ('image', 'document', 'binary');" \
247 | while IFS='|' read rowid author filename; do
248 # err isfilereadable $user article_m $1
249 isfilereadable $user article_m $1 || continue
250 err ok
251 i=$((i+1))
252 dir=`printf $bd/%03d $i`
253 mkdir $dir
254 query "select quote(bin) from article_m where rowid=$rowid;" \
255 | unhexize > $dir/$filename
256 done
257 if [ ! -d $bd/001 ]; then
258 contenttype; echo
259 echo "<p>取得できるファイルがありませんでした。</p>"
260 return
261 fi
262 (cd $bd
263 err cdto$bd; (pwd; ls -lFa) 1>&3
264 tar zcf .archive.tar.gz * && mv .archive.tar.gz archive.tar.gz
265 )
266 arc=$bd/archive.tar.gz
267 echo "Content-type: application/x-gzip"
268 echo "Content-Length: `cat $arc|wc -c`"
269 echo "Content-Disposition: filename=\"archive.tar.gz\""
270 echo
271 cat $arc
272 }
273
274 listblog() (
275 # $1=user
276 cond="where a.id in (select id from blog_s where key='owner' and val='$1') order by ctime desc"
277 DT_CHLD=article:blogid
278 cgi_form searchart<<EOF
279 <label>`cgi_text kwd`という語を含む記事を検索</label>
280 `cgi_hidden owner $user`
281 EOF
282 dumptable html blog 'ctime title heading' "$cond"
283 )
284
285 blog_addentry() {
286 # $1=GRPname(if it is a group)
287 grprowid=$1
288 rowid=`getpar rowid`
289 err ba: rowid=$rowid
290 #if [ -z "$rowid" ]; then
291 # When rowid is SET, it is updation of existing entry
292 if [ -z "$1" ]; then
293 listing=$user guide="[個人]"
294 #listing代入は rowid 時でもするべき
295 else
296 grp=`getgroupbyid $grprowid`
297 if [ -n "$grp" ]; then
298 listing=$1 guide="[${grp}]" GF_OWNER=$grp
299 else
300 echo "<p>無効なグループ指定です。</p>"
301 return
302 fi
303 fi
304 #fi
305 if [ -n "`getpar title`" ]; then
306 owner=`getpar owner`
307 if isuser $owner; then
308 if [ x"$user" != x"$owner" ]; then
309 echo "<p>他人の日記は書けません</p>"
310 return
311 fi
312 elif isgroup $owner; then # if write to group log
313 grp=$owner #\`getpar grp\`
314 err ismember: $user $grp
315 if ! ismember "$user" "$grp"; then
316 echo "<p>(話題作成はこのグループに加入してから)</p>"
317 return
318 fi
319 fi
320 par2table $formdir/blog.def
321 serial=`getpar serial`
322 err SERIAL: $serial ROWID=$rowid listing=$listing
323 id=""
324 if [ -n "$rowid" ]; then
325 # Here, id becomes NULL when removal of entries at par2table
326 id=`query "select rowid from blog where rowid=$rowid;"`
327 elif [ -n "$serial" ]; then
328 # If new blog leader created, traverse to its head.
329 id=`query "select rowid from blog where id='$serial';"`
330 fi
331 if [ -n "$id" ]; then
332 ## If new aritcle is entered, JUMP to blog_reply
333 blog_reply $id
334 return
335 fi
336 fi
337 m4 -D_TITLE_="${guide}新規話題作成" -D_DIARY_="新規話題の記入" \
338 -D_SUBTITLE_="序文は簡単に詳しくはコメントに" \
339 -D_BLOGS_="これまでの蓄積" \
340 -D_FORM_="`genform $formdir/blog.def`" \
341 -D_DUMPTABLE_="`listblog $listing`" \
342 $layout/html.m4.html \
343 $layout/diary.m4.html
344 }
345
346 blog_reply() {
347 rowid=$1
348 err rowid=$1
349
350 if [ -z "$rowid" ]; then
351 echo "<p>表示する日記番号が未指定です。</p>"
352 return
353 fi
354 title=`getvalbyid blog title $rowid`
355 owner=`getvalbyid blog owner $rowid`
356 if isuser "$owner"; then
357 subtitle="`gecos $owner` さんの話題"
358 else
359 grprowid=`query "select rowid from grp where gname=\"$owner\";"`
360 subtitle="<a href=\"?grp+$grprowid\">`gecos $owner`</a> での話題"
361 fi
362 if [ -z "$title" ]; then
363 echo "<p>日記番号指定が無効です。</p>"
364 return
365 fi
366
367 text=`getpar text`
368 if [ -n "$text" ]; then
369 if blog_writable $rowid $user; then
370 if par2table $formdir/article.def; then
371 blog_notify_reply $rowid $user "$text"
372 fi
373 else
374 title="$title(加入してないので書き込み不可)"
375 fi
376 fi
377 def=$formdir/article.def
378 cat $layout/html.m4.html $layout/diary.m4.html \
379 | sed '/_DIARY_/q' \
380 | m4 -D_TITLE_="$title" -D_DIARY_="" -D_BODYCLASS_=general \
381 -D_SUBTITLE_="$subtitle"
382 blog_showentry blog $rowid
383 sed '1,/_DIARY_/d' $layout/diary.m4.html | m4 -D_FORM_= -D_DUMPTABLE_=
384 }

yatex.org