add audiotype/ tags; reenable filters
[id3fs.git] / lib / ID3FS / Path.pm
index cd8b5ac..fca5d95 100644 (file)
@@ -2,12 +2,6 @@ package ID3FS::Path;
 
 use strict;
 use warnings;
-use ID3FS::PathElement::Artist;
-use ID3FS::PathElement::Album;
-use ID3FS::PathElement::Boolean;
-use ID3FS::PathElement::File;
-use ID3FS::PathElement::Tag;
-use ID3FS::PathElement::Tagval;
 use ID3FS::Path::Node;
 
 our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL,
@@ -16,9 +10,9 @@ our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL,
 
 our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 );
 
-our $PATH_ALLTRACKS="TRACKS";
-our $PATH_NOARTIST="NOARTIST";
-our $PATH_NOALBUM="NOALBUM";
+our $PATH_ALLTRACKS= "TRACKS";
+our $PATH_NOARTIST = "NOARTIST";
+our $PATH_NOALBUM  = "NOALBUM";
 
 sub new
 {
@@ -87,11 +81,8 @@ sub dirents
     }
     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();
        }
@@ -101,14 +92,14 @@ sub dirents
            {
                @dents=qw(AND OR);
            }
-           push(@dents, $PATH_ALLTRACKS, $PATH_NOARTIST, $self->artists());
+           push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST));
+           push(@dents, $self->artists());
        }
     }
     elsif($state==$STATE_BOOLEAN)
     {
        my $parent=$self->tail();
-       unless(defined($parent) &&
-              ref($parent) eq "ID3FS::PathElement::Boolean" &&
+       unless($self->is("boolean", $parent) &&
               $parent->{name} eq "NOT")
        {
            @dents=("NOT");
@@ -121,7 +112,7 @@ sub dirents
     }
     elsif($state==$STATE_ALBUMS)
     {
-       @dents=($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
+       @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOALBUM), $self->albums());
     }
     elsif($state==$STATE_TRACKLIST)
     {
@@ -145,7 +136,6 @@ sub parse
     my @parts=@{$self->{components}};
     my($tag, $tagval);
     $self->{elements}=[];
-    $self->{bare_not}=0;
     $self->{in_all}=0;
     my $root_not=0;
     my $tags_seen=0;
@@ -169,12 +159,12 @@ sub parse
            elsif($name eq "NOT")
            {
                $root_not=1;
-               push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name));
+               push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
                $self->state($STATE_BOOLEAN);
            }
            else
            {
-               $tag=ID3FS::PathElement::Tag->new($self->{db}, $name);
+               $tag=ID3FS::Path::Node->new($self->{db},"tag", $name);
                if($tag)
                {
                    push(@{$self->{elements}}, $tag);
@@ -189,15 +179,13 @@ 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" &&
-              $self->{db}->tag_has_values($tag->{id}))
+#          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";
-               my $tagval=ID3FS::PathElement::Tag->new($self->{db}, $name, $tag->{id});
+#              print "Parsing: parent: $tag->id()\n";
+               my $tagval=ID3FS::Path::Node->new($self->{db}, "tag", $name, $tag->id());
                if(defined($tagval))
                {
                    $self->state($STATE_TAGVAL);
@@ -220,16 +208,16 @@ sub parse
            elsif($name eq "AND")
            {
                $self->state($STATE_BOOLEAN);
-               push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name));
+               push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
            }
            elsif($name eq "OR")
            {
                $self->state($STATE_BOOLEAN);
-               push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name));
+               push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
            }
            else
            {
-               my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name);
+               my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
                if($artist)
                {
                    push(@{$self->{elements}}, $artist);
@@ -246,8 +234,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;
@@ -255,11 +242,11 @@ sub parse
            if($allownot && $name eq "NOT")
            {
                $self->state($STATE_BOOLEAN);
-               push(@{$self->{elements}}, ID3FS::PathElement::Boolean->new($self->{db}, $name));
+               push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
            }
            else
            {
-               my $tag=ID3FS::PathElement::Tag->new($self->{db}, $name);
+               my $tag=ID3FS::Path::Node->new($self->{db}, "tag", $name);
                if($tag)
                {
                    push(@{$self->{elements}}, $tag);
@@ -285,7 +272,7 @@ sub parse
            }
            else
            {
-               my $album=ID3FS::PathElement::Album->new($self->{db}, $name);
+               my $album=ID3FS::Path::Node->new($self->{db}, "album", $name);
                if($album)
                {
                    push(@{$self->{elements}}, $album);
@@ -300,7 +287,7 @@ sub parse
        elsif($state==$STATE_TRACKLIST)
        {
 #          print "SM: TRACKLIST: $name\n";
-           my $track=ID3FS::PathElement::File->new($self->{db}, $name);
+           my $track=ID3FS::Path::Node->new($self->{db}, "file", $name);
            if($track)
            {
                push(@{$self->{elements}}, $track);
@@ -325,12 +312,11 @@ sub parse
            }
            elsif($name eq $PATH_NOARTIST)
            {
-               # FIXME
                $self->state($STATE_TRACKLIST);
            }
            else
            {
-               my $artist=ID3FS::PathElement::Artist->new($self->{db}, $name);
+               my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
                if($artist)
                {
                    push(@{$self->{elements}}, $artist);
@@ -349,14 +335,9 @@ sub parse
        }
     }
 
-    if($root_not && ($tags_seen < 2))
-    {
-       $self->{bare_not}=1;
-    }
-
     # 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;
     }
@@ -365,13 +346,8 @@ sub parse
     $self->{tagtree}=$self->elements_to_tree(\@elements);
     if($self->{tagtree})
     {
-       ($self->{sqlconditions},
-        $self->{joins}) = $self->{tagtree}->to_sql();
-#      print "TREE: ",  $self->{tagtree}->print(), "\n";
-#      print("SQL CONDITION(", scalar(@{$self->{joins}}), "): ",
-#            $self->{sqlconditions}, "\n");
-#      use Data::Dumper;
-#      print Dumper $self->{tagtree};
+#      my ($conditions, @joins)=$self->{tagtree}->to_sql();
+#      print "CONDITIONS(", scalar(@joins), "): ", $conditions, "\n";
     }
 }
 
@@ -392,20 +368,17 @@ 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);
-       if($op->{name} ne "NOT")
+       if($thing->{name} ne "NOT")
        {
            $left=$self->elements_to_tree($elements);
        }
-       return ID3FS::Path::Node->new($left, $op, $right);
-    }
-    else
-    {
-       return ID3FS::Path::Node->new($thing);
+       $thing->left($left);
+       $thing->right($right);
     }
+    return $thing;
 }
 
 # Dijkstra's shunting-yard algorithm
