今天是2024年7月5日 第27周 星期五

代人,时大变了。

我们生活在大地上,但我们的梦想超越天空。

模块:BilibiliVideoIDValidator

来自Akarin
DrTompkins讨论 | 贡献2023年4月12日 (三) 12:17的版本 (导入1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索
50px 模块文档[创建]
return {
    validation = function(frame)
        local result = true
        local id = mw.text.trim(frame.args.id or '')
        if mw.ustring.match(id, '^%d+$') then
            -- 纯数字
            if not (string.sub(id, 1, 1) ~= '0' and string.len(id) <= 9) then -- 若id为纯数字时,当首位不为0且长度在1-9位时合法。
                result = false
            end
        elseif mw.ustring.match(id, '^[aA][vV]%d+$') then
            -- 若id为【av+纯数字】(不区分大小写)时,当且仅当纯数字部分首位不为0且长度在1-9位时合法,其余情况添加分类
            if not (string.sub(id, 3, 3) ~= '0' and string.len(id) <= 11) then
                result = false
            end
        elseif mw.ustring.match(id, '4.1.7..$') then
            if string.len(id) == 12 then
                -- 若id为【BV1】开头时,当且仅当id长度为12位,且除去【BV1】后的部分均不包含小写l大写I、大写O和数字0时合法。
                if not mw.ustring.match(id, '^[bB][vV]1[a-km-zA-HJ-NP-Z0-9]+$') then
                    result = false
                end
            elseif string.len(id) == 10 then
                -- 若id为【1】开头时,当且仅当id长度为10位,且除去【1】后的部分均不包含小写l大写I、大写O和数字0时合法。
                if not mw.ustring.match(id, '^1[a-km-zA-HJ-NP-Z0-9]+$') then
                    result = false
                end
            elseif string.len(id) == 9 then
                -- 若id不满足上述四种情况时,当且仅当id长度为9位,且不包含小写l大写I、大写O和数字0时合法。
                if not mw.ustring.match(id, '^[a-km-zA-HJ-NP-Z0-9]+$') then
                    result = false
                end
            else
                result = false
            end
        else
            result = false
        end

        if result then
            return ''
        else
            return frame:expandTemplate {title = 'ArticleCategory', args = {'Bilibili视频ID错误'}} ..
                    '<span class="BilibiliVideoIDValidatorArgument" style="display: none;">' ..
                        frame.args.id .. '</span>'
        end
    end
}