Module:PageUtil

local util_cargo = require("Module:CargoUtil") local util_map = require("Module:MapUtil") local util_table = require('Module:TableUtil') local util_text = require('Module:TextUtil') local util_title = require("Module:TitleUtil") local util_vars = require('Module:VarsUtil')

local p = {} local h = {}

function p.whatRedirectsHereAndSelf(title) title = title or mw.title.getCurrentTitle.text return h.runPagedataQuery(h.pagedataQuery(h.getWhatRedirectsHereWhere(title, false))) end

function p.whatRedirectsHere(title) title = title or mw.title.getCurrentTitle.text return h.runPagedataQuery(h.pagedataQuery(h.getWhatRedirectsHereWhere(title, true))) end

function p.getListOfPages(where) return h.runPagedataQuery(h.pagedataQuery(where)) end

function h.runPagedataQuery(query) return util_cargo.getOrderedList(query, '_pageName') end

function h.pagedataQuery(where) local ret = { tables = '_pageData', fields = '_pageData._pageName', where = where }	return ret end

function h.getWhatRedirectsHereWhere(title, excludeself) local tbl = { ('_pageNameOrRedirect="%s"'):format(title), excludeself and ('_isRedirect="1"') }	return util_cargo.concatWhere(tbl) end

function p.getSubpageList(title, nottag) local basepage = util_title.titleparts(title, 1, 1) return h.runPagedataQuery(h.getSubpageQuery(basepage,nottag)) end

function h.getSubpageQuery(basepage, nottag) local tbl = { tables = 'IgnorePagedata=IP', join = '_pageData._pageName=IP._pageName', where = h.getSubpageWhere(basepage, nottag), groupBy = '_pageData._pageName', }	local ret = util_cargo.concatQueriesAnd(h.pagedataQuery, tbl) return ret end

function h.getSubpageWhere(basepage, nottag) local tbl = { ('(_pageData._pageName LIKE "%s/%%" OR _pageData._pageName="%s")'):format(basepage, basepage), util_cargo.whereFromArg('(IP.TypeOfIgnore != "%s" OR IP.TypeOfIgnore IS NULL)', nottag) }	return util_cargo.concatWhere(tbl) end

function p.whichPagesExist(tbl) return p.getListOfPages(		util_table.concat(tbl, ' OR ', h.pageToWhere)	) end

function h.pageToWhere(str) return ('_pageName="%s"'):format(str) end

return p