]> git.plutz.net Git - shellwiki/blob - handlers/60_move_rename_delete.sh
Merge commit 'f12ebac72fc935c789fddcce165c07c91577e425'
[shellwiki] / handlers / 60_move_rename_delete.sh
1 #!/bin/sh
2
3 case "${PATH_INFO}" in
4   */\[move\]|*/\[rename\]|*/\[delete\])
5     page="${PATH_INFO%\[*\]}"
6     if [ ! -d "$_DATA/pages/${page}" -a ! -d "$_EXEC/pages/${page}" ]; then
7       theme_error 404
8       return 0
9     elif ! acl_write "$page"; then
10       printf 'Refresh: %i, url=%s\r\n' 4 ./
11       theme_error 403
12       return 0
13     elif [ -d "$_EXEC/pages/${page}/" ]; then
14       theme_page - <<-EOF
15         <article>
16           <p class=error>
17           <h1>Immutable Page</h1>
18           This is a core page of the wiki system. Its name and position cannot be changed.
19           You may however update this page and you can use ACLs to hide it from various listings.
20           </p>
21         </article>
22         EOF
23       return 0
24     fi
25     ;;
26   *) return 1;;
27 esac
28
29 if [ "$REQUEST_METHOD" = POST ]; then
30   action="$(POST action)"
31   newname="$(POST newname |grep -m1 -xE '[^#/]*')"
32   newlocation="$(POST newlocation |grep -m1 -xE '/[^#]*')"
33 else case "${PATH_INFO}" in
34   */\[move\])
35       location="${page%/}" location="${location%/*}/"
36       theme_page - <<-EOF
37         <form method=POST id=movepage>
38           <input type=hidden name=session_id value="$SESSION_KEY">
39           <h1>Move Page</h1>
40           <p class="pageid">$(HTML "${page}")</p>
41           <input name="newlocation" value="$(HTML "$location")" placeholder="New Location">
42           <ul>
43             <li>A page with the same name must not already exist at the new location.</li>
44             <li>You must have permission to create new pages at this location.</li>
45             <li>All subpages will become available under the new path name.</li>
46             <li>Subpages will become unavailable under their current name.</li>
47           </ul>
48           <button type=submit name=action value=move>Move</button>
49           <button type=submit name=action value=cancel>Cancel</button>
50         </form>
51         EOF
52       return 0
53     ;;
54   */\[rename\])
55       name="${page%/}" name="${name##*/}"
56       theme_page - <<-EOF
57         <form method=POST id=renamepage>
58           <input type=hidden name=session_id value="$SESSION_KEY">
59           <h1>Rename Page</h1>
60           <p class="pageid">$(HTML "${page}")</p>
61           <input name="newname" value="$(HTML "$name")" placeholder="New Name">
62           <ul>
63             <li>A page with the new name must not already exist.</li>
64             <li>You must have permission to create new pages at this location.</li>
65             <li>All subpages will become available under the new path name.</li>
66             <li>Subpages will become unavailable under their current name.</li>
67           </ul>
68           <button type=submit name=action value=rename>Rename</button>
69           <button type=submit name=action value=cancel>Cancel</button>
70         </form>
71         EOF
72       return 0
73     ;;
74   */\[delete\])
75       theme_page - <<-EOF
76         <form method=POST id=deletepage>
77           <input type=hidden name=session_id value="$SESSION_KEY">
78           <h1>Delete Page</h1>
79           <p class="pageid">$(HTML "${page}")</p>
80           <p>This page and its attachments will be deleted</p>
81           <ul>
82             <li>Past revisions of the page text (including the current one) will remain accessible and can be restored.</li>
83             <li>Attachments will be deleted completely, and cannot be restored.</li>
84             <li>Subpages will not be affected and can still be accessed normally.</li>
85           </ul>
86           <button type=submit name=action value=delete>Delete</button>
87           <button type=submit name=action value=cancel>Cancel</button>
88         </form>
89         EOF
90       return 0
91     ;;
92   esac
93 fi
94
95 if [ "$action" = rename -a "$newname" ]; then
96   oldname="${PATH_INFO%\[*\]}"
97   newname="${oldname%/*/}$(PATH "${newname}/")"
98
99   if [ -d "$_DATA/pages/$newname" ]; then
100     printf 'Refresh: %i\r\n' 4
101     export ERRORMSG="A location of that name already exists."
102     theme_error 400
103     return 0
104   elif ! acl_write "$oldname" || ! acl_write "$newname"; then
105     printf 'Refresh: %i\r\n' 4
106     theme_error 403
107     return 0
108   elif [ "$REV_PAGES" = true ]; then
109     git -C "$_DATA" mv "pages/$oldname" "pages/$newname"
110     git -C "$_DATA" commit -m 'Page # '"$oldname"' # renamed to # '"$newname"' # by user @ '"$USER_NAME"' @' \
111         -- "pages/$oldname" "pages/$newname"
112     REDIRECT "$_BASE${newname}"
113   else
114     mv -- "$_DATA/pages/$oldname" "$_DATA/pages/$newname"
115     REDIRECT "$_BASE${newname}"
116   fi
117 elif [ "$action" = move -a "$newlocation" ]; then
118   oldname="${PATH_INFO%\[*\]}"
119   newlocation="$(PATH "$newlocation")"
120   newname="${oldname%/}"
121   newname="${newlocation%/}/${newname##*/}/"
122
123   if [ -d "$_DATA/pages/$newname" ]; then
124     printf 'Refresh: %i\r\n' 4
125     export ERRORMSG="A page of that name already exists at the given location."
126     theme_error 400
127     return 0
128   elif [ ! -d "$_DATA/pages/$newlocation" ]; then
129     printf 'Refresh: %i\r\n' 4
130     export ERRORMSG="The given location does not exist."
131     theme_error 400
132     return 0
133   elif ! acl_write "$oldname" || ! acl_write "$newname"; then
134     printf 'Refresh: %i\r\n' 4
135     theme_error 403
136     return 0
137   elif [ "$REV_PAGES" = true ]; then
138     git -C "$_DATA" mv "pages/${oldname}" "pages/${newname}"
139     git -C "$_DATA" commit -m 'Page # '"$oldname"' # moved to # '"$newname"' # by user @ '"$USER_NAME"' @' \
140         -- "pages/${oldname}" "pages/${newname}"
141     REDIRECT "$_BASE${newname}"
142   else
143     mv -- "$_DATA/pages/$oldname" "$_DATA/pages/$newname"
144     REDIRECT "$_BASE${newname}"
145   fi
146 elif [ "$action" = delete ]; then
147   oldname="${PATH_INFO%\[*\]}"
148   if ! acl_write "$oldname"; then
149     printf 'Refresh: %i\r\n' 4
150     theme_error 403
151     return 0
152   elif [ "$REV_PAGES" = true ]; then
153     git -C "$_DATA" rm "pages/${oldname}/#page.md"
154     git -C "$_DATA" commit -m 'Page # '"$oldname"' # deleted by user @ '"$USER_NAME"' @' \
155         -- "pages/${oldname}/#page.md"
156     rm -r -- "$_DATA/pages/${oldname}"/\#*
157     rmdir -- "$_DATA/pages/${oldname}/" || true
158     REDIRECT ./
159   else
160     rm -- "$_DATA/pages/${oldname}/#page.md"
161     rm -r -- "$_DATA/pages/${oldname}"/\#*
162     rmdir -- "$_DATA/pages/${oldname}/" || true
163     REDIRECT ./
164   fi
165 elif [ "$action" = cancel ]; then
166   REDIRECT ./
167 elif [ "$action" ]; then
168   printf 'Refresh: %i\r\n' 4
169   export ERRORMSG="Missing parameters."
170   theme_error 400
171   return 0
172 fi