1 package NDWeb::Controller::Wiki;
5 use parent 'Catalyst::Controller';
7 use Text::MediawikiFormat prefix => '/wiki/';
11 NDWeb::Controller::Wiki - Catalyst Controller
27 my ( $self, $c ) = @_;
29 $c->stash(wikiformat => \&wikiformat);
32 sub index :Path :Args(1) {
33 my ( $self, $c, $page ) = @_;
35 $c->forward('page',$page);
36 $c->stash(template => 'wiki/page.tt2');
39 sub main :Path :Args(0) {
40 my ( $self, $c ) = @_;
42 $c->forward('page', ['Info:Main']);
43 $c->stash(template => 'wiki/page.tt2');
47 my ( $self, $c, $p ) = @_;
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');
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});
65 $c->stash(title => $c->stash->{page}->{fullname});
68 sub edit :Local :Args(1) {
69 my ( $self, $c, @p ) = @_;
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');
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};
86 sub history :Local :Args(1) {
87 my ( $self, $c ) = @_;
90 $c->forward('findPage');
92 my $query = $dbh->prepare(q{SELECT wprev,time,username,comment
93 FROM wiki_page_revisions JOIN users u USING (uid)
97 $query->execute($c->stash->{page}->{wpid});
98 $c->stash(revisions => $query->fetchall_arrayref({}) );
99 $c->stash(title => 'History for ' . $c->stash->{page}->{fullname});
102 sub postedit : Local {
103 my ( $self, $c, $p ) = @_;
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';
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});
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;
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};
127 my $query = $dbh->prepare(q{INSERT INTO wiki_page_revisions
128 (wpid,parent,text,comment,uid) VALUES($1,$2,$3,$4,$5)
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}
139 $c->res->redirect($c->uri_for($c->stash->{page}->{fullname}));
141 } if ($c->req->param('cmd') eq 'Submit');
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');
149 $c->stash(error => $@);
154 $c->forward('findPage') if $p;
155 $c->forward('findNamespaces');
157 $c->stash->{page}->{namespace} = $c->req->param('namespace');
158 $c->stash->{page}->{name} = $c->req->param('name');
160 $c->stash(text => $c->req->param('text'));
161 $c->stash(template => 'wiki/edit.tt2');
165 my ( $self, $c ) = @_;
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
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)
184 $posts->execute($c->stash->{UID},$c->req->param('search'));
186 while (my $post = $posts->fetchrow_hashref){
189 $c->stash(searchresults => \@posts);
192 $c->stash( searcherror => $dbh->errstr);
198 sub findPage : Private {
199 my ( $self, $c, $p ) = @_;
202 my @arguments = ($c->stash->{UID});
205 $where = q{AND wpid = $2};
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);
213 $c->detach('/default');
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
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)
228 } . $where . q{ GROUP BY wpid,namespace,name,wprev};
229 $query = $dbh->prepare($query);
230 $query->execute(@arguments);
232 my $page = $query->fetchrow_hashref;
233 $c->stash(page => $page);
236 sub loadText : Private {
237 my ( $self, $c, $p ) = @_;
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);
246 sub findNamespaces : Private {
247 my ( $self, $c, $p ) = @_;
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)))
254 $query->execute($c->stash->{UID});
255 $c->stash(namespaces => $query->fetchall_arrayref({}) );
261 Michael Andreen (harv@ruin.nu)