]> git.plutz.net Git - shellwiki/blobdiff - handlers/60_move_rename_delete.sh
Page titles for special function pages
[shellwiki] / handlers / 60_move_rename_delete.sh
index 33f79d00270f381636c1f110765de79f6277444d..f399e0388bf907cd9cc198d5f0ba6a0280b5c849 100755 (executable)
@@ -1,5 +1,19 @@
 #!/bin/sh
 
+# Copyright 2022 - 2023 Paul Hänsch
+# 
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+# 
+# THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 l10n_immutablepage >/dev/null 2>&1 \
 || l10n_immutablepage(){  #TRANSLATION
   cat <<-EOF
@@ -19,7 +33,7 @@ case "${PATH_INFO}" in
       printf 'Refresh: %i, url=%s\r\n' 4 ./
       theme_error 403
       return 0
-    elif [ -d "$_EXEC/pages/${page}/" ]; then
+    elif [ ! -d "$_DATA/pages/${page}" -a -d "$_EXEC/pages/${page}" ]; then
       theme_page - <<-EOF
        <article>
          <p class=error>
@@ -38,7 +52,7 @@ l10n_movepage >/dev/null 2>&1 \
   cat <<-EOF
          <h1>Move Page</h1>
          <p class="pageid">$(HTML "${page}")</p>
-         <input name="newlocation" value="$(HTML "$location")" placeholder="New Location">
+         <input list="locations" autocomplete="off" name="newlocation" value="$(HTML "$location")" placeholder="New Location">
          <ul>
            <li>A page with the same name must not already exist at the new location.</li>
            <li>You must have permission to create new pages at this location.</li>
@@ -54,7 +68,7 @@ l10n_renamepage >/dev/null 2>&1 \
   cat <<-EOF
          <h1>Rename Page</h1>
          <p class="pageid">$(HTML "${page}")</p>
-         <input name="newname" value="$(HTML "$name")" placeholder="New Name">
+         <input autocomplete="off" name="newname" value="$(HTML "$name")" placeholder="New Name">
          <ul>
            <li>A page with the new name must not already exist.</li>
            <li>You must have permission to create new pages at this location.</li>
@@ -71,26 +85,53 @@ l10n_deletepage >/dev/null 2>&1 \
          <h1>Delete Page</h1>
          <p class="pageid">$(HTML "${page}")</p>
          <p>This page and its attachments will be deleted</p>
+         <input type=checkbox name=delete_subpages value=true id=delsub><label for=delsub>Delete subpages</label>
          <ul>
-           <li>Past revisions of the page text (including the current one) will remain accessible and can be restored.</li>
-           <li>Attachments will be deleted completely, and cannot be restored.</li>
-           <li>Subpages will not be affected and can still be accessed normally.</li>
+       EOF
+  [ "$REV_PAGES" = true ] \
+  && printf '    <li>Past revisions of the page text (including the current one) will remain accessible and can be restored.</li>'
+  [ "$REV_PAGES" = true -a "$REV_ATTACHMENTS" = true ] \
+  && printf '    <li>Attachments can be restored as well.</li>' \
+  || printf '    <li>Attachments will be deleted completely, and cannot be restored.</li>'
+  cat <<-EOF
+           <li class="delsub">Subpages will not be affected and can still be accessed normally.</li>
          </ul>
          <button type=submit name=action value=delete>Delete</button>
          <button type=submit name=action value=cancel>Cancel</button>
        EOF
 }
 
+list_writable() {
+  local PATH_INFO page="${1%/}/"
+
+  if acl_write "$page"; then
+    PATH_INFO="$page"
+    page_glob "*" 0 \
+    | while read page; do
+      list_writable "${PATH_INFO}${page}"
+    done
+    printf %s\\n "$page" |debug
+  fi
+}
+
 if [ "$REQUEST_METHOD" = POST ]; then
   action="$(POST action)"
   newname="$(POST newname |grep -m1 -xE '[^#/]*')"
   newlocation="$(POST newlocation |grep -m1 -xE '/[^#]*')"
