| 1 | package Yukki::Web::Controller::Page; | ||
| 2 | $Yukki::Web::Controller::Page::VERSION = '0.991_002'; # TRIAL | ||
| 3 | |||
| 4 | $Yukki::Web::Controller::Page::VERSION = '0.991002';use v5.24; | ||
| 5 | use utf8; | ||
| 6 | use Moo; | ||
| 7 | |||
| 8 | with 'Yukki::Web::Controller'; | ||
| 9 | |||
| 10 | use Try::Tiny; | ||
| 11 | use Yukki::Error qw( http_throw ); | ||
| 12 | |||
| 13 | use namespace::clean; | ||
| 14 | |||
| 15 | # ABSTRACT: controller for viewing and editing pages | ||
| 16 | |||
| 17 | |||
| 18 | sub fire { | ||
| 19 | my ($self, $ctx) = @_; | ||
| 20 | |||
| 21 | my $action = $ctx->request->path_parameters->{action}; | ||
| 22 | if ($action eq 'view') { $self->view_page($ctx) } | ||
| 23 | elsif ($action eq 'edit') { $self->edit_page($ctx) } | ||
| 24 | elsif ($action eq 'history') { $self->view_history($ctx) } | ||
| 25 | elsif ($action eq 'diff') { $self->view_diff($ctx) } | ||
| 26 | elsif ($action eq 'preview') { $self->preview_page($ctx) } | ||
| 27 | elsif ($action eq 'attach') { $self->upload_attachment($ctx) } | ||
| 28 | elsif ($action eq 'rename') { $self->rename_page($ctx) } | ||
| 29 | elsif ($action eq 'remove') { $self->remove_page($ctx) } | ||
| 30 | else { | ||
| 31 | http_throw('That page action does not exist.', { | ||
| 32 | status => 'NotFound', | ||
| 33 | }); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub repo_name_and_path { | ||
| 39 | my ($self, $ctx) = @_; | ||
| 40 | |||
| 41 | my $repo_name = $ctx->request->path_parameters->{repository}; | ||
| 42 | my $path = $ctx->request->path_parameters->{page}; | ||
| 43 | |||
| 44 | if (not defined $path) { | ||
| 45 | my $repo_config | ||
| 46 | = $self->app->settings->repositories->{$repo_name}; | ||
| 47 | |||
| 48 | my $path_str = $repo_config->default_page; | ||
| 49 | |||
| 50 | $path = [ split m{/}, $path_str ]; | ||
| 51 | } | ||
| 52 | |||
| 53 | return ($repo_name, $path); | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub lookup_page { | ||
| 58 | my ($self, $repo_name, $page) = @_; | ||
| 59 | |||
| 60 | my $repository = $self->model('Repository', { name => $repo_name }); | ||
| 61 | |||
| 62 | my $final_part = pop @$page; | ||
| 63 | my $filetype; | ||
| 64 | if ($final_part =~ s/\.(?<filetype>[a-z0-9]+)$//) { | ||
| 65 | $filetype = $+{filetype}; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $path = join '/', @$page, $final_part; | ||
| 69 | return $repository->file({ path => $path, filetype => $filetype }); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub view_page { | ||
| 74 | my ($self, $ctx) = @_; | ||
| 75 | |||
| 76 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 77 | |||
| 78 | my $page = $self->lookup_page($repo_name, $path); | ||
| 79 | |||
| 80 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 81 | |||
| 82 | my $body; | ||
| 83 | if (not $page->exists) { | ||
| 84 | my @files = $page->list_files; | ||
| 85 | |||
| 86 | $body = $self->view('Page')->blank($ctx, { | ||
| 87 | title => $page->file_name, | ||
| 88 | breadcrumb => $breadcrumb, | ||
| 89 | repository => $repo_name, | ||
| 90 | page => $page->full_path, | ||
| 91 | files => \@files, | ||
| 92 | }); | ||
| 93 | } | ||
| 94 | |||
| 95 | else { | ||
| 96 | $body = $self->view('Page')->view($ctx, { | ||
| 97 | title => $page->title, | ||
| 98 | breadcrumb => $breadcrumb, | ||
| 99 | repository => $repo_name, | ||
| 100 | page => $page->full_path, | ||
| 101 | file => $page, | ||
| 102 | }); | ||
| 103 | } | ||
| 104 | |||
| 105 | $ctx->response->body($body); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub edit_page { | ||
| 110 | my ($self, $ctx) = @_; | ||
| 111 | |||
| 112 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 113 | |||
| 114 | my $page = $self->lookup_page($repo_name, $path); | ||
| 115 | |||
| 116 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 117 | |||
| 118 | if ($ctx->request->method eq 'POST') { | ||
| 119 | my $new_content = $ctx->request->parameters->{yukkitext}; | ||
| 120 | my $position = $ctx->request->parameters->{yukkitext_position}; | ||
| 121 | my $comment = $ctx->request->parameters->{comment}; | ||
| 122 | |||
| 123 | if (my $user = $ctx->session->{user}) { | ||
| 124 | $page->author_name($user->{name}); | ||
| 125 | $page->author_email($user->{email}); | ||
| 126 | } | ||
| 127 | |||
| 128 | $page->store({ | ||
| 129 | content => $new_content, | ||
| 130 | comment => $comment, | ||
| 131 | }); | ||
| 132 | |||
| 133 | $ctx->response->redirect(join '/', '/page/edit', $repo_name, $page->full_path, '?yukkitext_position='.$position); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | my @attachments = grep { $_->filetype ne 'yukki' } $page->list_files; | ||
| 138 | my $position = $ctx->request->parameters->{yukkitext_position} // -1; | ||
| 139 | |||
| 140 | $ctx->response->body( | ||
| 141 | $self->view('Page')->edit($ctx, { | ||
| 142 | title => $page->title, | ||
| 143 | breadcrumb => $breadcrumb, | ||
| 144 | repository => $repo_name, | ||
| 145 | page => $page->full_path, | ||
| 146 | position => $position, | ||
| 147 | file => $page, | ||
| 148 | attachments => \@attachments, | ||
| 149 | }) | ||
| 150 | ); | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | sub rename_page { | ||
| 155 | my ($self, $ctx) = @_; | ||
| 156 | |||
| 157 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 158 | |||
| 159 | my $page = $self->lookup_page($repo_name, $path); | ||
| 160 | |||
| 161 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 162 | |||
| 163 | if ($ctx->request->method eq 'POST') { | ||
| 164 | my $new_name = $ctx->request->parameters->{yukkiname_new}; | ||
| 165 | |||
| 166 | my $part = qr{[_a-z0-9-.]+(?:\.[_a-z0-9-]+)*}i; | ||
| 167 | if ($new_name =~ m{^$part(?:/$part)*$}) { | ||
| 168 | |||
| 169 | if (my $user = $ctx->session->{user}) { | ||
| 170 | $page->author_name($user->{name}); | ||
| 171 | $page->author_email($user->{email}); | ||
| 172 | } | ||
| 173 | |||
| 174 | $page->rename({ | ||
| 175 | full_path => $new_name, | ||
| 176 | comment => 'Renamed ' . $page->full_path . ' to ' . $new_name, | ||
| 177 | }); | ||
| 178 | |||
| 179 | $ctx->response->redirect(join '/', '/page/edit', $repo_name, $new_name); | ||
| 180 | return; | ||
| 181 | |||
| 182 | } | ||
| 183 | else { | ||
| 184 | $ctx->add_errors('the new name must contain only letters, numbers, underscores, dashes, periods, and slashes'); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | $ctx->response->body( | ||
| 189 | $self->view('Page')->rename($ctx, { | ||
| 190 | title => $page->title, | ||
| 191 | breadcrumb => $breadcrumb, | ||
| 192 | repository => $repo_name, | ||
| 193 | page => $page->full_path, | ||
| 194 | file => $page, | ||
| 195 | }) | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | sub remove_page { | ||
| 201 | my ($self, $ctx) = @_; | ||
| 202 | |||
| 203 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 204 | |||
| 205 | my $page = $self->lookup_page($repo_name, $path); | ||
| 206 | |||
| 207 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 208 | |||
| 209 | my $confirmed = $ctx->request->body_parameters->{confirmed}; | ||
| 210 | if ($ctx->request->method eq 'POST' and $confirmed) { | ||
| 211 | my $return_to = $page->parent // $page->repository->default_file; | ||
| 212 | if ($return_to->full_path ne $page->full_path) { | ||
| 213 | if (my $user = $ctx->session->{user}) { | ||
| 214 | $page->author_name($user->{name}); | ||
| 215 | $page->author_email($user->{email}); | ||
| 216 | } | ||
| 217 | |||
| 218 | $page->remove({ | ||
| 219 | comment => 'Removing ' . $page->full_path . ' from repository.', | ||
| 220 | }); | ||
| 221 | |||
| 222 | $ctx->response->redirect(join '/', '/page/view', $repo_name, $return_to->full_path); | ||
| 223 | return; | ||
| 224 | |||
| 225 | } | ||
| 226 | |||
| 227 | else { | ||
| 228 | $ctx->add_errors('you may not remove the top-most page of a repository'); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | $ctx->response->body( | ||
| 233 | $self->view('Page')->remove($ctx, { | ||
| 234 | title => $page->title, | ||
| 235 | breadcrumb => $breadcrumb, | ||
| 236 | repository => $repo_name, | ||
| 237 | page => $page->full_path, | ||
| 238 | file => $page, | ||
| 239 | return_link => join('/', '/page/view', $repo_name, $page->full_path), | ||
| 240 | }) | ||
| 241 | ); | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub view_history { | ||
| 246 | my ($self, $ctx) = @_; | ||
| 247 | |||
| 248 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 249 | |||
| 250 | my $page = $self->lookup_page($repo_name, $path); | ||
| 251 | |||
| 252 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 253 | |||
| 254 | $ctx->response->body( | ||
| 255 | $self->view('Page')->history($ctx, { | ||
| 256 | title => $page->title, | ||
| 257 | breadcrumb => $breadcrumb, | ||
| 258 | repository => $repo_name, | ||
| 259 | page => $page->full_path, | ||
| 260 | revisions => [ $page->history ], | ||
| 261 | }) | ||
| 262 | ); | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | sub view_diff { | ||
| 267 | my ($self, $ctx) = @_; | ||
| 268 | |||
| 269 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 270 | |||
| 271 | my $page = $self->lookup_page($repo_name, $path); | ||
| 272 | |||
| 273 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 274 | |||
| 275 | my $r1 = $ctx->request->query_parameters->{r1}; | ||
| 276 | my $r2 = $ctx->request->query_parameters->{r2}; | ||
| 277 | |||
| 278 | try { | ||
| 279 | |||
| 280 | my $diff = ''; | ||
| 281 | for my $chunk ($page->diff($r1, $r2)) { | ||
| 282 | if ($chunk->[0] eq ' ') { $diff .= $chunk->[1] } | ||
| 283 | elsif ($chunk->[0] eq '+') { $diff .= sprintf '<ins markdown="1">%s</ins>', $chunk->[1] } | ||
| 284 | elsif ($chunk->[0] eq '-') { $diff .= sprintf '<del markdown="1">%s</del>', $chunk->[1] } | ||
| 285 | else { warn "unknown chunk type $chunk->[0]" } | ||
| 286 | } | ||
| 287 | |||
| 288 | my $file_preview = $page->file_preview( | ||
| 289 | content => $diff, | ||
| 290 | ); | ||
| 291 | |||
| 292 | $ctx->response->body( | ||
| 293 | $self->view('Page')->diff($ctx, { | ||
| 294 | title => $page->title, | ||
| 295 | breadcrumb => $breadcrumb, | ||
| 296 | repository => $repo_name, | ||
| 297 | page => $page->full_path, | ||
| 298 | file => $file_preview, | ||
| 299 | }) | ||
| 300 | ); | ||
| 301 | } | ||
| 302 | |||
| 303 | catch { | ||
| 304 | my $ERROR = $_; | ||
| 305 | if ("$_" =~ /usage: git diff/) { | ||
| 306 | 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.'; | ||
| 307 | } | ||
| 308 | die $ERROR; | ||
| 309 | }; | ||
| 310 | } | ||
| 311 | |||
| 312 | |||
| 313 | sub preview_page { | ||
| 314 | my ($self, $ctx) = @_; | ||
| 315 | |||
| 316 | my ($repo_name, $path) = $self->repo_name_and_path($ctx); | ||
| 317 | |||
| 318 | my $page = $self->lookup_page($repo_name, $path); | ||
| 319 | |||
| 320 | my $breadcrumb = $self->breadcrumb($page->repository, $path); | ||
| 321 | |||
| 322 | my $content = $ctx->request->body_parameters->{yukkitext}; | ||
| 323 | my $position = $ctx->request->parameters->{yukkitext_position}; | ||
| 324 | my $file_preview = $page->file_preview( | ||
| 325 | content => $content, | ||
| 326 | position => $position, | ||
| 327 | ); | ||
| 328 | |||
| 329 | $ctx->response->body( | ||
| 330 | $self->view('Page')->preview($ctx, { | ||
| 331 | title => $page->title, | ||
| 332 | breadcrumb => $breadcrumb, | ||
| 333 | repository => $repo_name, | ||
| 334 | page => $page->full_path, | ||
| 335 | file => $file_preview, | ||
| 336 | }) | ||
| 337 | ); | ||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | sub upload_attachment { | ||
| 342 | my ($self, $ctx) = @_; | ||
| 343 | |||
| 344 | my $repo_name = $ctx->request->path_parameters->{repository}; | ||
| 345 | my $path = delete $ctx->request->path_parameters->{page}; | ||
| 346 | |||
| 347 | my $page = $self->lookup_page($repo_name, $path); | ||
| 348 | |||
| 349 | my @file = split m{/}, $page->path; | ||
| 350 | push @file, $ctx->request->uploads->{file}->filename; | ||
| 351 | |||
| 352 | $ctx->request->path_parameters->{action} = 'upload'; | ||
| 353 | $ctx->request->path_parameters->{file} = \@file; | ||
| 354 | |||
| 355 | $self->controller('Attachment')->fire($ctx); | ||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | sub breadcrumb { | ||
| 360 | my ($self, $repository, $path_parts) = @_; | ||
| 361 | |||
| 362 | my @breadcrumb; | ||
| 363 | my @path_acc; | ||
| 364 | |||
| 365 | push @breadcrumb, { | ||
| 366 | label => $repository->title, | ||
| 367 | href => join('/', '/page/view/', $repository->name), | ||
| 368 | }; | ||
| 369 | |||
| 370 | for my $path_part (@$path_parts) { | ||
| 371 | push @path_acc, $path_part; | ||
| 372 | my $file = $repository->file({ | ||
| 373 | path => join('/', @path_acc), | ||
| 374 | filetype => 'yukki', | ||
| 375 | }); | ||
| 376 | |||
| 377 | push @breadcrumb, { | ||
| 378 | label => $file->title, | ||
| 379 | href => join('/', '/page/view', $repository->name, $file->full_path), | ||
| 380 | }; | ||
| 381 | } | ||
| 382 | |||
| 383 | return \@breadcrumb; | ||
| 384 | } | ||
| 385 | |||
| 386 | 1; | ||
| 387 | |||
| 388 | __END__ | ||
| 389 | |||
| 390 | =pod | ||
| 391 | |||
| 392 | =encoding UTF-8 | ||
| 393 | |||
| 394 | =head1 NAME | ||
| 395 | |||
| 396 | Yukki::Web::Controller::Page - controller for viewing and editing pages | ||
| 397 | |||
| 398 | =head1 VERSION | ||
| 399 | |||
| 400 | version 0.991_002 | ||
| 401 | |||
| 402 | =head1 DESCRIPTION | ||
| 403 | |||
| 404 | Controller for viewing and editing pages | ||
| 405 | |||
| 406 | =head1 METHODS | ||
| 407 | |||
| 408 | =head2 fire | ||
| 409 | |||
| 410 | On a view request routes to L</view_page>, edit request to L</edit_page>, preview request to L</preview_page>, and attach request to L</upload_attachment>. | ||
| 411 | |||
| 412 | =head2 repo_name_and_path | ||
| 413 | |||
| 414 | This is a helper for looking up the repository name and path for the request. | ||
| 415 | |||
| 416 | =head2 lookup_page | ||
| 417 | |||
| 418 | Given a repository name and page, returns a L<Yukki::Model::File> for it. | ||
| 419 | |||
| 420 | =head2 view_page | ||
| 421 | |||
| 422 | Tells either L<Yukki::Web::View::Page/blank> or L<Yukki::Web::View::Page/view> | ||
| 423 | to show the page. | ||
| 424 | |||
| 425 | =head2 edit_page | ||
| 426 | |||
| 427 | Displays or processes the edit form for a page using. | ||
| 428 | |||
| 429 | =head2 rename_page | ||
| 430 | |||
| 431 | Displays the rename page form. | ||
| 432 | |||
| 433 | =head2 remove_page | ||
| 434 | |||
| 435 | Displays the remove confirmation. | ||
| 436 | |||
| 437 | =head2 view_history | ||
| 438 | |||
| 439 | Displays the page's revision history. | ||
| 440 | |||
| 441 | =head2 view_diff | ||
| 442 | |||
| 443 | Displays a diff of the page. | ||
| 444 | |||
| 445 | =head2 preview_page | ||
| 446 | |||
| 447 | Shows the preview for an edit to a page using L<Yukki::Web::View::Page/preview>.. | ||
| 448 | |||
| 449 | =head2 upload_attachment | ||
| 450 | |||
| 451 | This is a facade that wraps L<Yukki::Web::Controller::Attachment/upload>. | ||
| 452 | |||
| 453 | =head2 breadcrumb | ||
| 454 | |||
| 455 | Given the repository and path, returns the breadcrumb. | ||
| 456 | |||
| 457 | =head1 AUTHOR | ||
| 458 | |||
| 459 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||
| 460 | |||
| 461 | =head1 COPYRIGHT AND LICENSE | ||
| 462 | |||
| 463 | This software is copyright (c) 2017 by Qubling Software LLC. | ||
| 464 | |||
| 465 | This is free software; you can redistribute it and/or modify it under | ||
| 466 | the same terms as the Perl 5 programming language system itself. | ||
| 467 | |||
| 468 | =cut |