Module:MwJson: Difference between revisions

Update package: OSW Core
(import from wiki-dev)
(Update package: OSW Core)
 
(9 intermediate revisions by 3 users not shown)
Line 69: Line 69:
end
end
local generated_content = json["$defs"] and json["$defs"]["generated"]
if generated_content then
    -- Check if "$ref": "#/$defs/generated" exists directly in the table
    if json["$ref"] == "#/$defs/generated" then
        json = p.tableMerge(p.copy(generated_content), json)
        json["$ref"] = nil -- Remove the reference after merging
    end
    -- Check if "$ref": "#/$defs/generated" is contained in "allOf"
    if json["allOf"] then
        for _, item in ipairs(json["allOf"]) do
            if item["$ref"] == "#/$defs/generated" then
           
            -- Remove the reference after merging
                for i, v in ipairs(json["allOf"]) do
                    if v["$ref"] == "#/$defs/generated" then
                        table.remove(json["allOf"], i)
                        break
                    end
                end
                json = p.tableMerge(p.copy(generated_content), json)
               
                break
            end
        end
    end
    json["$defs"]["generated"] = nil
end
--[[local defs = p.defaultArgPath(json, {"$defs", "generated"}, nil)
if defs == nil then defs = p.defaultArgPath(json, {"definitions", "generated"}, nil) end
if defs ~= nil then
json = p.tableMerge(p.copy(defs), json)
json["allOf"] = p.tableMerge(json["allOf"], p.copy(defs["allOf"]))
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
mw.logObject("INSERT")
mw.logObject(super_jsonschema)
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 and p.tableLength(jsonschema) > 0) then
table.insert(visited, "_") -- dummy category for own schema
jsonschemas["_"] = p.copy(jsonschema)
templates["_"] = template
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, template=template, 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})
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>"
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(jsondata, smw_res.properties)
local display_label = p.getDisplayLabel(json_res_store.res, smw_res.properties)
if title.nsText == "Property" then display_label = p.defaultArgPath(jsondata, {p.keys.name}, display_label) end
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})
if schema['title'] ~= nil then schema_label = schema['title'] end
-- 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 = k
local label = p.renderMultilangValue({jsonschema=def, default=k})
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 = ""
local description = p.renderMultilangValue({jsonschema=def, key="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
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 = "&#x2705;" else v = "&#x274C;" end -- green check mark or red cross
if (v) then v = "&#x2705;" else v = "&#x274C;" 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 = "&#x2705;" else v = "&#x274C;" end -- green check mark or red cross
if (v) then v = "&#x2705;" else v = "&#x274C;" 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