add should_index() method to articles
authorTony Cook <tony@develop-help.com>
Fri, 13 Jun 2014 10:08:27 +0000 (20:08 +1000)
committerTony Cook <tony@develop-help.com>
Fri, 13 Jun 2014 10:08:27 +0000 (20:08 +1000)
site/cgi-bin/modules/Article.pm
site/cgi-bin/modules/BSE/Index.pm
site/cgi-bin/modules/BSE/TB/SiteCommon.pm
t/050-local/010-api.t

index 403a3203bd3975ab94baf464abd4c188e28ad9a8..25e98e8573d20e7a4df5f5226fbbe3eddf7596d2 100644 (file)
@@ -8,7 +8,7 @@ use vars qw/@ISA/;
 @ISA = qw/Squirrel::Row BSE::TB::SiteCommon BSE::TB::TagOwner/;
 use Carp 'confess';
 
-our $VERSION = "1.022";
+our $VERSION = "1.023";
 
 =head1 NAME
 
@@ -163,7 +163,7 @@ sub section {
 
   my $section = $self;
   while ($section->{parentid} > 0
-        and my $parent = Articles->getByPkey($section->{parentid})) {
+        and my $parent = $section->parent) {
     $section = $parent;
   }
 
@@ -178,8 +178,14 @@ Return the article's parent.
 
 sub parent {
   my ($self) = @_;
-  $self->{parentid} == -1 and return;
-  return Articles->getByPkey($self->{parentid});
+
+  my $parentid = $self->parentid;
+
+  $parentid == -1
+    and return;
+  $self->{_parent} && $self->{_parent}->id == $parentid
+    and return $self->{_parent};
+  return ($self->{_parent} = Articles->getByPkey($self->{parentid}));
 }
 
 sub update_dynamic {
@@ -626,6 +632,61 @@ sub should_generate {
   return $self->is_linked && $self->listed && $self->is_released && !$self->is_expired;
 }
 
+=item should_index
+
+Returns true if the article should be indexed.
+
+=cut
+
+sub should_index {
+  my ($self) = @_;
+
+  return ($self->listed || $self->is_index_even_if_hidden)
+    && $self->is_linked
+      && !$self->is_dont_index
+       && !$self->is_dont_index_or_kids
+}
+
+=item is_index_even_if_hidden
+
+Return true if the article's index even if hidden flag is set.
+
+=cut
+
+sub is_index_even_if_hidden {
+  my ($self) = @_;
+
+  return $self->flags =~ /I/;
+}
+
+=item is_dont_index
+
+Return true if the "don't index" flag (C<N>) is set.
+
+=cut
+
+sub is_dont_index {
+  return $_[0]->flags =~ /N/;
+}
+
+=item is_dont_index_or_kids
+
+Return true if the article or any of it's parents have the "don't
+index me or my children" flag (C<C>) set.
+
+=cut
+
+sub is_dont_index_or_kids {
+  my ($self) = @_;
+
+  $self->flags =~ /C/ and return 1;
+
+  my $parent = $self->parent
+    or return 0;
+
+  return $parent->is_dont_index_or_kids;
+}
+
 sub restricted_method {
   my ($self, $name) = @_;
 
@@ -661,6 +722,20 @@ sub mark_modified {
   $self->set_lastModifiedBy(ref $opts{actor} ? $opts{actor}->logon : "");
 }
 
+=item uncache
+
+Free any cached data.
+
+=cut
+
+sub uncache {
+  my ($self) = @_;
+
+  delete @{$self}{qw/_parent/};
+
+  $self->SUPER::uncache();
+}
+
 1;
 
 __END__
index 31753ea5204bda6aa7fb993680dc1e97242a2227..b02f10d3470b9bac4b088e200f7dfa0ec35f022e 100644 (file)
@@ -4,7 +4,7 @@ use Time::HiRes qw(time);
 use Constants qw(@SEARCH_EXCLUDE @SEARCH_INCLUDE);
 use Articles;
 
-our $VERSION = "1.003";
+our $VERSION = "1.004";
 
 my %default_scores =
   (
@@ -98,14 +98,8 @@ sub make_index {
     # find the section
     my $article = Articles->getByPkey($id);
     next unless $article;
-    next unless ($article->{listed} || $article->{flags} =~ /I/);
-    next unless $article->is_linked;
-    next if $article->{flags} =~ /[CN]/;
-    my $section = $article;
-    while ($section->{parentid} >= 1) {
-      $section = Articles->getByPkey($section->{parentid});
-      next INDEX if $section->{flags} =~ /C/;
-    }
+    next unless $article->should_index;
+    my $section = $article->section;
     my $id = $article->{id};
     my $indexas = $article->{level} > $self->{max_level} ? $article->{parentid} : $id;
     my $sectionid = $section->{id};
index b9e9274442c5cf6ea2be0d993d12a32ef01b5477..9cdbcd7306b91fde22a00f7076c38723bb53df64 100644 (file)
@@ -2,7 +2,7 @@ package BSE::TB::SiteCommon;
 use strict;
 use Carp qw(confess);
 
-our $VERSION = "1.017";
+our $VERSION = "1.018";
 
 =head1 NAME
 
@@ -611,6 +611,19 @@ sub set_image_order {
   return @new_order;
 }
 
+=item uncache
+
+Clear any locally cached info.
+
+=cut
+
+sub uncache {
+  my ($self) = @_;
+
+  $self->uncache_images;
+  $self->uncache_files;
+}
+
 1;
 
 __END__
index 2184103cb526474b4c989d127a179b1c76f0d32f..370ac60ede33bc5451b1599d96fd70a00bc00267 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use BSE::Test qw(make_ua base_url);
-use Test::More tests => 68;
+use Test::More tests => 76;
 use File::Spec;
 use File::Slurp;
 use Carp qw(confess);
@@ -213,6 +213,39 @@ SKIP: {
   }
 }
 
+{ # testing the indexing flags
+  my $index = bse_make_article(cfg => $cfg,
+                              title => "index test child",
+                              parentid => $art->id);
+  ok($index, "make article for should_index tests");
+  ok($index->should_index, "default should be indexed");
+  $index->set_listed(0);
+  $index->uncache;
+  ok(!$index->should_index, "not indexed if not listed");
+  $index->set_flags("I");
+  $index->uncache;
+  ok($index->should_index, "indexed if I flag set");
+  $index->set_listed(1);
+  $index->set_flags("N");
+  $index->uncache;
+  ok(!$index->should_index, "not indexed if N flag set");
+  $index->set_flags("C");
+  $index->uncache;
+  ok(!$index->should_index, "not indexed if C flag set");
+  $index->set_flags("");
+  $art->set_flags("C");
+  $art->save;
+  $index->uncache;
+  ok(!$index->should_index, "not indexed if parent's C flag set");
+
+  ok($index->remove($cfg), "remove index test");
+  undef $index;
+
+  END {
+    $index->remove($cfg) if $index;
+  }
+}
+
 ok($child->remove($cfg), "remove child");
 undef $child;
 ok($art->remove($cfg), "remove article");