filter out empty TRACKS, NOARTIST, NOALBUM
[id3fs.git] / lib / ID3FS / Path.pm
index 9020f44..cc0c041 100644 (file)
@@ -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,7 +83,7 @@ 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)
     {
@@ -96,27 +97,32 @@ sub dirents
        }
        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" &&
               $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)
     {
@@ -373,7 +379,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};
 }
 
@@ -443,7 +453,7 @@ sub used_tags
     return($self->{tagtree}->used_tags());
 }
 
-sub tag_has_values
+sub expecting_values
 {
     my($self)=@_;
     my $tail=$self->tail();
@@ -493,12 +503,12 @@ sub tail_parent
 sub tags
 {
     my($self)=@_;
-    if(!$self->{tagtree}) # / or /NOT  # FIXME: /ALL too?
+    if(!$self->{tagtree}) # / or /NOT
     {
        my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
        return($self->{db}->cmd_firstcol($sql));
     }
-    my $hasvals=$self->tag_has_values();
+    my $hasvals=$self->expecting_values();
     my $parent=$self->trailing_tag_parent();
 #    print "THASVALS: $hasvals\n";
 #    print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
@@ -568,16 +578,6 @@ sub tags
     return(@tagnames);
 }
 
-sub tag_values
-{
-    my($self, $tagid)=@_;
-    my $sql=("SELECT DISTINCT name FROM tags\n" .
-            "WHERE parents_id=?");
-    my @tags=$self->{db}->cmd_firstcol($sql, $tagid);
-    @tags=map { length($_) ? $_ : "NOVALUE"; } @tags;
-    return @tags;
-}
-
 sub artists
 {
     my($self)=@_;
@@ -587,11 +587,8 @@ sub artists
        return($self->{db}->cmd_firstcol($sql));
     }
     my @ids=();
-    my $sql=("SELECT artists.name FROM (\n" .
-            $self->tags_subselect() .
-            ") AS subselect\n" .
-            "INNER JOIN files ON subselect.files_id=files.id\n" .
-            "INNER JOIN artists ON files.artists_id=artists.id\n" .
+    my $sql=$self->sql_start("artists.name");
+    $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
             "WHERE artists.name != ''\n" .
             "GROUP BY artists.name;");
     print "SQL(ARTISTS): $sql\n" if($self->{verbose});
@@ -610,22 +607,10 @@ sub albums
     {
        return $self->artist_albums($tail->{id});
     }
-    my $sql;
-    if($self->{in_all})
-    {
-       $sql="SELECT name FROM albums";
-    }
-    else
-    {
-       $sql=("SELECT albums.name\n" .
-             "\tFROM (\n" .
-             $self->tags_subselect() .
-             "\t) AS subselect\n" .
-             "INNER JOIN files ON subselect.files_id=files.id\n" .
-             "INNER JOIN albums ON files.albums_id=albums.id\n" .
-             "WHERE albums.name != ''\n" .
-             "GROUP BY albums.name;");
-    }
+    my $sql=$self->sql_start("albums.name");
+    $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
+            "WHERE albums.name != ''\n" .
+            "GROUP BY albums.name;");
     print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
     my @names=$self->{db}->cmd_firstcol($sql);
     print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
@@ -635,18 +620,7 @@ sub albums
 sub artist_albums
 {
     my($self, $artist_id)=@_;
-    my $sql="SELECT albums.name FROM ";
-    if($self->{in_all})
-    {
-       $sql .= "files\n";
-    }
-    else
-    {
-       $sql .= ("(\n" .
-                $self->tags_subselect() .
-                ") AS subselect\n" .
-                "INNER JOIN files ON subselect.files_id=files.id\n");
-    }
+    my $sql=$self->sql_start("albums.name");
     $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
             "INNER JOIN artists ON artists.id=files.artists_id\n" .
             "WHERE artists.id=? and albums.name <> ''\n" .
@@ -660,18 +634,7 @@ sub artist_albums
 sub artist_tracks
 {
     my($self, $artist_id)=@_;
-    my $sql="SELECT files.name FROM ";
-    if($self->{in_all})
-    {
-       $sql .= "files\n";
-    }
-    else
-    {
-       $sql .= ("(\n" .
-                $self->tags_subselect() .
-                "\t) AS subselect\n" .
-                "INNER JOIN files ON subselect.files_id=files.id\n");
-    }
+    my $sql=$self->sql_start("files.name");
     $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
             "INNER JOIN albums  ON albums.id=files.albums_id\n" .
             "WHERE artists.id=? AND albums.name=''\n" .
@@ -716,18 +679,7 @@ sub tracks
        }
        return $self->album_tracks($artist_id, $tail->{id});
     }
-    my $sql="SELECT files.name FROM ";
-    if($self->{in_all})
-    {
-       $sql .= "files\n";
-    }
-    else
-    {
-       $sql .= ("(\n" .
-                $self->tags_subselect() .
-                ") AS subselect\n" .
-                "INNER JOIN files ON files.id=subselect.files_id\n");
-    }
+    my $sql=$self->sql_start("files.name");
     $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
     if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
     {
@@ -754,7 +706,7 @@ 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
 }
@@ -762,7 +714,7 @@ sub filename
 sub tags_subselect
 {
     my($self)=@_;
-    my $hasvals=$self->tag_has_values();
+    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})
@@ -836,109 +788,24 @@ sub bare_not_subselect
     return($sql);
 }
 