+  delsub="$(POST delete_subpages |grep -m1 -xE 'true|false')"
 else case "${PATH_INFO}" in
   */\[move\])
       location="${page%/}" location="${location%/*}/"
-      theme_page - <<-EOF
+      theme_page - "$(_ Move): ${PAGE_TITLE}"<<-EOF
        <form method=POST id=movepage>
          <input type=hidden name=session_id value="$SESSION_KEY">
+         <datalist id="locations">
+         $(page_glob / -1 |while read loc; do
+           [ "$loc" = "$page" ] && continue
+           acl_write "$loc" || continue
+           printf '\n    <option>%s</option>' "$(HTML "$loc")"
+         done)
+         </datalist>
          $(l10n_movepage)
        </form>
        EOF
@@ -98,7 +139,7 @@ else case "${PATH_INFO}" in
     ;;
   */\[rename\])
       name="${page%/}" name="${name##*/}"
-      theme_page - <<-EOF
+      theme_page - "$(_ Rename): ${PAGE_TITLE}"<<-EOF
        <form method=POST id=renamepage>
          <input type=hidden name=session_id value="$SESSION_KEY">
          $(l10n_renamepage)
@@ -107,7 +148,7 @@ else case "${PATH_INFO}" in
       return 0
     ;;
   */\[delete\])
-      theme_page - <<-EOF
+      theme_page - "$(_ Delete): ${PAGE_TITLE}"<<-EOF
        <form method=POST id=deletepage>
          <input type=hidden name=session_id value="$SESSION_KEY">
          $(l10n_deletepage)
@@ -124,22 +165,27 @@ if [ "$action" = rename -a "$newname" ]; then
 
   if [ -d "$_DATA/pages/$newname" ]; then
     printf 'Refresh: %i\r\n' 4
-    export ERRORMSG="A location of that name already exists."
+    export ERROR_MSG="A location of that name already exists."
     theme_error 400
     return 0
   elif ! acl_write "$oldname" || ! acl_write "$newname"; then
     printf 'Refresh: %i\r\n' 4
     theme_error 403
     return 0
-  elif [ "$REV_PAGES" = true ]; then
+  fi
+  if [ "$REV_PAGES" = true ]; then
     git -C "$_DATA" mv "pages/$oldname" "pages/$newname"
     git -C "$_DATA" commit -m 'Page # '"$oldname"' # renamed to # '"$newname"' # by user @ '"$USER_NAME"' @' \
         -- "pages/$oldname" "pages/$newname"
-    REDIRECT "$_BASE${newname}"
   else
     mv -- "$_DATA/pages/$oldname" "$_DATA/pages/$newname"
-    REDIRECT "$_BASE${newname}"
   fi
+  if [ "$SEARCH_INDEX" = true ]; then
+    find "$_DATA/pages/$newname" -name "#index.flag" -delete
+    ( "$_EXEC/searchindex.sh" index --location "$newname" & ) &
+  fi
+  REDIRECT "$_BASE${newname}"
+
 elif [ "$action" = move -a "$newlocation" ]; then
   oldname="${PATH_INFO%\[*\]}"
   newlocation="$(PATH "$newlocation")"
@@ -148,51 +194,83 @@ elif [ "$action" = move -a "$newlocation" ]; then
 
   if [ -d "$_DATA/pages/$newname" ]; then
     printf 'Refresh: %i\r\n' 4
-    export ERRORMSG="A page of that name already exists at the given location."
+    export ERROR_MSG="A page of that name already exists at the given location."
     theme_error 400
     return 0
   elif [ ! -d "$_DATA/pages/$newlocation" ]; then
     printf 'Refresh: %i\r\n' 4
-    export ERRORMSG="The given location does not exist."
+    export ERROR_MSG="The given location does not exist."
     theme_error 400
     return 0
   elif ! acl_write "$oldname" || ! acl_write "$newname"; then
     printf 'Refresh: %i\r\n' 4
     theme_error 403
     return 0
