changeset 771:b97f1c84bdb1 feature-world

New script s4-newworld supports NewWorld creation
author HIROSE Yuuji <yuuji@gentei.org>
date Tue, 09 Jun 2020 12:26:15 +0900
parents c06c2df8a25a
children 825646678e33
files s4-funcs.sh s4-newworld.sh s4-world.sh
diffstat 3 files changed, 133 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/s4-funcs.sh	Mon Jun 08 20:23:42 2020 +0900
+++ b/s4-funcs.sh	Tue Jun 09 12:26:15 2020 +0900
@@ -2,7 +2,7 @@
 # Here's global variable table.  Do not use this names.
 # $HGid$
 
-test -n "$HTTP_HOST" && isCGI=true
+test -n "$HTTP_HOST" && isCGI=true || isCGI=false
 if $isCGI; then
   case "$SCRIPT_NAME" in
     *-world-*)
@@ -20,6 +20,7 @@
 fi
 myname=`basename ${SCRIPT_NAME:-$0}`
 mydir=`dirname ${SCRIPT_FILENAME:-$0}`
+cgiext=${CGIEXT:-.cgi}
 myargs="$@"
 PATH=/usr/local/sqlite3/bin:/usr/local/vim7/bin:/usr/iekei/ImageMagick/bin:/usr/local/ImageMagick/bin:$PATH
 tmpdir=${TMPDIR:-tmp}
@@ -93,19 +94,46 @@
 if [ -n "$S4MASTERDB" -a -s "$S4MASTERDB" ]; then
   . ./s4-world.sh 2>> tmp/debug.out
 fi
-v=""
-for i in $S4WORLDLIST; do
-  _aname=${i%:*}; _apath=${i##*:}
-  v=${v}"<tr><td>&rArr; <a href=\"$_apath\">$_aname</a></td></tr>$nl"
-done
-if [ -n "$v" ]; then
-  err S4MASTERURL=$S4MASTERURL
-  i="<tr><th>World List</th></tr>$nl"
-  if [ -n "$S4MASTERURL" ]; then
-    i="$i<tr><td>&rArr; <a href=\"$S4MASTERURL\">Base</a></td></tr>$nl"
-  fi
-  S4WORLDS="${touchpanel:+ : ▼}<div><table>$i$v</table></div>"
-fi
+case "$S4WORLDLIST" in
+  *:*:*)
+    worldlistfile=cache/worldlist
+    worldoptionfile=cache/worldoption
+    if [ ! -e $worldlistfile -o $worldlistfile -ot s4-config.sh \
+	   -o ! -e $worldoptionfile -o $worldoptionfile -ot s4-config.sh ]
+    then
+      echo S4MASTERURL=$S4MASTERURL >> tmp/debug.out
+      cat <<-EOF > $worldlistfile
+	${touchpanel:+ : ▼}<div><table>
+	 <tr><th>World List</th></tr>
+	 <tr><td>&rArr; <a href="$S4MASTERURL">Base</a></td></tr>
+	EOF
+      true > $worldoptionfile
+      for i in $S4WORLDLIST; do
+	echo $i | {
+	  IFS=: read name short d
+	  cgi="s4-world-$short$cgiext"
+	  conf="s4-config-$short.sh"
+	  cat<<-EOF >>$worldlistfile
+	    <tr><td title="$d">&rArr; <a href="$cgi">$name</a></td></tr>
+		EOF
+	  cat<<-EOF >>$worldoptionfile
+	    <option title="$d" value="$conf">$name</option>
+		EOF
+	}
+	if [ -n "$S4MASTERDB" ]; then
+	  echo "<option value=\"s4-config.sh\">Base</option>" \
+	       >> $worldoptionfile
+	fi
+      done
+      if [ -s "$worldoptionfile" ]; then
+	echo "</table>$nl</div>" >> $worldlistfile
+	S4WORLDS=": spaste(\`$worldlistfile')"
+      else
+	true > $worldoptionfile; true > $worldlistfile	# Remove contents
+      fi
+    fi
+    ;;
+esac
 
 
 [ -f ./s4-cgi.sh ] && . ./s4-cgi.sh
@@ -411,7 +439,7 @@
   #tail -f $sqi | sq $db &	# "tail -f" is too heavy. DO NOT USE!!
   sq  $db < $sqi &
   sq3pid="`jobs -p` $!"
-  if [ -n "$isCGI" ]; then
+  if $isCGI; then
      exec 2>> $tmpdir/error.out
   fi
   exec 5> $sqi # Turning $sqi access through fd5 for continuous open state
