1 # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the
15 # Free Software Foundation, Inc.
16 # 59 Temple Place, Suite 330
17 # Boston, MA 02111-1307 USA
19 $ModulesDescription .= '<p><a href="http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/thread.pl">thread.pl</a>, see <a href="http://www.oddmuse.org/cgi-bin/oddmuse/Thread_Server_Extension">Thread Server Extension</a></p>';
21 $Action{getthread
} = \
&ThreadGet
;
22 $Action{addthread
} = \
&ThreadAdd
;
24 push(@MyRules, \
&ThreadRule
);
27 if (m/\G(\[\[thread:$FreeLinkPattern\]\])/gcs) {
38 my ($id, $interactive, $inline) = @_;
39 my ($page, $thread) = ThreadExtract
($id);
40 print GetHttpHeader
('text/html') . GetHtmlHeader
(Ts
('Thread: %s', $id), '') unless $inline;
41 if (GetParam
('interactive', $interactive)) {
42 $thread = ThreadInteractive
($id, $thread);
45 print $q->end_html unless $inline;
50 ReportError
(T
('ID parameter is missing.'), '400 BAD REQUEST') unless $id;
51 $page = GetPageContent
($id);
52 ReportError
(Ts
('Thread %s does not exist.', $id), '404 NOT FOUND') unless $page;
53 # ignore all the stuff that gets processed anyway
54 foreach my $tag ('nowiki', 'pre', 'code') {
55 $page =~ s
|<$tag>(.*\n)*?
</$tag>||gi
;
57 if ($page =~ m/(^|\n)(\*(.+\n)+)/) {
60 ReportError
(Ts
('Page %s does not contain a thread.', $id), '404 NOT FOUND');
64 sub ThreadInteractive
{
65 my ($id, $thread) = @_;
66 my @items = split(/(^|\n)(\*+)/, $thread);
70 while (@items and substr($level, 0, 1) ne '*') {
71 $level = shift(@items);
73 my $rest = shift(@items);
74 if ($rest =~ m/\[$UrlPattern\s+([^\]]+?)\]/) {
75 my $url = UrlEncode
($1);
77 my $link = "[$ScriptName?action=addthread;id=$id;url=$url $add]";
78 $result .= $level . ' '. $link . ' ' . $rest . "\n";
81 ReportError
('Unable to parse thread', '500 INTERNAL SERVER ERROR') unless $result;
87 ReportError
(T
('ID parameter is missing.'), '400 BAD REQUEST') unless $id;
88 my $url = GetParam
('url', '');
89 ReportError
(T
('URL parameter is missing.'), '400 BAD REQUEST') unless $url;
90 if (not (GetParam
('new', '')) or not(GetParam
('name', ''))) {
91 print GetHeader
('', Ts
('Add to %s thread', $id), '');
92 print $q->div({-class=>'thread'}, '<p>'
94 . GetHiddenValue
('action', 'addthread')
95 . GetHiddenValue
('id', $id)
99 . $q->textfield(-name
=>'url', -value
=>$url,
100 -size
=>100, -maxlength
=>500)
101 . '</td></tr><tr><td>'
104 . $q->textfield(-name
=>'new',
105 -size
=>100, -maxlength
=>500)
106 . '</td></tr><tr><td>'
109 . $q->textfield(-name
=>'name',
110 -size
=>50, -maxlength
=>100)
111 . '</td></tr></table>'
113 . $q->p($q->submit(-name
=>'Save', -value
=>T
('Save')))
117 my ($page, $thread) = ThreadExtract
($id);
118 my $new = GetParam
('new', '');
119 my $name = GetParam
('name', '');
120 my @items = split(/(^|\n)(\*+)/, $thread);
124 while (@items and substr($level, 0, 1) ne '*') {
125 $level = shift(@items);
127 my $rest = shift(@items);
129 if ($rest =~ m/\[$UrlPattern\s+([^\]]+?)\]/) {
131 $result .= $level . $rest . "\n";
132 if ($current eq $url) {
133 $result .= $level . "* [$new $name]\n";
137 # print GetHttpHeader('text/html', $Now) . GetHtmlHeader(Ts('Thread: %s', $id), '');
138 # ApplyRules($result);
139 # print $q->pre($new . "\n" . $result);
140 # print $q->end_html;
141 $thread = quotemeta($thread);
142 $page =~ s/$thread/$result/;
143 SetParam
('text', $page);