Module:DPL

local table = require('Module:Table') local string = require('Module:String') local yesno = require('Module:Yesno')

local p = {} p.COUNT = 500 local frame = mw.getCurrentFrame

- -- function: .parse(t: table ) -- -- Takes a table of arguments and parses it through ``. - function p.parse(t) checkType(1, t, 'table') return frame:callParserFunction{ name='#dpl', args = table.unshift(t, '') } end

- -- function: .recurse(t: table ) -- -- Returns all DPL results concatenated. - function p.recurse(t) t.noresultsheader = t.noresultsheader or '' local offset = t.offset or 0 local buffer = {} local parsed = p.parse(t) if parsed == t.noresutltsheader then if t.noresultsheader == '' then return '' else return t.noresultsheader end end while parsed ~= t.noresultsheader do       table.push(buffer, parsed) offset = offset + p.COUNT t.offset = offset parsed = p.parse(t) end return table.concat(buffer) end

- -- function: .list(t: table ) -- -- Takes a table of arguments and returns the DPL results of the arguments passed. - function p.list(t) checkType(1, t, 'table') t.format = ',,%PAGE%#,' local content = p.recurse(t) local results = {} for page in content:gmatch("[^#]+") do       table.push(results, page) end return results end

- -- function: getSubpages(namespace: string, page: string, recurse?: boolean) -- -- Gets the subpages of a page. - function p.getSubpages(namespace, page, recurse) checkType(1, namespace, { 'string', 'number' }) if (type(page) == 'boolean' or not page) and namespace:match(':') then namespace, page = namespace:match('^(.-):(.+)$') end checkType(2, page, 'string') namespace = mw.site.namespaces[namespace] and mw.site.namespaces[namespace].name or formattedError('Invalid namespace %q', namepace) recurse = yesno(recurse, true) return p.list{ namespace=mw.site.namespaces[namespace].name, titleregexp=recurse and page..'/' or '^'..page..'/[^/]*$' } end

- -- function: getParent(page: string, parentPage?: string|number) -- -- Gets the parent pagename of a page. - function p.getParent(page, namespace, parentPage) local tpNamespace = type(namespace) if page:match(':') then parentPage = table.includes({ 'string', 'number', 'number' }, tpNamespace) and namespace or nil namespace = nil namespace, page = page:match('^(.-):(.+)$') end checkType(1, page, 'string') checkType(2, namespace, { 'string', 'number' }, true) checkType(3, parentPage, { 'string', 'number' }, true) namespace = mw.site.namespaces[namespace] and mw.site.namespaces[namespace].name or assertFalse(namespace, 'Invalid Namespace %q', 2, namespace) namespace = (namespace and namespace ~= ) and namespace..':' or  local function assembleTitleParts(parts, endIndex) return table.concat(table.slice(parts, 1, endIndex), '/') end local getRoot, partToGet, index local parts = string.split(page, '/') local tpParentPage = type(parentPage) if tpParentPage == 'boolean' then return parts[1] elseif tpParentPage == 'number' then parts[1] = namespace..parts[1] return assembleTitleParts(parts, #parts-parentPage) elseif tpParentPage == 'string' then return assembleTitleParts(parts, table.findIndex(parts, parentPage)) else return assembleTitleParts(parts, #parts-1) end end

return p