Module:Page: Difference between revisions

From WikiMD's Wellness Encyclopedia

CSV import
 
No edit summary
 
Line 1: Line 1:
Module:Page
local callAssert = require('Module:CallAssert')


The `Module:Page` is a Lua module used in MediaWiki to facilitate the creation and management of pages within the wiki environment. This module is particularly useful for automating repetitive tasks, managing complex page structures, and enhancing the functionality of templates.
local function main(frame, field)
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
local makeTitle=args.makeTitle or pargs.makeTitle
local namespace=args.namespace or pargs.namespace or ""
local fragment=args.fragment or pargs.fragment or ""
local interwiki=args.interwiki or pargs.interwiki or ""
local page=args.page or args[1] or pargs.page or pargs[1] or ""
local id= tonumber( args.id or pargs.id )
local pn = {}
local title -- holds the result of the mw.title.xxx call


== Overview ==
for i = 1,9 do pn[i] = args['p'..i] or pargs['p'..i] end
if not id and not mw.ustring.match( page, '%S' ) then page = nil end


Lua is a powerful, efficient, lightweight, embeddable scripting language. It is used in MediaWiki to allow for more complex and efficient scripting than what is possible with wikitext alone. The `Module:Page` is one of the many modules that can be created and utilized within a MediaWiki installation to extend its capabilities.
if id then
title = callAssert(mw.title.new, 'mw.title.new', id)
elseif not page then
title = callAssert(mw.title.getCurrentTitle, 'getCurrentTitle')
elseif makeTitle then
title = callAssert(mw.title.makeTitle, 'makeTitle', namespace, page, fragment, interwiki)
else
title = callAssert(mw.title.new, 'mw.title.new', page, namespace)
end


== Functionality ==
local result = title[field]
if type(result) == "function" then
result = result(title, unpack(pn))
end


The `Module:Page` can be used to perform a variety of tasks, including:
return tostring(result or "")
end


* '''Dynamic Content Generation''': It can generate content dynamically based on certain conditions or inputs, which is particularly useful for pages that need to display different information based on user input or other variables.
-- handle all errors in main
main = require('Module:Protect')(main)


* '''Template Management''': The module can be used to manage and manipulate templates, allowing for more complex template logic and reducing the need for extensive wikitext code.
local p = {}


* '''Data Processing''': It can process data from various sources, such as other pages or external databases, and present it in a structured format.
-- main function does all the work
 
local meta = {}
* '''Automated Page Creation''': The module can automate the creation of pages based on predefined templates or data inputs, which is useful for maintaining consistency across similar pages.
function meta.__index(self, key)
 
return function(frame)
== Implementation ==
return main(frame, key)
end
end
setmetatable(p, meta)


To implement a `Module:Page`, a user with appropriate permissions must create a new module page in the MediaWiki namespace. The module is written in Lua and can be invoked from templates or directly from wikitext using the `#invoke` function.
function p.getContent(frame)
local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
local fmt = args.as or pargs.as or "pre"
local text = main(frame, "getContent")


Example:
fmt = mw.text.split( fmt, ", ?" )


```lua
for _, how in ipairs( fmt ) do
-- This is a simple example of a Lua module for MediaWiki
if how == "pre" then
local p = {}
text = table.concat{ "<pre>", text, "</pre>" }
elseif how == "expand" then
text = frame:preprocess(text)
elseif how == "nowiki" then
text = mw.text.nowiki(text)
end
end


function p.hello()
return text
    return "Hello, world!"
end
end


return p
return p
```
This module can be invoked in a wiki page as follows:
```wikitext
{{#invoke:Page|hello}}
```
== Advantages ==
* '''Efficiency''': Lua modules are generally more efficient than equivalent wikitext, as they are executed server-side and can handle complex logic more effectively.
* '''Reusability''': Code written in Lua modules can be reused across multiple pages and templates, reducing duplication and maintenance effort.
* '''Flexibility''': Lua provides a full programming language, allowing for more sophisticated logic and data manipulation than wikitext alone.
== Challenges ==
* '''Learning Curve''': Users need to learn Lua, which may be a barrier for those familiar only with wikitext.
* '''Debugging''': Debugging Lua code can be more complex than wikitext, requiring familiarity with both the language and the MediaWiki environment.
== Also see ==
* [[Module:Infobox]]
* [[Module:Navbox]]
* [[Template:Infobox]]
* [[Template:Navbox]]
* [[Help:Lua scripting]]
{{Lua}}
[[Category:MediaWiki modules]]
[[Category:Lua scripting]]

Latest revision as of 18:16, 17 December 2024

local callAssert = require('Module:CallAssert')

local function main(frame, field) local args, pargs = frame.args, ( frame:getParent() or {} ).args or {} local makeTitle=args.makeTitle or pargs.makeTitle local namespace=args.namespace or pargs.namespace or "" local fragment=args.fragment or pargs.fragment or "" local interwiki=args.interwiki or pargs.interwiki or "" local page=args.page or args[1] or pargs.page or pargs[1] or "" local id= tonumber( args.id or pargs.id ) local pn = {} local title -- holds the result of the mw.title.xxx call

for i = 1,9 do pn[i] = args['p'..i] or pargs['p'..i] end if not id and not mw.ustring.match( page, '%S' ) then page = nil end

if id then title = callAssert(mw.title.new, 'mw.title.new', id) elseif not page then title = callAssert(mw.title.getCurrentTitle, 'getCurrentTitle') elseif makeTitle then title = callAssert(mw.title.makeTitle, 'makeTitle', namespace, page, fragment, interwiki) else title = callAssert(mw.title.new, 'mw.title.new', page, namespace) end

local result = title[field] if type(result) == "function" then result = result(title, unpack(pn)) end

return tostring(result or "") end

-- handle all errors in main main = require('Module:Protect')(main)

local p = {}

-- main function does all the work local meta = {} function meta.__index(self, key) return function(frame) return main(frame, key) end end setmetatable(p, meta)

function p.getContent(frame) local args, pargs = frame.args, ( frame:getParent() or {} ).args or {} local fmt = args.as or pargs.as or "pre" local text = main(frame, "getContent")

fmt = mw.text.split( fmt, ", ?" )

for _, how in ipairs( fmt ) do if how == "pre" then

text = table.concat{ "

", text, "

" }

elseif how == "expand" then text = frame:preprocess(text) elseif how == "nowiki" then text = mw.text.nowiki(text) end end

return text end

return p