跳转到内容

模組:Ilh/sandbox

维基百科,自由的百科全书

这是本页的一个历史版本,由Cwek留言 | 贡献2017年1月19日 (四) 02:13 (cool(笑))编辑。这可能和当前版本存在着巨大的差异。

local ilh = {}
local getArgs

local args
--local frameArgs
local clazz_pageExist_framehead='ilh-blue'
local TRA_CAT='[[Category:有蓝链却未移除内部链接助手模板的页面]]'

function ilh.main(frame)
    if not getArgs then
        getArgs = require('Module:Arguments').getArgs
    end
    args = getArgs(frame, {parentFirst=true})
    --frameArgs = getArgs(frame, {frameOnly=true})

    local context={}
    context["localPage"]=args[1]
    context["foreignPage"]=args[2]
    context["displayName"]=ilh.displayName(args)
    context["langCode"]=args["lang-code"]
    context["lang"]=args["lang"]
    context["nocat"]=(args["nocat"] and true) or false

    context["isMainPage"]=ilh.isMainPage()
    context["isExist"]=ilh.isExist(context["localPage"])

    local curPage_obj=mw.title.getCurrentTitle()
    context["isNoCatWithNamespace"]=curPage_obj:inNamespaces(2,828) --User,Module
    --context["curPageNamespace"]=curPage_obj.namespace

    return (context["isMainPage"] and ilh.onlyLink(context)) or ilh.functionLink(context)
end

function ilh.onlyLink(context)
    return ( context["isExist"] and mw.ustring.format( '[[%s|%s]]', context["localPage"], context["displayName"] ) ) or context["displayName"]
end

function ilh.functionLink(context)
    context["_localPage"]=mw.ustring.gsub(context["localPage"],'"','"')
    context["_foreignPage"]=mw.ustring.gsub(context["foreignPage"],'"','"')
    local need_cat=
                   (not context["nocat"])
                   and
                   (not context["isNoCatWithNamespace"])
                   --[[not (
                       context["curPageNamespace"]==2 --User
                    or context["curPageNamespace"]==828 --Module
                   )]]
    --mw.log(context["nocat"])
    --mw.log(context["curPageNamespace"])

    --output
    --all
    local node_all =mw.html.create('span')
    node_all
            :addClass('ilh-all')
            :attr('data-orig-title'   ,context["_localPage"]  )
            :attr('data-lang-code'    ,context["langCode"]    )
            :attr('data-lang-name'    ,context["lang"]        )
            :attr('data-foreign-title',context["_foreignPage"])
            :tag('span') --page
                :addClass('ilh-page')
                :wikitext(
                    mw.ustring.format('[[:%s|%s]]' ,
                            context["localPage"],context["displayName"]
                    )
                )
                :done()
    if not context["isExist"] then
        local node_comment=mw.html.create('span')
        node_comment
                    :addClass('noprint')
                    :addClass('ilh-comment')
                    :wikitext('(')
                    :tag('span') --lang
                        :addClass('ilh-lang')
                        :wikitext(context["lang"])
                        :done()
                    :tag('span') --colon
                        :addClass('ilh-colon')
                        :wikitext(':')
                        :done()
                    :tag('span') --link
                        :addClass('ilh-link')
                        :wikitext( '-{[[:',context["langCode"],':'
                                  ,(context["foreignPage"] or context["localPage"])
                                  ,'|'
                        )
                        :tag('span')
                           :attr('lang',context["langCode"])
                           :attr('dir','auto')
                           :wikitext(context["foreignPage"] or context["localPage"])
                           :done()
                        :wikitext(']]}-')
                        :done()
                    :wikitext(')')
        node_all:node(node_comment)
    else
        node_all:addClass(clazz_pageExist_framehead)
        if need_cat then
            node_all:wikitext(TRA_CAT)
        end
    end

    return tostring(node_all)
end

function ilh.displayName(args)
    local _d=args["d"]
    local _1=args["1"]
    local _3=args["3"]
    local dpN=_3 or _d
    return (dpN and {dpN} or {_1})[1]
end

--以下需要更高效的实现

--确定主页
--使用mw信息获得主页名
--使用language库获得本站默认语言代码(zh)来确定信息的对应语言,获得全主页名
---因为其他zh分语言只有名,没有命名空间,可以识别出来,但麻烦
--然后判断当前页和主页是否一致
---计划做重定向判断,但没需要
function ilh.isMainPage()
    local mainpage_msgobj=mw.message.new('Mainpage')
    mainpage_msgobj=mainpage_msgobj:inLanguage(mw.getContentLanguage():getCode())
    local mainPage_obj=mw.title.makeTitle(0,mainpage_msgobj:plain())
    local curpage_obj=mw.title.getCurrentTitle()
    --local curpage_redirectFrom_obj=curpage_obj.redirectTarget
    --[[if curpage_redirectFrom_obj ~=false then
        curpage_obj=curpage_redirectFrom_obj
    end]]
    return mw.title.equals(mainPage_obj,curpage_obj) --and curpage_obj.namespace==4
end

--确定页面存在
---exists是高开销方法,需要更高效的实现
--带保护的包装方法
--由于exists和解析器函数ifexist都是高开销方法
--而ifexist达到限制时默认返回结果为false的操作
--而exists会直接抛出错误而中断执行
--所以将相应调用包裹,压制exists的抛错,按照ifexist的理念,返回false
--正常情况下则一切正常
function ilh.isExist(pageName)
    local execStatus,result=pcall(ilh._isExist,pageName)
    return ( execStatus and result )
end
--真实方法
function ilh._isExist(pageName)
    local localPage_obj=mw.title.makeTitle(0,pageName)
    return localPage_obj.exists
end
--end

return ilh