@@ -417,16 +390,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 &&
@@ -456,9 +429,9 @@ 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}));
+       return($self->{db}->tag_has_values($tail->id()));
     }
 }
 
@@ -466,9 +439,9 @@ 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});
+       return($tail->id());
     }
     return undef;
 }
@@ -477,7 +450,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});
     }
@@ -490,6 +463,21 @@ 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);
+    return 0 unless($thing->type());
+    return 1 if($type eq $thing->type());
+    return 0;
+}
+
 # the one before last
 sub tail_parent
 {
@@ -517,40 +505,10 @@ sub tags
             ") AS subselect\n" .
             "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" .
             "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n");
-    my @allused=$self->used_tags();
-    my @used=grep { ref($_) ne "ARRAY"; } @allused;
-    my @used_with_vals=grep { ref($_) eq "ARRAY"; } @allused;
-#    print "tags(): USED: ", join(", ", @used), "\n";
-#    print "tags(): USED_WITH_VALS: ", join(", ", map { "[".$_->[0]. ", ".$_->[1]."]";} @used_with_vals), "\n";
-    my @orclauses=();
     my @andclauses=();
     my $id=$self->trailing_tag_id();
-    if($hasvals)
-    {
-#      print "HAS_VALUES\n";
-       my @values=map { "'".$_->[1]."'"; } grep { $_->[0] == $id; } @used_with_vals;
-       my $clause="(tags.parents_id='$id'";
-       if(@values)
-       {
-           $clause .= " AND tags.id NOT IN (" . join(', ', @values) . ")";
-       }
-       $clause .= ")";
-       push(@andclauses, $clause);
-    }
-    else
-    {
-#      print "HASNT VALUES\n";;
-       if(@used)
-       {
-           push(@andclauses, "(NOT (tags.parents_id='' AND tags.id IN (" . join(', ', @used) . ")))");
-       }
-       for my $pair (@used_with_vals)
-       {
-           push(@andclauses, "(NOT (tags.parents_id='" . $pair->[0] . "' AND tags.id='" . $pair->[1] . "'))");
-       }
-    }
 
