]> git.plutz.net Git - busy/blob - Wiki.page
c8034897b2bce641e788fe81ec0bf3453af648c5
[busy] / Wiki.page
1 #!/bin/zsh
2
3 info="$(egrep -o '(^|&)(i=)[0-9]{10}_[0-9]{3}(&|$)' <<<"${QUERY_STRING}" |tr -d '&i=')"
4 edit="$(egrep -o '(^|&)(e=)[0-9]{10}_[0-9]{3}(&|$)' <<<"${QUERY_STRING}" |tr -d '&e=')"
5 rev="$(egrep -o '(^|&)(r=)[0-9]{10}(&|$)' <<<"${QUERY_STRING}" |tr -d '&r=')"
6 search="$(egrep -o '(^|&)(s=).+(&|$)' <<<"${QUERY_STRING}" |sed -r 's:^&?s=::;s:\+: :g;s:%:\\x:g')"
7 search="$(echo -e "${search}" |sed 's:\t: :g;s:\r::g;s:\\:\\\\:g' |head -n1)"
8
9 [ -z "$ACL_WIKIEDIT" ] && ACL_WIKIEDIT=false
10
11 [ -n "$rev" ] && rev=".$rev"
12 if [ -n "$search" ]; then
13   info=''
14   edit=''
15   rev=''
16 fi
17
18 lock="Wiki/$info.lock"
19 [ -r "$lock" -o -n "$lock" ] && if [ "$(cut -d: -f2 "$lock")" -lt "$(date +%s)" ]; then
20   rm "$lock"
21   lock=''
22 else
23   lock="$(cut -d: -f1 "$lock")"
24 fi
25
26 if [ -n "$edit" -a -r "Wiki/${edit}${rev}" ]; then
27   title="$(egrep -x 'title=.+' "Wiki/$edit.meta" |cut -d= -f2- || echo '(Untitled)')"
28   cat <<WikiEND
29     <form class="edit" action="write/$($LOGIN && echo user_)wikiedit.cgi?i=$edit" method="post" accept-charset="UTF-8">
30       <div id="LEFT">
31         Editing <b>$title</b><br>
32         <input type="submit" value="Submit">
33         <input type="submit" name="cancel" value="Cancel">
34         <hr>
35       </div>
36       <div id="MAIN">
37         <div class="wiki">
38           <h1>$title</h1>
39           <textarea name="page" rows=30 cols=40>$(cat "Wiki/${edit}${rev}")</textarea>
40         </div>
41       </div>
42     </form>
43     <div id="RIGHT">
44     $(stat -c '' Wiki/${edit}.comment.<0000000000-9999999999>_<000-999> && for each in $(ls -c Wiki/${edit}.comment.<0000000000-9999999999>_<000-999>); do
45       author=$(grep -m1 '^author=' "$each" |cut -d= -f2-)
46       echo "<div class='comment'><h1>$author wrote:</h1><div class='wikitext'>"
47       tail -n+2 $each |_wiki
48       echo '</div></div>'
49     done)
50     </div>
51 WikiEND
52
53 elif [ -n "$info" -a -r "Wiki/${info}${rev}" ]; then
54   title="$(egrep -x 'title=.+' "Wiki/$info.meta" |cut -d= -f2- || echo '(Untitled)')"
55
56   cat <<WikiEND
57     <div id="LEFT">
58       <form method="GET" action="/${SCRIPTNAME}" accept-charset="utf-8">
59         <input type="hidden" name="p" value="Wiki">
60         <input type="text" name="s" placeholder="Search"><br>
61         <input type="submit" value="Search Wiki">
62       </form>
63       <hr>
64
65       <b>Revisions:</b>
66       $(rno=1; for each in Wiki/$info.<0000000000-9999999999>; do
67         revi=$(cut -d. -f2 <<<"$each")
68         echo "<a class='rev' href='?p=Wiki&amp;i=$info&amp;r=$revi'>$rno</a>"
69         rno=$(($rno+1))
70         done) <a class='rev' href='?p=Wiki&amp;i=$info'>Latest</a><br>
71       <hr>
72       $($LOGIN && if [ -z "$lock" ]; then
73           [ -z "$rev" ] && echo "<a class='function' href='write/$($LOGIN && echo user_)lock.cgi?p=Wiki&amp;e=$info'>edit this page</a>"
74           [ -n "$rev" ] && echo "Click <a class='function' href='write/$($LOGIN && echo user_)lock.cgi?p=Wiki&amp;e=${info}&amp;r=${rev:s/.//}'>edit</a> to derive a new page revision from this one."
75         else
76           echo "<b>This page is currently being edited by $lock</b><a class='function' href='write/$($LOGIN && echo user_)lock.cgi?p=Wiki&amp;e=$info'>edit anyway</a>"
77         fi
78         $LOGIN && echo '<hr>')
79       <h1>Table of Content</h1>
80       <div class="toc">
81         $(cat Wiki/"${info}${rev}" |while read line; do
82         if (egrep -q '^ *!!!' <<<"$line"); then
83           echo "<a class='i2' href='#$(sed 's,^ *!!! *,,;s,[^a-zA-Z0-9],.,g' <<<"$line")'>$(sed 's,^ *!!!,,' <<<"$line")</a><br>"
84         elif (egrep -q '^ *!!' <<<"$line"); then
85           echo "<a class='i1' href='#$(sed 's,^ *!! *,,;s,[^a-zA-Z0-9],.,g' <<<"$line")'>$(sed 's,^ *!!,,' <<<"$line")</a><br>"
86         elif (egrep -q '^ *!' <<<"$line"); then
87           echo "<a class='i0' href='#$(sed 's,^ *! *,,;s,[^a-zA-Z0-9],.,g' <<<"$line")'>$(sed 's,^ *!,,' <<<"$line")</a><br>"
88           fi
89         done)
90       </div>
91       <hr>
92     </div>
93     <div id="MAIN">
94       <div class="wiki"><div class="wikitext">
95         <h1>$title</h1>
96         $(_wiki Wiki/"${info}${rev}")
97       </div></div>
98     </div>
99 WikiEND
100
101   echo '<div id="RIGHT">'
102 $LOGIN && cat <<WikiEND
103       <a id='comment_button' class='function' href='#' onclick='javascript:show_screen("comment");hide_screen("comment_button");'>Comment / Ask</a>
104       <div id="comment" style="display: none;">
105         <form action="/write/$($LOGIN && echo user_)wikimeta.cgi?i=$info" method="post" accept-charset="UTF-8">
106           <b>Comment:</b><br>
107           <textarea name="comm" rows="6" style="width:99%;"></textarea><br>
108           <input type="submit" value="Submit">
109           <a class="function" href="#" onclick="javascript:hide_screen('comment');show_screen('comment_button');">Cancel</a>
110         </form>
111       </div>
112 WikiEND
113   stat -c '' Wiki/${info}.comment.<0000000000-9999999999>_<000-999> && for each in $(ls -c Wiki/${info}.comment.<0000000000-9999999999>_<000-999>); do
114     author=$(grep -m1 '^author=' "$each" |cut -d= -f2-)
115     echo "<div class='comment'><h1>$author wrote:</h1><div class='wikitext'>"
116     tail -n+2 $each |_wiki
117     echo '</div></div>'
118   done
119   echo '</div>'
120   
121 elif [ -z "$info" ]; then
122   cat <<WikiEND
123     <div id="LEFT">
124       <form method="GET" action="/${SCRIPTNAME}" accept-charset="utf-8">
125         <input type="hidden" name="p" value="Wiki">
126         <input type="text" name="s" placeholder="Search"><br>
127         <input type="submit" value="Search Wiki">
128       </form>
129       <hr>
130 WikiEND
131   if [ -n "$search" ]; then
132     echo "<h1>Wiki search results for: $search</h1>"
133     for each in $(grep -ile "$search" Wiki/<0000000000-9999999999>_<000-999>); do
134       echo "<div class='search'><a href='?p=Wiki&amp;i=${each:t}'><h2>$(grep '^title=' "$each.meta" |cut -d= -f2-)</h2></a>"
135       grep -C1 -ie "$search" "$each" |sed "s:$search:<b>&</b>:g;s:$:<br>:g"
136       echo '</div>'
137     done
138     echo '<hr>'
139   fi
140   echo '</div><div id="RIGHT">'
141   $LOGIN && cat <<WikiEND
142     <a id='addp_button' class='function' href='#' onclick='javascript:show_screen("add_page");hide_screen("addp_button");'>Add a new Page</a>
143     <div id="add_page" style="display: none;">
144       <form action="/write/$($LOGIN && echo user_)wikimeta.cgi" method="post" accept-charset="UTF-8">
145         <br><b>Title:</b><br>
146         <input type="text" name="title" placeholder="Fill in a title"><br>
147         <b>Description:</b><br>
148         <textarea name="desc" rows="6" ></textarea><br>
149         <input type="submit" value="Submit">
150         <a class="function" href="#" onclick="javascript:hide_screen('add_page');show_screen('addp_button');">Cancel</a>
151       </form>
152     </div>
153 WikiEND
154   echo '</div><div id="MAIN">'
155
156   stat -c '' Wiki/<0000000000-9999999999>_<000-999> && for each in Wiki/<0000000000-9999999999>_<000-999>; do
157     info=$(sed 's,Wiki/,,' <<<"$each")
158     title="$(egrep -x 'title=.+' "$each.meta" |cut -d= -f2- || echo '(Untitled)')"
159     description="$(egrep -x 'description=.+' "$each.meta" |cut -d= -f2-)"
160     owner="$(egrep -x 'owner=.+' "$each.meta" |cut -d= -f2-)"
161
162     cat <<WikiEND
163     <div class="article">
164       <div id="display_$info">
165         <a name="$info" href="/?p=Wiki&amp;i=$info"><h1>$title</h1></a>
166         $description<br><br>
167         <a class="function" href="/?p=Wiki&amp;i=$info">View</a> 
168         $($ACL_WIKIEDIT && echo "<a class='function' href='#' onclick=\"javascript:show_screen('change_$info');hide_screen('display_$info');\">Change Info</a>")
169       </div>
170 WikiEND
171     $ACL_WIKIEDIT && cat <<WikiEND
172       <div id="change_$info" style="display: none;">
173         <form action="/write/$($LOGIN && echo user_)wikimeta.cgi?i=$info" method="post" accept-charset="UTF-8">
174           <b>Title:</b> <input type="text" name="title" value="$title"><br>
175           <b>Description:</b><br>
176           <textarea name="desc" rows="6" style="width:100%;">$description</textarea><br>
177           <input type="submit" value="Submit">
178           <a class="function" href="#" onclick="javascript:hide_screen('change_$info');show_screen('display_$info');">Cancel</a>
179         </form>
180       </div>
181 WikiEND
182     echo '</div>'
183   done
184   echo '</div>'
185 fi