Fandom supplies a library of convenience functions for typical wiki operations.
Wikitext library
This library was originally developed for the Lua InfoboxBuilder's High Frequency functions. Due to the cumbersome name, it is suggested that frequently used functions be stored as a local table as in local HF = mw.InfoboxBuilderHF.
mw.InfoboxBuilderHF.explode
mw.InfoboxBuilderHF.explode( sep, text )
Similar to mw.text.split
mw.InfoboxBuilderHF.isempty
mw.InfoboxBuilderHF.isempty( s )
Returns "true" if the object supplied is nil, an empty string, or a table with no objects.
mw.InfoboxBuilderHF.firstToUpper
mw.InfoboxBuilderHF.firstToUpper( str )
Applies string.upper to the first character of a string.
mw.InfoboxBuilderHF.round
mw.InfoboxBuilderHF.round( num, idp )
mw.InfoboxBuilderHF.trim
mw.InfoboxBuilderHF.trim( s )
mw.InfoboxBuilderHF.unique
mw.InfoboxBuilderHF.unique( raw_table )
De-duplicates the items in raw_table and returns them as a table.
mw.InfoboxBuilderHF.Link
mw.InfoboxBuilderHF.Link( link, text )
Returns a wikitext internal link / "wiki-link" to the target page link.
If text is supplied, it will be used as a text label for the link.
mw.InfoboxBuilderHF.Link_safe
mw.InfoboxBuilderHF.Link_safe( object )
Returns a wikitext internal link / "wiki-link" to the target page object if and only if the target page exists. If the target page does not exist, returns object as an unlinked plain text string.
If object supplied is already a wikitext link, it will pass through as-is without checking for page existence.
mw.InfoboxBuilderHF.ExternalLink
mw.InfoboxBuilderHF.ExternalLink( target, label, plain )
Returns a wikitext external link to a URL in target.
If label is supplied, it will be used as a text label for the link.
If plain is supplied, the link will be enveloped in a span.plainlinks.
mw.InfoboxBuilderHF.Category
mw.InfoboxBuilderHF.Category( category, sortkey )
Adds the invoking page to a MediaWiki Category.
If sortkey is supplied, it will be used as a sort key for the association.
It is not needed to prefix the category with "Category:".
mw.InfoboxBuilderHF.LinkToCategory
mw.InfoboxBuilderHF.LinkToCategory( category, label )
Returns a link to a MediaWiki category.
If label is supplied, it will be used as a text label for the link.
It is not needed to prefix the category with "Category:".
mw.InfoboxBuilderHF.CategoryLink
mw.InfoboxBuilderHF.CategoryLink( category, sortkey, label )
Adds the invoking page to a MediaWiki Category.
If sortkey is supplied, it will be used as a sort key for the association.
If label is supplied, it will also return an internal link to the Category page using the value provided as a link label.
It is not needed to prefix the category with "Category:".
mw.InfoboxBuilderHF.LinkToCategory_safe
mw.InfoboxBuilderHF.LinkToCategory_safe( object )
Returns a wikitext internal link to the target Category page object if and only if the target Category page exists (and has content). If the target page does not exist, returns object as an unlinked plain text string.
It is not needed to prefix the category with "Category:".
This function increments the expensive parser function count.
mw.InfoboxBuilderHF.KillNewCategories
mw.InfoboxBuilderHF.KillNewCategories( object )
Adds the invoking page to a MediaWiki Category object if and only if the target Category page exists (and has content). If the target page does not exist, returns object as an unlinked plain text string.
It is not needed to prefix the category with "Category:".
local HF = {}
function HF.explode( sep, text )
local sep, fields = sep or "::", {}
local pattern = string.format("([^%s]+)", sep)
text:gsub( pattern, function( c ) fields[#fields+1] = c end )
return fields
end
function HF.isempty(s)
local result = false
if type( s ) == "nil" then
result = true
elseif type( s ) == "string" then
if s == "" then
result = true
end
elseif type( s ) == "table" then
if next( s ) == nil then
result = true
end
end
return result
end
function HF.firstToUpper( str )
return ( str:gsub( "^%l", string.upper ) )
end
function HF.round(num, idp)
local mult = 10^(idp or 0)
return math.floor( num * mult + 0.5 ) / mult
end
-- eliminates whitespace from the front and back of a string
function HF.trim(s)
if type(s) == 'string' then
return (s:gsub("^%s*(.-)%s*$", '%1'))
else
return false
end
end
-- This creates an external link.
function HF.ExternalLink( target, label, plain )
local output = string.format('[%s %s]', target, label)
if plain == true then
output = string.format('<span class="plainlinks">%s</span>', output)
end
return output
end
-- This creates a link to a category, as well as placing it in that category.
-- `sortkey` and `label` are optional
-- If there's no `label` given, it will only place it in the category,
-- which is what HF.Category is for.
function HF.CategoryLink( category, sortkey, label )
if not HF.isempty( label ) then
return HF.LinkToCategory( category, label ) ..
HF.Category( category, sortkey )
else
return HF.Category( category, sortkey )
end
end
-- Adds a Category
-- `sortkey` is optional
function HF.Category( category, sortkey )
if sortkey == nil then sortkey = '' else sortkey = '|' .. sortkey end
return string.format('%s' .. mw.site.namespaces[14].canonicalName .. ':%s%s]]', '[[', category, sortkey)
end
-- Adds a link to a Category
function HF.LinkToCategory( category, label )
local category_namespace_name = mw.site.namespaces[14].canonicalName
return string.format('%s'..category_namespace_name..':%s|%s]]', '[[:', category,
label or category_namespace_name..':' .. category )
end
-- Adds an internal link
-- `label` is optional
function HF.Link( link, text )
if not HF.isempty( text ) then
return string.format('%s'..'%s|%s]]', '[[', link, text)
else
return string.format('%s'..'%s]]', '[[', link)
end
end
-- Checks to see if there's an existing article at the target.
-- If there is, creates a link.
-- If there's not, only write the target's name as text.
-- If it is passed a link, it will not perform this check and will only write the link.
function HF.Link_safe( object )
if object:match("^%[%[(.*)%]%]$") then
return object
elseif mw.title.new( object ).exists == true then
return HF.Link( object )
else
return object
end
end
-- Checks to see if there's an existing category at the target.
-- If there is, creates a link.
-- If there's not, only write the target's name as text.
function HF.LinkToCategory_safe( object )
if mw.title.new( object, 14 ).exists == true then
return HF.Link( ':'..mw.site.namespaces[14].canonicalName..':'..object, object )
else
return object
end
end
-- Checks to see if there's an existing category at the target.
-- If there is, creates a link.
-- If there's not, only write the target's name as text.
function HF.KillNewCategories( object )
if mw.title.new( object, 14 ).exists == true then
return HF.Category( object, object, object)
else
return object
end
end
-- Unique table items
function HF.unique( raw_table )
local hash = {}
local unique_results = {}
for _,v in ipairs( raw_table ) do
if (not hash[v] and v ~= '') then
table.insert( unique_results, mw.text.trim(v) )
hash[v] = true
end
end
return unique_results
end