]> git.plutz.net Git - shellwiki/blob - handlers/30_move_rename_delete.sh
introduce css pragma as separate handler
[shellwiki] / handlers / 30_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   else
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   fi
114 elif [ "$action" = move -a "$newlocation" ]; then
115   oldname="${PATH_INFO%\[*\]}"
116   newlocation="$(PATH "$newlocation")"
117   newname="${oldname%/}"
118   newname="${newlocation%/}/${newname##*/}/"
119
120   if [ -d "$_DATA/pages/$newname" ]; then
121     printf 'Refresh: %i\r\n' 4
122     export ERRORMSG="A page of that name already exists at the given location."
123     theme_error 400
124     return 0
125   elif [ ! -d "$_DATA/pages/$newlocation" ]; then
126     printf 'Refresh: %i\r\n' 4
127     export ERRORMSG="The given location does not exist."
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   else
135     git -C "$_DATA" mv "pages/${oldname}" "pages/${newname}"
136     git -C "$_DATA" commit -m 'Page # '"$oldname"' # moved to # '"$newname"' # by user @ '"$USER_NAME"' @' \
137         -- "pages/${oldname}" "pages/${newname}"
138     REDIRECT "$_BASE${newname}"
139   fi
140 elif [ "$action" = delete ]; then
141   oldname="${PATH_INFO%\[*\]}"
142   if ! acl_write "$oldname"; then
143     printf 'Refresh: %i\r\n' 4
144     theme_error 403
145     return 0
146   else
147     git -C "$_DATA" rm "pages/${oldname}/#page.md"
148     git -C "$_DATA" commit -m 'Page # '"$oldname"' # deleted by user @ '"$USER_NAME"' @' \
149         -- "pages/${oldname}/#page.md"
150     rm -r -- "$_DATA/pages/${oldname}"/\#*
151     rmdir -- "$_DATA/pages/${oldname}/" || true
152     REDIRECT ./
153   fi
154 elif [ "$action" = cancel ]; then
155   REDIRECT ./
156 elif [ "$action" ]; then
157   printf 'Refresh: %i\r\n' 4
158   export ERRORMSG="Missing parameters."
159   theme_error 400
160   return 0
161 fi