]> git.plutz.net Git - shellwiki/blob - handlers/60_move_rename_delete.sh
Merge commit '131ebbe3155cdd45d2f7473302157f5ba4594759'
[shellwiki] / handlers / 60_move_rename_delete.sh
1 #!/bin/sh
2
3 # Copyright 2022 - 2023 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 l10n_immutablepage >/dev/null 2>&1 \
18 || l10n_immutablepage(){  #TRANSLATION
19   cat <<-EOF
20           <h1>Immutable Page</h1>
21           This is a core page of the wiki system. Its name and position cannot be changed.
22           You may however update this page and you can use ACLs to hide it from various listings.
23         EOF
24 }
25
26 case "${PATH_INFO}" in
27   */\[move\]|*/\[rename\]|*/\[delete\])
28     page="${PATH_INFO%\[*\]}"
29     if [ ! -d "$_DATA/pages/${page}" -a ! -d "$_EXEC/pages/${page}" ]; then
30       theme_error 404
31       return 0
32     elif ! acl_write "$page"; then
33       printf 'Refresh: %i, url=%s\r\n' 4 ./
34       theme_error 403
35       return 0
36     elif [ -d "$_EXEC/pages/${page}/" ]; then
37       theme_page - <<-EOF
38         <article>
39           <p class=error>
40           $(l10n_immutablepage)
41           </p>
42         </article>
43         EOF
44       return 0
45     fi
46     ;;
47   *) return 1;;
48 esac
49
50 l10n_movepage >/dev/null 2>&1 \
51 || l10n_movepage(){  # TRANSLATION
52   cat <<-EOF
53           <h1>Move Page</h1>
54           <p class="pageid">$(HTML "${page}")</p>
55           <input name="newlocation" value="$(HTML "$location")" placeholder="New Location">
56           <ul>
57             <li>A page with the same name must not already exist at the new location.</li>
58             <li>You must have permission to create new pages at this location.</li>
59             <li>All subpages will become available under the new path name.</li>
60             <li>Subpages will become unavailable under their current name.</li>
61           </ul>
62           <button type=submit name=action value=move>Move</button>
63           <button type=submit name=action value=cancel>Cancel</button>
64         EOF
65 }
66 l10n_renamepage >/dev/null 2>&1 \
67 || l10n_renamepage(){  # TRANSLATION
68   cat <<-EOF
69           <h1>Rename Page</h1>
70           <p class="pageid">$(HTML "${page}")</p>
71           <input name="newname" value="$(HTML "$name")" placeholder="New Name">
72           <ul>
73             <li>A page with the new name must not already exist.</li>
74             <li>You must have permission to create new pages at this location.</li>
75             <li>All subpages will become available under the new path name.</li>
76             <li>Subpages will become unavailable under their current name.</li>
77           </ul>
78           <button type=submit name=action value=rename>Rename</button>
79           <button type=submit name=action value=cancel>Cancel</button>
80         EOF
81 }
82 l10n_deletepage >/dev/null 2>&1 \
83 || l10n_deletepage(){  # TRANSLATION
84   cat <<-EOF
85           <h1>Delete Page</h1>
86           <p class="pageid">$(HTML "${page}")</p>
87           <p>This page and its attachments will be deleted</p>
88           <ul>
89             <li>Past revisions of the page text (including the current one) will remain accessible and can be restored.</li>
90             <li>Attachments will be deleted completely, and cannot be restored.</li>
91             <li>Subpages will not be affected and can still be accessed normally.</li>
92           </ul>
93           <button type=submit name=action value=delete>Delete</button>
94           <button type=submit name=action value=cancel>Cancel</button>
95         EOF
96 }
97
98 if [ "$REQUEST_METHOD" = POST ]; then
99   action="$(POST action)"
100   newname="$(POST newname |grep -m1 -xE '[^#/]*')"
101   newlocation="$(POST newlocation |grep -m1 -xE '/[^#]*')"
102 else case "${PATH_INFO}" in
103   */\[move\])
104       location="${page%/}" location="${location%/*}/"
105       theme_page - <<-EOF
106         <form method=POST id=movepage>
107           <input type=hidden name=session_id value="$SESSION_KEY">
108           $(l10n_movepage)
109         </form>
110         EOF
111       return 0
112     ;;
113   */\[rename\])
114       name="${page%/}" name="${name##*/}"
115       theme_page - <<-EOF
116         <form method=POST id=renamepage>
117           <input type=hidden name=session_id value="$SESSION_KEY">
118           $(l10n_renamepage)
119         </form>
120         EOF
121       return 0
122     ;;
123   */\[delete\])
124       theme_page - <<-EOF
125         <form method=POST id=deletepage>
126           <input type=hidden name=session_id value="$SESSION_KEY">
127           $(l10n_deletepage)
128         </form>
129         EOF
130       return 0
131     ;;
132   esac
133 fi
134
135 if [ "$action" = rename -a "$newname" ]; then
136   oldname="${PATH_INFO%\[*\]}"
137   newname="${oldname%/*/}$(PATH "${newname}/")"
138
139   if [ -d "$_DATA/pages/$newname" ]; then
140     printf 'Refresh: %i\r\n' 4
141     export ERRORMSG="A location of that name already exists."
142     theme_error 400
143     return 0
144   elif ! acl_write "$oldname" || ! acl_write "$newname"; then
145     printf 'Refresh: %i\r\n' 4
146     theme_error 403
147     return 0
148   elif [ "$REV_PAGES" = true ]; then
149     git -C "$_DATA" mv "pages/$oldname" "pages/$newname"
150     git -C "$_DATA" commit -m 'Page # '"$oldname"' # renamed to # '"$newname"' # by user @ '"$USER_NAME"' @' \
151         -- "pages/$oldname" "pages/$newname"
152     REDIRECT "$_BASE${newname}"
153   else
154     mv -- "$_DATA/pages/$oldname" "$_DATA/pages/$newname"
155     REDIRECT "$_BASE${newname}"
156   fi
157 elif [ "$action" = move -a "$newlocation" ]; then
158   oldname="${PATH_INFO%\[*\]}"
159   newlocation="$(PATH "$newlocation")"
160   newname="${oldname%/}"
161   newname="${newlocation%/}/${newname##*/}/"
162
163   if [ -d "$_DATA/pages/$newname" ]; then
164     printf 'Refresh: %i\r\n' 4
165     export ERRORMSG="A page of that name already exists at the given location."
166     theme_error 400
167     return 0
168   elif [ ! -d "$_DATA/pages/$newlocation" ]; then
169     printf 'Refresh: %i\r\n' 4
170     export ERRORMSG="The given location does not exist."
171     theme_error 400
172     return 0
173   elif ! acl_write "$oldname" || ! acl_write "$newname"; then
174     printf 'Refresh: %i\r\n' 4
175     theme_error 403
176     return 0
177   elif [ "$REV_PAGES" = true ]; then
178     git -C "$_DATA" mv "pages/${oldname}" "pages/${newname}"
179     git -C "$_DATA" commit -m 'Page # '"$oldname"' # moved to # '"$newname"' # by user @ '"$USER_NAME"' @' \
180         -- "pages/${oldname}" "pages/${newname}"
181     REDIRECT "$_BASE${newname}"
182   else
183     mv -- "$_DATA/pages/$oldname" "$_DATA/pages/$newname"
184     REDIRECT "$_BASE${newname}"
185   fi
186 elif [ "$action" = delete ]; then
187   oldname="${PATH_INFO%\[*\]}"
188   if ! acl_write "$oldname"; then
189     printf 'Refresh: %i\r\n' 4
190     theme_error 403
191     return 0
192   elif [ "$REV_PAGES" = true ]; then
193     git -C "$_DATA" rm "pages/${oldname}/#page.md"
194     git -C "$_DATA" commit -m 'Page # '"$oldname"' # deleted by user @ '"$USER_NAME"' @' \
195         -- "pages/${oldname}/#page.md"
196     rm -r -- "$_DATA/pages/${oldname}"/\#*
197     rmdir -- "$_DATA/pages/${oldname}/" || true
198     REDIRECT ./
199   else
200     rm -- "$_DATA/pages/${oldname}/#page.md"
201     rm -r -- "$_DATA/pages/${oldname}"/\#*
202     rmdir -- "$_DATA/pages/${oldname}/" || true
203     REDIRECT ./
204   fi
205 elif [ "$action" = cancel ]; then
206   REDIRECT ./
207 elif [ "$action" ]; then
208   printf 'Refresh: %i\r\n' 4
209   export ERRORMSG="Missing parameters."
210   theme_error 400
211   return 0
212 fi