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