Jump to content

Module:Protected edit request

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 12:27, 30 September 2013 (start a replacement for Template:Edit protected). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}

local yesno = require('Module:Yesno')
local makeToolbar = require('Module:Toolbar').main

local function getTitleObject(...)
    local success, title = pcall(mw.title.new, ...)
    if success then
        return title
    end
end

local function getSubjectTitleObject(title)
    return getTitleObject(title.text, mw.site.namespaces[title.namespace].subject.id)
end

local function getSubpageObject(title, subpage)
    local success, obj = pcall(title.subPageTitle, title, subpage)
    if success then
        return obj
    end
end

local function makeUrlLink(title, query, display)
    return mw.ustring.format('[%s %s]', title:fullUrl(query), display)
end

local function makeWikilink(title, display)
    return mw.ustring.format('[[%s|%s]]', title.prefixedText, display)
end

local function makeWikilink(title, display)
    return mw.ustring.format('[[%s|%s]]', title.prefixedText, display)
end

local function makeViewLink(title, display)
    return makeUrlLink(title, {redirect = 'no'}, display)
end

local function makeEditLink(title, display)
    return makeUrlLink(title, {action = 'edit'}, display)
end

local function makeHistoryLink(title, display)
    return makeUrlLink(title, {action = 'history'}, display)
end

local function makeCompareLink(title1, title2, display)
    display = display or 'diff'
    local compareTitle = getTitleObject('Special:ComparePages')
    return makeUrlLink(compareTitle, {page1 = title1.prefixedText, page2 = title2.prefixedText}, display)
end

local function makeLogLink(title, logType, display)
    local logTitle = getTitleObject('Special:Log')
    return makeUrlLink(logTitle, {type = logType, page = title.prefixedText}, display)
end

local function urlEncodeTitle(title)
    return mw.uri.encode(title.prefixedText)
end

local function makeLinksList(title)
    local tbargs = {} -- The argument list to pass to Module:Toolbar
    tbargs.style = 'font-size: smaller;'
    tbargs.separator = 'dot'
    -- Page links.
    table.insert(tbargs, makeViewLink(title, 'view'))
    table.insert(tbargs, makeEditLink(title, 'edit'))
    table.insert(tbargs, makeHistoryLink(title, 'history'))
    -- Sandbox links.
    local sandboxTitle = getSubpageObject(title, 'sandbox')
    if sandboxTitle and sandboxTitle.exists then
        table.insert(tbargs, makeViewLink(sandboxTitle, 'sandbox'))
        table.insert(tbargs, makeEditLink(sandboxTitle, 'edit sandbox'))
        table.insert(tbargs, makeHistoryLink(sandboxTitle, 'sandbox history'))
        table.insert(tbargs, makeCompareLink(title, sandboxTitle, 'sandbox diff'))
    end
    -- Test cases links.
    local testcasesTitle = getSubpageObject(title, 'testcases')
    if testcasesTitle and testcasesTitle.exists then
        table.insert(tbargs, makeViewLink(testcasesTitle, 'test cases'))
    end
    -- Transclusion count link.
    if title.namespace == 10 or title.namespace == 828 then -- Only add the transclusion count link for templates and modules.
        table.insert(tbargs, mw.ustring.format('[http://toolserver.org/~jarry/templatecount/index.php?lang=en&name=%s#bottom transclusion count]', urlEncodeTitle(title)))
    end
    -- Protection log link.
    if title.namespace ~= 8 then -- MediaWiki pages don't have protection log entries.
        table.insert(tbargs, makeLogLink(title, 'protect', 'protection log'))
    end
    return makeToolbar(tbargs)
end

function p.main(frame)
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().origArgs
        for k, v in pairs(frame.origArgs) do
            origArgs = frame.origArgs
            break
        end
    else
        origArgs = frame
    end
    local args = {}
    for k, v in pairs(origArgs) do
        if type(v) == 'string' then
            v = mw.text.trim(v)
        end
        if v ~= '' then
            args[k] = v
        end
    end
    return p._main(args)
end

function p.test(frame)
    local title = mw.title.new(frame.args[1])
    return makeLinksList(title)
end

return p