File Coverage

blib/lib/Catalyst/Plugin/Sitemap.pm
Criterion Covered Total %
statement 37 37 100.0
branch 11 14 78.5
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 57 60 95.0


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