From f763e8c6adc7363850b2bc3efa0ed9c2950373aa Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Tue, 5 Sep 2017 22:49:17 +0100 Subject: [PATCH] Shorter QuickStatements preview and better export UI --- .../module/scripts/menu-bar-extension.js | 62 ++++++++++--------- .../PreviewWikibaseSchemaCommand.java | 17 ++++- .../wikidata/schema/WbStringExpr.java | 4 +- .../wikidata/schema/WbStringVariable.java | 2 +- .../wikidata/schema/WbValueExpr.java | 2 +- 5 files changed, 52 insertions(+), 35 deletions(-) diff --git a/extensions/wikidata/module/scripts/menu-bar-extension.js b/extensions/wikidata/module/scripts/menu-bar-extension.js index b1c8de899..dba8b5482 100644 --- a/extensions/wikidata/module/scripts/menu-bar-extension.js +++ b/extensions/wikidata/module/scripts/menu-bar-extension.js @@ -1,18 +1,18 @@ /* ExporterManager.MenuItems.push({});//add separator ExporterManager.MenuItems.push( - { - "id" : "exportRdfXml", - "label":"RDF as RDF/XML", - "click": function() { RdfExporterMenuBar.exportRDF("rdf", "rdf");} - } + { + "id" : "exportRdfXml", + "label":"RDF as RDF/XML", + "click": function() { RdfExporterMenuBar.exportRDF("rdf", "rdf");} + } ); ExporterManager.MenuItems.push( - { - "id" : "exportRdfTurtle", - "label":"RDF as Turtle", - "click": function() { RdfExporterMenuBar.exportRDF("Turtle", "ttl"); } - } + { + "id" : "exportRdfTurtle", + "label":"RDF as Turtle", + "click": function() { RdfExporterMenuBar.exportRDF("Turtle", "ttl"); } + } ); RdfExporterMenuBar = {}; @@ -62,12 +62,13 @@ RdfExporterMenuBar.editRdfSchema = function(reset) { }; */ +ExporterManager.MenuItems.push({}); ExporterManager.MenuItems.push( - { - "id" : "exportQuickStatements", - "label":"QuickStatements", - "click": function() { WikibaseExporterMenuBar.exportTo("quickstatements"); } - } + { + "id" : "exportQuickStatements", + "label":"QuickStatements", + "click": function() { WikibaseExporterMenuBar.exportTo("quickstatements"); } + } ); WikibaseExporterMenuBar = {}; @@ -102,18 +103,23 @@ WikibaseExporterMenuBar.exportTo = function(format) { //extend the column header menu $(function(){ - ExtensionBar.MenuItems.push( - { - "id":"reconcile", - "label": "Wikidata", - "submenu" : [ - { - "id": "wikidata/edit-schema", - label: "Edit Wikibase schema...", - click: function() { SchemaAlignmentDialog.launch(false); } - }, - ] - } - ); + ExtensionBar.MenuItems.push( + { + "id":"reconcile", + "label": "Wikidata", + "submenu" : [ + { + "id": "wikidata/edit-schema", + label: "Edit Wikibase schema...", + click: function() { SchemaAlignmentDialog.launch(false); } + }, + { + "id":"wikidata/export-qs", + "label": "Export to QuickStatements", + "click": function() { WikibaseExporterMenuBar.exportTo("quickstatements"); } + } + ] + } + ); }); diff --git a/extensions/wikidata/src/org/openrefine/wikidata/commands/PreviewWikibaseSchemaCommand.java b/extensions/wikidata/src/org/openrefine/wikidata/commands/PreviewWikibaseSchemaCommand.java index d4e77180f..44b5a767e 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/commands/PreviewWikibaseSchemaCommand.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/commands/PreviewWikibaseSchemaCommand.java @@ -34,8 +34,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package org.openrefine.wikidata.commands; import java.io.IOException; +import java.io.LineNumberReader; +import java.io.StringReader; import java.io.StringWriter; -import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -60,8 +61,6 @@ public class PreviewWikibaseSchemaCommand extends Command { try { Project project = getProject(request); - Engine engine = getEngine(request, project); - FilteredRows filteredRows = engine.getAllFilteredRows(); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/json"); @@ -79,6 +78,18 @@ public class PreviewWikibaseSchemaCommand extends Command { QuickStatementsExporter exporter = new QuickStatementsExporter(); exporter.translateSchema(project, schema, stringWriter); + String fullQS = stringWriter.toString(); + stringWriter = new StringWriter(); + LineNumberReader reader = new LineNumberReader(new StringReader(fullQS)); + reader.setLineNumber(0); + int maxQSLinesForPreview = 50; + for(int i = 0; i != maxQSLinesForPreview; i++) { + stringWriter.write(reader.readLine()+"\n"); + } + if (reader.getLineNumber() == maxQSLinesForPreview) { + stringWriter.write("..."); + } + writer.key("quickstatements"); writer.value(stringWriter.toString()); } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringExpr.java index b882f62c9..2088e026a 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringExpr.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringExpr.java @@ -7,13 +7,13 @@ import org.wikidata.wdtk.datamodel.interfaces.StringValue; public abstract class WbStringExpr extends WbValueExpr { public abstract StringValue evaluate(ExpressionContext ctxt) throws SkipStatementException; - + public static WbStringExpr fromJSON(JSONObject obj) throws JSONException { String type = obj.getString(jsonTypeKey); if (WbStringConstant.jsonType.equals(type)) { return WbStringConstant.fromJSON(obj); } else if (WbStringVariable.jsonType.equals(type)) { - return WbStringVariable.fromJSON2(obj); + return WbStringVariable.fromJSON(obj); } else { throw new JSONException("unknown type for WbItemExpr"); } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java index a3a08c270..a6060b2ce 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java @@ -36,7 +36,7 @@ public class WbStringVariable extends WbStringExpr { writer.value(columnName); } - public static WbStringVariable fromJSON2(JSONObject obj) throws JSONException { + public static WbStringExpr fromJSON(JSONObject obj) throws JSONException { return new WbStringVariable(obj.getString("columnName")); } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java index 498560816..f1208a109 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java @@ -27,7 +27,7 @@ public abstract class WbValueExpr extends BiJsonizable { } else if (WbItemVariable.jsonType.equals(type)) { valueExpr = WbItemVariable.fromJSON(obj); } else if (WbStringVariable.jsonType.equals(type)) { - valueExpr = WbStringVariable.fromJSON2(obj); + valueExpr = WbStringVariable.fromJSON(obj); } else if (WbStringConstant.jsonType.equals(type)) { valueExpr = WbStringConstant.fromJSON(obj); } else {