Changeset 71 in bookmarks
- Timestamp:
- 02/13/14 16:10:42 (11 years ago)
- Location:
- trunk/lib
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Bookmarks.pm
r70 r71 2 2 3 3 use Moose; 4 4 5 use SQL::Interp qw{:all}; 5 6 use URI; 7 6 8 use Bookmark; 7 9 use Bookmarks::List; 10 use Bookmarks::Search; 8 11 9 12 has dbh => ( is => 'rw' ); … … 73 76 my $self = shift; 74 77 my $params = shift; 75 my $query = $params->{query}; 76 my $tags = $params->{tag} || []; 77 my $limit = $params->{limit}; 78 my $offset = $params->{offset}; 78 my $search = Bookmarks::Search->new($params); 79 79 80 80 # build the query 81 81 my @sql; 82 82 83 if (!ref $tags) { 84 $tags = [ $tags ]; 85 } 86 if (@$tags) { 83 if (@{ $search->tags }) { 87 84 my $intersect = 0; 88 for my $tag (@{ $ tags }) {85 for my $tag (@{ $search->tags }) { 89 86 push @sql, 'intersect' if $intersect; 90 87 push @sql, 'select resources.*, bookmarks.* from resources join bookmarks on resources.uri = bookmarks.uri'; … … 95 92 push @sql, 'select * from resources join bookmarks on resources.uri = bookmarks.uri'; 96 93 } 97 if ($query) { 98 push @sql, (@$tags ? 'and' : 'where'), 'title like', \"%$query%"; 94 if ($search->query) { 95 my $fuzzy_match = '%' . $search->query . '%'; 96 push @sql, (@{ $search->tags } ? 'and' : 'where'), 'title like', \$fuzzy_match; 99 97 } 100 98 push @sql, 'order by ctime desc'; 101 push @sql, ('limit', \$ limit) if $limit;99 push @sql, ('limit', \$search->limit) if $search->limit; 102 100 # an offset is only allowed if we have a limit clause 103 push @sql, ('offset', \$ offset) if $limit && $offset;101 push @sql, ('offset', \$search->offset) if $search->limit && $search->offset; 104 102 105 103 my ($sql, @bind) = sql_interp(@sql); 106 #die $sql;107 104 108 105 my $sth_resource = $self->dbh->prepare($sql); … … 119 116 return Bookmarks::List->new({ 120 117 bookmarks => $self, 121 tags => $tags, 122 query => $query, 123 limit => $limit, 124 offset => $offset, 118 search => $search, 125 119 results => \@resources, 126 120 }); … … 147 141 my $self = shift; 148 142 my $params = shift; 149 my $query = $params->{query}; 150 my $tags = $params->{tag} || []; 151 if (!ref $tags) { 152 $tags = [ $tags ]; 153 } 143 my $search = $params->{search}; 144 154 145 my @sql; 155 146 156 147 push @sql, 'select tag, count(tag) as count from tags'; 157 push @sql, 'join resources on tags.uri = resources.uri' if $ query;148 push @sql, 'join resources on tags.uri = resources.uri' if $search->query; 158 149 159 150 # build the where clause 160 if (@ $tags) {151 if (@{ $search->tags }) { 161 152 push @sql, 'where tags.uri in ('; 162 153 my $intersect = 0; 163 for my $tag (@{ $ tags }) {154 for my $tag (@{ $search->tags }) { 164 155 push @sql, 'intersect' if $intersect; 165 156 push @sql, 'select uri from tags where tag = ', \$tag; 166 157 $intersect++; 167 158 } 168 push @sql, ') and tag not in ', $tags, ''; 169 } 170 if ($query) { 171 push @sql, (@$tags ? 'and' : 'where'), 'title like', \"%$query%"; 159 push @sql, ') and tag not in ', $search->tags, ''; 160 } 161 if ($search->query) { 162 my $fuzzy_match = '%' . $search->query . '%'; 163 push @sql, (@{ $search->tags } ? 'and' : 'where'), 'title like', \$fuzzy_match; 172 164 } 173 165 -
trunk/lib/Bookmarks/Controller.pm
r70 r71 85 85 my $list = $self->bookmarks->get_bookmarks({ 86 86 query => $query, 87 tag 87 tags => \@tags, 88 88 limit => $limit, 89 89 offset => $offset, … … 104 104 105 105 # construct a feed from the most recent 12 bookmarks 106 my $list = $self->bookmarks->get_bookmarks({ query => $query, tag => \@tags, limit => 12 });106 my $list = $self->bookmarks->get_bookmarks({ query => $query, tags => \@tags, limit => 12 }); 107 107 return $list->as_atom; 108 108 } -
trunk/lib/Bookmarks/List.pm
r70 r71 6 6 use HTTP::Date qw{time2iso time2isoz}; 7 7 8 has bookmarks => (is => 'ro'); 9 has query => (is => 'ro'); 10 has tags => ( 8 has bookmarks => ( 11 9 is => 'ro', 12 default => sub { [] },10 isa => 'Bookmarks', 13 11 ); 14 has limit => (is => 'ro'); 15 has offset => (is => 'ro'); 12 has search => ( 13 is => 'ro', 14 isa => 'Bookmarks::Search', 15 handles => [qw{query tags limit offset}], 16 ); 16 17 has results => ( is => 'ro' ); 17 18 has title => ( … … 182 183 183 184 my @all_tags = $self->bookmarks->get_tags({ selected => @{ $self->tags }[0] }); 184 my @cotags = $self->bookmarks->get_cotags({ 185 query => $self->query, 186 tag => $self->tags, 187 }); 185 my @cotags = $self->bookmarks->get_cotags({ search => $self->search }); 188 186 189 187 $template->process(
Note: See TracChangeset
for help on using the changeset viewer.