| 1 | package Yukki::Web::Controller::Page; | ||
| 2 | { | ||
| 3 | $Yukki::Web::Controller::Page::VERSION = '0.140290'; | ||
| 4 | } | ||
| 5 | use 5.12.1; | ||
| 6 | use Moose; | ||
| 7 | |||
| 8 | with 'Yukki::Web::Controller'; | ||
| 9 | |||
| 10 | use Try::Tiny; | ||
| 11 | use Yukki::Error qw( http_throw ); | ||
| 12 | |||
| 13 | # ABSTRACT: controller for viewing and editing pages | ||
| 14 | |||
| 15 | |||
| 16 | sub fire { | ||
| 17 | my ($self, $ctx) = @_; | ||
| 18 | |||
| 19 | given ($ctx->request->path_parameters->{action}) { | ||
| 20 | when ('view') { $self->view_page($ctx) } | ||
| 21 | when ('edit') { $self->edit_page($ctx) } | ||
| 22 | when ('history') { $self->view_history($ctx) } | ||
| 23 | when ('diff') { $self->view_diff($ctx) } | ||
| 24 | when ('preview') { $self->preview_page($ctx) } | ||
| 25 | when ('attach') { $self->upload_attachment($ctx) } | ||
| 26 | when ('rename') { $self->rename_page($ctx) } | ||
| 27 | when ('remove') { $self->remove_page($ctx) } | ||
| 28 | default { | ||
| 29 | http_throw('That page action does not exist.', { | ||
| 30 | status => 'NotFound', | ||
| 31 | }); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | sub repo_name_and_path { | ||
| 38 | my ($self, $ctx) = @_; | ||
| 39 | |||
| 40 | my $repo_name = $ctx->request->path_parameters->{repository}; | ||
| 41 | my $path = $ctx->request->path_parameters->{page}; | ||
| 42 | |||
| 43 | if (not defined $path) { | ||
| 44 | my $repo_config | ||
| 45 | = $self->app->settings->repositories->{$repo_name}; | ||
| 46 | |||
| 47 | my $path_str = $repo_config->default_page; | ||
| 48 | |||
| 49 | $path = [ split m{/}, $path_str ]; | ||
| 50 | } | ||
| 51 | |||
| 52 | return ($repo_name, $path); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub lookup_page { | ||
| 57 | my ($self, $repo_name, $page) = @_; | ||
| 58 | |||
| 59 | my $repository = $self->model('Repository', { name => $repo_name }); | ||
| 60 | |||
| 61 | my $final_part = pop @$page; | ||
| 62 | my $filetype; | ||
| 63 | if ($final_part =~ s/\.(?<filetype>[a-z0-9]+)$//) { | ||
| 64 | $filetype = $+{filetype}; | ||
| 65 | } | ||
| 66 | |||
| 67 | my $path = join '/', @$page, $final_part; | ||
| 68 | return $repository->file({ path => $path, filetype => $filetype }); | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | sub view_page { | ||
| 73 | my ($self, $ctx) = @_; | ||
| 74 | |||
| 75 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 76 | |||
| 77 | my $page = $self->lookup_page($repo_name, $path); | ||
| 78 | |||
| 79 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 80 | |||
| 81 | my $body; | ||
| 82 | if (not $page->exists) { | ||
| 83 | my @files = $page->list_files; | ||
| 84 | |||
| 85 | $body = $self->view('Page')->blank($ctx, { | ||
| 86 | title => $page->file_name, | ||
| 87 | breadcrumb => $breadcrumb, | ||
| 88 | repository => $repo_name, | ||
| 89 | page => $page->full_path, | ||
| 90 | files => \@files, | ||
| 91 | }); | ||
| 92 | } | ||
| 93 | |||
| 94 | else { | ||
| 95 | $body = $self->view('Page')->view($ctx, { | ||
| 96 | title => $page->title, | ||
| 97 | breadcrumb => $breadcrumb, | ||
| 98 | repository => $repo_name, | ||
| 99 | page => $page->full_path, | ||
| 100 | file => $page, | ||
| 101 | }); | ||
| 102 | } | ||
| 103 | |||
| 104 | $ctx->response->body($body); | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | sub edit_page { | ||
| 109 | my ($self, $ctx) = @_; | ||
| 110 | |||
| 111 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 112 | |||
| 113 | my $page = $self->lookup_page($repo_name, $path); | ||
| 114 | |||
| 115 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 116 | |||
| 117 | if ($ctx->request->method eq 'POST') { | ||
| 118 | my $new_content = $ctx->request->parameters->{yukkitext}; | ||
| 119 | my $position = $ctx->request->parameters->{yukkitext_position}; | ||
| 120 | my $comment = $ctx->request->parameters->{comment}; | ||
| 121 | |||
| 122 | if (my $user = $ctx->session->{user}) { | ||
| 123 | $page->author_name($user->{name}); | ||
| 124 | $page->author_email($user->{email}); | ||
| 125 | } | ||
| 126 | |||
| 127 | $page->store({ | ||
| 128 | content => $new_content, | ||
| 129 | comment => $comment, | ||
| 130 | }); | ||
| 131 | |||
| 132 | $ctx->response->redirect(join '/', '/page/edit', $repo_name, $page->full_path, '?yukkitext_position='.$position); | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | my @attachments = grep { $_->filetype ne 'yukki' } $page->list_files; | ||
| 137 | my $position = $ctx->request->parameters->{yukkitext_position} // -1; | ||
| 138 | |||
| 139 | $ctx->response->body( | ||
| 140 | $self->view('Page')->edit($ctx, { | ||
| 141 | title => $page->title, | ||
| 142 | breadcrumb => $breadcrumb, | ||
| 143 | repository => $repo_name, | ||
| 144 | page => $page->full_path, | ||
| 145 | position => $position, | ||
| 146 | file => $page, | ||
| 147 | attachments => \@attachments, | ||
| 148 | }) | ||
| 149 | ); | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub rename_page { | ||
| 154 | my ($self, $ctx) = @_; | ||
| 155 | |||
| 156 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 157 | |||
| 158 | my $page = $self->lookup_page($repo_name, $path); | ||
| 159 | |||
| 160 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 161 | |||
| 162 | if ($ctx->request->method eq 'POST') { | ||
| 163 | my $new_name = $ctx->request->parameters->{yukkiname_new}; | ||
| 164 | |||
| 165 | my $part = qr{[_a-z0-9-.]+(?:\.[_a-z0-9-]+)*}i; | ||
| 166 | if ($new_name =~ m{^$part(?:/$part)*$}) { | ||
| 167 | |||
| 168 | if (my $user = $ctx->session->{user}) { | ||
| 169 | $page->author_name($user->{name}); | ||
| 170 | $page->author_email($user->{email}); | ||
| 171 | } | ||
| 172 | |||
| 173 | $page->rename({ | ||
| 174 | full_path => $new_name, | ||
| 175 | comment => 'Renamed ' . $page->full_path . ' to ' . $new_name, | ||
| 176 | }); | ||
| 177 | |||
| 178 | $ctx->response->redirect(join '/', '/page/edit', $repo_name, $new_name); | ||
| 179 | return; | ||
| 180 | |||
| 181 | } | ||
| 182 | else { | ||
| 183 | $ctx->add_errors('the new name must contain only letters, numbers, underscores, dashes, periods, and slashes'); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | $ctx->response->body( | ||
| 188 | $self->view('Page')->rename($ctx, { | ||
| 189 | title => $page->title, | ||
| 190 | breadcrumb => $breadcrumb, | ||
| 191 | repository => $repo_name, | ||
| 192 | page => $page->full_path, | ||
| 193 | file => $page, | ||
| 194 | }) | ||
| 195 | ); | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | sub remove_page { | ||
| 200 | my ($self, $ctx) = @_; | ||
| 201 | |||
| 202 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 203 | |||
| 204 | my $page = $self->lookup_page($repo_name, $path); | ||
| 205 | |||
| 206 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 207 | |||
| 208 | my $confirmed = $ctx->request->body_parameters->{confirmed}; | ||
| 209 | if ($ctx->request->method eq 'POST' and $confirmed) { | ||
| 210 | my $return_to = $page->parent // $page->repository->default_file; | ||
| 211 | if ($return_to->full_path ne $page->full_path) { | ||
| 212 | if (my $user = $ctx->session->{user}) { | ||
| 213 | $page->author_name($user->{name}); | ||
| 214 | $page->author_email($user->{email}); | ||
| 215 | } | ||
| 216 | |||
| 217 | $page->remove({ | ||
| 218 | comment => 'Removing ' . $page->full_path . ' from repository.', | ||
| 219 | }); | ||
| 220 | |||
| 221 | $ctx->response->redirect(join '/', '/page/view', $repo_name, $return_to->full_path); | ||
| 222 | return; | ||
| 223 | |||
| 224 | } | ||
| 225 | |||
| 226 | else { | ||
| 227 | $ctx->add_errors('you may not remove the top-most page of a repository'); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | $ctx->response->body( | ||
| 232 | $self->view('Page')->remove($ctx, { | ||
| 233 | title => $page->title, | ||
| 234 | breadcrumb => $breadcrumb, | ||
| 235 | repository => $repo_name, | ||
| 236 | page => $page->full_path, | ||
| 237 | file => $page, | ||
| 238 | return_link => join('/', '/page/view', $repo_name, $page->full_path), | ||
| 239 | }) | ||
| 240 | ); | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | sub view_history { | ||
| 245 | my ($self, $ctx) = @_; | ||
| 246 | |||
| 247 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 248 | |||
| 249 | my $page = $self->lookup_page($repo_name, $path); | ||
| 250 | |||
| 251 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 252 | |||
| 253 | $ctx->response->body( | ||
| 254 | $self->view('Page')->history($ctx, { | ||
| 255 | title => $page->title, | ||
| 256 | breadcrumb => $breadcrumb, | ||
| 257 | repository => $repo_name, | ||
| 258 | page => $page->full_path, | ||
| 259 | revisions => [ $page->history ], | ||
| 260 | }) | ||
| 261 | ); | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | sub view_diff { | ||
| 266 | my ($self, $ctx) = @_; | ||
| 267 | |||
| 268 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 269 | |||
| 270 | my $page = $self->lookup_page($repo_name, $path); | ||
| 271 | |||
| 272 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 273 | |||
| 274 | my $r1 = $ctx->request->query_parameters->{r1}; | ||
| 275 | my $r2 = $ctx->request->query_parameters->{r2}; | ||
| 276 | |||
| 277 | try { | ||
| 278 | |||
| 279 | my $diff = ''; | ||
| 280 | for my $chunk ($page->diff($r1, $r2)) { | ||
| 281 | given ($chunk->[0]) { | ||
| 282 | when (' ') { $diff .= $chunk->[1] } | ||
| 283 | when ('+') { $diff .= sprintf '<ins markdown="1">%s</ins>', $chunk->[1] } | ||
| 284 | when ('-') { $diff .= sprintf '<del markdown="1">%s</del>', $chunk->[1] } | ||
| 285 | default { warn "unknown chunk type $chunk->[0]" } | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | my $file_preview = $page->file_preview( | ||
| 290 | content => $diff, | ||
| 291 | ); | ||
| 292 | |||
| 293 | $ctx->response->body( | ||
| 294 | $self->view('Page')->diff($ctx, { | ||
| 295 | title => $page->title, | ||
| 296 | breadcrumb => $breadcrumb, | ||
| 297 | repository => $repo_name, | ||
| 298 | page => $page->full_path, | ||
| 299 | file => $file_preview, | ||
| 300 | }) | ||
| 301 | ); | ||
| 302 | } | ||
| 303 | |||
| 304 | catch { | ||
| 305 | my $ERROR = $_; | ||
| 306 | if ("$_" =~ /usage: git diff/) { | ||
| 307 | http_throw 'Diffs will not work with git versions before 1.7.2. Please use a newer version of git. If you are using a newer version of git, please file a support issue.'; | ||
| 308 | } | ||
| 309 | die $ERROR; | ||
| 310 | }; | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | sub preview_page { | ||
| 315 | my ($self, $ctx) = @_; | ||
| 316 | |||
| 317 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 318 | |||
| 319 | my $page = $self->lookup_page($repo_name, $path); | ||
| 320 | |||
| 321 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 322 | |||
| 323 | my $content = $ctx->request->body_parameters->{yukkitext}; | ||
| 324 | my $position = $ctx->request->parameters->{yukkitext_position}; | ||
| 325 | my $file_preview = $page->file_preview( | ||
| 326 | content => $content, | ||
| 327 | position => $position, | ||
| 328 | ); | ||
| 329 | |||
| 330 | $ctx->response->body( | ||
| 331 | $self->view('Page')->preview($ctx, { | ||
| 332 | title => $page->title, | ||
| 333 | breadcrumb => $breadcrumb, | ||
| 334 | repository => $repo_name, | ||
| 335 | page => $page->full_path, | ||
| 336 | file => $file_preview, | ||
| 337 | }) | ||
| 338 | ); | ||
| 339 | } | ||
| 340 | |||
| 341 | |||
| 342 | sub upload_attachment { | ||
| 343 | my ($self, $ctx) = @_; | ||
| 344 | |||
| 345 | my $repo_name = $ctx->request->path_parameters->{repository}; | ||
| 346 | my $path = delete $ctx->request->path_parameters->{page}; | ||
| 347 | |||
| 348 | my $page = $self->lookup_page($repo_name, $path); | ||
| 349 | |||
| 350 | my @file = split m{/}, $page->path; | ||
| 351 | push @file, $ctx->request->uploads->{file}->filename; | ||
| 352 | |||
| 353 | $ctx->request->path_parameters->{action} = 'upload'; | ||
| 354 | $ctx->request->path_parameters->{file} = \@file; | ||
| 355 | |||
| 356 | $self->controller('Attachment')->fire($ctx); | ||
| 357 | } | ||
| 358 | |||
| 359 | |||
| 360 | sub breadcrumb { | ||
| 361 | my ($self, $repository, $path_parts) = @_; | ||
| 362 | |||
| 363 | my @breadcrumb; | ||
| 364 | my @path_acc; | ||
| 365 | |||
| 366 | push @breadcrumb, { | ||
| 367 | label => $repository->title, | ||
| 368 | href => join('/', '/page/view/', $repository->name), | ||
| 369 | }; | ||
| 370 | |||
| 371 | for my $path_part (@$path_parts) { | ||
| 372 | push @path_acc, $path_part; | ||
| 373 | my $file = $repository->file({ | ||
| 374 | path => join('/', @path_acc), | ||
| 375 | filetype => 'yukki', | ||
| 376 | }); | ||
| 377 | |||
| 378 | push @breadcrumb, { | ||
| 379 | label => $file->title, | ||
| 380 | href => join('/', '/page/view', $repository->name, $file->full_path), | ||
| 381 | }; | ||
| 382 | } | ||
| 383 | |||
| 384 | return \@breadcrumb; | ||
| 385 | } | ||
| 386 | |||
| 387 | 1; | ||
| 388 | |||
| 389 | __END__ |