]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Wiki.pm
Implemented a basic Wiki.
[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 findPage : Private {
156         my ( $self, $c, $p ) = @_;
157         my $dbh = $c->model;
158
159         my @arguments = ($c->stash->{UID});
160         my $where;
161         if ($p){
162                 $where =  q{AND wpid = $2};
163                 push @arguments, $p;
164         }else{
165                 $where = q{AND (namespace = COALESCE($2,'') AND name = $3)};
166                 push @arguments, @{$c->req->captures};
167         }
168
169         my $query = q{SELECT wpid,namespace,name,wprev
170                 ,(CASE WHEN namespace <> '' THEN namespace || ':' ELSE '' END) || name AS fullname
171                 ,bool_or(COALESCE(wpa.edit,wna.edit)) AS edit
172                 ,bool_or(wna.post) AS post
173                 ,bool_or(wpa.moderate OR wna.moderate) AS moderate
174                 ,bool_or(wpa.wpid IS NOT NULL OR wna.namespace IS NOT NULL) AS view
175                 FROM wiki_pages wp
176                         LEFT OUTER JOIN (SELECT * FROM wiki_namespace_access
177                                 WHERE gid IN (SELECT groups($1))) wna USING (namespace)
178                         LEFT OUTER JOIN (SELECT * FROM wiki_page_access
179                                 WHERE uid =  $1) wpa USING (wpid)
180                 WHERE TRUE
181         } . $where . q{ GROUP BY wpid,namespace,name,wprev};
182         $query = $dbh->prepare($query);
183         $query->execute(@arguments);
184
185         my $page = $query->fetchrow_hashref;
186         $c->stash(page => $page);
187 }
188
189 sub loadText : Private {
190         my ( $self, $c, $p ) = @_;
191         my $dbh = $c->model;
192
193         my $text = $dbh->selectrow_array(q{SELECT text
194                 FROM wiki_page_revisions WHERE wprev = $1
195                 },undef,$c->stash->{page}->{wprev});
196         $c->stash(text => $text);
197 }
198
199 sub findNamespaces : Private {
200         my ( $self, $c, $p ) = @_;
201         my $dbh = $c->model;
202
203         my $query = $dbh->prepare(q{SELECT namespace FROM wiki_namespaces
204                 WHERE namespace IN (SELECT namespace FROM wiki_namespace_access WHERE post AND gid IN (SELECT groups($1)))
205                 ORDER BY namespace
206                 });
207         $query->execute($c->stash->{UID});
208         $c->stash(namespaces => $query->fetchall_arrayref({}) );
209 }
210
211
212 =head1 AUTHOR
213
214 Michael Andreen (harv@ruin.nu)
215
216 =head1 LICENSE
217
218 GPL 2.0, or later
219
220 =cut
221
222 1;