-  elif [ "$REV_PAGES" = true ]; then
+  fi
+  if [ "$REV_PAGES" = true ]; then
     git -C "$_DATA" mv "pages/${oldname}" "pages/${newname}"
     git -C "$_DATA" commit -m 'Page # '"$oldname"' # moved to # '"$newname"' # by user @ '"$USER_NAME"' @' \
         -- "pages/${oldname}" "pages/${newname}"
-    REDIRECT "$_BASE${newname}"
   else
     mv -- "$_DATA/pages/$oldname" "$_DATA/pages/$newname"
-    REDIRECT "$_BASE${newname}"
   fi
+  if [ "$SEARCH_INDEX" = true ]; then
+    find "$_DATA/pages/$newname" -name "#index.flag" -delete
+    ( "$_EXEC/searchindex.sh" index --location "$newname" & ) &
+  fi
+  REDIRECT "$_BASE${newname}"
+
 elif [ "$action" = delete ]; then
   oldname="${PATH_INFO%\[*\]}"
   if ! acl_write "$oldname"; then
     printf 'Refresh: %i\r\n' 4
     theme_error 403
     return 0
-  elif [ "$REV_PAGES" = true ]; then
-    git -C "$_DATA" rm "pages/${oldname}/#page.md"
-    git -C "$_DATA" commit -m 'Page # '"$oldname"' # deleted by user @ '"$USER_NAME"' @' \
-        -- "pages/${oldname}/#page.md"
-    rm -r -- "$_DATA/pages/${oldname}"/\#*
-    rmdir -- "$_DATA/pages/${oldname}/" || true
-    REDIRECT ./
-  else
-    rm -- "$_DATA/pages/${oldname}/#page.md"
-    rm -r -- "$_DATA/pages/${oldname}"/\#*
-    rmdir -- "$_DATA/pages/${oldname}/" || true
-    REDIRECT ./
   fi
+
+  printf 'Status: 202 Accepted\r\n'
+  { [ "$delsub" = true ] \
+    && list_writable "$oldname" \
+    || printf %s\\n "$oldname"
+  } | while read oldname; do
+    if [ "$REV_PAGES" = true -a "$REV_ATTACHMENTS" = true ]; then
+      git -C "$_DATA" rm "pages/${oldname}/#page.md" >&2
+      git -C "$_DATA" rm -r "pages/${oldname}/#attachments/" >&2
+      git -C "$_DATA" commit -m 'Page # '"$oldname"' # deleted by user @ '"$USER_NAME"' @' \
+          -- "pages/${oldname}/#page.md" "pages/${oldname}/#attachments/" >&2
+      rm -r -- "$_DATA/pages/${oldname}"/\#*
+      rmdir -- "$_DATA/pages/${oldname}/" || true
+    elif [ "$REV_PAGES" = true ]; then
+      git -C "$_DATA" rm "pages/${oldname}/#page.md" >&2
+      git -C "$_DATA" commit -m 'Page # '"$oldname"' # deleted by user @ '"$USER_NAME"' @' \
+          -- "pages/${oldname}/#page.md" >&2
+      rm -r -- "$_DATA/pages/${oldname}"/\#*
+      rmdir -- "$_DATA/pages/${oldname}/" || true
+    else
+      rm -- "$_DATA/pages/${oldname}/#page.md"
+      rm -r -- "$_DATA/pages/${oldname}"/\#*
+      rmdir -- "$_DATA/pages/${oldname}/" || true
+    fi
+    printf '%s\n' "$oldname"
+  done | {
+    cat <<-EOF
+       <article id="deleteconfirm">
+         <h1>$(_ "Pages deleted:")</h1>
+         <ul>
+         $(while read page; do
+           printf '<li>%s</li>' "$(HTML $page)"
+         done)
+         </ul>
+         <a class="button" href="./">$(_ OK)</a>
+       </article>
+       EOF
+  } | theme_page -
+  return 0
 elif [ "$action" = cancel ]; then
   REDIRECT ./
 elif [ "$action" ]; then
   printf 'Refresh: %i\r\n' 4
-  export ERRORMSG="Missing parameters."
+  export ERROR_MSG="Missing parameters."
   theme_error 400
   return 0
 fi