259
edits
(import from wiki-dev) |
(Update package: OSW Core) |
||
(9 intermediate revisions by 3 users not shown) | |||
Line 69: | Line 69: | ||
end | end | ||
--mw.logObject(json) | --mw.logObject(json) | ||
p.cache[page_title][slot] = json | p.cache[page_title][slot] = json | ||
Line 144: | Line 106: | ||
if p.splitString(category, ':')[1] == "JsonSchema" then super_jsonschema = p.loadJson({title=category, slot=p.slots.main}).json | if p.splitString(category, ':')[1] == "JsonSchema" then super_jsonschema = p.loadJson({title=category, slot=p.slots.main}).json | ||
else super_jsonschema = p.loadJson({title=category, slot=p.slots.jsonschema}).json end | else super_jsonschema = p.loadJson({title=category, slot=p.slots.jsonschema}).json end | ||
if (super_jsonschema ~= nil) then | if (super_jsonschema ~= nil) then | ||
if (recursive) then | if (recursive) then | ||
Line 164: | Line 124: | ||
end | end | ||
end | end | ||
if (root) then | if (root) then | ||
for i, category in ipairs(visited) do | for i, category in ipairs(visited) do | ||
Line 339: | Line 294: | ||
if (not p.nilOrEmpty(jsondata[p.keys.category])) then categories = jsondata[p.keys.category] end -- let json property overwrite function param | if (not p.nilOrEmpty(jsondata[p.keys.category])) then categories = jsondata[p.keys.category] end -- let json property overwrite function param | ||
local schema_res = p.walkJsonSchema({jsonschema=jsonschema | 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}) | local expand_res = p.expandJsonRef({json=schema_res.jsonschema, debug=debug}) | ||
jsonschema = expand_res.json | jsonschema = expand_res.json | ||
--mw.log(mw.text.jsonEncode(jsonschema)) | --mw.log(mw.text.jsonEncode(jsonschema)) | ||
local jsonld = p.copy(jsondata) | local jsonld = p.copy(jsondata) | ||
local json_data_store = p.copy(jsondata) | local json_data_store = p.copy(jsondata) | ||
Line 349: | Line 305: | ||
json_res_store = p.expandEmbeddedTemplates({jsonschema=jsonschema, jsondata=json_data_store, mode='store'}) | json_res_store = p.expandEmbeddedTemplates({jsonschema=jsonschema, jsondata=json_data_store, mode='store'}) | ||
msg = msg .. json_res_store.debug_msg | msg = msg .. json_res_store.debug_msg | ||
mw.log("JSONDATA STORE") | --mw.log("JSONDATA STORE") | ||
mw.logObject(json_res_store.res) | --mw.logObject(json_res_store.res) | ||
local smw_res = nil | local smw_res = nil | ||
Line 378: | Line 334: | ||
end | end | ||
end | end | ||
wikitext = wikitext .. "<div class='jsonld-header' style='display:none' data-jsonld='" .. mw.text.jsonEncode( jsonld ):gsub("'","`") .. "'></div>" | |||
end | end | ||
Line 406: | Line 362: | ||
if j > i then | if j > i then | ||
local subjsonschema = schema_res.jsonschemas[subcategory] | local subjsonschema = schema_res.jsonschemas[subcategory] | ||
for k, v in pairs(subjsonschema['properties']) do | for k, v in pairs(p.defaultArg(subjsonschema['properties'], {})) do | ||
-- skip properties that are overwritten in subschemas, render them only once at the most specific position | -- skip properties that are overwritten in subschemas, render them only once at the most specific position | ||
ignore_properties[k] = true | ignore_properties[k] = true | ||
Line 425: | Line 381: | ||
p.tableMerge(set_categories_in_wikitext, json_res_store.res[p.keys.category]) -- categories from schema type | p.tableMerge(set_categories_in_wikitext, json_res_store.res[p.keys.category]) -- categories from schema type | ||
end | 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 | -- 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 | ||
local display_label = p.getDisplayLabel( | local display_label = p.getDisplayLabel(json_res_store.res, smw_res.properties) | ||
if title.nsText == "Property" then display_label = p.defaultArgPath( | if title.nsText == "Property" then display_label = p.defaultArgPath(json_res_store.res, {p.keys.name}, display_label) end | ||
if (debug) then msg = msg .. "Store page properties" end | if (debug) then msg = msg .. "Store page properties" end | ||
Line 480: | Line 434: | ||
local ignore_properties = p.defaultArg(args.ignore_properties, {}) | local ignore_properties = p.defaultArg(args.ignore_properties, {}) | ||
local schema_label = | local schema_label = p.renderMultilangValue({jsonschema=schema}) | ||
-- see also: https://help.fandom.com/wiki/Extension:Scribunto/HTML_Library_usage_notes | -- see also: https://help.fandom.com/wiki/Extension:Scribunto/HTML_Library_usage_notes | ||
Line 498: | Line 451: | ||
--mw.logObject(def) | --mw.logObject(def) | ||
local label = | local label = p.renderMultilangValue({jsonschema=def, default=k}) | ||
local description = | local description = p.renderMultilangValue({jsonschema=def, key="description"}) | ||
if (p.tableLength(p.defaultArgPath(property_definitions, {k, 'defined_in'}, {})) > 0) then description = description .. "<br>Definition: " 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 | for i, c in pairs(p.defaultArgPath(property_definitions, {k, 'defined_in'}, {})) do | ||
Line 547: | Line 488: | ||
elseif (type(v) == 'boolean') then | elseif (type(v) == 'boolean') then | ||
if (v) then v = "✅" else v = "❌" end -- green check mark or red cross | 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) and (string.find(e, "%[%[") == nil)) then -- no markup, no links | elseif (def['eval_template'] == nil and (string.len(e) > 100) and (string.find(e, "{{") == nil) and (string.find(e, "</") == nil) and (string.find(e, "%[%[") == nil)) then -- no markup, no links | ||
e = string.sub(e, 1, 100) .. "..."; -- limit infobox plain text to max 100 chars | e = string.sub(e, 1, 100) .. "..."; -- limit infobox plain text to max 100 chars | ||
elseif (debug) then | elseif (debug) then | ||
Line 574: | Line 515: | ||
elseif (type(v) == 'boolean') then | elseif (type(v) == 'boolean') then | ||
if (v) then v = "✅" else v = "❌" end -- green check mark or red cross | 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) and (string.find(v, "%[%[") == nil)) then -- no markup, no links | elseif (def['eval_template'] == nil and (string.len(v) > 100) and (string.find(v, "{{") == nil) and (string.find(v, "</") == nil) and (string.find(v, "%[%[") == nil)) then -- no markup, no links | ||
v = string.sub(v, 1, 100) .. "..."; -- limit infobox plain text to max 100 chars | v = string.sub(v, 1, 100) .. "..."; -- limit infobox plain text to max 100 chars | ||
elseif (debug) then | elseif (debug) then | ||
Line 1,110: | Line 1,051: | ||
else display_label = properties["HasName"] end | else display_label = properties["HasName"] end | ||
-- fall back to unmapped keywords | -- fall back to unmapped keywords | ||
elseif (jsondata[p.keys.label] ~= nil and jsondata[p.keys.label][1] ~= nil) then display_label = p.splitString(jsondata[p.keys.label][1], '@')[1] | elseif (jsondata[p.keys.label] ~= nil and jsondata[p.keys.label][1] ~= nil) then | ||
if type(jsondata[p.keys.label][1]) ~= 'table' then display_label = p.splitString(jsondata[p.keys.label][1], '@')[1] | |||
else display_label = jsondata[p.keys.label][1][p.keys.text] end -- no eval_template applied | |||
elseif (jsondata[p.keys.name] ~= nil) then display_label = jsondata[p.keys.name] | elseif (jsondata[p.keys.name] ~= nil) then display_label = jsondata[p.keys.name] | ||
end | end | ||
Line 1,148: | Line 1,091: | ||
end | end | ||
end | end | ||
end | |||
function p.renderMultilangValue(args) | |||
local jsondata = p.defaultArg(args.jsondata, {}) | |||
local jsonschema = p.defaultArg(args.jsonschema, {}) | |||
local key = p.defaultArg(args.key, "title") | |||
local result = p.defaultArg(args.default, "") | |||
local default = p.defaultArg(args.default, nil) | |||
-- "title*": {"de": ...} | |||
if jsonschema[key] ~= nil then result = jsonschema[key] end | |||
if jsonschema[key .. '*'] ~= nil then -- multilang label with switch | |||
result = "{{#switch:{{USERLANGUAGECODE}} |#default=" .. result | |||
for k,v in pairs(jsonschema[key .. '*']) do | |||
if k == en then default = v | |||
else result = result .. " |" .. k .. "=" .. v end | |||
end | |||
if default ~= nil then result = result .. " |#default=" .. default end | |||
result = result .. " }}" | |||
end | |||
-- "some_property": [{"lang": "de", "text": ...}] | |||
if jsondata[key] ~= nil then -- multilang label with switch | |||
result = "{{#switch:{{USERLANGUAGECODE}}" | |||
for k,v in pairs(jsondata[key]) do | |||
if v["lang"] ~= nil and v["text"] ~= nil then | |||
if v["lang"] == "en" then default = v["text"] | |||
else result = result .. " |" .. v["lang"] .. "=" .. v["text"] end | |||
end | |||
end | |||
if default ~= nil then result = result .. " |#default=" .. default end | |||
result = result .. " }}" | |||
end | |||
return result | |||
end | end | ||
return p | return p |