X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FPath.pm;h=96fd32308cf1cffe9b88b0f6e856cac65a224d28;hb=573886d6fd0eb1cb1a9746c3276f98cc2a0f18e5;hp=56e1e800bf8dd81fc5bc03489151075a82412e61;hpb=a961fc0c45fa6af30b79bf9ea366a9535af4f16d;p=id3fs.git diff --git a/lib/ID3FS/Path.pm b/lib/ID3FS/Path.pm index 56e1e80..96fd323 100644 --- a/lib/ID3FS/Path.pm +++ b/lib/ID3FS/Path.pm @@ -31,6 +31,8 @@ sub new $self->{db}=shift; $self->{path}=shift; $self->{verbose}=shift; + $self->{maxtagdepth}=shift; + $self->{curtagdepth}=0; $self->{path} =~ s/\/\//\//g; # drop doubled slashes $self->parse(); @@ -52,8 +54,7 @@ sub isdir sub isfile { my($self)=@_; - return 1 if($self->state() == $STATE_FILE); - return 0; + return($self->state() == $STATE_FILE); } sub isvalid @@ -67,7 +68,7 @@ sub dest my($self, $mountpoint)=@_; if($self->state() == $STATE_FILE) { - return $self->filename($mountpoint, $self); + return $self->filename($mountpoint); } return "ERROR"; #should never happen? } @@ -82,41 +83,42 @@ sub dirents # print "DIRENTS: FILE: $self->{path}\n"; if($state==$STATE_ALL) { - @dents=($self->artists(), $PATH_ALLTRACKS, $PATH_NOARTIST); + @dents=($PATH_ALLTRACKS, $PATH_NOARTIST, $self->artists()); } elsif($state==$STATE_TAG || $state==$STATE_TAGVAL) { - my $tag=$self->tail(); - if($state==$STATE_TAG && - defined($tag) && - ref($tag) eq "ID3FS::PathElement::Tag" && - $self->{db}->tag_has_values($tag->{id})) + if($state==$STATE_TAG && $self->at("tag") && + $self->{db}->tag_has_values($self->tail()->{id})) { @dents=$self->tags(); } else { - @dents=($self->artists(), qw(AND OR), $PATH_ALLTRACKS, $PATH_NOARTIST); + if($self->{maxtagdepth} && ($self->{curtagdepth} < $self->{maxtagdepth})) + { + @dents=qw(AND OR); + } + push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST)); + push(@dents, $self->artists()); } } elsif($state==$STATE_BOOLEAN) { - @dents=$self->tags(); my $parent=$self->tail(); - unless(defined($parent) && - ref($parent) eq "ID3FS::PathElement::Boolean" && + unless($self->is("boolean", $parent) && $parent->{name} eq "NOT") { - push(@dents, "NOT"); + @dents=("NOT"); } + push(@dents,$self->tags()); } elsif($state==$STATE_ROOT) { - @dents=($self->tags(), qw(ALL NOT)); + @dents=(qw(ALL NOT), $self->tags()); } elsif($state==$STATE_ALBUMS) { - @dents=($self->albums(), $PATH_ALLTRACKS, $PATH_NOALBUM); + @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOALBUM), $self->albums()); } elsif($state==$STATE_TRACKLIST) { @@ -184,11 +186,9 @@ sub parse } elsif($state==$STATE_TAG || $state==$STATE_TAGVAL) { -# print "SM: TAG/TAGVAL($state): $name\n"; my $tag=$self->tail(); - if($state==$STATE_TAG && - defined($tag) && - ref($tag) eq "ID3FS::PathElement::Tag" && +# print "SM: TAG/TAGVAL($state): $name\n"; + if($state==$STATE_TAG && $self->is("tag", $tag) && $self->{db}->tag_has_values($tag->{id})) { # print "Parsing: parent: $tag->{id}\n"; @@ -241,8 +241,7 @@ sub parse # print "SM: BOOLEAN: $name\n"; my $parent=$self->tail(); my $allownot=1; - if(defined($parent) && - ref($parent) eq "ID3FS::PathElement::Boolean" && + if($self->is("boolean", $parent) && $parent->{name} eq "NOT") { $allownot=0; @@ -320,7 +319,6 @@ sub parse } elsif($name eq $PATH_NOARTIST) { - # FIXME $self->state($STATE_TRACKLIST); } else @@ -351,7 +349,7 @@ sub parse # remove trailing boolean my @elements=@{$self->{elements}}; - while(@elements && ref($elements[$#elements]) eq "ID3FS::PathElement::Boolean") + while(@elements && $self->is("boolean", $elements[$#elements])) { pop @elements; } @@ -373,7 +371,11 @@ sub parse sub state { my($self, $newstate)=@_; - $self->{state}=$newstate if(defined($newstate)); + if(defined($newstate)) + { + $self->{state}=$newstate; + $self->{curtagdepth}++ if($newstate == $STATE_TAG); + } return $self->{state}; } @@ -383,7 +385,7 @@ sub elements_to_tree return undef unless(@$elements); my ($left, $right, $op)=(undef, undef, undef); my $thing=pop @$elements; - if(ref($thing) eq "ID3FS::PathElement::Boolean") + if($self->is("boolean", $thing)) { my $op=$thing; $right=$self->elements_to_tree($elements); @@ -408,16 +410,16 @@ sub sort_elements # print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n"; while(my $thing = shift @input) { - if(ref($thing) eq "ID3FS::PathElement::Tag") + if($self->is("tag", $thing)) { # Handle tag values by dropping parent - if(@input && ref($input[0]) eq "ID3FS::PathElement::Tag") + if(@input && $self->is("tag", $input[0]) { $thing=shift @input; } push(@output, $thing); } - elsif(ref($thing) eq "ID3FS::PathElement::Boolean") + elsif($self->is("boolean", $thing)) { # bool while(@opstack && @@ -447,7 +449,7 @@ sub expecting_values { my($self)=@_; my $tail=$self->tail(); - if($tail && ref($tail) eq "ID3FS::PathElement::Tag") + if($self->is("tag", $tail)) { return($self->{db}->tag_has_values($tail->{id})); } @@ -457,7 +459,7 @@ sub trailing_tag_id { my($self)=@_; my $tail=$self->tail(); - if($tail && ref($tail) eq "ID3FS::PathElement::Tag") + if($self->is("tag", $tail)) { return($tail->{id}); } @@ -468,7 +470,7 @@ sub trailing_tag_parent { my($self)=@_; my $tail=$self->tail(); - if($tail && ref($tail) eq "ID3FS::PathElement::Tag") + if($self->is("tag", $tail)) { return($tail->{parents_id}); } @@ -481,6 +483,22 @@ sub tail return($self->{elements}->[$#{$self->{elements}}]); } +sub at +{ + my($self, $type)=@_; + return($self->is($type, $self->tail())); +} + +sub is +{ + my($self, $type, $thing)=@_; + return 0 unless($thing); + my $ref=ref($thing); + my $typestr="ID3FS::PathElement::" . ucfirst($type); + return 1 if($ref eq $typestr); + return 0; +} + # the one before last sub tail_parent { @@ -593,7 +611,7 @@ sub albums my @ids=(); my $tail=$self->tail(); # FIXME: rework PathElements - if(ref($tail) eq "ID3FS::PathElement::Artist") + if($self->is("artist", $tail)) { return $self->artist_albums($tail->{id}); } @@ -654,15 +672,15 @@ sub tracks my($self)=@_; # FIXME: rework PathElements my $tail=$self->tail(); - if(ref($tail) eq "ID3FS::PathElement::Artist") + if($self->is("artist", $tail)) { return $self->artist_tracks($tail->{id}); } - elsif(ref($tail) eq "ID3FS::PathElement::Album") + elsif($self->is("album", $tail)) { my $artist_id=0; my $artist=$self->tail_parent(); - if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist")) + if($self->is("artist", $artist)) { # should always happen $artist_id=$artist->{id}; @@ -686,7 +704,7 @@ sub filename { my($self, $mountpoint)=@_; my $tail=$self->tail(); - if(ref($tail) eq "ID3FS::PathElement::File") + if($self->is("file", $tail)) { my $id=$tail->{id}; my $sql=("SELECT paths.name, files.name FROM files\n" . @@ -696,9 +714,10 @@ sub filename print "FILENAME SQL: $sql\n" if($self->{verbose}); my ($path, $name)=$self->{db}->cmd_onerow($sql, $id); my $id3fs_path=join('/', map { $_->{name}; } @{$self->{elements}}); - return($self->{db}->relativise($path, $name, $mountpoint, $id3fs_path)); + return($self->{db}->relativise($path, $name, $mountpoint)); } - die("DB::filename: unhandled case\n"); #FIXME + # should never happen + return "ERROR"; } sub tags_subselect @@ -760,7 +779,7 @@ sub tags_subselect sub bare_not_subselect { my($self)=@_; - my @tags=grep { ref($_) eq "ID3FS::PathElement::Tag"; } @{$self->{elements}}; + my @tags=grep { $self->is("tag", $_); } @{$self->{elements}}; my $sql=("\tSELECT f1.id AS files_id FROM files f1 WHERE f1.id NOT IN (\n" . "\t\tSELECT fxt1.files_id FROM tags t1\n" . "\t\tINNER JOIN files_x_tags fxt1 ON t1.id=fxt1.tags_id\n" . @@ -806,7 +825,7 @@ sub constraints_tag_list for my $constraint (@constraints) { # print ref($constraint), ": ", $constraint->{name}, "\n"; - if(ref($constraint) eq "ID3FS::PathElement::Tag") + if($self->is("tag", $constraint)) { if(defined($lasttag)) { @@ -826,32 +845,30 @@ sub constraints_tag_list } } } - unless($self->{db}->{postgres}) - { - @tags=map{ "\"$_\""; } @tags; - @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals); - $lasttag="\"$lasttag\"" if defined($lasttag); - } + @tags=map{ "\"$_\""; } @tags; + @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals); + $lasttag="\"$lasttag\"" if defined($lasttag); return(\@tags, \@tags_vals, $lasttag); } +# we just filter $ALLTRACKS, $NOARTIST and $NOALBUM +# filtering tags properly requires up to four levels of recursion +# (tag/tagval/AND/NOT) and is too slow sub filter { my($self, @dirs)=@_; my $base=$self->{path}; my @outdirs=(); - # depth 4 to allow for tag/tagval/AND/NOT - my $maxdepth=4; for my $dir (@dirs) { -# print "\nFILTER (",$self->state(), "): $base / $dir\n"; - if($self->empty("$base/$dir", $maxdepth)) + print "\nFILTER (",$self->state(), "): $base / $dir\n"; + if($self->empty("$base/$dir")) { -# print "empty: $base / $dir\n"; + print "empty: $base / $dir\n"; } else { -# print "non-empty, accepting: $base / $dir\n"; + print "non-empty, accepting: $base / $dir\n"; push(@outdirs, $dir); } } @@ -860,51 +877,13 @@ sub filter sub empty { - my($self, $dir, $maxdepth)=@_; - return 0 unless($maxdepth); -# print "testing($maxdepth): $dir\n"; - my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose}); -# print "PATH INVALID\n" unless($path->isvalid()); + my($self, $dir)=@_; + my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose}, + ($self->{maxtagdepth} - $self->{curtagdepth})); return 1 unless($path->isvalid()); my($subdirs,$subfiles)=$path->dirents(); -# print "SUBDENTS: ", join(", ", @$subdirs, @$subfiles), "\n"; -# print("SUBFILES: ", join(', ', @$subfiles), "\n") if(@$subfiles); - return 0 if(@$subfiles); - for my $subdir (@$subdirs) - { -# print "SUBSUB $dir/$subdir\n"; - if(1) #$self->dir_is_special($subdir)) - { - if($self->empty("$dir/$subdir", ($maxdepth-1))) - { -# print "EMPTY: $dir / $subdir\n"; - } - else - { -# print "NONEMPTY: $dir / $subdir\n"; - return 0; - } - } - else - { - return 0; - } -# return 0 if($self->nonempty("$dir/$subdir", ($maxdepth-1))); - } + return 0 if(@$subfiles || @$subdirs); return 1; } -sub dir_is_special -{ - my($self, $dir)=@_; - my $id=$self->{db}->lookup_id("tags", $dir); - if((grep { $_ eq $dir; } (qw(AND OR NOT), $PATH_ALLTRACKS, - $PATH_NOARTIST, $PATH_NOALBUM)) || - ($id && $self->{db}->tag_has_values($id))) - { - return 1; - } - return 0; -} - 1;