| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Catalyst::Plugin::Sitemap; | 
| 2 |  |  |  |  |  |  | BEGIN { | 
| 3 | 1 |  |  | 1 |  | 1347610 | $Catalyst::Plugin::Sitemap::AUTHORITY = 'cpan:YANICK'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  | # ABSTRACT: Sitemap support for Catalyst. | 
| 6 |  |  |  |  |  |  | $Catalyst::Plugin::Sitemap::VERSION = '1.0.1'; | 
| 7 | 1 |  |  | 1 |  | 11 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 51 |  | 
| 8 | 1 |  |  | 1 |  | 8 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 1 |  |  | 1 |  | 5 | use Moose::Role; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 1 |  |  | 1 |  | 4524 | no warnings qw/uninitialized/; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 57 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 1 |  |  | 1 |  | 268 | use WWW::Sitemap::XML; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | use List::Util qw/ first /; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | has sitemap => ( | 
| 18 |  |  |  |  |  |  | is      => 'ro', | 
| 19 |  |  |  |  |  |  | builder => '_build_sitemap', | 
| 20 |  |  |  |  |  |  | lazy    => 1, | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub sitemap_as_xml { | 
| 24 |  |  |  |  |  |  | return $_[0]->sitemap->as_xml->toString; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub _build_sitemap { | 
| 28 |  |  |  |  |  |  | my $self = shift; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | my $sitemap = WWW::Sitemap::XML->new; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | for my $controller ( map { $self->controller($_) } $self->controllers ) { | 
| 33 |  |  |  |  |  |  | for my $action ( $controller->get_action_methods ) { | 
| 34 |  |  |  |  |  |  | $self->_add_controller_action_to_sitemap( $sitemap, $controller, $action ); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | return $sitemap; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub _add_controller_action_to_sitemap { | 
| 42 |  |  |  |  |  |  | my( $self, $sitemap, $controller, $act ) = @_; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | my $action = $controller->action_for($act->name); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | my $attr = $action->attributes->{Sitemap} or return; | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | die "more than one attribute 'Sitemap' for sub ", $act->fully_qualified_name | 
| 49 |  |  |  |  |  |  | if @$attr > 1; | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | my @attr = split /\s*(?:,|=>)\s*/, $attr->[0]; | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | my %uri_params; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | if ( @attr == 1 ) { | 
| 56 |  |  |  |  |  |  | if ( $attr[0] eq '*' ) { | 
| 57 |  |  |  |  |  |  | my $sitemap_method = $action->name . "_sitemap"; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | return $controller->$sitemap_method( $self, $sitemap ) | 
| 60 |  |  |  |  |  |  | if  $controller->can($sitemap_method); | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | elsif ( $attr[0] + 0 > 0 ) { # it's a number | 
| 63 |  |  |  |  |  |  | $uri_params{priority} = $attr[0]; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | elsif ( @attr > 0 ) { | 
| 67 |  |  |  |  |  |  | %uri_params = @attr; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | $uri_params{loc} = $self->uri_for_action( $action->private_path ); | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | $sitemap->add(%uri_params); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | 1; | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | __END__ | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =pod | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =encoding UTF-8 | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =head1 NAME | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | Catalyst::Plugin::Sitemap - Sitemap support for Catalyst. | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =head1 VERSION | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | version 1.0.1 | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # in MyApp.pm | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | use Catalyst qw/ Sitemap /; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | # in the controller | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub alone :Local :Sitemap { | 
| 100 |  |  |  |  |  |  | ... | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub with_priority :Local :Sitemap(0.75) { | 
| 104 |  |  |  |  |  |  | ... | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | sub with_args :Local | 
| 108 |  |  |  |  |  |  | :Sitemap( lastmod => 2010-09-27, changefreq => daily ) { | 
| 109 |  |  |  |  |  |  | ... | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub with_function :Local :Sitemap(*) { | 
| 113 |  |  |  |  |  |  | ... | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub with_function_sitemap { | 
| 117 |  |  |  |  |  |  | $_[2]->add( 'http://localhost/with_function' ); | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | # and then... | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | sub sitemap : Path('/sitemap') { | 
| 123 |  |  |  |  |  |  | my ( $self, $c ) = @_; | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | $c->res->body( $c->sitemap_as_xml ); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | L<Catalyst::Plugin::Sitemap> provides a way to semi-automate the creation | 
| 131 |  |  |  |  |  |  | of the sitemap of a Catalyst application. | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =head1 CONTEXT METHOD | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head2 sitemap() | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | Returns a L<WWW::Sitemap::XML> object. The sitemap object is populated by | 
| 138 |  |  |  |  |  |  | inspecting the controllers of the application for actions with the | 
| 139 |  |  |  |  |  |  | sub attribute C<:Sitemap>. | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =head2 sitemap_as_xml() | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | Returns the sitemap as a string containing its XML representation. | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =head1 C<:Sitemap> Subroutine Attribute | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | The sitemap is populated by actions ear-marked with the <:Sitemap> sub | 
| 148 |  |  |  |  |  |  | attribute.  It can be invoked in different ways: | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =over | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =item C<:Sitemap> | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | sub alone :Local :Sitemap { | 
| 155 |  |  |  |  |  |  | ... | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | Adds the url of the action to the sitemap. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | If the action does not | 
| 161 |  |  |  |  |  |  | resolves in a single url, this will results in an error. | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | =item C<:Sitemap($priority)> | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | sub with_priority :Local :Sitemap(0.9) { | 
| 166 |  |  |  |  |  |  | ... | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | Adds the url, with the given number, which has to be between 1 (inclusive) | 
| 170 |  |  |  |  |  |  | and 0 (exclusive), as its priority. | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | If the action does not | 
| 173 |  |  |  |  |  |  | resolves in a single url, this will results in an error. | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =item C<:Sitemap( %attributes )> | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | sub with_args :Local | 
| 178 |  |  |  |  |  |  | :Sitemap( lastmod => 2010-09-27, changefreq => daily ) { | 
| 179 |  |  |  |  |  |  | ... | 
| 180 |  |  |  |  |  |  | } | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | Adds the url with the given entry attributes (as defined by | 
| 183 |  |  |  |  |  |  | L<WWW::Sitemap::XML::URL>). | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | If the action does not | 
| 186 |  |  |  |  |  |  | resolves in a single url, this will results in an error. | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | =item C<:Sitemap(*)> | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | sub with_function :Local :Sitemap(*) { } | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | sub with_function_sitemap { | 
| 193 |  |  |  |  |  |  | my ( $self, $c, $sitemap ) = @_; | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | $sitemap->add( 'http://localhost/with_function' ); | 
| 196 |  |  |  |  |  |  | } | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | Calls the function 'I<action>_sitemap', if it exists, and passes it the | 
| 199 |  |  |  |  |  |  | controller, context and sitemap objects. | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | This is currently the only way to invoke C<:Sitemap> on an action | 
| 202 |  |  |  |  |  |  | resolving to many urls. | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | =back | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =over | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | =item L<WWW::Sitemap::XML> | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | Module that C<Catalyst::Plugin::Sitemap> currently uses under the hood. | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | =item L<Search::Sitemap> | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | Original module that this plugin was using under the hood. | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | =item L<Dancer::Plugin::SiteMap> | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | Similar plugin for the L<Dancer> framework, which inspired | 
| 221 |  |  |  |  |  |  | C<Catalyst::Plugin::Sitemap>. | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | =item L<http://babyl.dyndns.org/techblog/entry/catalyst-plugin-sitemap> | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | Blog article introducing C<Catalyst::Plugin::Sitemap>. | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | =back | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | =head1 AUTHOR | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | Yanick Champoux <yanick@cpan.org> | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | This software is copyright (c) 2010 by Yanick Champoux. | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 238 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | =cut |