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