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