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