模組:Summon

萌娘百科,萬物皆可萌的百科全書!轉載請標註來源頁面的網頁連結,並聲明引自萌娘百科。內容不可商用。
貢獻者:
Template-info.svg 模塊文檔  [] [刷新]
  1. local module = {}
  2. local getArgs = require('Module:Arguments').getArgs
  3. local getCode = require('Module:GetPageCode')
  4. local wrappers = {
  5. 'Template:大召喚術',
  6. 'Template:大召喚術/群組'
  7. }
  8. -- 可匹配多個正則。
  9. mw.ustring.gsub_m = function(s, patterns, repl, n)
  10. local length = mw.ustring.len(s)
  11. local init = 1
  12. local v1, v2
  13. local i = 0
  14. local ss = {}
  15. while (n == nil or i < n) and init < length do
  16. v1, v2 = length + 1, length
  17. local p = nil
  18. for _, pattern in ipairs(patterns) do
  19. local v3, v4 = mw.ustring.find(s, pattern, init, false)
  20. if v3 ~= nil and (v3 < v1 or (v3 == v1 and v4 <= v2)) then
  21. v1, v2 = v3, v4 -- 更新匹配範圍。
  22. p = pattern -- 更新使用的正則。
  23. end
  24. end
  25. if p ~= nil then
  26. table.insert(ss, mw.ustring.sub(s, init, v1 - 1))
  27. local new_s = mw.ustring.gsub(mw.ustring.sub(s, v1, v2), p, repl, 1) -- 調用原生替換函數。
  28. table.insert(ss, new_s)
  29. else break
  30. end
  31. init = v2 + 1
  32. i = i + 1
  33. end
  34. table.insert(ss, mw.ustring.sub(s, init))
  35. return table.concat(ss)
  36. end
  37. -- 一個值是否在表裡。
  38. table.has = function(table, value)
  39. for _, item in ipairs(table) do
  40. if value == item then return true end
  41. end
  42. end
  43. function module._main(args, frame)
  44. local users = nil
  45. local users_show = {}
  46. local group = args['group'] or args['群組'] or args['群組'] or ''
  47. local filter = args['filter'] or args['篩選'] or args['篩選'] or ''
  48. local offset = tonumber(args['offset']) or 0
  49. local page = mw.title.new(group) or mw.title.new('Module:UserGroup') -- 過濾掉含有不能使用字符的標題。
  50. if page.exists then
  51. if page.contentModel == "Scribunto" then -- 頁面存在且頁面內容模型為Lua源碼,嘗試獲取枚舉接口。
  52. local success = nil
  53. success, users = pcall(function(p, f)
  54. local _mod = require(tostring(p))
  55. if type(_mod.enumerate) == 'function' then -- 接口正確。
  56. local _list = _mod.enumerate(f)
  57. if type(_list) == 'table' then -- 接口返回值正確。
  58. return _list
  59. end
  60. end
  61. return nil -- 接口錯誤,或接口返回值錯誤。
  62. end, page, filter)
  63. if not success then users = nil end -- 通過接口獲取返回值過程中發生錯誤而失敗。
  64. end
  65. page = tostring(page)
  66. else
  67. page = tostring(mw.title.makeTitle('Template', tostring(page))) -- 頁面不存在默認添加Template名字空間。
  68. end
  69. if users == nil then -- 若已通過接口獲取過Module頁面提供的用戶列表,則不對其代碼進行識別。
  70. users = {}
  71. local code = getCode(page)
  72. -- 構造正則列表。
  73. local templates = { 'User', 'Supu', 'Supuc' }
  74. local patterns = {
  75. '%[%[%s*[Uu][Ss][Ee][Rr]%s*:([^|%]/\\]+)%]%]', -- 匹配[[User:XXX]]。
  76. '%[%[%s*[Uu][Ss][Ee][Rr]%s*:([^|%]/\\]+)|.-%]%]', -- 匹配[[User:XXX|YYYY]]。
  77. }
  78. for _, template in ipairs(templates) do
  79. -- 將模板名中首位西文字母替換為正則表達式“[【大寫】【小寫】]”。
  80. template = mw.ustring.gsub(template, '^%a', function(letter)
  81. return '[' .. mw.ustring.upper(letter) .. mw.ustring.lower(letter) .. ']'
  82. end)
  83. table.insert(patterns, '{{%s*' .. template .. '%s*|([^|}/\\]+)}}') -- 匹配{{【模板名】:XXX}}模板。
  84. table.insert(patterns, '{{%s*' .. template .. '%s*|([^|}/\\]+)|.-}}') -- 匹配{{【模板名】:XXX|YYYY}}模板。
  85. end
  86. for _, alias in ipairs(mw.site.namespaces['User'].aliases) do
  87. -- 將別名中所有西文字母替換為正則表達式“[【大寫】【小寫】]”。
  88. alias = mw.ustring.gsub(alias, '%a', function(letter)
  89. return '[' .. mw.ustring.upper(letter) .. mw.ustring.lower(letter) .. ']'
  90. end)
  91. table.insert(patterns, '%[%[%s*' .. alias .. '%s*:([^|%]/\\]+)%]%]') -- 匹配[[【User名字空間的別名】:XXX]]。
  92. table.insert(patterns, '%[%[%s*' .. alias .. '%s*:([^|%]/\\]+)|.-%]%]') -- 匹配[[【User名字空間的別名】:XXX|YYYY]]。
  93. end
  94. mw.ustring.gsub_m(code, patterns, function(name)
  95. name = tostring(mw.title.new(name) or name) -- 標準化。
  96. if users[name] == nil then
  97. users[name] = true -- 標誌已存在這個用戶名。
  98. users[#users + 1] = name
  99. end
  100. end)
  101. end
  102. local except = args[1] or args['except'] or args['除去'] or ''
  103. for name in mw.text.gsplit(except, ',') do
  104. name = tostring(mw.title.new(name) or name) -- 標準化。
  105. users[name] = false -- 標誌除去這個用戶名。
  106. end
  107. for i = #users, 1, -1 do
  108. if users[users[i]] == false then
  109. table.remove(users, i)
  110. end
  111. end
  112. -- 由於表中無用的標誌會被expandTemplate識別為調用參數,因此需要清除,保留鍵為正整數的鍵值對。
  113. for k, _ in pairs(users) do
  114. if type(k) ~= "number" then users[k] = nil end
  115. end
  116. -- 分頁召喚群組成員
  117. local msg =""
  118. if offset ~= 0 then
  119. if (offset-1)*50 >= #users then
  120. msg = require('Module:Error').error('offset過大,無可召喚用戶!')
  121. elseif offset*50 <= #users then
  122. for i=(offset-1)*50+1,offset*50,1 do
  123. table.insert(users_show, users[i])
  124. end
  125. msg = string.format("(%d~%d)", (offset-1)*50+1, offset*50)
  126. else
  127. for i=(offset-1)*50+1,#users,1 do
  128. table.insert(users_show, users[i])
  129. end
  130. msg = string.format("(%d~%d)", (offset-1)*50+1, #users)
  131. end
  132. else
  133. if #users>50 then
  134. for i=1,50,1 do
  135. table.insert(users_show, users[i])
  136. msg = "(1~50)"
  137. end
  138. else
  139. for i=1,#users,1 do
  140. table.insert(users_show, users[i])
  141. end
  142. end
  143. end
  144. return msg .. frame:expandTemplate{ title = 'Reply to', args = users_show }
  145. end
  146. function module.main(frame)
  147. local args = getArgs(frame, {
  148. trim = true,
  149. removeBlanks = false,
  150. valueFunc = function(key, value)
  151. if value == '' then
  152. return nil
  153. else
  154. return value
  155. end
  156. end,
  157. wrappers = wrappers
  158. })
  159. if not mw.isSubsting() and (os.time() >= os.time({ year = 2020, month = 7, day = 22, hour = 16, minute = 0, second = 0 })) then
  160. local title = frame:getTitle()
  161. local caller = '調用模塊[['..title..'|'..mw.title.new(title).text..']]'
  162. local parent = frame:getParent()
  163. if parent then
  164. title = parent:getTitle()
  165. if table.has(wrappers, title) then
  166. caller = '使用'..frame:expandTemplate{ title = 'Tl', args = { mw.title.new(title).text } }
  167. end
  168. end
  169. local time = frame:callParserFunction{ name = '#time', args = { 'Y年n月j日(D)H:i:s', '2020-7-23' } }
  170. return require('Module:Error').error({ '自'..time..'(CST)起,'..caller..'時必須被[[Help:替換引用|替換引用]]。[[Category:有腳本錯誤的頁面]]' })
  171. else
  172. return module._main(args, frame)
  173. end
  174. end
  175. return module
此頁面最後編輯於 2022年7月14日 (週四) 11:09。
搜尋萌娘百科 (按"/"快速搜尋)
有新的未讀公告

阅读更多:模塊:Summon(http://mzh.moegirl.tw/%E6%A8%A1%E5%A1%8A%3ASummon )
本文引自萌娘百科(http://mzh.moegirl.tw ),文字内容默认使用《知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆》协议。