モジュールの解説[表示] [編集] [履歴] [キャッシュを破棄]

Template:RM用のモジュール。

使い方

書式

{{#invoke:RM|Main}}

local yesno = require('Module:Yesno')
local noredirect = require('Module:Noredirect').noredirect

---Wrap a string by plainlinks
---@param str string
---@return string
local function plainlinks(str)
    return '<span class="plainlinks">' .. str .. '</span>'
end

---Get link to the history of a given page
---@param pagetitle string
---@return string
local function history(pagetitle)
    return plainlinks('[' .. tostring(mw.uri.fullUrl(pagetitle, {action = 'history'})) .. ' 履歴]')
end

---Get link to the log of a given page
---@param pagetitle string
---@return string
local function log(pagetitle)
    return plainlinks('[' .. tostring(mw.uri.fullUrl('Special:Log', {page = pagetitle})) .. ' 記録]')
end

---Get 'whatlinkshere' link
---@param pagetitle string
---@return string
local function linkshere(pagetitle)
    return '[[Special:WhatLinksHere/' .. pagetitle .. '|リンク元]]'
end

---Get 'move' link
---@param pagetitle string
---@param targettitle string
---@return string
local function move(pagetitle, targettitle)
    local settings = {
        wpReasonList = '[[Wikipedia:移動依頼|移動依頼]]による',
        wpNewTitle = targettitle
    }
    return plainlinks('[' .. tostring(mw.uri.fullUrl('Special:MovePage/' .. pagetitle, settings)) .. ' 移動]')
end

---Get 'delete' link
---@param pagetitle string
---@return string
local function delete(pagetitle)
    local settings = {
        action = 'delete',
        wpReason = '[[Wikipedia:移動依頼|移動依頼]]による事前削除'
    }
    return plainlinks('[' .. tostring(mw.uri.fullUrl(pagetitle, settings)) .. ' 削除]')
end

---Generate additional link
---@param linkall boolean|nil \'move' and 'delete' links if true, 'move' link if moveTo is provided, otherwise 'delete' link
---@param pagetitle string
---@param moveTo string|nil Must be string if linkall == true
---@return string
local function additionalLink(linkall, pagetitle, moveTo)
    if linkall then
        if type(moveTo) == 'string' then
            return move(pagetitle, moveTo) .. ' / ' .. delete(pagetitle)
        else
            error('TypeMismatch: moveTo must be string when linkall is true')
        end
    else
        if moveTo then
            return move(pagetitle, moveTo)
        else
            return delete(pagetitle)
        end
    end
end

---Create links to display
---@param pagetitle string Page title with which the main link should be associated
---@param bold boolean Whether to boldface the main link
---@param moveTo string? Generates link to Special:MovePage if provided, or to Special:DeletePage if not
---@param linkall boolean? If true, generates link to both Special:MovePage and Special:DeletePage
---@return string
local function links(pagetitle, bold, moveTo, linkall)

    local title = mw.title.new(pagetitle)
    local thisTitle, relatedTitle, relatedTitleType
    if title.isTalkPage then
        thisTitle = title.talkPageTitle.prefixedText
        relatedTitle = title.subjectPageTitle.prefixedText
        relatedTitleType = 'メイン'
    else
        thisTitle = title.subjectPageTitle.prefixedText
        relatedTitle = title.talkPageTitle.prefixedText
        relatedTitleType = 'ノート'
    end

    local moveToThisTitle, moveToRelatedTitle
    if moveTo then
        local moveToTitle = mw.title.new(moveTo)
        if moveToTitle.isTalkPage then
            moveToThisTitle = moveToTitle.talkPageTitle.prefixedText
            moveToRelatedTitle = moveToTitle.subjectPageTitle.prefixedText
        else
            moveToThisTitle = moveToTitle.subjectPageTitle.prefixedText
            moveToRelatedTitle = moveToTitle.talkPageTitle.prefixedText
        end
    end

    -- Primary link
    local primary = noredirect(thisTitle)
    if bold then
        primary = '<b>' .. primary .. '</b>'
    end

    -- Secondary links
    local thisTitleLinks = table.concat({history(thisTitle), log(thisTitle), linkshere(thisTitle), additionalLink(linkall, thisTitle, moveToThisTitle)}, ' / ') .. ' |'
    local relatedTitleLinks = ' ' .. noredirect(relatedTitle, relatedTitleType) .. ': '
    relatedTitleLinks = relatedTitleLinks .. table.concat({history(relatedTitle), log(relatedTitle), linkshere(relatedTitle), additionalLink(linkall, relatedTitle, moveToRelatedTitle)}, ' / ')
    local secondary = ' <small>(' .. thisTitleLinks .. relatedTitleLinks .. ')</small>'

    return primary .. secondary

end

-- Main package function
local p = {}

function p.Main(frame)

    local args = frame.args
    local source = args.source
    local goal = args.goal

    -- Are the neccesary fields filled?
    if source == nil or source == '' and goal == nil or goal == '' then
        error('移動元・移動先が指定されていません')
    elseif source == nil or source == '' then
        error('移動元が指定されていません')
    elseif goal == nil or goal == '' then
        error('移動先が指定されていません')
    end

    -- Create anchor for the request
    local anchor = '<span id="RM' .. source .. '"/>'

    -- Return links
    local swap = yesno(args.swap) -- Whether this is for swapping move
    if swap then
        return anchor .. links(source, true, goal, true) .. ' ⇔ ' .. links(goal, true, source, true)
    else
        return anchor .. links(source, false, goal) .. ' → ' .. links(goal, true)
    end

end

return p