(Imported by PageSync)
 
(Imported by PageSync)
Label: wsps-content-edit-tag
 
(Een tussenliggende versie door een andere gebruiker niet weergegeven)
Regel 175: Regel 175:


--[[
--[[
function p.getParentArg(frame)
function p.propValues(frame)


{{#invoke:CspFunctions|getParentArg|$pageData}}
frame args:
{{#invoke:CspFunctions|getParentArg|$classData}}
- property (text) name of a property, for example "Tag""
- query (optional: smw query) to limit pages for which property values are found, for example "[ [Class::Person] ]" (the additional space between brackets should be removed, it is used here to not break the lua comment)
- limit (optional: number) maximum number of most frequently used property values to return, defaults to 100
- resultFormat (optional: af) use "af" to return the results as an ArrayFunctions export, else defaults to comma-separated list
-- sorting: resultFormat "af" will have no sorting while comma-separated list will sort alphabetically
- sep (optional: text) optional separator to use between values, defaults to ","


This function loops through  parents using frame:getParent until the specified parameter
Example function calls:
is found in one of the parent frames or  the maximum number of 10 iterations is reached.
--]]
 
-- {{#invoke:CspFunctions|propValues|property=Class}}
debug console tests
-- {{#invoke:CspFunctions|propValues|property=Class|query=[[Class::!Class definition]] }}
=p.getParentArg(mw.getCurrentFrame():newChild{title="whatever",args={"a1","a2",["un"]="ua","a3"}})
-- {{#invoke:CspFunctions|propValues|property=Class|query=[[Class::!Class definition]]|limit=5 }}
=p.getParentArg(mw.getCurrentFrame():newChild{title="Template:SHA",args={"sha1","sha2",["ASH"]="SAH"}}:newChild{title="Module:Module",args={"a1","a2",["un"]="ua","a3"}})
--
=p.getParentArg(mw.getCurrentFrame():newChild{args={["test"]="123"}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={"test"}})
--[[
=p.getParentArg(mw.getCurrentFrame():newChild{args={["test"]="123"}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={"test"}})
debug console tests:
=p.getParentArg(mw.getCurrentFrame():newChild{args={["test"]="123"}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={}}:newChild{args={"test"}})
=p.propValues{["args"]={}}
=p.propValues{["args"]={["property"] = "XYZ"}}
=p.propValues{["args"]={["property"] = "Class"}}
=p.propValues{["args"]={["property"] = "Class",["resultFormat"]="af",["limit"]="5"}}
=p.propValues{["args"]={["property"] = "Class",["sep"]="_"}}
--]]
--]]
function p.getParentArg(frame)
function p.propValues(frame)
  local i = 1
-- get input parameters
  local result = nil
local parameters = {}
  local parent = frame
local property = frame.args["property"]
 
local query = frame.args["query"]
  while i <= 10 and result == nil and parent ~= nil do
local limit = frame.args["limit"]
    parent = parent:getParent()
if limit == nil or limit == "" then
    if parent ~= nil then
limit = 100
      result = parent.args[frame.args[1]]
else
      mw.log("i = " .. tostring(i) .. ", result = " .. tostring(result) )
limit = tonumber(limit)
    else
end
      mw.log("i = ".. tostring(i) .. ", no parent")
local resultFormat = frame.args["resultFormat"]
    end
local sep = frame.args["sep"] or ","
    i = i+1
local result
  end
 
-- build parameter table as required for mw.wikisearch.propValues
  if result == nil then result = "" end
if property == nil or property == "" then
 
return "<span style='color:red'>CspFunctions.propValues error: invalid property input</span>"
  return result
else
parameters["property"] = property
end
parameters["query"] = query
parameters["limit"] = limit
-- get results and process them to the desired resultformat
result = mw.wikisearch.propValues(parameters);
if resultFormat == "af" then
result = mw.af.export(result)
else
-- define new table with only the keys from the result table
local resultList = {}
for i,v in ipairs(result) do
table.insert(resultList,v["key"])
end
-- sort alphabetically
table.sort(resultList, function(a, b)
    return mw.ustring.toNFKD(string.lower(a)) < mw.ustring.toNFKD(string.lower(b))
end)
-- combine values in a string with separator
result = mw.text.listToText(resultList, sep, sep)
end
mw.logObject(result)
return result
end
end


Regel 260: Regel 294:
Examples:
Examples:


{{#invoke:CspFunctions|nowiki|1= ==Example==}}
{{#invoke:CspFunctions|nowiki|This text contains a bullet list:
{{#invoke:CspFunctions|nowiki|This text contains a bullet list:
* item 1
* item 1
Regel 266: Regel 301:
}}
}}


Note that when using unnamed parameters spaces and newlines will be included in the parameter value, for example:
"{{#invoke:CspFunctions|nowiki|A }}B" produces the output "A B"
"{{#invoke:CspFunctions|nowiki|1=A }}B" produces the output "AB"
--]]
--]]
function p.nowiki(frame)
function p.nowiki(frame)
ws-base-props
Regel 3: Regel 3:
|Title=Module:CspFunctions
|Title=Module:CspFunctions
|Version history={{Version history item  
|Version history={{Version history item  
|Version number=2.0
|Version description=Removed getParentArg function because it did not work and cannot be fixed. Added propValues function to allow calling of mw.wikisearch.propValues from an #invoke
|Version date=2024-11-08 13:31:46
}}{{Version history item
|Version number=1.2
|Version description=Added comment explaining potential issue with spaces when using nowiki function
|Version date=2024-10-18 07:15:17
}}{{Version history item
|Version number=1.1
|Version number=1.1
|Version description=Added nowiki function for passing text containing wikitext links to FlexForm inputs
|Version description=Added nowiki function for passing text containing wikitext links to FlexForm inputs
Regel 10: Regel 18:
|Version description=Added function p.gmatch which can be used to replace uses of the #regexall parserfunction
|Version description=Added function p.gmatch which can be used to replace uses of the #regexall parserfunction
|Version date=2023-8-18 12:38:53
|Version date=2023-8-18 12:38:53
}}
}}
}}
}}
}}
}}
}}
}}

