From 63d488d74fc179ac276496b97c26d271098127fb Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Wed, 10 Jan 2018 13:08:10 +0000 Subject: [PATCH] Add properties to format scrutinizer --- .../wikidata/module/langs/translation-en.json | 7 ++- .../dialogs/schema-alignment-dialog.js | 45 +++++++++++++++++-- .../dialogs/schema-alignment-dialog.less | 12 +++++ .../FormatConstraintScrutinizer.java | 13 +++++- .../wikidata/schema/WbItemConstant.java | 6 ++- 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/extensions/wikidata/module/langs/translation-en.json b/extensions/wikidata/module/langs/translation-en.json index 880011cd1..d8882f207 100644 --- a/extensions/wikidata/module/langs/translation-en.json +++ b/extensions/wikidata/module/langs/translation-en.json @@ -13,6 +13,9 @@ "save-button": "Save", "close-button": "Close" }, + "wikidata-preview": { + "new-id": "new item" + }, "warnings-messages": { "new-item-created": { "title": "This batch will create new Wikidata items.", @@ -35,8 +38,8 @@ "body": "You should provide an \"instance of\" (P31) or \"subclass of\" (P279) statement for each item that you create." }, "add-statements-with-invalid-format": { - "title": "Text statements with invalid format.", - "body": "Please consult the documentation of the properties to find out the correct format for their values." + "title": "{property_entity} statements with invalid format.", + "body": "Values for this property are expected to match the regular expression {regex}, which is not the case for {example_value} added on {example_item_entity}." }, "remove-statements-with-invalid-format": { "title": "Removed statements with invalid format.", diff --git a/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js b/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js index 2e28a89e7..954dee927 100644 --- a/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js +++ b/extensions/wikidata/module/scripts/dialogs/schema-alignment-dialog.js @@ -862,13 +862,50 @@ SchemaAlignmentDialog.preview = function(initial) { * WARNINGS RENDERING * *************************/ +// renders a Wikibase entity into a link +SchemaAlignmentDialog._renderEntity = function(entity) { + var id = entity.id; + var is_new = id == "Q0"; + if (is_new) { + id = $.i18n._('wikidata-preview')['new-id']; + } + var fullLabel = id; + if (entity.label) { + fullLabel = entity.label + ' (' + id + ')'; + } + + if (is_new) { + return ''+fullLabel+''; + } else { + return ''+fullLabel+''; + } +} + +// replaces the issue properties in localization template +SchemaAlignmentDialog._replaceIssueProperties = function(template, properties) { + if (!properties) { + return template; + } + var expanded = template; + for (var key in properties) { + if (properties.hasOwnProperty(key)) { + var rendered = properties[key]; + if (key.endsWith('_entity')) { + rendered = SchemaAlignmentDialog._renderEntity(properties[key]); + } + expanded = expanded.replace('{'+key+'}', rendered); + } + } + return expanded; +} + SchemaAlignmentDialog._renderWarning = function(warning) { var localized = $.i18n._('warnings-messages')[warning.type]; var title = warning.type; var body = ""; if (localized) { - title = localized.title; - body = localized.body; + title = SchemaAlignmentDialog._replaceIssueProperties(localized.title, warning.properties); + body = SchemaAlignmentDialog._replaceIssueProperties(localized.body, warning.properties); } var tr = $('').addClass('wb-warning'); var severityTd = $('') @@ -879,10 +916,10 @@ SchemaAlignmentDialog._renderWarning = function(warning) { .addClass('wb-warning-body') .appendTo(tr); var h1 = $('

') - .text(title) + .html(title) .appendTo(bodyTd); var p = $('

') - .text(body) + .html(body) .appendTo(bodyTd); var countTd = $('') .addClass('wb-warning-count') diff --git a/extensions/wikidata/module/styles/dialogs/schema-alignment-dialog.less b/extensions/wikidata/module/styles/dialogs/schema-alignment-dialog.less index 2d2469713..226499a72 100644 --- a/extensions/wikidata/module/styles/dialogs/schema-alignment-dialog.less +++ b/extensions/wikidata/module/styles/dialogs/schema-alignment-dialog.less @@ -282,6 +282,18 @@ div.schema-alignment-dialog-warnings { overflow-y: scroll; } +.wb-issue-preformat { + border: 1px solid #eaecf0; + background-color: #f8f9fa; + padding: 1px 3px; + border-radius: 2px; + font-family: monospace; +} + +.wb-preview-new-entity { + color: #11c; +} + /*** QuickStatements Preview ***/ div.schema-alignment-dialog-preview { diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/FormatConstraintScrutinizer.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/FormatConstraintScrutinizer.java index 61e81787a..f8e78bce1 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/FormatConstraintScrutinizer.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/FormatConstraintScrutinizer.java @@ -2,8 +2,10 @@ package org.openrefine.wikidata.qa.scrutinizers; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import java.util.regex.Pattern; +import org.openrefine.wikidata.qa.QAWarning; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.Snak; @@ -53,7 +55,16 @@ public class FormatConstraintScrutinizer extends SnakScrutinizer { Pattern pattern = getPattern(pid); if (!pattern.matcher(value).matches()) { if (added) { - important("add-statements-with-invalid-format"); + QAWarning issue = new QAWarning( + "add-statements-with-invalid-format", + pid.getId(), + QAWarning.Severity.IMPORTANT, + 1); + issue.setProperty("property_entity", pid); + issue.setProperty("regex", pattern.toString()); + issue.setProperty("example_value", value); + issue.setProperty("example_item_entity", entityId); + addIssue(issue); } else { info("remove-statements-with-invalid-format"); } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemConstant.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemConstant.java index 34d8a58d2..abcbed58b 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemConstant.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemConstant.java @@ -1,5 +1,6 @@ package org.openrefine.wikidata.schema; +import org.openrefine.wikidata.schema.entityvalues.TermedItemIdValue; import org.wikidata.wdtk.datamodel.implementation.ItemIdValueImpl; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; @@ -24,7 +25,10 @@ public class WbItemConstant extends WbItemExpr { @Override public ItemIdValue evaluate(ExpressionContext ctxt) { - return ItemIdValueImpl.create(qid, ctxt.getBaseIRI()); + return new TermedItemIdValue( + qid, + ctxt.getBaseIRI(), + label); } public String getQid() {