| 1 | package SVN::Web::action; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = 0.63; | ||
| 7 | |||
| 8 | 3 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use File::Temp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 3 | ▶ | use POSIX (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 3 | ▶ | use Time::Local qw(timegm_nocheck); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 3 | ▶ | use Time::Zone (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | 0 | ▶ | use SVN::Core; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | SVN::Web::action - base class for SVN::Web::actions | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | This is the base class for all SVN::Web actions. It provides a | ||
| 23 | constructor and some useful utility methods that actions may find | ||
| 24 | useful. It also contains documentation for anyone interested in | ||
| 25 | writing new SVN::Web actions. | ||
| 26 | |||
| 27 | =head1 OVERVIEW | ||
| 28 | |||
| 29 | SVN::Web actions are Perl modules loaded by SVN::Web. They are | ||
| 30 | expected to retrieve some information from the Subversion repository, | ||
| 31 | and return that information ready for the user's browser, optionally | ||
| 32 | via formatting by a Template::Toolkit template. | ||
| 33 | |||
| 34 | Action names are listed in the SVN::Web configuration file, | ||
| 35 | F<config.yaml>, in the C<actions:> clause. Each entry specifies the | ||
| 36 | class that implements the action, options that are set globally | ||
| 37 | for that action, and metadata that describes when and how the action | ||
| 38 | should appear in the action menu. | ||
| 39 | |||
| 40 | actions: | ||
| 41 | ... | ||
| 42 | new_action: | ||
| 43 | class: Class::That::Implements::Action | ||
| 44 | action_menu: # Optional | ||
| 45 | show: | ||
| 46 | - file # Zero or more of this, ... | ||
| 47 | - directory # ... this ... | ||
| 48 | - revision # ... or this. | ||
| 49 | - global # Or possibly just this one | ||
| 50 | link_text: (text) # Mandatory | ||
| 51 | head_only: 1 # Optional | ||
| 52 | icon: /a/path # Optional | ||
| 53 | opts: | ||
| 54 | option1: value1 | ||
| 55 | option2: value2 | ||
| 56 | ... | ||
| 57 | |||
| 58 | Each action is a class that must implement a C<run()> method. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub new { | ||
| 63 | my $class = shift; | ||
| 64 | my $self = bless {}, $class; | ||
| 65 | %$self = @_; | ||
| 66 | |||
| 67 | return $self; | ||
| 68 | } | ||
| 69 | |||
| 70 | =head1 SUBCLASSING | ||
| 71 | |||
| 72 | Actions should derive from L<SVN::Web::action>. This gives them a | ||
| 73 | default constructor that generates a hash based object. | ||
| 74 | |||
| 75 | use base 'SVN::Web::action'; | ||
| 76 | |||
| 77 | =head1 METHODS | ||
| 78 | |||
| 79 | =head2 run() | ||
| 80 | |||
| 81 | The C<run> method is where the action carries out its work. | ||
| 82 | |||
| 83 | =head3 Parameters | ||
| 84 | |||
| 85 | The method is passed a single parameter, the standard C<$self> hash | ||
| 86 | ref. This contains numerous useful keys. | ||
| 87 | |||
| 88 | =over 4 | ||
| 89 | |||
| 90 | =item $self->{opts} | ||
| 91 | |||
| 92 | The options for this action from F<config.yaml>. Using the example from the | ||
| 93 | L<OVERVIEW>, this would lead to: | ||
| 94 | |||
| 95 | $self->{opts} = { 'option1' => 'value1', | ||
| 96 | 'option2' => 'value2', | ||
| 97 | }; | ||
| 98 | |||
| 99 | =item $self->{cgi} | ||
| 100 | |||
| 101 | An instance of a CGI compatible object corresponding to the current request. This is normally an object from either the L<CGI> or L<CGI::Fast> modules, although it is possible to specify another class with the C<cgi_class> directive in | ||
| 102 | F<config.yaml>. Since we now use Plack, this is a L<Plack::Request> object. | ||
| 103 | |||
| 104 | You can use this object to retrieve the values of any parameters passed to | ||
| 105 | your action. | ||
| 106 | |||
| 107 | For example, if your action takes a C<rev> parameter, indicating the | ||
| 108 | repository revision to work on; | ||
| 109 | |||
| 110 | my $rev = $self->{cgi}->param('rev'); | ||
| 111 | |||
| 112 | =item $self->{path} | ||
| 113 | |||
| 114 | The path in the repository that was passed to the action. | ||
| 115 | |||
| 116 | =item $self->{navpaths} | ||
| 117 | |||
| 118 | A reference to an array of path components, one for each directory | ||
| 119 | (and possible final file) in $self->{path}. Equivalent to S<C<< [ | ||
| 120 | split('/', $self->{path}) ] >>> | ||
| 121 | |||
| 122 | =item $self->{config} | ||
| 123 | |||
| 124 | The config hash, as read by L<YAML> from F<config.yaml>. Directives | ||
| 125 | from the config file are second level hash keys. For example, the | ||
| 126 | C<actions> configuration directive contains a list of valid actions. | ||
| 127 | |||
| 128 | my @valid_actions = @{ $self->{config}->{actions} }; | ||
| 129 | |||
| 130 | =item $self->{reposname} | ||
| 131 | |||
| 132 | The symbolic name of the repository being accessed. | ||
| 133 | |||
| 134 | =item $self->{repos} | ||
| 135 | |||
| 136 | A instance of the L<SVN::Repos> class, corresponding to the repository | ||
| 137 | being accessed. This repository has already been opened. | ||
| 138 | |||
| 139 | For example, to find the youngest (i.e., most recent) revision of the | ||
| 140 | repository; | ||
| 141 | |||
| 142 | my $yr = $self->{repos}->fs()->youngest_rev(); | ||
| 143 | |||
| 144 | =item $self->{action} | ||
| 145 | |||
| 146 | The action that has been requested. It's possible for multiple action | ||
| 147 | names to be mapped to a single class in the config file, and this lets | ||
| 148 | you differentiate between them. | ||
| 149 | |||
| 150 | =item $self->{script} | ||
| 151 | |||
| 152 | The URL for the currently running script. | ||
| 153 | |||
| 154 | =back | ||
| 155 | |||
| 156 | =head3 Return value | ||
| 157 | |||
| 158 | The return value from C<run()> determines how the data from the action is | ||
| 159 | displayed. | ||
| 160 | |||
| 161 | =head4 Using a template | ||
| 162 | |||
| 163 | If C<run()> wants a template to be displayed containing formatted data | ||
| 164 | from the method then the hash ref should contain two keys. | ||
| 165 | |||
| 166 | =over 4 | ||
| 167 | |||
| 168 | =item template | ||
| 169 | |||
| 170 | This is the name of the template to return. By convention the template and | ||
| 171 | the action share the same name. | ||
| 172 | |||
| 173 | =item data | ||
| 174 | |||
| 175 | This is a hash ref. The hash keys become variables of the same name in the | ||
| 176 | template. | ||
| 177 | |||
| 178 | =back | ||
| 179 | |||
| 180 | The character set and MIME type can also be specified, in the | ||
| 181 | C<charset> and C<mimetype> keys. If these values are not specified | ||
| 182 | then they default to C<UTF-8> and C<text/html> respectively. | ||
| 183 | |||
| 184 | E.g., for an action named C<my_action>, using a template called | ||
| 185 | C<my_action> that looks like this: | ||
| 186 | |||
| 187 | <p>The youngest interesting revision of [% file %] is [% rev %].</p> | ||
| 188 | |||
| 189 | then this code would be appropriate. | ||
| 190 | |||
| 191 | # $rev and $file set earlier in the method | ||
| 192 | return { template => 'my_action', | ||
| 193 | data => { rev => $rev, | ||
| 194 | file => $file, | ||
| 195 | }, | ||
| 196 | }; | ||
| 197 | |||
| 198 | =head4 Returning data with optional charset and MIME type | ||
| 199 | |||
| 200 | If the action does not want to use a template and just wants to return | ||
| 201 | data, but retain control of the character set and MIME type, C<run()> | ||
| 202 | should return a hash ref. This should contain a key called C<body>, | ||
| 203 | the value of which will be sent directly to the browser. | ||
| 204 | |||
| 205 | The character set and MIME type can also be specified, in the | ||
| 206 | C<charset> and C<mimetype> keys. If these values are not specified | ||
| 207 | then they default to C<UTF-8> and C<text/html> respectively. | ||
| 208 | |||
| 209 | E.g., for an action that generates a PNG image from data in the | ||
| 210 | repository (perhaps using L<SVN::Churn>); | ||
| 211 | |||
| 212 | # $png contains the PNG image, created earlier in the method | ||
| 213 | return { mimetype => 'image/png', | ||
| 214 | body => $png | ||
| 215 | }; | ||
| 216 | |||
| 217 | =head4 Returning HTML with default charset and MIME type | ||
| 218 | |||
| 219 | If the action just wants to return HTML in UTF-8, it can return a single | ||
| 220 | scalar that contains the HTML to be sent to the browser. | ||
| 221 | |||
| 222 | return "<p>hello, world</p>"; | ||
| 223 | |||
| 224 | =head1 UTILITY METHODS | ||
| 225 | |||
| 226 | The following methods are intended to share common code among actions. | ||
| 227 | |||
| 228 | =head2 recent_interesting_rev($path, $rev) | ||
| 229 | |||
| 230 | Given a repository path, and a revision number, returns the most recent | ||
| 231 | interesting revision for the path that is the same as, or older (i.e., | ||
| 232 | smaller) than the revision number. | ||
| 233 | |||
| 234 | If called in an array context it returns all the arguments normally passed | ||
| 235 | to a log message receiver. | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | sub recent_interesting_rev { | ||
| 240 | my ($self, $path, $rev) = @_; | ||
| 241 | |||
| 242 | my $ra = $self->{repos}{ra}; | ||
| 243 | |||
| 244 | my @log_result; | ||
| 245 | |||
| 246 | $ra->get_log( [ Encode::encode('utf8',$self->rpath($path)) ], $rev, 1, 1, 0, 1, sub { @log_result = @_; } ); | ||
| 247 | |||
| 248 | return @log_result if wantarray(); | ||
| 249 | return $log_result[1]; # Revision number | ||
| 250 | } | ||
| 251 | |||
| 252 | =head2 get_revs() | ||
| 253 | |||
| 254 | Returns a list of 4 items. In order, they are: | ||
| 255 | |||
| 256 | =over | ||
| 257 | |||
| 258 | =item Explicit rev | ||
| 259 | |||
| 260 | The value of any CGI C<rev> parameter passed to the action ($exp_rev). | ||
| 261 | |||
| 262 | =item Youngest rev | ||
| 263 | |||
| 264 | The repository's youngest revision ($yng_rev) for the current path. | ||
| 265 | This is not necessarily the same as the repositories youngest | ||
| 266 | revision. | ||
| 267 | |||
| 268 | =item Actual rev | ||
| 269 | |||
| 270 | The actual revision ($act_rev) that will be acted on. This is the | ||
| 271 | explicit rev, if it's defined, otherwise it's the youngest rev. | ||
| 272 | |||
| 273 | =item Head | ||
| 274 | |||
| 275 | A boolean value indicating whether or not we can be considered to be | ||
| 276 | at the HEAD of the repository ($at_head). | ||
| 277 | |||
| 278 | =back | ||
| 279 | |||
| 280 | =cut | ||
| 281 | |||
| 282 | sub get_revs { | ||
| 283 | my $self = shift; | ||
| 284 | my $path = $self->{path}; | ||
| 285 | |||
| 286 | my $exp_rev = $self->{cgi}->param('rev'); | ||
| 287 | my $yng_rev = $self->{repos}{ra}->get_latest_revnum(); | ||
| 288 | my $act_rev = | ||
| 289 | defined $exp_rev | ||
| 290 | ? $self->recent_interesting_rev( $path, $exp_rev ) | ||
| 291 | : $self->recent_interesting_rev( $path, $yng_rev ); | ||
| 292 | |||
| 293 | my $at_head = 0; | ||
| 294 | if ( !defined $exp_rev or $exp_rev eq '' ) { | ||
| 295 | $at_head = 1; | ||
| 296 | } | ||
| 297 | else { | ||
| 298 | if ( $exp_rev == $yng_rev ) { | ||
| 299 | $at_head = 1; | ||
| 300 | } | ||
| 301 | } | ||
| 302 | return ( $exp_rev, $yng_rev, $act_rev, $at_head ); | ||
| 303 | } | ||
| 304 | |||
| 305 | =head2 format_svn_timestamp() | ||
| 306 | |||
| 307 | Given a cstring that represents a Subversion time, format the time using | ||
| 308 | POSIX::strftime() and the current settings of the C<timedate_format> and | ||
| 309 | C<timezone> configuration directives. | ||
| 310 | |||
| 311 | =cut | ||
| 312 | |||
| 313 | my $tz_offset = undef; # Cache the timezone offset | ||
| 314 | |||
| 315 | sub format_svn_timestamp { | ||
| 316 | my $self = shift; | ||
| 317 | my $cstring = shift; | ||
| 318 | |||
| 319 | # Note: Buggy on Solaris | ||
| 320 | # my $time = SVN::Core::time_from_cstring($cstring) / 1_000_000; | ||
| 321 | my (@time) = $cstring =~ /^(....)-(..)-(..)T(..):(..):(..)/; | ||
| 322 | |||
| 323 | my $time = | ||
| 324 | timegm_nocheck( $time[5], $time[4], $time[3], $time[2], $time[1] - 1, | ||
| 325 | $time[0] ); | ||
| 326 | |||
| 327 | if ( $self->{config}->{timezone} eq 'local' ) { | ||
| 328 | return POSIX::strftime( $self->{config}->{timedate_format}, | ||
| 329 | localtime($time) ); | ||
| 330 | } | ||
| 331 | |||
| 332 | if ( ( not defined $tz_offset ) and ( $self->{config}->{timezone} ne '' ) ) | ||
| 333 | { | ||
| 334 | $tz_offset = Time::Zone::tz_offset( $self->{config}->{timezone} ); | ||
| 335 | $time += $tz_offset; | ||
| 336 | } | ||
| 337 | |||
| 338 | return POSIX::strftime( $self->{config}->{timedate_format}, gmtime($time) ); | ||
| 339 | } | ||
| 340 | |||
| 341 | =head1 CACHING | ||
| 342 | |||
| 343 | If the output from the action can usefully be cached then consider | ||
| 344 | implementing a C<cache_key> method. | ||
| 345 | |||
| 346 | This method receives the same parameters as the C<run()> method, and | ||
| 347 | must use those parameters to generate a unique key for the content | ||
| 348 | generated by the C<run()> method. | ||
| 349 | |||
| 350 | For example, consider the standard C<Revision> action. This action | ||
| 351 | only depends on a single parameter -- the repository revision number. | ||
| 352 | So that makes a good cache key. | ||
| 353 | |||
| 354 | sub cache_key { | ||
| 355 | my $self = shift; | ||
| 356 | |||
| 357 | return $self->{cgi}->param('rev'); | ||
| 358 | } | ||
| 359 | |||
| 360 | Other actions may have more complicated keys. | ||
| 361 | |||
| 362 | =head1 ERRORS AND EXCEPTIONS | ||
| 363 | |||
| 364 | If your action needs to fail for some reason -- perhaps the parameters | ||
| 365 | passed to it are incorrect, or the user lacks the necessary permissions, | ||
| 366 | then throw an exception. | ||
| 367 | |||
| 368 | Exceptions, along with examples, are described in L<SVN::Web::X>. | ||
| 369 | |||
| 370 | =head1 COPYRIGHT | ||
| 371 | |||
| 372 | Copyright 2005-2007 by Nik Clayton C<< <nik@FreeBSD.org> >>. | ||
| 373 | |||
| 374 | Copyright 2012 by Dean Hamstead C<< <dean@fragfest.com.au> >>. | ||
| 375 | |||
| 376 | This program is free software; you can redistribute it and/or modify it | ||
| 377 | under the same terms as Perl itself. | ||
| 378 | |||
| 379 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 380 | |||
| 381 | =cut | ||
| 382 | |||
| 383 | sub rpath { | ||
| 384 | my ( $self, $p ) = @_; | ||
| 385 | my $path = $p || $self->{path}; | ||
| 386 | $path =~ s{^/}{} if $path; | ||
| 387 | return $path | ||
| 388 | } | ||
| 389 | |||
| 390 | sub svn_get_node_kind { | ||
| 391 | my ($self, $uri, $peg_revision, $revision, $pool) = @_; | ||
| 392 | |||
| 393 | my $node_kind; | ||
| 394 | |||
| 395 | my @args = ($self->encode_svn_uri($uri), $peg_revision, $revision, sub { $node_kind = $_[1]->kind() }, 0); | ||
| 396 | push @args, $pool if $pool; | ||
| 397 | $self->{repos}{client}->info(@args); | ||
| 398 | |||
| 399 | return $node_kind; | ||
| 400 | } | ||
| 401 | |||
| 402 | sub svn_get_diff { | ||
| 403 | my ($self, $target1, $rev1, $target2, $rev2, $recursive, $pool) = @_; | ||
| 404 | |||
| 405 | my ( $out_h, $out_fn ) = File::Temp::tempfile(); | ||
| 406 | my ( $err_h, $err_fn ) = File::Temp::tempfile(); | ||
| 407 | |||
| 408 | my @args = ([], $self->encode_svn_uri($target1), $rev1, $self->encode_svn_uri($target2), $rev2, $recursive, 1, 0, $out_h, $err_h); | ||
| 409 | push @args, $pool if $pool; | ||
| 410 | $self->{repos}{client}->diff(@args); | ||
| 411 | |||
| 412 | my $out; | ||
| 413 | local $/ = undef; | ||
| 414 | seek($out_h, 0, 0); | ||
| 415 | $out = <$out_h>; | ||
| 416 | unlink( $out_fn ); unlink( $err_fn ); | ||
| 417 | close( $out_h ); close( $err_h ); | ||
| 418 | |||
| 419 | return $out; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub ctx_ls { | ||
| 423 | my ($self, $uri) = splice(@_, 0, 2); | ||
| 424 | return $self->{repos}{client}->ls( $self->encode_svn_uri($uri), @_ ); | ||
| 425 | } | ||
| 426 | |||
| 427 | sub ctx_revprop_get { | ||
| 428 | my ($self, $prop_name, $uri, $rev) = splice(@_, 0, 4); | ||
| 429 | return $self->{repos}{client}->revprop_get( $prop_name, $self->encode_svn_uri($uri), $rev, @_ ); | ||
| 430 | } | ||
| 431 | |||
| 432 | sub ctx_propget { | ||
| 433 | my ($self, $prop_name, $uri, $rev, $recursive) = splice(@_, 0, 5); | ||
| 434 | return $self->{repos}{client}->propget( $prop_name, $self->encode_svn_uri($uri), $rev, $recursive, @_ ); | ||
| 435 | } | ||
| 436 | |||
| 437 | sub ctx_cat { | ||
| 438 | my ($self, $fh, $uri, $rev) = splice(@_, 0, 4); | ||
| 439 | return $self->{repos}{client}->cat( $fh, $self->encode_svn_uri($uri), $rev, @_ ); | ||
| 440 | } | ||
| 441 | |||
| 442 | sub ctx_blame { | ||
| 443 | my ($self, $uri, $start_rev, $end_rev, $cb) = splice(@_, 0, 5); | ||
| 444 | return $self->{repos}{client}->blame( $self->encode_svn_uri($uri), $start_rev, $end_rev, $cb, @_ ); | ||
| 445 | } | ||
| 446 | |||
| 447 | sub encode_svn_uri { | ||
| 448 | my $uri = Encode::encode('utf8', $_[1]); | ||
| 449 | # same as in svn_path_uri_encode (see subversion/libsvn_subr/path.c) | ||
| 450 | $uri =~ s#([^\-\!\$\&\'\(\)\*\+\,\.\/\:\=\@\~\_0-9A-Za-z])#sprintf("%%%02X",ord($1))#eg; | ||
| 451 | return $uri; | ||
| 452 | } | ||
| 453 | |||
| 454 | sub decode_svn_uri { | ||
| 455 | my ($self, $uri) = @_; | ||
| 456 | $uri =~ s#%([0-9A-Fa-f]{2})#chr(hex($1))#eg; | ||
| 457 | return Encode::decode('utf8', $uri); | ||
| 458 | } | ||
| 459 | |||
| 460 | 1; | ||