| 1 | package DocSet::NavigateCache; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use DocSet::RunTime; | ||
| 7 | use DocSet::Util; | ||
| 8 | use Storable; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | # cache the loaded cache files | ||
| 12 | use vars qw(%CACHE); | ||
| 13 | %CACHE = (); | ||
| 14 | |||
| 15 | #use vars qw(@ISA); | ||
| 16 | use DocSet::Cache (); | ||
| 17 | #@ISA = qw(DocSet::Cache); | ||
| 18 | |||
| 19 | use constant CACHE => 0; | ||
| 20 | use constant ID => 1; | ||
| 21 | use constant CUR_PATH => 2; | ||
| 22 | use constant REL_PATH => 3; | ||
| 23 | |||
| 24 | # $rel_path (to the parent) is optional (e.g. root doesn't have a parent) | ||
| 25 | sub new { | ||
| 26 | my ($class, $cache_path, $id, $rel_path) = @_; | ||
| 27 | |||
| 28 | croak "no cache path specified" unless defined $cache_path; | ||
| 29 | |||
| 30 | my $cache = get_cache($cache_path); | ||
| 31 | my $self = bless [], ref($class)||$class; | ||
| 32 | $self->[CACHE] = $cache; | ||
| 33 | $self->[CUR_PATH] = $cache_path; | ||
| 34 | $self->[REL_PATH] = $rel_path if $rel_path; | ||
| 35 | |||
| 36 | # get the first (#0) node if id wasn't provided (if there are any | ||
| 37 | # nodes at all) | ||
| 38 | $self->[ID] = defined $id | ||
| 39 | ? $id | ||
| 40 | : $cache->total_ids ? $cache->seq2id(0) : undef; | ||
| 41 | return undef unless defined $self->[ID]; # an empty docset | ||
| 42 | |||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub parent_rel_path { | ||
| 47 | my ($self) = @_; | ||
| 48 | return defined $self->[REL_PATH] ? $self->[REL_PATH] : undef; | ||
| 49 | } | ||
| 50 | |||
| 51 | # get next item's object or undef if there are no more | ||
| 52 | sub next { | ||
| 53 | my ($self) = @_; | ||
| 54 | my $cache = $self->[CACHE]; | ||
| 55 | |||
| 56 | my $seq = $cache->id2seq($self->[ID]); | ||
| 57 | my $last_seq = $cache->total_ids - 1; | ||
| 58 | |||
| 59 | # if the next object is hidden, it's like there is no next object, | ||
| 60 | # because the hidden objects, if any, are always coming last | ||
| 61 | if ($seq < $last_seq) { | ||
| 62 | my $id = $cache->seq2id($seq + 1); | ||
| 63 | if ($cache->is_hidden($id)) { | ||
| 64 | return undef; | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | return $self->new($self->[CUR_PATH], $id); | ||
| 68 | } | ||
| 69 | } else { | ||
| 70 | return undef; | ||
| 71 | } | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | # get prev node's object or undef if there are no more | ||
| 76 | sub prev { | ||
| 77 | my ($self) = @_; | ||
| 78 | my $cache = $self->[CACHE]; | ||
| 79 | my $seq = $cache->id2seq($self->[ID]); | ||
| 80 | |||
| 81 | # if the current node is hidden, it's like there is no prev | ||
| 82 | # node, because we don't want hidden node to be linked to the | ||
| 83 | # exposed or hidden sibling nodes if any | ||
| 84 | if ($cache->is_hidden($self->[ID])) { | ||
| 85 | return undef; | ||
| 86 | } | ||
| 87 | |||
| 88 | if ($seq) { | ||
| 89 | my $id = $cache->seq2id($seq - 1); | ||
| 90 | return $self->new($self->[CUR_PATH], $id); | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | return undef; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | # get the object by its id (string) within the current cache | ||
| 98 | sub by_id { | ||
| 99 | my ($self, $id) = @_; | ||
| 100 | return defined $id ? $self->new($self->[CUR_PATH], $id) : undef; | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | # get the object of the first item on the same level | ||
| 105 | sub first { | ||
| 106 | my ($self) = @_; | ||
| 107 | my $cache = $self->[CACHE]; | ||
| 108 | |||
| 109 | # it's possible that the whole docset is made of hidden objects. | ||
| 110 | # since the hidden objects, if any, are always coming last | ||
| 111 | # we simply return undef in such a case | ||
| 112 | if ($cache->total_ids) { | ||
| 113 | my $id = $cache->seq2id(0); | ||
| 114 | if ($cache->is_hidden($id)) { | ||
| 115 | return undef; | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | return $self->new($self->[CUR_PATH], $id); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | return undef; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | # the index node of the current level | ||
| 128 | sub index_node { | ||
| 129 | my ($self) = @_; | ||
| 130 | return $self->[CACHE]->index_node; | ||
| 131 | } | ||
| 132 | |||
| 133 | # get the object of the parent | ||
| 134 | sub up { | ||
| 135 | my ($self) = @_; | ||
| 136 | my ($path, $id, $rel_path) = $self->[CACHE]->parent_node; | ||
| 137 | |||
| 138 | $rel_path = "." unless defined $rel_path; | ||
| 139 | if (defined $self->[REL_PATH] && length $self->[REL_PATH]) { | ||
| 140 | # append the relative path of each child, so the overall | ||
| 141 | # relative path is correct | ||
| 142 | $rel_path .= "/$self->[REL_PATH]"; | ||
| 143 | } | ||
| 144 | |||
| 145 | # it's ok to have a hidden parent, we don't mind to see it | ||
| 146 | # as non-hidden, since the children of the hidden parent aren't | ||
| 147 | # linked from other non-hidden pages. In fact we must ignore the | ||
| 148 | # fact that it's hidden (if it is) because otherwise the navigation | ||
| 149 | # won't work. | ||
| 150 | if ($path) { | ||
| 151 | return $self->new($path, $id, $rel_path); | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | return undef; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | # get the first child node | ||
| 159 | # note that in order not to break the navigation links, it always | ||
| 160 | # returns a value if there is a child node, no matter if it's hidden | ||
| 161 | # or not. so the check for hidden must be done in the caller's code, | ||
| 162 | # e.g.: $o->down->first - first() will return undef if the first is | ||
| 163 | # hidden. | ||
| 164 | sub down { | ||
| 165 | my ($self) = @_; | ||
| 166 | |||
| 167 | if (my $path = $self->[CACHE]->child_cache_path($self->[ID])) { | ||
| 168 | return $self->new($path); | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | return undef; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | # retrieve the meta data of the current node | ||
| 176 | sub meta { | ||
| 177 | my ($self) = @_; | ||
| 178 | return $self->[CACHE]->get($self->[ID], 'meta'); | ||
| 179 | } | ||
| 180 | |||
| 181 | # retrieve the node groups | ||
| 182 | sub node_groups { | ||
| 183 | my ($self) = @_; | ||
| 184 | #print "OK: "; | ||
| 185 | #dumper $self->[CACHE]->node_groups; | ||
| 186 | return $self->[CACHE]->node_groups; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub id { | ||
| 190 | shift->[ID]; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | sub get_cache { | ||
| 195 | my ($path) = @_; | ||
| 196 | |||
| 197 | unless ($CACHE{$path}) { | ||
| 198 | $CACHE{$path} = DocSet::Cache->new($path); | ||
| 199 | die "Failed to read cache from $path: " . $CACHE{$path}->read_error | ||
| 200 | if $CACHE{$path}->read_error; | ||
| 201 | } | ||
| 202 | |||
| 203 | return $CACHE{$path}; | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | 1; | ||
| 208 | __END__ |