モジュール:Category handler
このモジュールについての説明文ページを モジュール:Category handler/doc に作成できます
1 --------------------------------------------------------------------------------
2 -- --
3 -- CATEGORY HANDLER --
4 -- --
5 -- This module implements the {{category handler}} template in Lua, --
6 -- with a few improvements: all namespaces and all namespace aliases --
7 -- are supported, and namespace names are detected automatically for --
8 -- the local wiki. This module requires [[Module:Namespace detect]] --
9 -- and [[Module:Yesno]] to be available on the local wiki. It can be --
10 -- configured for different wikis by altering the values in --
11 -- [[Module:Category handler/config]], and pages can be blacklisted --
12 -- from categorisation by using [[Module:Category handler/blacklist]]. --
13 -- --
14 --------------------------------------------------------------------------------
15
16 -- Load required modules
17 local yesno = require('Module:Yesno')
18
19 -- Lazily load things we don't always need
20 local mShared, mappings
21
22 local p = {}
23
24 --------------------------------------------------------------------------------
25 -- Helper functions
26 --------------------------------------------------------------------------------
27
28 local function trimWhitespace(s, removeBlanks)
29 if type(s) ~= 'string' then
30 return s
31 end
32 s = s:match('^%s*(.-)%s*$')
33 if removeBlanks then
34 if s ~= '' then
35 return s
36 else
37 return nil
38 end
39 else
40 return s
41 end
42 end
43
44 --------------------------------------------------------------------------------
45 -- CategoryHandler class
46 --------------------------------------------------------------------------------
47
48 local CategoryHandler = {}
49 CategoryHandler.__index = CategoryHandler
50
51 function CategoryHandler.new(data, args)
52 local obj = setmetatable({ _data = data, _args = args }, CategoryHandler)
53
54 -- Set the title object
55 do
56 local pagename = obj:parameter('demopage')
57 local success, titleObj
58 if pagename then
59 success, titleObj = pcall(mw.title.new, pagename)
60 end
61 if success and titleObj then
62 obj.title = titleObj
63 if titleObj == mw.title.getCurrentTitle() then
64 obj._usesCurrentTitle = true
65 end
66 else
67 obj.title = mw.title.getCurrentTitle()
68 obj._usesCurrentTitle = true
69 end
70 end
71
72 -- Set suppression parameter values
73 for _, key in ipairs{'nocat', 'categories'} do
74 local value = obj:parameter(key)
75 value = trimWhitespace(value, true)
76 obj['_' .. key] = yesno(value)
77 end
78 do
79 local subpage = obj:parameter('subpage')
80 local category2 = obj:parameter('category2')
81 if type(subpage) == 'string' then
82 subpage = mw.ustring.lower(subpage)
83 end
84 if type(category2) == 'string' then
85 subpage = mw.ustring.lower(category2)
86 end
87 obj._subpage = trimWhitespace(subpage, true)
88 obj._category2 = trimWhitespace(category2) -- don't remove blank values
89 end
90 return obj
91 end
92
93 function CategoryHandler:parameter(key)
94 local parameterNames = self._data.parameters[key]
95 local pntype = type(parameterNames)
96 if pntype == 'string' or pntype == 'number' then
97 return self._args[parameterNames]
98 elseif pntype == 'table' then
99 for _, name in ipairs(parameterNames) do
100 local value = self._args[name]
101 if value ~= nil then
102 return value
103 end
104 end
105 return nil
106 else
107 error(string.format(
108 'invalid config key "%s"',
109 tostring(key)
110 ), 2)
111 end
112 end
113
114 function CategoryHandler:isSuppressedByArguments()
115 return
116 -- See if a category suppression argument has been set.
117 self._nocat == true
118 or self._categories == false
119 or (
120 self._category2
121 and self._category2 ~= self._data.category2Yes
122 and self._category2 ~= self._data.category2Negative
123 )
124
125 -- Check whether we are on a subpage, and see if categories are
126 -- suppressed based on our subpage status.
127 or self._subpage == self._data.subpageNo and self.title.isSubpage
128 or self._subpage == self._data.subpageOnly and not self.title.isSubpage
129 end
130
131 function CategoryHandler:shouldSkipBlacklistCheck()
132 -- Check whether the category suppression arguments indicate we
133 -- should skip the blacklist check.
134 return self._nocat == false
135 or self._categories == true
136 or self._category2 == self._data.category2Yes
137 end
138
139 function CategoryHandler:matchesBlacklist()
140 if self._usesCurrentTitle then
141 return self._data.currentTitleMatchesBlacklist
142 else
143 mShared = mShared or require('Module:Category handler/shared')
144 return mShared.matchesBlacklist(
145 self.title.prefixedText,
146 mw.loadData('Module:Category handler/blacklist')
147 )
148 end
149 end
150
151 function CategoryHandler:isSuppressed()
152 -- Find if categories are suppressed by either the arguments or by
153 -- matching the blacklist.
154 return self:isSuppressedByArguments()
155 or not self:shouldSkipBlacklistCheck() and self:matchesBlacklist()
156 end
157
158 function CategoryHandler:getNamespaceParameters()
159 if self._usesCurrentTitle then
160 return self._data.currentTitleNamespaceParameters
161 else
162 if not mappings then
163 mShared = mShared or require('Module:Category handler/shared')
164 mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData
165 end
166 return mShared.getNamespaceParameters(
167 self.title,
168 mappings
169 )
170 end
171 end
172
173 function CategoryHandler:namespaceParametersExist()
174 -- Find whether any namespace parameters have been specified.
175 -- We use the order "all" --> namespace params --> "other" as this is what
176 -- the old template did.
177 if self:parameter('all') then
178 return true
179 end
180 if not mappings then
181 mShared = mShared or require('Module:Category handler/shared')
182 mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData
183 end
184 for ns, params in pairs(mappings) do
185 for i, param in ipairs(params) do
186 if self._args[param] then
187 return true
188 end
189 end
190 end
191 if self:parameter('other') then
192 return true
193 end
194 return false
195 end
196
197 function CategoryHandler:getCategories()
198 local params = self:getNamespaceParameters()
199 local nsCategory
200 for i, param in ipairs(params) do
201 local value = self._args[param]
202 if value ~= nil then
203 nsCategory = value
204 break
205 end
206 end
207 if nsCategory ~= nil or self:namespaceParametersExist() then
208 -- Namespace parameters exist - advanced usage.
209 if nsCategory == nil then
210 nsCategory = self:parameter('other')
211 end
212 local ret = {self:parameter('all')}
213 local numParam = tonumber(nsCategory)
214 if numParam and numParam >= 1 and math.floor(numParam) == numParam then
215 -- nsCategory is an integer
216 ret[#ret + 1] = self._args[numParam]
217 else
218 ret[#ret + 1] = nsCategory
219 end
220 if #ret < 1 then
221 return nil
222 else
223 return table.concat(ret)
224 end
225 elseif self._data.defaultNamespaces[self.title.namespace] then
226 -- Namespace parameters don't exist, simple usage.
227 return self._args[1]
228 end
229 return nil
230 end
231
232 --------------------------------------------------------------------------------
233 -- Exports
234 --------------------------------------------------------------------------------
235
236 local p = {}
237
238 function p._exportClasses()
239 -- Used for testing purposes.
240 return {
241 CategoryHandler = CategoryHandler
242 }
243 end
244
245 function p._main(args, data)
246 data = data or mw.loadData('Module:Category handler/data')
247 local handler = CategoryHandler.new(data, args)
248 if handler:isSuppressed() then
249 return nil
250 end
251 return handler:getCategories()
252 end
253
254 function p.main(frame, data)
255 data = data or mw.loadData('Module:Category handler/data')
256 local args = require('Module:Arguments').getArgs(frame, {
257 wrappers = data.wrappers,
258 valueFunc = function (k, v)
259 v = trimWhitespace(v)
260 if type(k) == 'number' then
261 if v ~= '' then
262 return v
263 else
264 return nil
265 end
266 else
267 return v
268 end
269 end
270 })
271 return p._main(args, data)
272 end
273
274 return p