]> git.plutz.net Git - serve0/blobdiff - widgets.sh
improved tag caching, removed obsolete HTML unescaping
[serve0] / widgets.sh
index 658d2bb2291d87bdf979f6912a6f5ecd62e3139c..fc6818e206748cb2c933e409e35afc52b0c14bbe 100644 (file)
@@ -5,61 +5,133 @@ include_widgets="$0"
 
 . "$_EXEC/cgilite/storage.sh"
 
-w_refuri="$(HTML "$REQUEST_URI")"
+w_refuri="$(URL "$PATH_INFO")?$(HTML "$QUERY_STRING")"
 
-w_tags="$(find "$_DATA/" -path '*/.index/meta' -print0 \
-        | xargs -r0 cut -f4 \
-        | cut -d= -f2- |tr , '\n' | sort -u)"
-w_tags="$(UNSTRING "$w_tags")"
-w_tagcategories="$(printf %s "$w_tags" |cut -sd: -f1 |sort -u)"
+w_str_s="$(STRING "$SEARCH")"
+w_str_f="$(STRING "$FILTER")"
 
-CHECK(){ [ "$1" = "$2" ] && printf checked; }
+c_tags="$_DATA/.index/tags.cache"; c_tagcategories="$_DATA/.index/tagcategories.cache"
+if [ ! -s "$c_tags" -o ! -s "$c_tagcategories" ] \
+   || [ "$(find "$_DATA/" -path '*/.index/meta' -newer "$c_tags")" ]
+  then
+  w_tags="$( local cn=1
+    find "$_DATA/" -path '*/.index/meta' -print0 \
+    | xargs -r0 sed -r '
+      s;^.*\t.*\t.*\ttags=(.*)\tcomment=.*\t.*\r$;\1;;
+      s;,;\n;g;'"$UNSTRING" \
+    | { sort; printf '\n'; } \
+    | while read -r tag; do
+      [ "$tag" = "$otag" ] \
+      && cn=$((cn + 1)) \
+      || {
+        printf "%i     %s\n" "$cn" "$otag"
+        cn=1
+      }
+      otag="$tag"
+    done \
+    |sort -rn |cut -f2- |HTML \
+    |sed "s-
-\n-g; s;\n\n;\n;g;"
+  )"
+  w_tagcategories="$(printf %s "$w_tags" | sed -rn '/:/s;^-?([^:]+):.*$;\1;p' |sort -u )"
+  printf %s "$w_tags" >"$c_tags"
+  printf %s "$w_tagcategories" >"$c_tagcategories"
+else
+  w_tags="$(cat "$c_tags")"
+  w_tagcategories="$(cat "$c_tagcategories")"
+fi
+
+
+[ "$ORDER" = Name   ] && w_coname=checked
+[ "$ORDER" = Date   ] && w_codate=checked
+[ "$ORDER" = Length ] && w_colength=checked
+[ "$ORDER" = Group  ] && w_cogroup=checked
+
+w_bmname=
+w_bmname(){
+  [ "$w_bmname" ] || w_bmname="$(
+    bm="$_DATA/.index/bookmarks"
+    name="$(grep -m1 -aF "     search=${w_str_s}       filter=${w_str_f}${CR}" "$bm" 2>&-)"
+
+    if [ "$name" ]; then
+      printf '%s' "$name" |cut -f1 |UNSTRING |HTML
+    else
+      printf '%s\t%s' "$SEARCH" "$FILTER" \
+      | sed -r '/^\t$/{  s;\t;All;; q;}
+                /.*\t$/{ s;\t$;;; q;}
+                /^\t.*/{ s;^\t;;;
+                         :x; s;(^|[~^|])([^|^~:]+):;\1;; tx;
+                         s;\^; and ;g; s;\|;,;g; s;~;not ;g; q;}' \
+      | HTML
+    fi
+  )"
+  printf '%s' "$w_bmname"
+}
 
 w_bookmarks(){
-  local name link bm
-  bm="$_DATA/.index/bookmarks"
+  local bm="$_DATA/.index/bookmarks" name='' search='' filter=''
+  [ ! -d "${bm%/*}" ] && return 0
+  [ ! -f "$bm" ] && touch "$bm"
+
+  grep -qaF "  search=$w_str_s filter=${w_str_f}${CR}" "$bm" && name=Update || name=Add
 
-  printf '[form #bookmarks action=?a=bookmarks method=POST
-            [a href="#" X]
+  printf '[form #bookmarks action=?a=bookmark method=POST
+            [a href="#" x]
             [hidden "ref" "%s"]
-          ' "$w_refuri"
+            [hidden "search" "%s"][hidden "filter" "%s"]
+            [label Name for current page:]
+            [input name="name" value="%s" placeholder="Name" ]
+            [button type="submit" %s]' \
+            "$w_refuri" \
+            "$(HTML "$SEARCH")" "$(HTML "$FILTER")" \
+            "$(w_bmname)" "${name}"
+  [ "$name" ] && printf ' [submit "delete" "delete" Delete]'
 
-  name="$(grep -m1 -F "        $(STRING "${REQUEST_URI}")" "$bm")"
-  printf '[hidden "link" "%s"][input name="name" value="%s"]' \
-         "$(HTML "$link")" "$(HTML "$name")"
+  sort "$bm" |while read -r name search filter; do
+    search="${search#search=}" filter="${filter#filter=}" filter="${filter%${CR}}"
+    [ "$search" = "${w_str_s}" -a "$filter" = "${w_str_f}" ] && continue
 
-  sort "$bm" |while read -r name link; do
-    name="$(UNSTRING "$name")"
-    link="$(UNSTRING "$link")"
-    if [ "$link" != "$REQUEST_URI" ]; then
-      printf '[a href="%s"\n%s]' "$(HTML "$link")" "$(HTML "$name")"
-    fi
+    name="$(UNSTRING "$name")";
+    search="$(UNSTRING "${search}" |URL)";
+    filter="$(UNSTRING "${filter}" |URL)";
+    printf '[label .link %s]
+            [a .link target=blank href="/?o=Name&s=%s&f=%s" by Name]
+            [a .link target=blank href="/?o=Date&s=%s&f=%s" by Date]
+            [a .link target=blank href="/?o=Length&s=%s&f=%s" by Length]
+            [a .link target=blank href="/?o=Group&s=%s&f=%s" by Group]
+            [br]' \
+            "$(HTML "$name" |sed 's;&#44\;;&[wbr];g;')" \
+            "$search" "$filter" \
+            "$search" "$filter" \
+            "$search" "$filter" \
+            "$search" "$filter"
   done
   printf ']'
 }
 
 w_search(){
   printf '
-  [form #search method=GET action=?
+  [form #search method=GET action=./?
     [select name=o size=1 
       [option disabled=disabled Order By]
       [option value=Name %s Name]
       [option value=Date %s Date]
       [option value=Length %s Length]
+      [option value=Group %s Group]
     ]
     [input name=s placeholder=Search value="%s"]
   ]
   ' \
-  "$(CHECK "$ORDER" Name)" \
-  "$(CHECK "$ORDER" Date)" \
-  "$(CHECK "$ORDER" Length)" \
+  "$w_coname" "$w_codate" "$w_colength" "$w_cogroup" \
   "$(HTML "$SEARCH")"
 }
 
 w_prefs(){
-  local fakemp4 mode
-  fakemp4="$(COOKIE fakemp4)"
-  mode="$(COOKIE mode)"
+  local tm tf td
+
+  tm=''; [ "$(COOKIE mode)" = index ] && tm=' '
+  tf=''; [ "$(COOKIE fakemp4)" = yes ] && tf=checked
+  td=''; [ "$(COOKIE downscale)" = yes ] && td=checked
+
   printf '
   [form #prefs method="POST" action="?a=setprefs"
     [a href="#" x]
@@ -68,18 +140,18 @@ w_prefs(){
     [input #prefs_ps type=number name=pagesize value="%s"][br]
     [radio "mode" "browse" %s #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
     [radio "mode" "index"  %s #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
-    [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file ending][br]
+    [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file type][br]
+    [checkbox "downscale" "yes" %s #prefs_downscale] [label for=prefs_downscale Prefer downscale to 480p][br]
+    [submit "index" "update" Force Index Update][br]
     [submit "store" "store" Set Cookie]
   ]
   ' \
   "$w_refuri" "$LISTSIZE" \
-  "$([ "$mode" = index  ] || CHECK)" \
-  "$(CHECK "$mode" index)" \
-  "$(CHECK "$fakemp4" yes)"
+  "${tm:-checked}" "${tm:+checked}" "$tf" "$td"
 }
 
 w_index(){
-  [ -d "$_DATA/$ITEM/.index" ] || printf '
+  printf '
   [form #index method="POST" action="?a=spawnindex"
     [hidden "ref" "%s"]
     [label Set up for Index view: ]
@@ -87,50 +159,58 @@ w_index(){
     [submit "spawn" "spawn" Set up]
   ]
   ' "$w_refuri"
+  return 0
 }
 
 w_advsearch(){
-  local n lbid tag category tn filter f
-  filter="${FILTER}^"
+  local n lbid tag category filter f t d
+  filter="$(HTML "${FILTER}^")"
 
-  printf '[form #advsearch action=?a=advsearch method=POST
+  printf '[form #advsearch action=./?a=advsearch method=POST
             [a href="#" Hide]
             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.[br]
-            Refine the search further by setting additional search tags using the [strong "+and"] button.]
-         '
+            Refine the search further by setting additional search tags using the [strong "+and"] button.]'
 
   for n in 1 2 3 4 5 6 7 8 9 10; do
     f="${filter%%^*}"; filter="${filter#*^}"
 
-    lbid="$(HTML "cat_${n}_(none)")"
+    t=''; [ "$f" -a ! "${f%%~*}" ] && t=" "
+
+    lbid="cat_${n}_(none)"
     printf '[input .and type=checkbox name=and id="and_%i" %s][label for="and_%i" +and
             ][fieldset .select
             [radio "pol_%i" "pos" .pol %s #pol_pos_%i"][label for=pol_pos_%i Any]
             [radio "pol_%i" "neg" .pol %s #pol_neg_%i"][label for=pol_neg_%i None]
             [label .head Category:]' \
-            $n "$([ "$f" ] && printf checked)" $n \
-            $n "$([ "$f" -a ! "${f%%~*}" ] || printf checked)" $n $n \
-            $n "$([ "$f" -a ! "${f%%~*}" ] && printf checked)" $n $n
-    f="|${f#~}|"
+            $n "${f:+checked}" $n \
+            $n "${t:-checked}" $n $n \
+            $n "${t:+checked}" $n $n
 
+    f="|${f#\~}|"
     printf '*\n%s\n' "$w_tagcategories" \
     | while read -r category; do
-      catn="$(HTML "$category")"
-      lbid="cat_${n}_${catn}"
-      printf '[radio "cat_%i" "%s" .cat id="%s" %s][label for="%s" %s]
+      lbid="cat_${n}_${category}"
+
+      t=''
+      [ "$category"  = '*' -a   "${f%%|${category}:*}" ] && t=checked
+      [ "$category" != '*' -a ! "${f%%|${category}:*}" ] && t=checked
+      [ "$category" != '*' -a ! "${f%%|-${category}:*}" ] && t=checked
+
+      printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
               [select name=tag_%s size=10 multiple' \
-              $n "$catn" "$lbid" \
-              "$([ ! "${f%%*|${category}:*}" -o ! "${category%\*}" ] && printf checked)" \
-              "$lbid" "$catn" $n
-
-      printf %s "$w_tags" \
-      | { [ "$category" = '*' ] && grep -vF ':' || grep -wF "${category}"; } \
-      | while read -r tag; do
-        [ ! "$tag" ] && continue
-        tg="$(HTML "$tag")"; tn="${tg#*:}"
-        printf '[option %s value="%s"\n%s]' \
-          "$([ ! "${f%%*|${tag}|*}" ] && printf selected)" \
-          "$tg" "$tn"
+              $n "$category" "$t" "$lbid" "$lbid" "$category" $n
+
+      printf '%s\n' "$w_tags" \
+      | { [ "$category" = '*' ] && grep -avF ':' || grep -awF "${category}"; } \
+      | { for n in 1 2 3 4 5 6 7 8 9 0; do
+          read -r line && printf '%s\n' "$line" || break;
+          done;  # pass 10 lines through without modification
+          sort;  # and sort remaining lines
+      } | while read -r tag; do
+        [ "$tag" ] || continue
+        t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
+        d="${tag#-}"; d="${d#*:}"
+        printf '[option %s value="%s"\n%s]' "$t" "$tag" "$d"
       done
       printf '\n]'
     done
@@ -142,30 +222,47 @@ w_advsearch(){
             [option value=Name %s Name]
             [option value=Date %s Date]
             [option value=Length %s Length]
+            [option value=Group %s Group]
           ][button type=submit Apply Filter]]
           ]' \
-          "$(CHECK "$ORDER" Name)" \
-          "$(CHECK "$ORDER" Date)" \
-          "$(CHECK "$ORDER" Length)"
+          "$w_coname" "$w_codate" \
+          "$w_colength" "$w_cogroup"
+}
+
+w_delete(){
+  printf '[a href="#multitag" Add Tags / Remove Tags]
+          [div #multitag [input type="hidden" name="ref" value="%s"]
+          [a href="#" Hide][br]
+          [fieldset [legend New:]
+          [submit "op" "filedelete" Delete Files]
+          ]]' "$w_refuri"
 }
 
 w_tagging(){
-  local tag category
-  printf '[div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
+  local tag category d
+  printf '[a href="#multitag" Add Tags / Remove Tags]
+          [div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
   printf '[a href="#" Hide][br]'
 
   printf 'Tags\n%s\n' "$w_tagcategories" \
   | while read -r category; do
-    printf '[fieldset [legend %s:][select name=tag size=4 multiple' "$(HTML "$category")"
+    [ "$category" ] || continue
+    printf '[fieldset [legend %s:][select name=tag size=4 multiple\n' "$category"
     printf %s "$w_tags" \
-    | { [ "$category" = 'Tags' ] && grep -vF ':' || grep -wF "${category}"; } \
-    | while read -r tag; do
-      [ ! "$tag" ] && continue
-      tag="$(HTML "$tag")"; tn="${tag#*:}"
-      printf '[option value="%s"\n%s]' "$tag" "$tn"
+    | { [ "$category" = 'Tags' ] && grep -avF ':' || grep -awF "${category}"; } \
+    | { for n in 1 2 3 4 5 6 7 8 9 0; do
+        read -r line && printf '%s\n' "$line" || break
+        done;
+        sort;
+    } | while read -r tag; do
+      [ "$tag" ] || continue
+      d="${tag#-}"; d="${d#*:}"
+      printf '[option value="%s"\n%s]' "$tag" "$d"
     done
     printf ']]'
   done
 
-  printf '[fieldset [legend New:][textarea name=newtag\n][button type=Submit Add Tags]]]'
+  printf '[fieldset [legend New:][textarea name=newtag\n]
+          [submit "op" "del" Remove Tags][submit "op" "add" Add Tags]
+          ]]'
 }