[tracker/wip/carlosg/cli-improvements: 7/8] tracker: Add "tracker3 export" support to extract specific IRIs




commit 4af566b47f480ffe4ff630fb75d41c910d47390b
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Aug 19 11:21:56 2020 +0200

    tracker: Add "tracker3 export" support to extract specific IRIs
    
    This is similar to "tracker3 info", but without file and uri specific.

 docs/manpages/tracker-export.1.txt |  7 +++---
 src/tracker/tracker-export.c       | 49 ++++++++++++++++++++++++++++----------
 2 files changed, 40 insertions(+), 16 deletions(-)
---
diff --git a/docs/manpages/tracker-export.1.txt b/docs/manpages/tracker-export.1.txt
index d03461b2e..04f407cc8 100644
--- a/docs/manpages/tracker-export.1.txt
+++ b/docs/manpages/tracker-export.1.txt
@@ -7,12 +7,13 @@ tracker-export - Export all data from a Tracker database.
 
 == SYNOPSIS
 
-*tracker export* [_options_...]
+*tracker export* [_options_...] [*IRI*...]
 
 == DESCRIPTION
 
-*tracker export* exports all data stored in a Tracker database, in
-Turtle format.
+*tracker export* exports data stored in a Tracker database, in
+Turtle format. By default all data is exported, if any *IRI*, only those
+resources will be printed.
 
 The output is intended to be machine-readable, not human readable. Use a
 tool such as rapper(1) to convert the data to different formats.
diff --git a/src/tracker/tracker-export.c b/src/tracker/tracker-export.c
index 32fbda856..2484e0296 100644
--- a/src/tracker/tracker-export.c
+++ b/src/tracker/tracker-export.c
@@ -37,6 +37,7 @@ static gchar *database_path;
 static gchar *dbus_service;
 static gchar *remote_service;
 static gboolean show_graphs;
+static gchar **iris;
 
 static GOptionEntry entries[] = {
        { "database", 'd', 0, G_OPTION_ARG_FILENAME, &database_path,
@@ -55,6 +56,9 @@ static GOptionEntry entries[] = {
          N_("Output TriG format which includes named graph information"),
          NULL
        },
+       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &iris,
+         N_("IRI"),
+         N_("IRI")},
        { NULL }
 };
 
@@ -246,7 +250,8 @@ export_run_default (void)
        g_autoptr(TrackerSparqlCursor) cursor = NULL;
        g_autoptr(GError) error = NULL;
        g_autoptr(GHashTable) prefixes = NULL;
-       const gchar *query;
+       g_autoptr(GString) query = NULL;
+       guint i;
 
        connection = create_connection (&error);
 
@@ -259,18 +264,36 @@ export_run_default (void)
 
        prefixes = tracker_sparql_get_prefixes (connection);
 
-       query = "SELECT ?g ?u ?p ?v "
-               "       (EXISTS { ?p rdfs:range [ rdfs:subClassOf rdfs:Resource ] }) AS ?is_resource "
-               "{ "
-               "    GRAPH ?g { "
-               "        ?u ?p ?v "
-               "        FILTER NOT EXISTS { ?u a rdf:Property } "
-               "        FILTER NOT EXISTS { ?u a rdfs:Class } "
-               "        FILTER NOT EXISTS { ?u a nrl:Namespace } "
-               "    } "
-               "} ORDER BY ?g ?u";
-
-       cursor = tracker_sparql_connection_query (connection, query, NULL, &error);
+       query = g_string_new (NULL);
+       g_string_append (query,
+                        "SELECT ?g ?u ?p ?v "
+                        "       (EXISTS { ?p rdfs:range [ rdfs:subClassOf rdfs:Resource ] }) AS ?is_resource 
"
+                        "{ "
+                        "    GRAPH ?g { "
+                        "        ?u ?p ?v ");
+
+       if (iris) {
+               g_string_append (query, "FILTER (?u IN (");
+
+               for (i = 0; iris[i]; i++) {
+                       if (i != 0)
+                               g_string_append_c (query, ',');
+                       g_string_append_printf (query, "<%s>", iris[i]);
+               }
+
+               g_string_append (query, "))");
+       } else {
+               g_string_append (query,
+                                "FILTER NOT EXISTS { ?u a rdf:Property } "
+                                "FILTER NOT EXISTS { ?u a rdfs:Class } "
+                                "FILTER NOT EXISTS { ?u a nrl:Namespace } ");
+       }
+
+       g_string_append (query,
+                        "    } "
+                        "} ORDER BY ?g ?u");
+
+       cursor = tracker_sparql_connection_query (connection, query->str, NULL, &error);
 
        if (error) {
                g_printerr ("%s, %s\n",


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