-    my $parentclause= "(tags.parents_id='";
+    my $parentclause= "tags.parents_id='";
     if($hasvals)
     {
        $parentclause .= $id;
@@ -559,17 +517,19 @@ sub tags
     {
        $parentclause .= $parent;
     }
-    $parentclause .= "')";
+    $parentclause .= "'";
     push(@andclauses, $parentclause);
 
-    if(@orclauses)
+    my @used=$self->used_tags();
+    if(@used)
     {
-       push(@andclauses, '( ' . join(' OR ', @orclauses) . ' )');
+       push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
     }
     if(@andclauses)
     {
        $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
     }
+
     $sql .= "GROUP BY tags.name;";
     print "SQL(TAGS): $sql\n" if($self->{verbose});
     my @tagnames=$self->{db}->cmd_firstcol($sql);
@@ -601,10 +561,9 @@ sub albums
     my($self)=@_;
     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});
+       return $self->artist_albums($tail->id());
     }
     my $sql=$self->sql_start("albums.name");
     $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
@@ -661,22 +620,21 @@ sub album_tracks
 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});
+       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};
+           $artist_id=$artist->id();
        }
-       return $self->album_tracks($artist_id, $tail->{id});
+       return $self->album_tracks($artist_id, $tail->id());
     }
     my $sql=$self->sql_start("files.name");
     $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
@@ -695,9 +653,9 @@ 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 $id=$tail->id();
        my $sql=("SELECT paths.name, files.name FROM files\n" .
                 "INNER JOIN paths ON files.paths_id=paths.id\n" .
                 "WHERE files.id=?\n" .
@@ -707,24 +665,21 @@ sub filename
        my $id3fs_path=join('/', map { $_->{name}; }  @{$self->{elements}});
        return($self->{db}->relativise($path, $name, $mountpoint));
     }
-    die("DB::filename: unhandled case\n"); #FIXME
+    # should never happen
+    return "ERROR";
 }
 
 sub tags_subselect
 {
     my($self)=@_;
     my $hasvals=$self->expecting_values();
-    # we need to specially handle a bare /NOT/tag with no other clauses,
-    # using a simple WHERE id !='tagid' instead of a LEFT JOIN
-    if($self->{bare_not})
-    {
-       return $self->bare_not_subselect();
-    }
     if($self->{in_all})
     {
        return "\tSELECT id FROM files AS files_id\n";
     }
     my $tree=$self->{tagtree};
+    # FIXME
+    print "UNDEF!!\n" unless($self->{tagtree});
     my $parent=$self->trailing_tag_parent();
 
 #    print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n";
@@ -737,7 +692,7 @@ sub tags_subselect
     }
     my ($sqlclause, @joins)=(undef, ());
     ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
-#    print "SQL(" . scalar(@joins) .": $sqlclause\n";
+#    print "SQL(" . scalar(@joins) ."): $sqlclause\n";
     my $sql="\tSELECT fxt1.files_id FROM tags t1";
     my @crosses=();
     my @inners=();
@@ -766,27 +721,6 @@ sub tags_subselect
     return $sql;
 }
 
-sub bare_not_subselect
-{
-    my($self)=@_;
-    my @tags=grep { ref($_) eq "ID3FS::PathElement::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" .
-            "\t\tWHERE ");
-    if(scalar(@tags) > 1)
-    {
-       $sql .= ("(t1.parents_id='" . $tags[0]->{id} . "' AND t1.id='" .
-                $tags[1]->{id} . "')");
-    }
-    else
-    {
-       $sql .= ("(t1.parents_id='' AND t1.id='" . $tags[0]->{id} . "')");
-    }
-    $sql .= "\n\t\tGROUP BY fxt1.files_id\n\t)\n";
-    return($sql);
-}
-
 sub sql_start
 {
     my($self, $tables)=@_;
@@ -815,47 +749,44 @@ 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))
            {
 #              print "TAGVAL\n";
-               push(@tags_vals, [$lasttag, $constraint->{id}]) if defined($constraint->{id});
+               push(@tags_vals, [$lasttag, $constraint->id()]) if defined($constraint->id());
                $lasttag=undef;
            }
-           elsif($self->tag_has_values($constraint->{id}))
+           elsif($self->tag_has_values($constraint->id()))
            {
 #              print "HASVALUES\n";
-               $lasttag=$constraint->{id} if defined($constraint->{id});
+               $lasttag=$constraint->id() if defined($constraint->id());
            }
            else
            {
 #              print "NOVALUES\n";
-               push(@tags, $constraint->{id}) if(defined($constraint->{id}));
+               push(@tags, $constraint->id()) if(defined($constraint->id()));
            }
        }
     }
-    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);
 }
 
-# Not used, slows things down too much
+# 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))
+       if($self->empty("$base/$dir"))
        {
 #          print "empty: $base / $dir\n";
        }
@@ -870,51 +801,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}, $self->{tagdepth});
-#    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;