File: Tag.pm

package info (click to toggle)
libmojomojo-perl 1.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,272 kB
  • ctags: 879
  • sloc: perl: 14,055; sh: 145; xml: 120; ruby: 6; makefile: 2
file content (121 lines) | stat: -rw-r--r-- 2,872 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package MojoMojo::Schema::ResultSet::Tag;

use strict;
use warnings;
use parent qw/MojoMojo::Schema::Base::ResultSet/;

=head1 NAME

MojoMojo::Schema::ResultSet::Tag - resultset methods on tags

=head1 METHODS

=head2 most_used

Returns a list of all tags and the amount each of these tags
is used on any page.

=cut

sub most_used {
    my ( $self, $count ) = @_;
    return $self->search(
        { page => { '!=', undef }, },
        {
            select   => [ 'me.tag', 'count(me.tag) as refcount' ],
            as       => [ 'tag',    'refcount' ],
            group_by => ['me.tag'],
            order_by => ['refcount desc'],
        }
    );
}

=head2 by_page

Same as L</most_used> but for a particular page.

=cut

# TODO: Use join instead of from which is undocumented (on purpose)
sub by_page {
    my ( $self, $page ) = @_;
    return $self->search(
        {
            'ancestor.id' => $page,
            'me.page'     => \'=descendant.id',
            -or           => [
                -and => [
                    'descendant.lft' => \'> ancestor.lft',
                    'descendant.rgt' => \'< ancestor.rgt',
                ],
                'ancestor.id' => \'=descendant.id',
            ],
        },
        {
            from     => 'page as ancestor, page as descendant, tag as me',
            select   => [ 'me.page', 'me.tag', 'count(me.tag) as refcount' ],
            as       => [ 'page', 'tag', 'refcount' ],
            group_by => [ \'me.page', \'me.tag'],
            order_by => ['refcount'],
        }
    );
}

=head2 by_photo

Tags on photos with counts. Used to make the tag cloud for the gallery.

=cut

sub by_photo {
    my ($self) = @_;
    return $self->search(
        { photo => { '!=' => undef } },
        {
            select   => [ 'me.photo', 'me.tag', 'count(me.tag) as refcount' ],
            as       => [ 'photo',    'tag',    'refcount' ],
            group_by => [ 'me.photo', 'me.tag'],
            order_by => ['me.tag'],
        }
    );
}

=head2 related_to [<tag>] [<count>]

Returns popular tags related to this. Defaults to self.

=cut

sub related_to {
    my ( $self, $tag, $count ) = @_;
    $tag   ||= $self->tag;
    $count ||= 10;
    return $self->search(
        {
            'me.tag'    => $tag,
            'other.tag' => { '!=', $tag },
            'me.page'   => \'=other.page',
        },
        {
            select     => [ 'me.tag', 'count(me.tag) as refcount' ],
            as         => [ 'tag',    'refcount' ],
            'group_by' => ['me.tag'],
            'from'     => 'tag me, tag other',
            'order_by' => \'refcount',
            'rows'     => $count,
        }
    );
}

=head1 AUTHOR

Marcus Ramberg <mramberg@cpan.org>

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;