Difference between revisions of "Module:Purge"

From blackwiki
Jump to navigation Jump to search
test>Jo-Jo Eumerus
m (Changed protection level for "Template:Purge": Allowing template editors on some templates with full protection dating back to pre-TPROT times which aren't too often or too sensitively used. ([Edit=Require template editor access] (inde...)
test>Dinoguy1000
(from the sandbox: if the pagename is specified, don't add the title text or its span)
Line 1: Line 1:
{{#invoke:Purge|main}}<noinclude>
+
-- This module implements [[Template:Purge]].
{{documentation}}
+
 
</noinclude>
+
local p = {}
 +
 
 +
local function makeUrlLink(url, display)
 +
return string.format('[%s %s]', url, display)
 +
end
 +
 
 +
function p._main(args)
 +
-- Make the URL
 +
local url
 +
do
 +
local title
 +
if args.page then
 +
title = mw.title.new(args.page)
 +
if not title then
 +
error(string.format(
 +
"'%s' is not a valid page name",
 +
args.page
 +
), 2)
 +
end
 +
else
 +
title = mw.title.getCurrentTitle()
 +
end
 +
if args.anchor then
 +
title.fragment = args.anchor
 +
end
 +
url = title:fullUrl{action = 'purge'}
 +
end
 +
 +
-- Make the display
 +
local display
 +
if args.page then
 +
display = args[1] or 'Purge'
 +
else
 +
display = mw.html.create('span')
 +
display
 +
:attr('title', 'Purge this page')
 +
:wikitext(args[1] or 'Purge')
 +
display = tostring(display)
 +
end
 +
 +
-- Output the HTML
 +
local root = mw.html.create('span')
 +
root
 +
:addClass('noprint')
 +
:addClass('plainlinks')
 +
:addClass('purgelink')
 +
:wikitext(makeUrlLink(url, display))
 +
 +
return tostring(root)
 +
end
 +
 
 +
function p.main(frame)
 +
local args = frame:getParent().args
 +
return p._main(args)
 +
end
 +
 
 +
return p

Revision as of 16:20, 13 November 2017

This module produces a link to purge a page's cache. This is usually the current page, but can be another page.

Usage

From wiktext

From wikitext, this module should be accessed via Template:Purge. Please see the template page for documentation.

From Lua

From Lua, first you need to load the module:

local mPurge = require('Module:Purge')

Then you can create purge links with the _main function:

mPurge._main(args)

args is a table of arguments, identical to the ones accepted by Template:Purge. Please see the template documentation for more details.



-- This module implements [[Template:Purge]].

local p = {}

local function makeUrlLink(url, display)
	return string.format('[%s %s]', url, display)
end

function p._main(args)
	-- Make the URL
	local url
	do
		local title
		if args.page then
			title = mw.title.new(args.page)
			if not title then
				error(string.format(
					"'%s' is not a valid page name",
					args.page
				), 2)
			end
		else
			title = mw.title.getCurrentTitle()
		end
		if args.anchor then
			title.fragment = args.anchor
		end
		url = title:fullUrl{action = 'purge'}
	end
	
	-- Make the display
	local display
	if args.page then
		display = args[1] or 'Purge'
	else
		display = mw.html.create('span')
		display
			:attr('title', 'Purge this page')
			:wikitext(args[1] or 'Purge')
		display = tostring(display)
	end
	
	-- Output the HTML
	local root = mw.html.create('span')
	root
		:addClass('noprint')
		:addClass('plainlinks')
		:addClass('purgelink')
		:wikitext(makeUrlLink(url, display))
	
	return tostring(root)
end

function p.main(frame)
	local args = frame:getParent().args
	return p._main(args)
end

return p