-sub tags_subselect_and_not
-{
-    my($self,@constraints)=@_;
-    my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
-    my @tags=@$tags;
-    my @tags_vals=@$tags_vals;;
-    my $cnt=1;
-    my @andclauses=();
-    my $sql='';
-    for my $tag (@tags)
-    {
-       if($cnt == 1)
-       {
-           $sql="\tSELECT fxt" . scalar(@tags) . ".files_id FROM files_x_tags fxt1\n";
-           push(@andclauses, "\t\tfxt${cnt}.tags_id=$tag");
-       }
-       else
-       {
-           $sql .= ("\tLEFT JOIN files_x_tags fxt$cnt ON fxt" .
-                ($cnt-1) . ".files_id=fxt${cnt}.files_id\n");
-           push(@andclauses, "\t\tfxt${cnt}.tags_id IS NULL");
-       }
-       print "AND: @andclauses\n";
-       $cnt++;
-    }
-    if(@andclauses)
-    {
-       $sql .= "\tWHERE\n\t\t";
-       $sql .= join(" AND\n\t\t", @andclauses) . "\n";
-    }
-    $sql .= "\tGROUP BY fxt". scalar(@tags).".files_id\n";
-    return $sql;
-}
-
-
-sub tags_subselect_and
+sub sql_start
 {
-    my($self,@constraints)=@_;
-    my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
-    my @tags=@$tags;
-    my @tags_vals=@$tags_vals;;
-    my $cnt=1;
-    my @andclauses=();
-    my $sql='';
-    for my $tag (@tags)
+    my($self, $tables)=@_;
+    my $sql="SELECT $tables FROM ";
+    if($self->{in_all})
     {
-       if($cnt == 1)
-       {
-           $sql="\tSELECT fxt" . scalar(@tags) . ".files_id FROM files_x_tags fxt1\n";
-       }
-       else
-       {
-           $sql .= ("\tINNER JOIN files_x_tags fxt$cnt ON fxt" .
-                ($cnt-1) . ".files_id=fxt${cnt}.files_id\n");
-       }
-       push(@andclauses, "\t\tfxt${cnt}.tags_id = $tag");
-       print "AND: @andclauses\n";
-       $cnt++;
+       $sql .= "files\n";
     }
-    if(@andclauses)
+    else
     {
-       $sql .= "\tWHERE\n\t\t";
-       $sql .= join(" AND\n\t\t", @andclauses) . "\n";
+       $sql .= ("(\n" .
+                $self->tags_subselect() .
+                ") AS subselect\n" .
+                "INNER JOIN files ON subselect.files_id=files.id\n");
     }
-    $sql .= "\tGROUP BY fxt". scalar(@tags).".files_id\n";
     return $sql;
 }
 
-sub tags_subselect_or
-{
-    my($self,@constraints)=@_;
-    my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
-    my @tags=@$tags;
-    my @tags_vals=@$tags_vals;;
-
-    my $sql=("\tSELECT files_x_tags.files_id FROM tags t1\n" .
-            "\tINNER JOIN files_x_tags ON t1.id=files_x_tags.tags_id\n");
-    my @orclauses=();
-    my @andclauses=();
-    # FIXME: and / or?
-    if(@tags)
-    {
-       push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
-       push(@andclauses, "( t1.id IN ( " . join(', ', @tags) ." ) )");
-    }
-    for my $pair (@tags_vals)
-    {
-       my($tag, $val)=@$pair;
-       push(@orclauses, "( t1.parents_id=$tag AND t1.id=$val )");
-    }
-#    push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
-    if(@orclauses)
-    {
-       push(@andclauses, join("\n\t\tOR ", @orclauses));
-    }
-    if(@andclauses)
-    {
-       $sql .= "\tWHERE\n\t\t";
-       $sql .= join("\n\t\tAND ", @andclauses) . "\n";
-    }
-    $sql .= "\tGROUP BY files_x_tags.files_id\n";
-    return $sql;
-}
 
 sub constraints_tag_list
 {
@@ -978,52 +845,24 @@ sub constraints_tag_list
     return(\@tags, \@tags_vals, $lasttag);
 }
 
-
-sub bare_tags
-{
-    my($self)=@_;
-    my $sql=("SELECT tags.name FROM tags\n" .
-            "WHERE tags.parents_id=''\n" .
-            "GROUP BY tags.name\n");
-    my @names=$self->{db}->cmd_firstcol($sql);
-    return (@names);
-}
-
-sub tags_with_values
-{
-    # FIXME: only shows one level of tag depth
-    my($self)=@_;
-    my $sql=("SELECT p.name, t.name  FROM tags t\n" .
-            "INNER JOIN tags p ON t.parents_id=p.id\n" .
-            "GROUP BY p.name, t.name\n");
-#    print "SQL: $sql\n";
-    my $result=$self->{db}->cmd_rows($sql);
-    my $tags={};
-    for my $pair (@$result)
-    {
-       push(@{$tags->{$pair->[0]}}, $pair->[1]);
-    }
-    return $tags;
-}
-
-
+# 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);
        }
     }
@@ -1032,59 +871,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 tagname_has_values
-{
-    my($self, $name)=@_;
-    my $id=$self->{db}->lookup_id("tags", $name);
-    return 0 unless($id);
-    return $self->{db}->tag_has_values($id);
-
-}
-
-sub dir_is_special
-{
-    my($self, $dir)=@_;
-    if((grep { $_ eq $dir; }
-       qw(AND OR NOT), $PATH_ALLTRACKS, $PATH_NOARTIST, $PATH_NOALBUM) ||
-       ($self->tagname_has_values($dir)))
-    {
-       return 1;
-    }
-    return 0;
-}
-
 1;