FANDOM


--[=[
{{Helper module
|name = DPL scroll
|fname1 = _main(args)
|ftype1 = <code>table</code>
|fuse1 = Generates pagination for usage in DPL headers; see [[Template:Extension DPL scroll]] for template version. Table of attributes may include:
* <code>total</code> - The total number of results
* <code>count</code> - The number of results per page
* <code>offset</code> - The current offset
* <code>query</code> - Additional URL query to add; either:
** a table of param = value primitive pairs (both strings or numbers; don't nest tables) - the better option for when using this in another module
** a string of form param1=value1&value2=param2...etc - can (but doesn't need to) start with & or ?
Returns boolean, html where the boolean is true if there is more than one page, false otherwise, and a mw.html object of a span containing the html of the page links (or the empty string if one/no page).
}}
--]=]
--<nowiki>
local p = {}
local dt = require('Module:Paramtest').default_to
 
-- defaults for number
local function get_num(v1, v2)
	return tonumber(dt(v1, v2 or 0)) or 0
end
 
-- actually make the pages
-- args: total, count, offset, query
local function make_pages(a)
	local total = get_num(a.total) -- total results
	local count = get_num(a.count, 500) -- results per page
	local offset = get_num(a.offset) -- starting point
	local pages = math.ceil(total / count) -- total number of pages
	local currpage = math.ceil(offset / count) -- current page, starting at 0
	local css = {['padding-right'] = '24px'}
 
	local query = a.query or {}
	query.DPL_offset = 0
	query.action = 'purge'
 
	-- one (or no) page, don't do anything else
	if pages < 2 then
		return false, ''
	end
 
	local title = mw.title.getCurrentTitle()
	local span = mw.html.create('span')
	span:addClass('dpl-pagination')
	if currpage == 0 then
		-- first page, have first interval bold
		span:tag('span'):css(css):wikitext(string.format("'''1..%s''' ", count)):done()
	elseif currpage == 1 then
		-- second page, first interval linked, second bolded
		span:tag('span'):css(css)
			:wikitext(string.format("[%s 1..%s]", title:fullUrl(query), count)) -- offset=0
		:done()
 
		span:tag('span'):css(css)
			:wikitext(string.format("'''%s..%s''' ", count + 1, math.min(count * 2, total)))
		:done()
	else
		-- third and onwards, start link, prev link, bolded current
		span:tag('span'):css(css)
			:wikitext(string.format("[%s Start] ", title:fullUrl(query))) -- offset=0
		:done()
 
		query.DPL_offset = (currpage - 1) * count
 
		span:tag('span'):css(css)
			:wikitext(string.format("[%s %s..%s] ", title:fullUrl(query), query.DPL_offset + 1, query.DPL_offset + count)) -- offset=prev
		:done()
 
		span:tag('span'):css(css)
			:wikitext(string.format("'''%s..%s''' ", query.DPL_offset + count + 1, math.min(query.DPL_offset + count * 2, total)))
		:done()
	end
 
	if currpage < pages - 1 then
		-- at least another page left, so add next page link
		query.DPL_offset = (currpage + 1) * count
 
		span:tag('span'):css(css)
			:wikitext(string.format("[%s %s..%s]", title:fullUrl(query), query.DPL_offset + 1, math.min(query.DPL_offset + count, total))) -- offset=next
		:done()
 
		if currpage < pages - 2 then
			-- at least 2 pages left, so add 'end' link
			query.DPL_offset = (pages - 1) * count
 
			span:tag('span'):css(css)
				:wikitext(string.format("[%s End]", title:fullUrl(query))) -- offset=end
			:done()
		end
	end
 
	return true, span
end
 
 
 
--[=[ 
---- module access point ----
args:
	total: total number of results; default 0
	count: number of results per page; default 500 (as with dpl itself)
	offset: beginning of this page of results; default 0
	query: optional additional query, which can be either:
		a string of form param1=value1&value2=param2...etc - can (but doesn't need to) start with &
		a table of param = value primitive pairs (both strings or numbers; don't nest tables) - the better option for when using this in another module
returns: boolean, span
	boolean: true if there is more than one page, false if there is only one page
	span: mw.html object of span containing page navigation (or empty string)
--]=]
function p._main(args)
	local a = {}
	a.total = args.total or 0
	a.count = args.count or 500
	a.offset = args.offset or 0
	a.query = {}
 
	if type(args.query) == 'table' then
		a.query = args.query
	elseif type(args.query) == 'string' then
		a.query = mw.uri.parseQueryString(args.query)
	end
 
	return make_pages(a)
end
 
--[=[
---- template access point ----
args:
	as above
	obviously a table for query cannot be passed via template
--]=]
function p.main(frame)
	local b,ret = p._main(frame:getParent().args)
	return ret
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.