[tracker/tracker-0.16] tracker-tag: Fix --list (-t) to use logical OR, not AND



commit dc70a210b5f81475055b6e2f350fd10fee1a3953
Author: Martyn Russell <martyn lanedo com>
Date:   Mon Mar 25 15:00:15 2013 +0000

    tracker-tag: Fix --list (-t) to use logical OR, not AND
    
    Also improve the documentation.

 docs/manpages/tracker-tag.1     |   16 ++++++++-
 src/tracker-utils/tracker-tag.c |   66 ++++++++------------------------------
 2 files changed, 28 insertions(+), 54 deletions(-)
---
diff --git a/docs/manpages/tracker-tag.1 b/docs/manpages/tracker-tag.1
index c3e1a20..b5756ac 100644
--- a/docs/manpages/tracker-tag.1
+++ b/docs/manpages/tracker-tag.1
@@ -4,8 +4,10 @@
 tracker-tag \- Add, remove and list tags.
 
 .SH SYNOPSIS
-\fBtracker-tag\fR
-[\fIOPTION...\fR] FILE [\fIFILE...\fR]
+\fBtracker-tag\fR [\fIOPTION...\fR] FILE [\fIFILE...\fR]
+.nf
+\fBtracker-tag\fR [\fIOPTION...\fR] -t [[\fITAG\fR] [\fITAG\fR] ...\fR]
+.fi
 
 .SH DESCRIPTION
 .B tracker-tag
@@ -34,6 +36,16 @@ Use OR for search terms instead of AND (the default)
 List all tags. Results include the number of files associated with
 that tag and the tag's unique identifier. You can show the files
 associated with each tag by using --show-files.
+
+The \fITAG\fR arguments are optional. If no \fITAG\fR argument
+is specified, all tags are listed. If one or more \fITAG\fRs are
+given, all matching tags are listed. For example, this will match any
+tags named either \fIfoo\fR, \fIbar\fR or \fIbaz\fR:
+.nf
+.BR
+$ tracker-tag -t foo bar baz
+.fi
+
 .TP
 .B \-s, \-\-show-files
 Show the files associated with each tag. This option is ONLY available
diff --git a/src/tracker-utils/tracker-tag.c b/src/tracker-utils/tracker-tag.c
index 982a53b..36b5ad4 100644
--- a/src/tracker-utils/tracker-tag.c
+++ b/src/tracker-utils/tracker-tag.c
@@ -64,8 +64,8 @@ static GOptionEntry entries[] = {
          NULL
        },
        { "list", 't', 0, G_OPTION_ARG_NONE, &list,
-         N_("List all tags (using FILTER if specified)"),
-         NULL,
+         N_("List all tags (using FILTER if specified; FILTER always uses logical OR)"),
+         N_("FILTER"),
        },
        { "show-files", 's', 0, G_OPTION_ARG_NONE, &show_files,
          N_("Show files associated with each tag (this is only used with --list)"),
@@ -149,49 +149,6 @@ get_escaped_sparql_string (const gchar *str)
 }
 
 static gchar *
-get_fts_string (GStrv    search_words,
-                gboolean use_or_operator,
-                gboolean for_regex,
-                gboolean use_asterisk)
-{
-       GString *fts;
-       gint i, len;
-
-       if (!search_words) {
-               return NULL;
-       }
-
-       len = g_strv_length (search_words);
-       fts = g_string_new ("");
-
-       for (i = 0; i < len; i++) {
-               g_string_append (fts, search_words[i]);
-
-               if (use_asterisk) {
-                       g_string_append_c (fts, '*');
-               }
-
-               if (i < len - 1) {
-                       if (for_regex) {
-                               if (use_or_operator) {
-                                       g_string_append (fts, " || ");
-                               } else {
-                                       g_string_append (fts, " && ");
-                               }
-                       } else {
-                               if (use_or_operator) {
-                                       g_string_append (fts, " OR ");
-                               } else {
-                                       g_string_append (fts, " ");
-                               }
-                       }
-               }
-       }
-
-       return g_string_free (fts, FALSE);
-}
-
-static gchar *
 get_filter_string (GStrv        files,
                    gboolean     files_are_urns,
                    const gchar *tag)
@@ -388,12 +345,18 @@ get_all_tags (TrackerSparqlConnection *connection,
 {
        TrackerSparqlCursor *cursor;
        GError *error = NULL;
-       gchar *fts;
        gchar *query;
 
-       fts = get_fts_string (files, use_or_operator, TRUE, FALSE);
+       if (files && g_strv_length (files) > 0) {
+               gchar *filter;
+
+               /* e.g. '?label IN ("foo", "bar")' */
+               filter = g_strjoinv ("\",\"", files);
 
-       if (fts) {
+               /* You might be asking, why not logical AND here, why
+                * logical OR for FILTER, well, tags can't have
+                * multiple labels is the simple answer.
+                */
                query = g_strdup_printf ("SELECT ?tag ?label nao:description(?tag) COUNT(?urns) AS urns "
                                         "WHERE {"
                                         "  ?tag a nao:Tag ;"
@@ -401,15 +364,16 @@ get_all_tags (TrackerSparqlConnection *connection,
                                         "  OPTIONAL {"
                                         "     ?urns nao:hasTag ?tag"
                                         "  } ."
-                                        "  FILTER (?label = \"%s\")"
+                                        "  FILTER (?label IN (\"%s\"))"
                                         "} "
                                         "GROUP BY ?tag "
                                         "ORDER BY ASC(?label) "
                                         "OFFSET %d "
                                         "LIMIT %d",
-                                        fts,
+                                        filter,
                                         search_offset,
                                         search_limit);
+               g_free (filter);
        } else {
                query = g_strdup_printf ("SELECT ?tag ?label nao:description(?tag) COUNT(?urns) AS urns "
                                         "WHERE {"
@@ -427,8 +391,6 @@ get_all_tags (TrackerSparqlConnection *connection,
                                         search_limit);
        }
 
-       g_free (fts);
-
        cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
        g_free (query);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]