472
edits
(Update package: OSW Core) Tags: Mobile edit Mobile web edit |
(Update package: OSW Core) |
||
Line 1: | Line 1: | ||
-- mw.logObject(p.processJsondata({jsondata=p.loadJson({title="Item:OSW7d7193567ea14e4e89b74de88983b718", slot="jsondata"}).json, debug=true, mode="header"})) | |||
local lustache = require("Module:Lustache") | local lustache = require("Module:Lustache") | ||
Line 5: | Line 7: | ||
p.keys = { --jsonschema / json-ld keys | p.keys = { --jsonschema / json-ld keys | ||
category='type', | category='type', | ||
category_pseudoproperty='Category', -- Property:Category | |||
subcategory='subclass_of', | subcategory='subclass_of', | ||
schema_type='schema_type', | schema_type='schema_type', | ||
Line 105: | Line 108: | ||
end | end | ||
if (root) then | if (root) then | ||
for i, category in ipairs(visited) do | for i, category in ipairs(visited) do | ||
jsonschema = p.tableMerge(jsonschema, jsonschemas[category]) | --merge all schemas. we need to make a copy here, otherwise jsonschemas["Category:Entity"] contains the merged schema | ||
jsonschema = p.copy(p.tableMerge(jsonschema, jsonschemas[category])) | |||
end | end | ||
end | end | ||
Line 149: | Line 152: | ||
-- mustache can handle objects and array to we can parse it directly | -- mustache can handle objects and array to we can parse it directly | ||
-- todo: handle nested templates | -- todo: handle nested templates | ||
if (debug) then msg = msg .. "Parse mustache template " .. eval_template.value .. " with params " .. mw.dumpObject( | local template_param = {[k]=v} | ||
jsondata[k] = lustache:render(eval_template.value, { | if (eval_template.root_key == false) then template_param = v end | ||
if (debug) then msg = msg .. "Parse mustache template " .. eval_template.value .. " with params " .. mw.dumpObject( template_param ) .. "\n<br>" end | |||
jsondata[k] = lustache:render(eval_template.value, template_param, p.tableMerge({self=eval_template.value}, eval_template.partials)) -- render with self as registered partial for recursion | |||
if (eval_template.type == "mustache-wikitext") then | if (eval_template.type == "mustache-wikitext") then | ||
jsondata[k] = frame:preprocess( jsondata[k] ) | jsondata[k] = frame:preprocess( jsondata[k] ) | ||
Line 185: | Line 190: | ||
end | end | ||
else | else | ||
if (eval_template ~= nil and eval_template | if (eval_template ~= nil and eval_template.value ~= nil) then | ||
--evaluate single array item string as json {"self": "<value>", ".": "<value>"} | |||
--evaluate single array item string as json {"self": "<value>", ".": "<value>"} => does not work since jsondata is an object | |||
mw. | --e = p.expandEmbeddedTemplates({frame=frame, jsondata={["self"]=e,["."]=e}, jsonschema=p.defaultArgPath(jsonschema, {"properties", k, "items"}, {}), template=eval_template, mode=mode, stringify_arrays=stringify_arrays}) | ||
e = | |||
v[i] = e | |||
if (eval_template.type == "mustache" or eval_template.type == "mustache-wikitext") then | |||
if (debug) then msg = msg .. "Parse mustache template " .. eval_template.value .. " with params " .. mw.dumpObject( e ) .. "\n<br>" end | |||
-- {{.}} in the template will be the value of e | |||
e = lustache:render(eval_template.value, e, p.tableMerge({self=eval_template.value}, eval_template.partials)) -- render with self as registered partial for recursion | |||
end | |||
if (eval_template.type == "mustache-wikitext") then --or eval_template.type == "wikitext") then | |||
if (debug) then msg = msg .. "Parse wikitext template " .. e .. " with params " .. mw.dumpObject( e ) .. "\n<br>" end | |||
e = frame:preprocess( e ) | |||
end | |||
v[i] = e -- update array | |||
end | end | ||
if (stringify_arrays) then string_list = string_list .. e .. ";" end | if (stringify_arrays) then string_list = string_list .. e .. ";" end | ||
Line 262: | Line 277: | ||
local schema_res = p.walkJsonSchema({jsonschema=jsonschema, categories=categories, mode=mode, recursive=recursive, debug=debug}) | local schema_res = p.walkJsonSchema({jsonschema=jsonschema, categories=categories, mode=mode, recursive=recursive, debug=debug}) | ||
local expand_res = p.expandJsonRef({json=schema_res.jsonschema, debug=debug}) | |||
--mw. | jsonschema = expand_res.json | ||
--mw.log(mw.text.jsonEncode(jsonschema)) | |||
local display_label = p.defaultArgPath(jsondata, {p.keys.name}, "") | local display_label = p.defaultArgPath(jsondata, {p.keys.name}, "") | ||
Line 283: | Line 299: | ||
-- get the semantic properties by looking up the json keys in the json-ld context | -- get the semantic properties by looking up the json keys in the json-ld context | ||
smw_res = p.getSemanticProperties({jsonschema=jsonschema, jsondata=json_res_store.res, store=false, debug=debug}) | smw_res = p.getSemanticProperties({jsonschema=jsonschema, jsondata=json_res_store.res, store=false, debug=debug}) | ||
-- store metadata where properties were defined / overridden | |||
for i, category in ipairs(schema_res.visited) do | |||
for k, v in pairs(schema_res.jsonschemas[category]['properties']) do | |||
if smw_res.definitions[k] == nil then smw_res.definitions[k] = {} end | |||
if smw_res.definitions[k]['defined_in'] == nil then smw_res.definitions[k]['defined_in'] = {} end | |||
table.insert(smw_res.definitions[k]['defined_in'], category) | |||
end | |||
end | |||
-- embed json-ld in resulting html for search engine discovery | -- embed json-ld in resulting html for search engine discovery | ||
Line 331: | Line 356: | ||
-- render the infobox for the schema itself and every super_schema using always the global json-ld context (merged within walkJsonSchema()) | -- render the infobox for the schema itself and every super_schema using always the global json-ld context (merged within walkJsonSchema()) | ||
-- context needs to be preprocessed with buildContext() since the generic json/table merge of the @context atttribute produces a list of strings (remote context) and context objects | -- context needs to be preprocessed with buildContext() since the generic json/table merge of the @context atttribute produces a list of strings (remote context) and context objects | ||
local infobox_res = p.renderInfoBox({jsonschema=super_jsonschema, context= | -- context is already build in p.getSemanticProperties. schema_allOfMerged is used to provide the full schema for overridden properties | ||
local infobox_res = p.renderInfoBox({jsonschema=super_jsonschema, schema_allOfMerged=jsonschema, context=smw_res.context, property_definitions=smw_res.definitions, jsondata=jsondata, ignore_properties=ignore_properties}) | |||
wikitext = wikitext .. frame:preprocess( infobox_res.wikitext ) | wikitext = wikitext .. frame:preprocess( infobox_res.wikitext ) | ||
end | end | ||
Line 338: | Line 364: | ||
--local display_label = "" | --local display_label = "" | ||
--if (jsondata[p.keys.label] ~= nil) then display_label = p.splitString(jsondata[p.keys.label], '@')[1] end | --if (jsondata[p.keys.label] ~= nil) then display_label = p.splitString(jsondata[p.keys.label], '@')[1] end | ||
local set_categories_in_wikitext = {} | |||
p.tableMerge(set_categories_in_wikitext, json_res_store.res[p.keys.subcategory]) --classes/categories, nil for items | |||
if (title.nsText ~= "Category") then --items | |||
p.tableMerge(set_categories_in_wikitext, json_res_store.res[p.keys.category]) -- categories from schema type | |||
end | |||
-- Todo: Consider moving the category and this block to p.getSemanticProperties with store=true. However, settings categories with @category is only possible for subobjects | |||
if (smw_res ~= nil) then | if (smw_res ~= nil) then | ||
if (debug) then msg = msg .. "Store page properties" end | if (debug) then msg = msg .. "Store page properties" end | ||
-- category handling | |||
p.tableMerge(set_categories_in_wikitext, smw_res.properties[p.keys.category_pseudoproperty]) | |||
smw_res.properties[p.keys.category_pseudoproperty] = nil -- delete pseudo property | |||
smw_res.properties['HasOswId'] = mw.title.getCurrentTitle().fullText --set special property OswId to own title | |||
-- label and display title handling | |||
smw_res.properties['Display title of'] = display_label --set special property display title | smw_res.properties['Display title of'] = display_label --set special property display title | ||
smw_res.properties['Display title of lowercase'] = display_label:lower() --store lowercase for case insensitive query | smw_res.properties['Display title of lowercase'] = display_label:lower() --store lowercase for case insensitive query | ||
smw_res.properties['Display title of normalized'] = display_label:lower():gsub('[^%w]+','') --store with all non-alphanumeric chars removed for normalized query | smw_res.properties['Display title of normalized'] = display_label:lower():gsub('[^%w]+','') --store with all non-alphanumeric chars removed for normalized query | ||
p.setNormalizedLabel(smw_res.properties) --build normalized multilang label | |||
mw.ext.displaytitle.set(display_label) | mw.ext.displaytitle.set(display_label) | ||
--smw_res.properties['@category'] = jsondata[p.keys.category] | --smw_res.properties['@category'] = jsondata[p.keys.category] | ||
Line 358: | Line 396: | ||
--wikitext = mw.dumpObject(smw_res.properties) .. wikitext | --wikitext = mw.dumpObject(smw_res.properties) .. wikitext | ||
end | end | ||
wikitext = wikitext .. "\n" .. p.setCategories({categories=set_categories_in_wikitext, sortkey=display_label}).wikitext | |||
if (debug) then mw.logObject(res) end | if (debug) then mw.logObject(res) end | ||
Line 367: | Line 406: | ||
-- test: mw.logObject(p.renderInfoBox({jsonschema=p.loadJson({title="JsonSchema:Entity"}).json, jsondata={uuid="123123"}})) | -- test: mw.logObject(p.renderInfoBox({jsonschema=p.loadJson({title="JsonSchema:Entity"}).json, jsondata={uuid="123123"}})) | ||
function p.renderInfoBox(args) | function p.renderInfoBox(args) | ||
local debug = p.defaultArg(args.debug, false) | |||
local jsondata = p.defaultArg(args.jsondata, {}) | local jsondata = p.defaultArg(args.jsondata, {}) | ||
local schema = p.defaultArg(args.jsonschema, nil) | local schema = p.defaultArg(args.jsonschema, nil) -- local schema from the perspective of the current category | ||
local schema_allOfMerged = p.defaultArg(args.schema_allOfMerged, schema) -- global schema with allOfs merged | |||
local property_definitions = p.defaultArg(args.property_definitions, {}) -- dict schema_key: {property: <smw_property>, ...} | |||
local res = "" | local res = "" | ||
if schema == nil then return res end | if schema == nil then return res end | ||
Line 390: | Line 432: | ||
if (not ignore_properties[k]) then | if (not ignore_properties[k]) then | ||
if (schema['properties'] ~= nil and schema['properties'][k] ~= nil and (type(v) ~= 'table' or v[1] ~= nil)) then --literal or literal array | if (schema['properties'] ~= nil and schema['properties'][k] ~= nil and (type(v) ~= 'table' or v[1] ~= nil)) then --literal or literal array | ||
local def = | local def = schema_allOfMerged['properties'][k] | ||
--mw.logObject(def) | --mw.logObject(def) | ||
local label = k | local label = k | ||
if def['title'] ~= nil then label = def['title'] end | if def['title'] ~= nil then label = def['title'] end | ||
if def['title*'] ~= nil then -- multilang label with switch | |||
label = "{{#switch:{{USERLANGUAGECODE}} |#default=" .. label | |||
for k,v in pairs(def['title*']) do label = label .. " |" .. k .. "=" .. v end | |||
label = label .. " }}" | |||
end | |||
local description = "" | |||
if def['description'] ~= nil then description = def['description'] end | |||
if def['description*'] ~= nil then -- multilang label with switch | |||
description = "{{#switch:{{USERLANGUAGECODE}} |#default=" .. description | |||
for k,v in pairs(def['description*']) do description = description .. " |" .. k .. "=" .. v end | |||
description = description .. " }}" | |||
end | |||
if (p.tableLength(p.defaultArgPath(property_definitions, {k, 'defined_in'}, {})) > 0) then description = description .. "<br>Definition: " end | |||
for i, c in pairs(p.defaultArgPath(property_definitions, {k, 'defined_in'}, {})) do | |||
if (i > 1) then description = description .. ", " end | |||
description = description .. "[[:" ..c .. "]]" | |||
end | |||
if (description ~= "") then description = "{{#info: " .. description .. "|note }}" end -- smw tooltip | |||
label = label .. description | |||
--res = res .. title ": " .. v | --res = res .. title ": " .. v | ||
local cell = tbl:tag( 'tr' ) | local cell = tbl:tag( 'tr' ) | ||
Line 404: | Line 468: | ||
if (type(e) ~= 'table') then | if (type(e) ~= 'table') then | ||
local p_type = p.defaultArgPath(context, {k, '@type'}, '@value') | local p_type = p.defaultArgPath(context, {k, '@type'}, '@value') | ||
if (p_type == '@id') then | if (p_type == '@id' and p.defaultArgPath(def, {'items', 'type'}, 'unknown') == 'string' and def['eval_template'] == nil) then | ||
-- auto-link (OSW-)IDs if no eval_template is present | |||
e = string.gsub(e, "Category:", ":Category:") -- make sure category links work | e = string.gsub(e, "Category:", ":Category:") -- make sure category links work | ||
e = string.gsub(e, "File:", ":File:") -- do not embedd images but link to them | e = string.gsub(e, "File:", ":File:") -- do not embedd images but link to them | ||
e = "[[" .. e .. "]]" | e = "[[" .. e .. "]]" | ||
elseif (p_type == 'xsd:date') then -- formate date with user preferences | |||
e = "{{#dateformat:" .. e .. "|ymd}}" | |||
elseif (p_type == 'xsd:dateTime') then -- formate time with user preferences | |||
local smw_property = p.defaultArgPath(property_definitions, {k, 'property'}) | |||
if (smw_property ~= nil) then e = "{{#ask: [[{{FULLPAGENAME}}]]|?" .. smw_property .. "#LOCL#TO= |format=plain |mainlabel=-}}" | |||
else | |||
local _, _, date, hours, minutes = string.find(e, "(%S+)[T ](%S+)[:](%S+)[:?]") | |||
e = "{{#dateformat:" .. date .. "|ymd}} " .. hours .. ":" .. minutes .. " (UTC)" | |||
end | |||
elseif (type(v) == 'boolean') then | |||
if (v) then v = "✅" else v = "❌" end -- green check mark or red cross | |||
elseif ((string.len(e) > 100) and (string.find(e, "{{") == nil) and (string.find(e, "</") == nil)) then | |||
e = string.sub(e, 1, 100) .. "..."; -- limit infobox plain text to max 100 chars | |||
elseif (debug) then | |||
mw.log("Unformated: " .. k .. " " .. p.defaultArgPath(def, {'items', 'type'}, 'unknown')) | |||
mw.logObject(def) | |||
end | end | ||
cell:wikitext("\n* " .. e .. "") | cell:wikitext("\n* " .. e .. "") | ||
Line 414: | Line 495: | ||
else | else | ||
local p_type = p.defaultArgPath(context, {k, '@type'}, '@value') | local p_type = p.defaultArgPath(context, {k, '@type'}, '@value') | ||
if (p_type == '@id') then | if (p_type == '@id' and p.defaultArgPath(def, {'type'}, 'unknown') == 'string' and def['eval_template'] == nil) then | ||
-- auto-link (OSW-)IDs if no eval_template is present | |||
v = string.gsub(v, "Category:", ":Category:") -- make sure category links work | v = string.gsub(v, "Category:", ":Category:") -- make sure category links work | ||
v = string.gsub(v, "File:", ":File:") -- do not embedd images but link to them | v = string.gsub(v, "File:", ":File:") -- do not embedd images but link to them | ||
v = "[[" .. v .. "]]" | v = "[[" .. v .. "]]" | ||
elseif (p_type == 'xsd:date') then -- formate date & time with user preferences | |||
v = "{{#dateformat:" .. v .. "|ymd}}" | |||
elseif (p_type == 'xsd:dateTime') then -- formate time with user preferences | |||
local smw_property = p.defaultArgPath(property_definitions, {k, 'property'}) | |||
if (smw_property ~= nil) then v = "{{#ask: [[{{FULLPAGENAME}}]]|?" .. smw_property .. "#LOCL#TO= |format=plain |mainlabel=-}}" | |||
else | |||
local _, _, date, hours, minutes = string.find(v, "(%S+)[T ](%S+)[:](%S+)[:?]") | |||
v = "{{#dateformat:" .. date .. "|ymd}} " .. hours .. ":" .. minutes .. " (UTC)" | |||
end | |||
elseif (type(v) == 'boolean') then | |||
if (v) then v = "✅" else v = "❌" end -- green check mark or red cross | |||
elseif ((string.len(v) > 100) and (string.find(v, "{{") == nil) and (string.find(v, "</") == nil)) then | |||
v = string.sub(v, 1, 100) .. "..."; -- limit infobox plain text to max 100 chars | |||
elseif (debug) then | |||
mw.log("Unformated: " .. k .. " " .. p.defaultArgPath(def, {'type'}, 'unknown')) | |||
mw.logObject(def) | |||
end | end | ||
cell:wikitext( v ) | cell:wikitext("\n" .. v .. "") | ||
end | end | ||
end | end | ||
Line 483: | Line 581: | ||
jsonschema = p.expandJsonRef({json=p.loadJson({title=category, slot="jsonschema"}).json}).json | jsonschema = p.expandJsonRef({json=p.loadJson({title=category, slot="jsonschema"}).json}).json | ||
mw.logObject(p.buildContext({jsonschema=jsonschema, debug=true})) | mw.logObject(p.buildContext({jsonschema=jsonschema, debug=true})) | ||
mw.log(mw.text.jsonEncode(p.buildContext({jsonschema=jsonschema, debug=false}).context)) | |||
or | or | ||
jsonschema = { | jsonschema = { | ||
Line 517: | Line 616: | ||
elseif (type(v) == 'table' and v[1] ~= nil) then --custom addtional mappings, e. g. "type*": ["Property:HasType"] | elseif (type(v) == 'table' and v[1] ~= nil) then --custom addtional mappings, e. g. "type*": ["Property:HasType"] | ||
result[k] = v | result[k] = v | ||
elseif (type(v) == 'table' and v['@id'] == nil) then --subcontext | elseif (type(v) == 'table' and v['@id'] == nil and v['@reverse'] == nil) then --subcontext | ||
p.tableMerge(result, p.buildContext({context=v}).context) | p.tableMerge(result, p.buildContext({context=v}).context) | ||
else | else | ||
Line 533: | Line 632: | ||
subcontext = p.buildContext({jsonschema=properties[k]}).context | subcontext = p.buildContext({jsonschema=properties[k]}).context | ||
elseif (p.defaultArgPath(properties, {k, 'items', 'type'}) == 'object') then | elseif (p.defaultArgPath(properties, {k, 'items', 'type'}) == 'object') then | ||
mw.logObject(properties[k]['items']) | --mw.logObject(properties[k]['items']) | ||
subcontext = p.buildContext({jsonschema=properties[k]['items']}).context | subcontext = p.buildContext({jsonschema=properties[k]['items']}).context | ||
end | end | ||
Line 572: | Line 671: | ||
local schema = p.defaultArg(args.jsonschema, {}) | local schema = p.defaultArg(args.jsonschema, {}) | ||
local subschema = p.defaultArg(args.subschema, schema) | local subschema = p.defaultArg(args.subschema, schema) | ||
local parent_schema_property = p.defaultArg(args.parent_schema_property, {}) | local parent_schema_property = p.defaultArg(args.parent_schema_property, {}) -- ToDo: Not used except in getSemanticQuery => remove | ||
local store = p.defaultArg(args.store, false) | local store = p.defaultArg(args.store, false) | ||
local root = p.defaultArg(args.root, true) | local root = p.defaultArg(args.root, true) | ||
local properties = p.defaultArg(args.properties, {}) --semantic properties to store, dict key=property_name, value=array of string values | |||
local debug = p.defaultArg(args.debug, false) | local debug = p.defaultArg(args.debug, false) | ||
--if (debug) then mw.logObject("Call getSemanticProperties with args " .. mw.dumpObject( args ) .. "\n<br>") end | --if (debug) then mw.logObject("Call getSemanticProperties with args " .. mw.dumpObject( args ) .. "\n<br>") end | ||
local | local subjectId = mw.title.getCurrentTitle().fullText | ||
local subobjectId = nil | |||
if (root == false and jsondata['uuid'] ~= nil) then | |||
subobjectId = "OSW" .. string.gsub(jsondata['uuid'], "-", "") | |||
subjectId = subjectId .. '#' .. subobjectId | |||
end | |||
local property_data = {} | local property_data = {} | ||
local context = p.defaultArg(args.context, p.buildContext({jsonschema=schema}).context) | local context = p.defaultArg(args.context, p.buildContext({jsonschema=schema}).context) | ||
Line 594: | Line 700: | ||
for k,v in pairs(jsondata) do | for k,v in pairs(jsondata) do | ||
local property_names = {} | local property_names = {} | ||
local subobject_properties = {} -- reverse properties to store in the subobject | |||
local mapping_found = false | local mapping_found = false | ||
local property_definitions = {} | local property_definitions = {} -- list of objects {id=..., reverse=...} | ||
for term, def in pairs(context) do | for term, def in pairs(context) do | ||
local term_parts = p.splitString(term, "*") | local term_parts = p.splitString(term, "*") | ||
if ( | if (term_parts[1] == k) then --custom additional mapping term*(*...): "Property:..." | ||
if type(def) == 'table' then table.insert(property_definitions, def["@id"]) | if type(def) == 'table' then | ||
else table.insert(property_definitions, def) end | -- note: json-ld allows only @id OR @reverse | ||
if (def["@id"] ~= nil) then table.insert(property_definitions, {id=def["@id"], reverse=false}) end | |||
if (def["@reverse"] ~= nil) then table.insert(property_definitions, {id=def["@reverse"], reverse=true}) end | |||
else table.insert(property_definitions, {id=def}) end | |||
end | end | ||
end | end | ||
if (debug) then mw.logObject(property_definitions) end | if (debug) then mw.logObject(property_definitions) end | ||
for i,e in ipairs(property_definitions) do | for i,e in ipairs(property_definitions) do | ||
local property_definition = p.splitString( | local id = e["id"] | ||
local property_definition = p.splitString(id, ':') | |||
if property_definition[1] == p.keys.property_ns_prefix then | if property_definition[1] == p.keys.property_ns_prefix then | ||
mapping_found = true | mapping_found = true | ||
property_name = string.gsub( | property_name = string.gsub(id, p.keys.property_ns_prefix .. ":", "") -- also allow prefix properties like: Property:schema:url | ||
table.insert(property_names, property_name) | if (e["reverse"]) then -- reverse properties are handled in the respective subobject | ||
if (subobject_properties[property_name] == nil) then subobject_properties[property_name] = {} end --initialize empty list | |||
table.insert(subobject_properties[property_name], subjectId) -- add triple subobject -property-> subject | |||
else table.insert(property_names, property_name) end | |||
local schema_property = p.defaultArg(schema_properties[k], {}) | local schema_property = p.defaultArg(schema_properties[k], {}) | ||
local schema_type = p.defaultArg(schema_property.type, nil) --todo: also load smw property type on demand | local schema_type = p.defaultArg(schema_property.type, nil) --todo: also load smw property type on demand | ||
property_data[k] = {schema_type=schema_type, schema_data=schema_property, property=property_name, value=v} | property_data[k] = {schema_type=schema_type, schema_data=schema_property, property=property_name, value=v, reverse=e["reverse"]} | ||
end | end | ||
end | end | ||
Line 629: | Line 740: | ||
local values = {} | local values = {} | ||
if (v[1] == nil) then --key value array = object/dict | if (v[1] == nil) then --key value array = object/dict | ||
local subproperties_res = p.getSemanticProperties({jsonschema=schema, jsondata=v, store=true, root=false, debug=debug, context=context, subschema=schema_properties[k], parent_schema_property=property_data[k]}) | local subproperties_res = p.getSemanticProperties({jsonschema=schema, jsondata=v, properties=p.copy(subobject_properties), store=true, root=false, debug=debug, context=context, subschema=schema_properties[k], parent_schema_property=property_data[k]}) | ||
local id = subproperties_res.id --subobject_id | local id = subproperties_res.id --subobject_id | ||
if (id ~= nil) then | if (id ~= nil) then | ||
Line 639: | Line 750: | ||
for i, e in pairs(v) do | for i, e in pairs(v) do | ||
if (type(e) == 'table') then | if (type(e) == 'table') then | ||
local subproperties_res = p.getSemanticProperties({jsonschema=schema, jsondata=e, store=true, root=false, debug=debug, context=context, subschema=schema_properties[k], parent_schema_property=property_data[k]}) | local subproperties_res = p.getSemanticProperties({jsonschema=schema, jsondata=e, properties=p.copy(subobject_properties), store=true, root=false, debug=debug, context=context, subschema=schema_properties[k], parent_schema_property=property_data[k]}) | ||
local id = subproperties_res.id --subobject_id | local id = subproperties_res.id --subobject_id | ||
if (id ~= nil) then | if (id ~= nil) then | ||
Line 668: | Line 779: | ||
end | end | ||
local store_res = nil | local store_res = nil | ||
if (store) then | if (store) then | ||
properties['HasOswId'] = subjectId | |||
if (root) then | if (root) then | ||
if (debug) then mw.logObject("Store page properties") end | if (debug) then mw.logObject("Store page properties") end | ||
store_res = mw.smw.set( properties ) --store as semantic properties | store_res = mw.smw.set( properties ) --store as semantic properties | ||
else | else | ||
properties['@category'] = {} | |||
p.tableMerge(properties['@category'], jsondata[p.keys.category]) -- from json property 'type' | |||
properties['@category'] | p.tableMerge(properties['@category'], properties[p.keys.category_pseudoproperty]) -- from json-ld context 'Property:Category' | ||
properties[p.keys.category_pseudoproperty] = nil -- delete pseudo property | |||
if (jsondata[p.keys.name] ~= nil) then properties['Display title of'] = jsondata[p.keys.name] | if (jsondata[p.keys.name] ~= nil) then properties['Display title of'] = jsondata[p.keys.name] | ||
elseif (jsondata[p.keys.label] ~= nil and jsondata[p.keys.label][1] ~= nil) then properties['Display title of'] = p.splitString(jsondata[p.keys.label][1], '@')[1] | elseif (jsondata[p.keys.label] ~= nil and jsondata[p.keys.label][1] ~= nil) then properties['Display title of'] = p.splitString(jsondata[p.keys.label][1], '@')[1] | ||
else properties['Display title of'] = p.defaultArg(subschema['title'], "") end | else properties['Display title of'] = p.defaultArg(subschema['title'], "") end | ||
p.setNormalizedLabel(properties) --build normalized multilang label | |||
if (p.tableLength(properties) > 0) then | if (p.tableLength(properties) > 0) then | ||
store_res = mw.smw.subobject( properties, subobjectId ) --store as subobject | store_res = mw.smw.subobject( properties, subobjectId ) --store as subobject | ||
Line 796: | Line 909: | ||
if type(v) == "table" then | if type(v) == "table" then | ||
json[k] = p.expandJsonRef({json=v}).json | json[k] = p.expandJsonRef({json=v}).json | ||
end | |||
end | |||
local result = p.copy(json) | |||
for k,v in pairs(json) do | |||
if (k == "allOf") then | |||
if (type(v) == "table" and v[1] == nil) then v = {v} end -- ensure array | |||
for i,s in pairs(v) do | |||
result = p.tableMerge(s, result) | |||
if (debug) then mw.log("merge allOf with title " .. s["title"]) end | |||
end | |||
result[k] = nil | |||
end | end | ||
end | end | ||
return {json= | return {json=result} | ||
end | end | ||
function p.defaultArg(arg, default) | function p.defaultArg(arg, default) | ||
Line 879: | Line 1,005: | ||
--test: mw.logObject(p.tableMerge({"string", test1="test1", subtable1={"test"}}, {"string2", test1="test2", test3="test4"})) | --test: mw.logObject(p.tableMerge({"string", test1="test1", subtable1={"test"}}, {"string2", test1="test2", test3="test4"})) | ||
function p.tableMerge(t1, t2) | function p.tableMerge(t1, t2) | ||
if (t1 == nil) then t1 = {} elseif (type(t1) ~= 'table') then t1 = {t1} end | |||
if (t2 == nil) then t2 = {} elseif (type(t2) ~= 'table') then t2 = {t2} end | |||
for k,v in pairs(t2) do | for k,v in pairs(t2) do | ||
if type(v) == "table" then | if type(v) == "table" then | ||
Line 904: | Line 1,032: | ||
for k, v in pairs(obj) do res[p.copy(k, s)] = p.copy(v, s) end | for k, v in pairs(obj) do res[p.copy(k, s)] = p.copy(v, s) end | ||
return res | return res | ||
end | |||
-- build normalized multilang label | |||
function p.setNormalizedLabel(properties, use_fallbacks) | |||
if (use_fallbacks == nil) then use_fallbacks = true end | |||
if (properties['HasLabel'] ~= nil) then | |||
labels = properties['HasLabel'] | |||
if(type(labels) ~= 'table') then labels = {labels} end | |||
properties['HasNormalizedLabel'] = {} | |||
for i, label in ipairs(labels) do | |||
label_norm = p.splitString(label, '@')[1]:lower():gsub('[^%w]+','') | |||
label_lang = "en" | |||
if (p.splitString(label, '@')[2] ~= nil) then label_lang = p.splitString(label, '@')[2] end | |||
table.insert(properties['HasNormalizedLabel'], label_norm .. "@" .. label_lang) | |||
end | |||
elseif (use_fallbacks and properties['HasName'] ~= nil) then -- fallback, assume English lang | |||
labels = properties['HasName'] | |||
if(type(labels) ~= 'table') then labels = {labels} end | |||
properties['HasNormalizedLabel'] = {} | |||
for i, label in ipairs(labels) do | |||
label_norm = label:lower():gsub('[^%w]+','') | |||
table.insert(properties['HasNormalizedLabel'], label_norm .. "@en") | |||
end | |||
elseif (use_fallbacks and properties['Display title of'] ~= nil) then -- fallback, assume English lang | |||
labels = properties['Display title of'] | |||
if(type(labels) ~= 'table') then labels = {labels} end | |||
properties['HasNormalizedLabel'] = {} | |||
for i, label in ipairs(labels) do | |||
label_norm = label:lower():gsub('[^%w]+','') | |||
table.insert(properties['HasNormalizedLabel'], label_norm .. "@en") | |||
end | |||
end | |||
end | end | ||
return p | return p |
edits