Changeset 105
- Timestamp:
- 07/13/05 09:35:24 (3 years ago)
- Files:
-
- tagwire/trunk/README.txt (modified) (5 diffs)
- tagwire/trunk/tagwire-pdcleaner.cgi (added)
- tagwire/trunk/tagwire.pl (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
tagwire/trunk/README.txt
r104 r105 118 118 119 119 <$MTTag$> 120 Shows atag.120 Shows the tag. 121 121 122 122 <$MTTagCount$> 123 Shows the appearance count of a tag. 123 Shows the appearance count of the tag. 124 125 <$MTTagDate$> 126 Shows the last date the tag added. 124 127 125 128 <$MTTagsTotal$> 126 Shows the countof all tags.129 Shows the number of all tags. 127 130 128 131 <$MTTagsTotalSum$> … … 170 173 * Available tags in this container: 171 174 172 Available tags are same as MTTags. 175 <$MTTag$> 176 Shows the tag. 173 177 174 178 * Example: … … 196 200 relationship between tags is defined by how many common *entries* 197 201 includes them. This container can only be used in "tag context" which 198 means the inside of MTTags or MTEntryTags.202 means the inside of MTTags, MTEntryTags, or MTXSearchTags. 199 203 200 204 * Option(s): … … 228 232 229 233 <$MTTag$> 230 Shows atag.234 Shows the tag. 231 235 232 236 <$MTTagCount$> 233 Shows the appearance count of a tag. 237 Shows the appearance count of the tag. 238 239 <$MTTagDate$> 240 Shows the last date the tag added. 234 241 235 242 * Example: … … 370 377 Shows a tag. 371 378 372 <$MTTagCount$>373 Shows the appearance count of a tag.374 375 379 * Example: 376 380 tagwire/trunk/tagwire.pl
r104 r105 13 13 use MT::Template::Context; 14 14 use MT::Request; 15 use vars qw($VERSION); 16 17 $VERSION = '0.24'; 15 18 16 19 # DEBUG … … 28 31 author_name => 'Hirotaka Ogawa', 29 32 author_link => 'http://profile.typekey.com/ogawa/', 30 version => '0.24'33 version => $VERSION 31 34 }); 32 35 MT->add_plugin($plugin); … … 49 52 my (%eindex, %tindex); 50 53 my $data; 51 if (!$pd || $FORCE_PD_REFRESH) { 52 if (!$pd) { 53 $pd = new MT::PluginData(); 54 $pd->plugin($plugin->name); 55 $pd->key($blog_id); 56 } 57 $data = $pd->data() || {}; 54 my $refresh = $FORCE_PD_REFRESH || 0; 55 if (!$pd) { 56 $pd = new MT::PluginData(); 57 $pd->plugin($plugin->name); 58 $pd->key($blog_id); 59 $refresh = 1; 60 } 61 $data = $pd->data() || {}; 62 $refresh = 1 if !defined ${$data->{version}} || ${$data->{version}} ne $VERSION; 63 if ($refresh) { 58 64 my $iter = MT::Entry->load_iter({ blog_id => $blog_id, 59 65 status => MT::Entry::RELEASE() }); … … 64 70 } 65 71 } else { 66 $data = $pd->data() || {}; 67 my $entry_id = $entry->id; 72 my $eid = $entry->id; 68 73 %eindex = %{$data->{eindex}}; 69 delete $eindex{$e ntry_id} if exists $eindex{$entry_id};74 delete $eindex{$eid} if exists $eindex{$eid}; 70 75 if ($entry->status == MT::Entry::RELEASE()) { 71 76 my @tags = split_tags($entry->keywords, 1); 72 $eindex{$e ntry_id} = { tags => \@tags,73 created_on => $entry->created_on };77 $eindex{$eid} = { tags => \@tags, 78 created_on => $entry->created_on }; 74 79 } 75 80 } 76 81 foreach my $eid (keys %eindex) { 77 map { push @{$tindex{$_}}, $eid } @{$eindex{$eid}->{tags}}; 78 } 82 my $ts = $eindex{$eid}->{created_on}; 83 foreach (@{$eindex{$eid}->{tags}}) { 84 push @{$tindex{$_}->{eids}}, $eid; 85 $tindex{$_}->{ts} = $ts if $tindex{$_}->{ts} < $ts; 86 } 87 } 88 $data->{version} = \$VERSION; 79 89 $data->{eindex} = \%eindex; 80 90 $data->{tindex} = \%tindex; … … 83 93 if ($ENABLE_REQ_CACHE) { 84 94 my $r = MT::Request->instance; 85 my $cname = 'Tagwire::Cache::' . $blog_id; 86 $r->cache($cname, undef); 95 $r->cache('Tagwire::Cache::' . $blog_id, undef); 87 96 } 88 97 } … … 93 102 MT::Template::Context->add_tag('Tag' => \&tag); 94 103 MT::Template::Context->add_tag('TagCount' => \&tag_count); 104 MT::Template::Context->add_tag('TagDate' => \&tag_date); 95 105 MT::Template::Context->add_tag('TagsTotal' => \&tags_total); 96 106 MT::Template::Context->add_tag('TagsTotalSum' => \&tags_total_sum); … … 111 121 my ($string, $delimiter, $case_sensitive) = @_; 112 122 return unless $string; 113 my @tags;114 123 $string =~ s/(^\s+|\s+$)//g; 115 124 $string = lc $string unless $case_sensitive; … … 117 126 return split(/\s+/, $string) unless $delimiter; 118 127 128 my @tags; 119 129 foreach my $tag (split($delimiter, $string)) { 120 130 $tag =~ s/(^\s+|\s+$)//g; … … 166 176 key => $blog_id }); 167 177 $data = $pd->data() if $pd; 178 return if ${$data->{version}} ne $VERSION; 168 179 }; 169 180 $r->cache($cname, $data) if $ENABLE_REQ_CACHE && $data; … … 190 201 } 191 202 foreach my $eid (keys %eindex) { 192 map { push @{$tindex{$_}}, $eid } @{$eindex{$eid}->{tags}}; 203 my $ts = $eindex{$eid}->{created_on}; 204 foreach (@{$eindex{$eid}->{tags}}) { 205 push @{$tindex{$_}->{eids}}, $eid; 206 $tindex{$_}->{ts} = $ts if $tindex{$_}->{ts} < $ts; 207 } 193 208 } 194 209 $data->{eindex} = \%eindex; … … 213 228 my $blog_id = $ctx->stash('blog_id'); 214 229 my %tags = (); 230 my %ts = (); 215 231 216 232 my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) … … 218 234 my %tindex = %{$data->{tindex}}; 219 235 if ($case_sensitive) { 220 map { $tags{$_} = scalar(@{$tindex{$_}}) } keys %tindex; 221 } else { 222 map { $tags{lc $_} += scalar(@{$tindex{$_}}) } keys %tindex; 236 foreach (keys %tindex) { 237 $tags{$_} = scalar @{$tindex{$_}->{eids}}; 238 $ts{$_} = $tindex{$_}->{ts}; 239 } 240 } else { 241 foreach (keys %tindex) { 242 $tags{lc $_} += scalar @{$tindex{$_}->{eids}}; 243 $ts{lc $_} = $tindex{$_}->{ts} if $ts{lc $_} < $tindex{$_}->{ts}; 244 } 223 245 } 224 246 … … 241 263 242 264 my $total_sum = 0; 243 foreach (@list) { 244 $total_sum += $tags{$_}; 245 } 265 $total_sum += $tags{$_} foreach (@list); 246 266 $ctx->stash('Tagwire::tags_total_sum', $total_sum); 247 267 … … 252 272 foreach (@list) { 253 273 last if $lastn && $i >= $lastn; 254 $ctx->stash('Tagwire::tag', $case_sensitive ? $_ : ucfirst $_); 255 $ctx->stash('Tagwire::tag_count', $tags{$_}); 274 local $ctx->{__stash}{'Tagwire::tag'} = $_; 275 local $ctx->{__stash}{'Tagwire::tag_count'} = $tags{$_}; 276 local $ctx->{__stash}{'Tagwire::tag_date'} = $ts{$_}; 256 277 defined(my $out = $builder->build($ctx, $tokens)) 257 278 or return $ctx->error($ctx->errstr); … … 274 295 275 296 my @tags = split_tags($e->keywords, $case_sensitive); 276 my $total = scalar(@tags);277 $ctx->stash('Tagwire::tags_total', $total);278 $ctx->stash('Tagwire::tags_total_sum', $total);279 280 297 my @res; 281 298 my $builder = $ctx->stash('builder'); 282 299 my $tokens = $ctx->stash('tokens'); 283 300 foreach (@tags) { 284 $ctx->stash('Tagwire::tag', $case_sensitive ? $_ : ucfirst $_); 285 $ctx->stash('Tagwire::tag_count', 1); 301 local $ctx->{__stash}{'Tagwire::tag'} = $_; 286 302 defined(my $out = $builder->build($ctx, $tokens)) 287 303 or return $ctx->error($ctx->errstr); … … 295 311 my ($ctx, $args, $cond) = @_; 296 312 my $tag = $ctx->stash('Tagwire::tag') or return ''; 297 my $tag_count = $ctx->stash('Tagwire::tag_count');298 313 299 314 my $sort_by = $args->{sort_by} || 'tag'; … … 308 323 my $blog_id = $ctx->stash('blog_id'); 309 324 my %tags = (); 325 my %ts = (); 310 326 311 327 my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) … … 314 330 my %eindex = %{$data->{eindex}}; 315 331 if ($case_sensitive) { 316 foreach my $eid (@{$tindex{$tag}}) { 317 foreach my $mtag (@{$eindex{$eid}->{tags}}) { 318 $tags{$mtag} = exists $tags{$mtag} ? $tags{$mtag} + 1 : 1; 319 } 320 } 321 delete $tags{$tag}; 332 foreach my $eid (@{$tindex{$tag}->{eids}}) { 333 foreach (@{$eindex{$eid}->{tags}}) { 334 next if $_ eq $tag; 335 $tags{$_} = exists $tags{$_} ? $tags{$_} + 1 : 1; 336 $ts{$_} = $tindex{$_}->{ts} if !defined $ts{$_}; 337 } 338 } 322 339 } else { 323 340 $tag = lc $tag; 324 341 foreach my $nctag (grep { lc $_ eq $tag } keys %tindex) { 325 foreach my $eid (@{$tindex{$nctag} }) {326 foreach $_(@{$eindex{$eid}->{tags}}) {342 foreach my $eid (@{$tindex{$nctag}->{eids}}) { 343 foreach (@{$eindex{$eid}->{tags}}) { 327 344 my $mtag = lc $_; 345 next if $mtag eq $tag; 328 346 $tags{$mtag} = exists $tags{$mtag} ? $tags{$mtag} + 1 : 1; 347 $ts{$mtag} = $tindex{$_}->{ts} if $ts{$mtag} < $tindex{$_}->{ts}; 329 348 } 330 349 } 331 350 } 332 delete $tags{$tag};333 351 } 334 352 my @list; … … 347 365 } 348 366 367 $ctx->stash('Tagwire::tags_total', scalar @list); 368 369 my $total_sum = 0; 370 $total_sum += $tags{$_} foreach (@list); 371 $ctx->stash('Tagwire::tags_total_sum', $total_sum); 372 349 373 my @res; 350 374 my $builder = $ctx->stash('builder'); … … 353 377 foreach (@list) { 354 378 last if $lastn && $i >= $lastn; 355 $ctx->stash('Tagwire::tag', $case_sensitive ? $_ : ucfirst $_); 356 $ctx->stash('Tagwire::tag_count', $tags{$_}); 379 local $ctx->{__stash}{'Tagwire::tag'} = $_; 380 local $ctx->{__stash}{'Tagwire::tag_count'} = $tags{$_}; 381 local $ctx->{__stash}{'Tagwire::tag_date'} = $ts{$_}; 357 382 defined(my $out = $builder->build($ctx, $tokens)) 358 383 or return $ctx->error($ctx->errstr); … … 360 385 $i++; 361 386 } 362 $ctx->stash('Tagwire::tag', $tag);363 $ctx->stash('Tagwire::tag_count', $tag_count);364 365 387 my $glue = $args->{glue} || ''; 366 388 join $glue, @res; … … 373 395 sub tag_count { 374 396 $_[0]->stash('Tagwire::tag_count'); 397 } 398 399 sub tag_date { 400 my ($ctx, $args) = @_; 401 $args->{ts} = $ctx->stash('Tagwire::tag_date'); 402 MT::Template::Context::_hdlr_date($ctx, $args); 375 403 } 376 404 … … 400 428 my $lastn = $args->{lastn} || 0; 401 429 402 my @ patterns = split_args($search, $delimiter, $case_sensitive)430 my @tags = split_args($search, $delimiter, $case_sensitive) 403 431 or return ''; 404 432 405 433 my $blog_id = $ctx->stash('blog_id'); 406 my @entries;407 408 434 my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 409 435 or return ''; … … 412 438 my %match; 413 439 if ($case_sensitive) { 414 foreach my $tag (@ patterns) {415 foreach my $eid (@{$tindex{$tag}}) {416 $match{$ eid} = exists $match{$eid} ? $match{$eid} + 1 : 1;417 } 418 } 419 } else { 420 foreach my $tag (@ patterns) {440 foreach my $tag (@tags) { 441 foreach (@{$tindex{$tag}->{eids}}) { 442 $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 443 } 444 } 445 } else { 446 foreach my $tag (@tags) { 421 447 foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 422 foreach my $eid (@{$tindex{$mtag}}) {423 $match{$ eid} = exists $match{$eid} ? $match{$eid} + 1 : 1;448 foreach (@{$tindex{$mtag}->{eids}}) { 449 $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 424 450 } 425 451 } 426 452 } 427 453 } 428 my $count = scalar @ patterns;429 my @eids = grep { $match{$_} == $count } keys %match or return ;454 my $count = scalar @tags; 455 my @eids = grep { $match{$_} == $count } keys %match or return ''; 430 456 @eids = $sort_order eq 'descend' ? 431 457 sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : … … 433 459 splice(@eids, $lastn) if $lastn && (scalar @eids > $lastn); 434 460 require MT::Entry; 461 my @entries; 435 462 map { push @entries, MT::Entry->load($_) } @eids; 436 463 … … 473 500 my $lastn = $args->{lastn} || 0; 474 501 475 my @ patterns = split_tags($entry->keywords, $case_sensitive)502 my @tags = split_tags($entry->keywords, $case_sensitive) 476 503 or return ''; 477 504 478 505 my $blog_id = $ctx->stash('blog_id'); 479 my @entries;480 481 506 my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 482 507 or return ''; … … 485 510 my %match; 486 511 if ($case_sensitive) { 487 foreach my $tag (@ patterns) {488 foreach my $eid (@{$tindex{$tag}}) {489 next if $ eid== $entry->id;490 $match{$ eid} = exists $match{$eid} ? $match{$eid} + 1 : 1;491 } 492 } 493 } else { 494 foreach my $tag (@ patterns) {512 foreach my $tag (@tags) { 513 foreach (@{$tindex{$tag}->{eids}}) { 514 next if $_ == $entry->id; 515 $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 516 } 517 } 518 } else { 519 foreach my $tag (@tags) { 495 520 foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 496 foreach my $eid (@{$tindex{$mtag}}) {497 next if $ eid== $entry->id;498 $match{$ eid} = exists $match{$eid} ? $match{$eid} + 1 : 1;521 foreach (@{$tindex{$mtag}->{eids}}) { 522 next if $_ == $entry->id; 523 $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 499 524 } 500 525 } … … 508 533 splice(@eids, $lastn) if $lastn && (scalar @eids > $lastn); 509 534 require MT::Entry; 535 my @entries; 510 536 map { push @entries, MT::Entry->load($_) } @eids; 511 return '' unless @entries;512 537 513 538 my $res = ''; … … 562 587 my $tokens = $ctx->stash('tokens'); 563 588 foreach (@tags) { 564 $ctx->stash('Tagwire::tag', $_);589 local $ctx->{__stash}{'Tagwire::tag'} = $_; 565 590 defined(my $out = $builder->build($ctx, $tokens)) 566 591 or return $ctx->error($ctx->errstr); … … 579 604 sub xsearch_on_execute { 580 605 my $args = shift; 581 require MT::Entry;582 606 my $blog_id = $args->{blog_id} or MT->error('Blog ID is required.'); 583 607 my $sort_by = $args->{sort_by} || 'created_on'; … … 587 611 $args->{case_sensitive} : 1; 588 612 589 my @results; 590 my @patterns = split_args($args->{search}, $delimiter, $case_sensitive) 591 or return \@results; 613 my @tags = split_args($args->{search}, $delimiter, $case_sensitive) 614 or return \(); 592 615 593 616 my $r = MT::Request->instance; 594 $r->cache('Tagwire::xsearch_tags', \@ patterns);617 $r->cache('Tagwire::xsearch_tags', \@tags); 595 618 596 619 my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 597 or return \ @results;620 or return \(); 598 621 my %tindex = %{$data->{tindex}}; 599 622 my %eindex = %{$data->{eindex}}; 600 623 my %match; 601 624 if ($case_sensitive) { 602 foreach my $tag (@ patterns) {603 foreach my $eid (@{$tindex{$tag}}) {604 $match{$ eid} = exists $match{$eid} ? $match{$eid} + 1 : 1;605 } 606 } 607 } else { 608 foreach my $tag (@ patterns) {625 foreach my $tag (@tags) { 626 foreach (@{$tindex{$tag}->{eids}}) { 627 $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 628 } 629 } 630 } else { 631 foreach my $tag (@tags) { 609 632 foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 610 foreach my $eid (@{$tindex{$mtag}}) {611 $match{$ eid} = exists $match{$eid} ? $match{$eid} + 1 : 1;633 foreach (@{$tindex{$mtag}->{eids}}) { 634 $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 612 635 } 613 636 } 614 637 } 615 638 } 616 my $count = scalar @ patterns;639 my $count = scalar @tags; 617 640 my @eids = grep { $match{$_} == $count } keys %match 618 or return \ @results;641 or return \(); 619 642 @eids = $sort_order eq 'descend' ? 620 643 sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : 621 644 sort { $eindex{$a}->{created_on} <=> $eindex{$b}->{created_on} } @eids; 622 645 require MT::Entry; 623 map { push @results, MT::Entry->load($_) } @eids; 624 \@results; 646 my @entries; 647 map { push @entries, MT::Entry->load($_) } @eids; 648 \@entries; 625 649 } 626 650