@@ -463,7 +491,7 @@
 _m4() {
   #S4NAME=f,f,f
   m4 ${S4NAME:+"-D_S4NAME_=${S4NAME}"} ${S4CSS:+-D_S4CSS_="$S4CSS"} \
-     -D_S4WORLDS_="${S4WORLDS}" "$@"
+     -D_S4WORLDS_="$S4WORLDS" "$@"
 }
 ismember() {
   # $1=user, $2=group
@@ -1865,17 +1893,7 @@
   GF_STAGE="groupupdate" edittable "$formdir/grp.def" "grp" "$rowid"
   if [ -z "$STOPCLONEMSG" ]; then
     ## Setup migration menu
-    v=""
-    for i in $S4WORLDLIST; do
-      _aname=${i%:*}; _apath=${i##*:}
-      conf=`echo $_apath | sed -e 's/-world-/-config-/;s/\..*$/.sh/'`
-      if [ x"$myname" != x"$_apath" ]; then
-	v=${v}"<option value=\"$conf\">$_aname</option>$nl"
-      fi
-    done
-    if [ -n "$S4MASTERDB" ]; then
-      v=${v}"<option value=\"s4-config.sh\">Base</option>$nl"
-    fi
+    v=`fgrep -v "value=\"$worldconf\"" $worldoptionfile`
     err v=$v
     if [ -n "$v" ]; then
       migrate=$(cat<<-EOF
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/s4-newworld.sh	Tue Jun 09 12:26:15 2020 +0900
@@ -0,0 +1,84 @@
+#!/bin/ksh
+#!/bin/sh
+# Create New Wolrd in s4
+# Arguments:
+#   $1 = World Display Name in UTF-8
+#   $2 = World ShortName in alpha-numeric
+#   $3 = World Description in UTF-8
+. `dirname $0`/s4-config.sh
+if ! type htmlescape >/dev/null 2>&1; then
+  . `dirname $0`/s4-funcs.sh ### > /dev/null 2>&1
+fi
+dispname=$1
+shortname=$2
+desc=$3
+
+readvar() {	# $1=varname $2=PromptString
+  echo -n "${2:-$1=:} "
+  read $1
+}
+
+echo db=$db URL=$URL isCGI=$isCGI
+if ! $isCGI; then
+  while true; do
+    dispname=`echo $dispname | tr -d ': \t\n"' | fold -w 28 | head -1`
+    test -n "$dispname" && break
+    readvar dispname "分かりやすいWorld名14字以内"
+  done
+  while true; do
+    shortname=`echo $shortname | tr -c -d '_0-9A-Za-z.-' | colrm 11`
+    test -n "$shortname" && break
+    readvar shortname "英数字のみ10字以内のWorldシンボル(URLの一部)"
+  done
+  while true; do
+    desc=`echo $desc | tr -d ': \t\n"'`
+    test -n "$desc" && break
+    readvar desc "概要(どのような基準でこのWorldを使うかなど)"
+  done
+fi
+echo "wl=$S4WORLDLIST"
+echo "$dispname:$shortname:$desc"
+newworld=$(
+  { echo "$S4WORLDLIST" | tr ' ' '\n' \
+      | awk -F: "\$2 != \"$shortname\"{print}"
+    echo "$dispname:$shortname:$desc"
+  } | tr '\n' ' ' | tr -d '"'
+	)
+if [ -z "$newworld" ]; then
+  exit
+fi
+
+# Create config
+bgcolor=$(
+  od -An -tu1 -N3 < /dev/urandom \
+    | { read r g b
+	r=$((192+r/4)); g=$((192+g/4)); b=$((192+b/4))
+	printf "#%x%x%x" $r $g $b
+  })
+cat<<-EOF > s4-config-$shortname.sh
+	S4MASTERURL=\$URL
+	URL=`dirname $URL`/s4-world-$shortname$cgiext
+	S4COLOR="$bgcolor"			# Change this!
+	DB=$dbdir/$shortname.sq3
+	SESSDB=$dbdir/sess.sq3
+	S4MASTERDB=$db
+	S4CSS=$shortname.css
+	TMPDIR=$tmpdir/$shortname
+EOF
+# Create CSS
+cat<<-EOF > $shortname.css
+	body {background: $bgcolor;}
+	body.moderated {background: $bgcolor; border: 3px gold solid;}
+EOF
+mkdir -m 1775 $tmpdir/$shortname
+# Update s4-config.sh
+cat<<-EOF | ed s4-config.sh
+	g/^S4WORLDLIST=/d
+	\$a
+	S4WORLDLIST="`echo $newworld`"
+	.
+	wq
+EOF
+DB=db/$shortname.sq3 `dirname $0`/s4-init.sh
+(cd `dirname $0`; ln -s s4$cgiext s4-world-$shortname$cgiext)
+echo $newworld added
--- a/s4-world.sh	Mon Jun 08 20:23:42 2020 +0900
+++ b/s4-world.sh	Tue Jun 09 12:26:15 2020 +0900
@@ -10,9 +10,9 @@
 # Now Another world is ACTIVE
 # sessdb=`dirname $S4MASTERDB`/sess.sq3
 ## skey="skey-`basename $mydir`"
-S4NAME="[$S4WORLD]$S4NAME"
 syncflag=${db%.*}.synctime
 userupdateflag=`dirname $S4MASTERDB`/`basename $userupdateflag`
+err SYNCFLAG=$syncflag
 test ! -e "$userupdateflag"		&& return
 test "$syncflag" -nt "$userupdateflag"	&& return
 
@@ -27,7 +27,8 @@
 ## sqlite3 -cmd '.timer 1' -cmd '.echo 1' $db <<EOF
 err "Starting account synchronization"
 err "db=$db mas=$S4MASTERDB sessdb=$sessdb"
-num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db <<EOF
+# num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db <<EOF
+cat > tmp/sql <<EOF
 ATTACH DATABASE "$S4MASTERDB" AS m;
 CREATE TABLE IF NOT EXISTS user(name, primary key(name));
 BEGIN;
@@ -65,7 +66,7 @@
 -- SELECT rowid,* FROM user WHERE rowid NOT IN (SELECT rowid FROM m.user);
 DETACH DATABASE m;
 EOF
-)
+num=$(sqlite3 -bail -cmd 'PRAGMA FOREIGN_KEYS=on' $db < tmp/sql )
 if [ $num -eq 0 ]; then
   touch $syncflag
 fi

yatex.org