]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Wiki.pm
Added wiki search.
[ndwebbie.git] / lib / NDWeb / Controller / Wiki.pm
1 package NDWeb::Controller::Wiki;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 use Text::MediawikiFormat prefix => '/wiki/';
8
9 =head1 NAME
10
11 NDWeb::Controller::Wiki - Catalyst Controller
12
13 =head1 DESCRIPTION
14
15 Catalyst Controller.
16
17 =head1 METHODS
18
19 =cut
20
21
22 =head2 index
23
24 =cut
25
26 sub auto : Priate {
27         my ( $self, $c ) = @_;
28
29         $c->stash(wikiformat => \&wikiformat);
30 }
31
32 sub index :Path :Args(0) {
33         my ( $self, $c ) = @_;
34
35         push @{$c->req->captures}, ('Info','Main');
36         $c->forward('page');
37         $c->stash(template => 'wiki/page.tt2');
38 }
39
40 sub page : LocalRegex(^(?:([A-Z]\w*)(?::|%3A))?([A-Z]\w*)$) {
41         my ( $self, $c ) = @_;
42         my $dbh = $c->model;
43
44         $c->forward('findPage');
45         $c->acl_access_denied('test',$c->action,'No edit access for this page')
46                 if defined $c->stash->{page}->{view} && !$c->stash->{page}->{view};
47         $c->forward('loadText');
48
49         unless ($c->stash->{page}->{wpid}){
50                 $c->stash->{page}->{namespace} = $c->req->captures->[0];
51                 $c->stash->{page}->{name} = $c->req->captures->[1];
52                 $c->stash->{page}->{fullname} = ($c->stash->{page}->{namespace} ? $c->stash->{page}->{namespace}.':' : '')
53                         . $c->stash->{page}->{name};
54                 $c->stash->{page}->{post} = $dbh->selectrow_array(q{SELECT post
55                                 FROM wiki_namespace_access
56                                 WHERE namespace = COALESCE($1,'') AND post AND gid IN (SELECT groups($2))
57                         },undef,$c->stash->{page}->{namespace}, $c->stash->{UID});
58         }
59         $c->stash(title => $c->stash->{page}->{fullname});
60 }
61
62 sub edit : LocalRegex(^edit/(?:([A-Z]\w*)(?::|%3A))?([A-Z]\w*)$) {
63         my ( $self, $c ) = @_;
64         my $dbh = $c->model;
65
66         $c->forward('findPage');
67         $c->acl_access_denied('test',$c->action,'No edit access for this page')
68                 if defined $c->stash->{page}->{edit} && !$c->stash->{page}->{edit};
69         $c->forward('loadText');
70         $c->forward('findNamespaces');
71
72         unless ($c->stash->{page}->{wpid}){
73                 $c->acl_access_denied('test',$c->action,'No edit access for this page')
74                         unless @{$c->stash->{namespaces}};
75                 $c->stash->{page}->{namespace} = $c->req->captures->[0];
76                 $c->stash->{page}->{name} = $c->req->captures->[1];
77         }
78 }
79
80 sub history : LocalRegex(^history/(?:([A-Z]\w*)(?::|%3A))?([A-Z]\w*)$) {
81         my ( $self, $c ) = @_;
82         my $dbh = $c->model;
83
84         $c->forward('findPage');
85
86         my $query = $dbh->prepare(q{SELECT wprev,time,username,comment
87                 FROM wiki_page_revisions JOIN users u USING (uid)
88                 WHERE wpid = $1
89                 ORDER BY time DESC
90                 });
91         $query->execute($c->stash->{page}->{wpid});
92         $c->stash(revisions => $query->fetchall_arrayref({}) );
93         $c->stash(title => 'History for ' . $c->stash->{page}->{fullname});
94 }
95
96 sub postedit : Local {
97         my ( $self, $c, $p ) = @_;
98         my $dbh = $c->model;
99
100         eval {
101                 $dbh->begin_work;
102
103                 my $wpid = $c->req->param('wpid');
104                 if ( $wpid eq 'new'){
105                         my $namespace = $dbh->selectrow_array(q{SELECT namespace
106                                 FROM wiki_namespace_access
107                                 WHERE namespace = $1 AND post AND gid IN (SELECT groups($2))
108                         },undef,$c->req->param('namespace'), $c->stash->{UID});
109
110                         my $query = $dbh->prepare(q{INSERT INTO wiki_pages (namespace,name) VALUES($1,$2) RETURNING wpid});
111                         $query->execute($namespace,$c->req->param('name'));
112                         $wpid = $query->fetchrow;
113                 }
114                 $c->forward('findPage',[$wpid]);
115                 $c->acl_access_denied('test',$c->action,'No edit access for this page')
116                         if defined $c->stash->{page}->{edit} && !$c->stash->{page}->{edit};
117
118                 my $query = $dbh->prepare(q{INSERT INTO wiki_page_revisions
119                         (wpid,parent,text,comment,uid) VALUES($1,$2,$3,$4,$5)
120                         RETURNING wprev
121                         });
122                 $c->req->params->{parent}||= undef;
123                 $query->execute($wpid,$c->req->param('parent'),$c->req->param('text')
124                         ,$c->req->param('comment'),$c->stash->{UID});
125                 my $rev = $query->fetchrow;
126                 $dbh->do(q{UPDATE wiki_pages SET wprev = $1 WHERE wpid = $2}
127                         ,undef,$rev,$wpid);
128
129                 $dbh->commit;
130                 $c->res->redirect($c->uri_for($c->stash->{page}->{fullname}));
131                 return;
132         } if ($c->req->param('cmd') eq 'Submit');
133
134         if ($@){
135                 if ($@ =~ /duplicate key value violates unique constraint "wiki_pages_namespace_key"/){
136                         $c->stash(error => "Page does already exist");
137                 }elsif ($@ =~ /value too long for type character varying\(255\)/){
138                         $c->stash(error => 'The name is too long, keep it to max 255 characters');
139                 }else{
140                         $c->stash(error => $@);
141                 }
142                 $dbh->rollback;
143         }
144
145         $c->forward('findPage') if $p;
146         $c->forward('findNamespaces');
147
148         $c->stash->{page}->{namespace} = $c->req->param('namespace');
149         $c->stash->{page}->{name} = $c->req->param('name');
150
151         $c->stash(text => $c->req->param('text'));
152         $c->stash(template => 'wiki/edit.tt2');
153 }
154
155 sub search : Local {
156         my ( $self, $c ) = @_;
157         my $dbh = $c->model;
158
159         if ($c->req->param('search')){
160                 $c->stash(search => $c->req->param('search'));
161                 my $queryfunc = 'plainto_tsquery';
162                 $queryfunc = 'to_tsquery' if $c->req->param('advsearch');
163                 my $posts = $dbh->prepare(q{SELECT wp.wpid,namespace,name
164                         ,(CASE WHEN namespace <> '' THEN namespace || ':' ELSE '' END) || name AS fullname
165                         ,ts_headline(wpr.text,}.$queryfunc.q{($2)) AS headline
166                         ,ts_rank_cd(textsearch, }.$queryfunc.q{($2),32) AS rank
167                         FROM wiki_pages wp
168                                 JOIN wiki_page_revisions wpr USING (wprev)
169                         WHERE (namespace IN (SELECT namespace FROM wiki_namespace_access WHERE gid IN (SELECT groups($1)))
170                                         OR wp.wpid IN (SELECT wpid FROM wiki_page_access WHERE uid = $1))
171                                 AND textsearch @@ }.$queryfunc.q{($2)
172                         ORDER BY rank DESC
173                 });
174                 eval {
175                         $posts->execute($c->stash->{UID},$c->req->param('search'));
176                         my @posts;
177                         while (my $post = $posts->fetchrow_hashref){
178                                 push @posts,$post;
179                         }
180                         $c->stash(searchresults => \@posts);
181                 };
182                 if ($@){
183                         $c->stash( searcherror => $dbh->errstr);
184                 }
185         }
186
187 }
188
189 sub findPage : Private {
190         my ( $self, $c, $p ) = @_;
191         my $dbh = $c->model;
192
193         my @arguments = ($c->stash->{UID});
194         my $where;
195         if ($p){
196                 $where =  q{AND wpid = $2};
197                 push @arguments, $p;
198         }else{
199                 $where = q{AND (namespace = COALESCE($2,'') AND name = $3)};
200                 push @arguments, @{$c->req->captures};
201         }
202
203         my $query = q{SELECT wpid,namespace,name,wprev
204                 ,(CASE WHEN namespace <> '' THEN namespace || ':' ELSE '' END) || name AS fullname
205                 ,bool_or(COALESCE(wpa.edit,wna.edit)) AS edit
206                 ,bool_or(wna.post) AS post
207                 ,bool_or(wpa.moderate OR wna.moderate) AS moderate
208                 ,bool_or(wpa.wpid IS NOT NULL OR wna.namespace IS NOT NULL) AS view
209                 FROM wiki_pages wp
210                         LEFT OUTER JOIN (SELECT * FROM wiki_namespace_access
211                                 WHERE gid IN (SELECT groups($1))) wna USING (namespace)
212                         LEFT OUTER JOIN (SELECT * FROM wiki_page_access
213                                 WHERE uid =  $1) wpa USING (wpid)
214                 WHERE TRUE
215         } . $where . q{ GROUP BY wpid,namespace,name,wprev};
216         $query = $dbh->prepare($query);
217         $query->execute(@arguments);
218
219         my $page = $query->fetchrow_hashref;
220         $c->stash(page => $page);
221 }
222
223 sub loadText : Private {
224         my ( $self, $c, $p ) = @_;
225         my $dbh = $c->model;
226
227         my $text = $dbh->selectrow_array(q{SELECT text
228                 FROM wiki_page_revisions WHERE wprev = $1
229                 },undef,$c->stash->{page}->{wprev});
230         $c->stash(text => $text);
231 }
232
233 sub findNamespaces : Private {
234         my ( $self, $c, $p ) = @_;
235         my $dbh = $c->model;
236
237         my $query = $dbh->prepare(q{SELECT namespace FROM wiki_namespaces
238                 WHERE namespace IN (SELECT namespace FROM wiki_namespace_access WHERE post AND gid IN (SELECT groups($1)))
239                 ORDER BY namespace
240                 });
241         $query->execute($c->stash->{UID});
242         $c->stash(namespaces => $query->fetchall_arrayref({}) );
243 }
244
245
246 =head1 AUTHOR
247
248 Michael Andreen (harv@ruin.nu)
249
250 =head1 LICENSE
251
252 GPL 2.0, or later
253
254 =cut
255
256 1;