Huidige versie van 24 mrt 2025 om 09:29

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:CspFunctions/doc

local p = {}

--[[
function p.getSlotData(page,slot)

This function returns a Lua table with structured content of the slot.

function arguments:
- page (optional): pagename of the page for which you want the slot content, defaults to current page
- slot (optional): slot of which you want the content, defaults to "main"

debug console tests:
p.getSlotData() 
p.getSlotData('Wiki:Testpagina sidebar code','ws-data') 
p.getSlotData('Template:Sidebar item','ws-class-props') 
--]]
function p.getSlotData(page,slot)
  	-- get page from function arguments or default to current page
  	if page == nil or page == '' then
    	page = mw.title.getCurrentTitle().fullText
    end
  	-- get slot from function arguments or default to main
  	if slot == nil or slot == '' then
    	slot = 'main'
    end
  
    -- get data from the slot (works for both json and wikitext slots)
    local slotData = {}
    slotData = mw.slots.slotData(slot,page)

    --mw.log('slotData = .. ')
   	--mw.logObject(slotData)
    return slotData
end

--[[
function p.afExportSlots(frame)

frame args:
slots 	(text)	comma-separated list of slots, defaults to "ws-class-props,ws-base-props,ws-data"
page 	(text)	defaults to current page

{{#invoke:CspFunctions|afExportSlots}}
{{#invoke:CspFunctions|afExportSlots|page=Template:Sidebar item|slots=ws-base-props,ws-class-props}}

debug console tests:
=p.afExportSlots(mw.getCurrentFrame():newChild{title="whatever",args={["page"]="Template:Sidebar item",["slots"]="ws-base-props,ws-class-props"}}) 
=p.afExportSlots(mw.getCurrentFrame():newChild{title="whatever",args={["addClassDefinition"]=""}}) 
--]]
function p.afExportSlots(frame)

-- get slots and page from frame args or set default value
local slots = frame.args["slots"]
if slots == nil or slots == "" then slots = "ws-class-props,ws-base-props,ws-data" end
local page = frame.args["page"]
if page == nil or page == "" then page = mw.title.getCurrentTitle().fullText end
  
-- get slot data for each slot and add to data table
local data = {}
data[1] = {}
for slot in string.gmatch(slots, '([^,]+)') do
    local slotData = p.getSlotData(page,slot)
    if slotData == nil then slotData = "" end 
    data[1][slot] = slotData
end
  
--mw.logObject(data) 
return mw.af.export(data)
end

--[[
function p.getSlotContent(frame)

{{#invoke:CspFunctions|getSlotContent|page=Template:Sidebar item|slot=ws-class-props}}
{{#invoke:CspFunctions|getSlotContent|page=Template:Sidebar item|slot=ws-class-props|nowiki}}

This function returns a string with the exact content of a slot.

frame arguments:
- page (optional): pagename of the page for which you want the slot content, defaults to current page
- slot (optional): slot of which you want the content, defaults to "main"
- 1 (optional: nowiki / preprocess): uses mw.text.nowiki or frame:preprocess on the content before returning it

debug console tests:
p.getSlotContent() 
p.getSlotContent{args={['page']='Wiki:Testpagina sidebar code',['slot']='ws-data'}}
p.getSlotContent{args={['page']='Template:Sidebar item',['slot']='ws-class-props'}}
--]]
function p.getSlotContent(frame)
  	-- get page from frame arguments or default to current page
    local page = frame.args["page"]
  	if page == nil or page == '' then
    	page = mw.title.getCurrentTitle().fullText
    end
  	-- get slot from frame arguments or default to main
    local slot = frame.args["slot"]
  	if slot == nil or slot == '' then
    	slot = 'main'
    end
  
    -- get data from the slot
    local slotContent = mw.slots.slotContent(slot,page)
    if slotContent == nil then slotContent = "" end
  
    local result = slotContent
    -- apply the nowiki or preprocess options if needed
    if frame.args[1] == "nowiki" then
    	result = mw.text.nowiki(slotContent)
    elseif frame.args[1] == "preprocess" then
    	result = frame:preprocess(slotContent)
    end
      
    --mw.log('result = .. ')
    --mw.log(result)
    return result
end

--[[
function p.ClassToClassDefinitionPage(frame)

This function returns the Class definition pagename based on a class input.
Example:

{{#invoke:CspFunctions|ClassToClassDefinitionPage|Person}}
returns "Wiki:Class definition/Person"

If you want to call this inside another Lua function, make sure to include the class
as a frame argument, for example:
p.ClassToClassDefinitionPage{args={"Person"}}
returns "Wiki:Class definition/Person"

frame args used:
1 	(text) class, for example "Person" or "Application page"

=p.ClassToClassDefinitionPage{args={"Person"}}
--]]
function p.ClassToClassDefinitionPage(frame)
  local class = frame.args[1]
  if class == "" or class == nil then
    return
  end
  local classDefinitionPage = "Wiki:Class definition/" .. class
  return classDefinitionPage
end

--[[
function p.preprocess(frame)

{{#invoke:CspFunctions|preprocess|_content=...}}
{{#invoke:CspFunctions|preprocess|_content={{#time:r|now}} }}
{{#invoke:CspFunctions|preprocess|_content={{{Intro|}}} {{#time:r|now}} |_args=parent args}}

frame args used:
_content	(wikitext) for example "{{#time:r|now}}"
_args 		(optional: "parent args") use this to add parent args to frame args, so that {{{Example|}}} 
			parameter calls inside the wikitext will work with parameters from a parent page.
			Note that parent args do not overwrite frame args if they have the same name.

debug console tests:
=p.preprocess(mw.getCurrentFrame():newChild{title="test",args={["_content"]="{{#time:r|now}}"}}) 
=p.preprocess(mw.getCurrentFrame():newChild{args={["Intro"]="The current time is: "}}:newChild{title="test",args={["_content"]="{{{Intro|}}} {{#time:r|now}}",["_args"]="parent args"}}) 
--]]
function p.preprocess(frame)
  local content = frame.args["_content"]
  if frame.args["_args"] == "parent args" then
    modifiedArgs = {}
    for key,value in pairs(frame:getParent().args) do modifiedArgs[key] = value end
    for key,value in pairs(frame.args) do modifiedArgs[key] = value end
    modifiedFrame = frame:newChild{args=modifiedArgs}
    return modifiedFrame:preprocess(content)
  else
    return frame:preprocess(content)
  end
end

--[[
function p.propValues(frame)

frame args:
- property (text) name of a property, for example "Tag""
- query (optional: smw query) to limit pages for which property values are found, for example "[ [Class::Person] ]" (the additional space between brackets should be removed, it is used here to not break the lua comment)
- limit (optional: number) maximum number of most frequently used property values to return, defaults to 100
- resultFormat (optional: af) use "af" to return the results as an ArrayFunctions export, else defaults to comma-separated list
-- sorting: resultFormat "af" will have no sorting while comma-separated list will sort alphabetically
- sep (optional: text) optional separator to use between values, defaults to "," 

Example function calls:
--]]
-- {{#invoke:CspFunctions|propValues|property=Class}}
-- {{#invoke:CspFunctions|propValues|property=Class|query=[[Class::!Class definition]] }}
-- {{#invoke:CspFunctions|propValues|property=Class|query=[[Class::!Class definition]]|limit=5 }}
--
--[[
debug console tests:
=p.propValues{["args"]={}}
=p.propValues{["args"]={["property"] = "XYZ"}}
=p.propValues{["args"]={["property"] = "Class"}}
=p.propValues{["args"]={["property"] = "Class",["resultFormat"]="af",["limit"]="5"}}
=p.propValues{["args"]={["property"] = "Class",["sep"]="_"}}
--]]
function p.propValues(frame)
	-- get input parameters
	local parameters = {}
	local property = frame.args["property"]
	local query = frame.args["query"]
	local limit = frame.args["limit"]
	if limit == nil or limit == "" then 
		limit = 100 
	else
		limit = tonumber(limit)
	end
	local resultFormat = frame.args["resultFormat"]
	local sep = frame.args["sep"] or ","
	local result
	
	-- build parameter table as required for mw.wikisearch.propValues
	if property == nil or property == "" then
		return "<span style='color:red'>CspFunctions.propValues error: invalid property input</span>"
	else
		parameters["property"] = property
	end
	parameters["query"] = query
	parameters["limit"] = limit
	
	-- get results and process them to the desired resultformat
	result = mw.wikisearch.propValues(parameters);
	if resultFormat == "af" then
		result = mw.af.export(result)
	else
		-- define new table with only the keys from the result table
		local resultList = {}
		for i,v in ipairs(result) do
			table.insert(resultList,v["key"])
		end
		-- sort alphabetically
		table.sort(resultList, function(a, b)
    	return mw.ustring.toNFKD(string.lower(a)) < mw.ustring.toNFKD(string.lower(b))
		end)
		-- combine values in a string with separator
		result = mw.text.listToText(resultList, sep, sep)
	end
	
	mw.logObject(result)
	return result
end

--[[
function p.gmatch(frame)

This function applies the pattern matching function mw.ustring.match to an input string and returns a string containing matched values (only unique values).

frame args used:
1 	input to which pattern matching will be applied
2 	pattern 
3 	optional separator, defaults to ","
4 	optional conjunction (separator between final 2 items), defaults to separator

examples use case:
{{#invoke:CspFunctions|gmatch|'sub-header sidebar' 'main sidebar'|([^ \'"]+)|,}} will return: sub-header,sidebar,main

debug console test:
=p.gmatch(mw.getCurrentFrame():newChild{args={"'sub-header sidebar' 'main sidebar'",'([^ \'"]+)',","}}) 

--]]
function p.gmatch(frame)
  local layoutAreas = frame.args[1]
  local pattern = frame.args[2]
  local separator = frame.args[3]
  local conjunction = frame.args[4]
  
  if separator == nil then separator = "" end
  if conjunction == nil then conjunction = separator end
  
  local resultTable = {}
  local duplicateCheck = {}
  for item in mw.ustring.gmatch(layoutAreas,pattern) do
    if duplicateCheck[item] == nil then
      table.insert(resultTable,item)
      duplicateCheck[item] = true
    end
  end
  
  return mw.text.listToText(resultTable,separator,conjunction)
end

--[[
function p.nowiki(frame)

This function applies mw.text.nowiki to input argument 1
and returns it. Can be useful when you want to display text
that contains wikitext formatting characters without them
being processed.

Examples:

{{#invoke:CspFunctions|nowiki|1= ==Example==}}
{{#invoke:CspFunctions|nowiki|This text contains a bullet list:
* item 1
* item 2
* item 3
}}

Note that when using unnamed parameters spaces and newlines will be included in the parameter value, for example:
"{{#invoke:CspFunctions|nowiki|A }}B" produces the output "A B"
"{{#invoke:CspFunctions|nowiki|1=A }}B" produces the output "AB"
--]]
function p.nowiki(frame)
  if frame.args[1] == nil then 
    return ""
  else 
    return mw.text.nowiki(frame.args[1])
  end
end

return p