]> git.plutz.net Git - shellwiki/commitdiff
handler for moving and deleting pages
authorPaul Hänsch <paul@plutz.net>
Wed, 25 May 2022 19:26:10 +0000 (21:26 +0200)
committerPaul Hänsch <paul@plutz.net>
Wed, 25 May 2022 19:26:10 +0000 (21:26 +0200)
handlers/30_move_rename_delete.sh [new file with mode: 0644]
themes/default.css
themes/default.sh

diff --git a/handlers/30_move_rename_delete.sh b/handlers/30_move_rename_delete.sh
new file mode 100644 (file)
index 0000000..12e7d45
--- /dev/null
@@ -0,0 +1,154 @@
+#!/bin/sh
+
+case "${PATH_INFO}" in
+  */\[move\]|*/\[rename\]|*/\[delete\])
+    page="${PATH_INFO%\[*\]}"
+    if [ ! -d "$_DATA/pages/${page}" -a ! -d "$_EXEC/pages/${page}" ]; then
+      theme_error 404
+      return 0
+    elif ! acl_write "$page"; then
+      printf 'Refresh: %i, url=%s\r\n' 4 ./
+      theme_error 403
+      return 0
+    elif [ -d "$_EXEC/pages/${page}/" ]; then
+      theme_page - <<-EOF
+       <article>
+         <p class=error>
+         <h1>Immutable Page</h1>
+         This is a core page of the wiki system. Its name and position cannot be changed.
+         You may however update this page and you can use ACLs to hide it from various listings.
+         </p>
+       </article>
+       EOF
+      return 0
+    fi
+    ;;
+  *) return 1;;
+esac
+
+if [ "$REQUEST_METHOD" = POST ]; then
+  action="$(POST action)"
+  newname="$(POST newname |grep -m1 -xE '[^#/]*')"
+  newlocation="$(POST newlocation |grep -m1 -xE '/[^#]*')"
+else case "${PATH_INFO}" in
+  */\[move\])
+      location="${page%/}" location="${location%/*}/"
+      theme_page - <<-EOF
+       <form method=POST id=movepage>
+         <input type=hidden name=session_id value="$SESSION_KEY">
+         <h1>Move Page</h1>
+         <p class="pageid">$(HTML "${page}")</p>
+         <input 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>
+           <li>All subpages will become available under the new path name.</li>
+           <li>Subpages will become unavailable under their current name.</li>
+         </ul>
+         <button type=submit name=action value=move>Move</button>
+         <button type=submit name=action value=cancel>Cancel</button>
+       </form>
+       EOF
+      return 0
+    ;;
+  */\[rename\])
+      name="${page%/}" name="${name##*/}"
+      theme_page - <<-EOF
+       <form method=POST id=renamepage>
+         <input type=hidden name=session_id value="$SESSION_KEY">
+         <h1>Rename Page</h1>
+         <p class="pageid">$(HTML "${page}")</p>
+         <input 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>
+           <li>All subpages will become available under the new path name.</li>
+           <li>Subpages will become unavailable under their current name.</li>
+         </ul>
+         <button type=submit name=action value=rename>Rename</button>
+         <button type=submit name=action value=cancel>Cancel</button>
+       </form>
+       EOF
+      return 0
+    ;;
+  */\[delete\])
+      theme_page - <<-EOF
+       <form method=POST id=deletepage>
+         <input type=hidden name=session_id value="$SESSION_KEY">
+         <h1>Delete Page</h1>
+         <p class="pageid">$(HTML "${page}")</p>
+         <p>This page and its attachments will be deleted</p>
+         <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>
+         </ul>
+         <button type=submit name=action value=delete>Delete</button>
+         <button type=submit name=action value=cancel>Cancel</button>
+       </form>
+       EOF
+      return 0
+    ;;
+  esac
+fi
+
+if [ "$action" = rename -a "$newname" ]; then
+  oldname="${PATH_INFO%\[*\]}"
+  newname="${oldname%/*/}$(PATH "${newname}/")"
+
+  if [ -d "$_DATA/pages/$newname" ]; then
+    printf 'Refresh: %i\r\n' 4
+    export ERRORMSG="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
+  else
+    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}"
+  fi
+elif [ "$action" = move -a "$newlocation" ]; then
+  oldname="${PATH_INFO%\[*\]}"
+  newlocation="$(PATH "$newlocation")"
+  newname="${oldname%/}"
+  newname="${newlocation%/}/${newname##*/}/"
+
+  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."
+    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."
+    theme_error 400
+    return 0
+  elif ! acl_write "$oldname" || ! acl_write "$newname"; then
+    printf 'Refresh: %i\r\n' 4
+    theme_error 403
+    return 0
+  else
+    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}"
+  fi
+elif [ "$action" = delete ]; then
+  oldname="${PATH_INFO%\[*\]}"
+  if ! acl_write "$oldname"; then
+    printf 'Refresh: %i\r\n' 4
+    theme_error 403
+    return 0
+  else
+    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 ./
+  fi
+fi
index 11c3daeda249b2e16ae6bf4f1e70326e492cce79..91458c3f169e4e1576e1fc419b8305a0f9ea3357 100644 (file)
@@ -52,6 +52,8 @@ main .pagemenu li {
 main .pagemenu li a { color: #FFF; }
 
 main article,
+main > form#renamepage, main > form#movepage,
+main > form#deletepage, 
 [id$="/[attachment]"] main form.upload {
   margin: 1em;
   padding: .125em 1em 1em 1em;
index 89d4fad6c12c934dc71dd2f61a89903c6592676b..0b850078a06c321a6892813b1d129b8b1fabd9af 100755 (executable)
@@ -28,6 +28,9 @@ theme_pagemenu(){
       <li><a href="./[edit]">Edit</a></li>
       <li><a href="./[attachment]">Attachments</a></li>
       <li><a href="./[revision]">Revisions</a></li>
+      <li><a href="./[rename]">Rename</a></li>
+      <li><a href="./[move]">Move</a></li>
+      <li><a href="./[delete]">Delete</a></li>
     </ul>'
   fi
 }