コンテンツにスキップ

モジュール:Article stub box

提供:KANOTYPE WIKI

このモジュールについての説明文ページを モジュール:Article stub box/doc に作成できます

  1 --[[
  2 This module was created by User:CodeHydro (Alexander Zhikun He).
  3 User:Jackmcbarn and User:Mr._Stradivarius provided a great deal of assistance in writting p.main()
  4 
  5 p.main() draw heavily from the following version of Template:Asbox of the English Wikipedia, authored primarily by User:Rich_Farmbrough
  6 https://en.wikipedia.org/w/index.php?title=Template:Asbox&oldid=619510287
  7 
  8 p.templatepage() is derived from the following revision of Template:Asbox/templatepage, authored primarily by User:MSGJ
  9 https://en.wikipedia.org/w/index.php?title=Template:Asbox/templatepage&oldid=632914791
 10 
 11 Both templates had significant contributions from numerous others listed in the revision history tab of their respective pages.
 12 --]]
 13 local WRAPPER_TEMPLATE, args = 'Template:Article stub box'
 14 -- TODO: update this instance of Asbox to Article stub box eventually
 15 local templatestyles = 'Module:Article stub box/styles.css'
 16 local p, Buffer, stubCats = {
 17 	--Prevents dupli-cats... get it? Maybe not?
 18 	cats = setmetatable({}, {__newindex = function(t, i, v)
 19 		if not rawget(t, i) then
 20 			rawset(t, i, v)
 21 			table.insert(t, i)
 22 		end
 23 	end}),
 24 	--initializes variables required by both p.main and p.templatepage
 25 	init = function(self, frame, page)
 26 		args, page = args or require('Module:Arguments').getArgs(frame, {
 27 			wrappers = WRAPPER_TEMPLATE
 28 		}), page or mw.title.getCurrentTitle()
 29 		--Ensures demo parameter will never affect category() output for articles
 30 		self.demo = self.demo or page.namespace ~= 0 and args.demo
 31 		return args, page
 32 	end
 33 }, require('Module:Buffer')
 34 
 35 --[[
 36 Formats category links. Stores them until called with cat.done=true
 37 Takes multiple or single categories in the form of 'cat'
 38 or a table of strings and/or tables containing parts. (See below)
 39 ]]
 40 local attention, catTag, catKey = Buffer'Stub message templates needing attention', '[[Category:%s]]', '%s|%s%s'
 41 local function category(cat)
 42 	for _, v in ipairs((tostring(cat) == cat or cat.t) and {cat} or cat) do
 43 		--[[
 44 		If v is a table:
 45 			[1] = full category name; defaults to local attention if blank
 46 			k = Category sort key. Prefix before v.t
 47 			t = page.text or args.tempsort#; appended after k (or in its place if omitted). Required if v is not a string
 48 		Basically the same as v = (v[1] or attention) .. ' | ' .. (v.k or '') .. v.t
 49 		]]
 50 		if v and v ~= true then--reject v = nil, false, or true
 51 			p.cats[catTag:format(tostring(v) == v and
 52 				v
 53 				or (v[1] and Buffer(v[1]) or attention):_in(v.k):_(v.t):_str(2, nil, nil, '|')
 54 			)] = true
 55 		end
 56 	end
 57 	return cat.done and table.concat(p.cats, p.demo and ' | ' or nil) or ''
 58 end
 59 
 60 --[[
 61 Makes an ombox warning;
 62 Takes table {ifNot = Boolean, text, {cat. sort key, cat. sort name}}
 63 Will return an empty string instead when ifNot evaluates to true 
 64 ]]
 65 local function ombox(v)
 66 	if v.ifNot then return end
 67 	p.ombox = p.ombox or require('Module:Message box').ombox
 68 	category{v[2]}
 69 	return p.ombox{
 70 		type = 'content',
 71 		text = v[1]
 72 	}
 73 end
 74 
 75 --[[
 76 Unlike original template, module now takes unlimited cats! This function also performs
 77 most stub category error checks except for the ombox for when main |category= is omitted (See p.template())
 78 ]]
 79 local function catStub(page, pageDoc)
 80 	stubCats = {missing = {}, v = {}}
 81 	-- zwj and zwnj have semantical use in other other wikis, don't remove them
 82 	local zwj = '\226\128\141'  -- U+200D, E2 80 8D
 83 	local zwnj = '\226\128\140' -- U+200C, E2 80 8C
 84 	local disallowedUnicodeChars = '[^%w%p%s' .. zwj .. zwnj .. ']' -- for i18n we make this a separate string
 85 	local code
 86 	for k, _ in pairs(args) do
 87 		--Find category parameters and store the number (main cat = '')
 88 		table.insert(stubCats, string.match(k, '^category(%d*)$'))
 89 	end
 90 	table.sort(stubCats)
 91 	for k, v in ipairs(stubCats) do
 92 		--Get category names and, if called by p.templatepage, the optional sort key
 93 		local tsort, cat = args['tempsort' .. v], mw.ustring.gsub(args['category' .. v], disallowedUnicodeChars, '')--remove all hidden unicode chars 
 94 		--Do not place template in main category if |tempsort = 'no'. However, DO place articles of that template in the main category.
 95 		table.insert(stubCats.v,
 96 			 page and (--p.templatepage passes page; p.main does not, i.e. articles are categorized without sort keys.
 97 				v=='' and tsort == 'no'--if true, inserts 'true' in table, which category() will reject
 98 				or tsort and {cat, k = ' ', t = tsort}
 99 				or {cat, k = ' *', t = page.text}--note space in front of sort key
100 			)
101 			or cat
102 		)
103 		--Check category existance only if on the template page (i.e. stub documentation)
104 		if page then
105 			if not mw.title.new('Category:' .. cat).exists then
106 				code = code or mw.html.create'code':wikitext'|category'
107 				table.insert(stubCats.missing, tostring(mw.clone(code):wikitext(v)))
108 			end
109 			--[[
110 			Checks non-demo stub template for documentation and flags if doc is present.
111 			All stub cats names are checked and flagged if it does not match 'Category: [] stub'.
112 			The main stub cat is exempt from the name check if the stub template has its own doc
113 			(presumably, this doc would have an explanation as to why the main stub cat is non-conforming).
114 			]]
115 			table.insert(stubCats.v, v == '' and not p.demo and pageDoc.exists and
116 				'Stub message templates with documentation subpages'
117 				or not cat:match' stubs$' and {k = 'S', t = page.text}
118 			)
119 		end
120 	end
121 	--Add category names after loop is completed
122 	category(stubCats.v)
123 	return #stubCats.missing > 0 and ombox{
124 		--Changed, original msg:
125 		--One or more of the stub categories defined in this template do not seem to exist!
126 		--Please double-check the parameters {{para|category}}, {{para|category1}} and {{para|category2}}.
127 		'The following parameter'
128 			.. (#stubCats.missing == 1 and ' defines a stub category that does' or 's define stub categories that do')
129 			.. ' not exist: ' .. mw.text.listToText(stubCats.missing),
130 		{k = 'N', t = page.text}
131 	}
132 end
133 
134 --Shows population of categories found by catStub(). Outputs demo values if none
135 local function population()
136 	local wikitext, base = {}, '* [[:Category:%s]] (population: %s)\n'
137 	if not args.category and stubCats[1] ~= false then
138 		table.insert(stubCats, 1, false)
139 	end
140 	for _, v in ipairs(stubCats) do
141 		table.insert(wikitext, base:format(
142 			v and args['category' .. v] or '{{{category}}}',
143 			v and mw.site.stats.pagesInCategory(args['category' .. v], 'all') or 0
144 		))
145 	end
146 	return table.concat(wikitext)
147 end
148 
149 --Includes standard stub documention and flags stub templates with bad parameter values.
150 function p.templatepage(frame, page)
151 	args, page = p:init(frame, page)
152 	local tStubDoc = mw.title.new'Template:Stub documentation'
153 	local pageDoc = page:subPageTitle('doc')
154 	--Reorganization note: Original Asbox alternates between outputting categories and checking on params |category#=.
155 	--Rather than checking multiple times and switching tasks, all stub category param operations have been rolled into catStub()
156 	return Buffer(
157 		ombox{--Show ombox warnings for missing args.
158 			ifNot = args.category,
159 			'The <code>|category</code> parameter is not set. Please add an appropriate stub category.',
160 			{k = 'C', t = page.text}
161 		})
162 		:_(ombox{
163 			ifNot = args.subject or args.article or args.qualifier,
164 			'This stub template contains no description! At least one of the parameters <code>|subject</code>, <code>|article</code> or <code>|qualifier</code> must be defined.',
165 			{k = 'D', t = page.text}
166 		})
167 		:_(catStub(page, pageDoc))--catStub() may also return an ombox if there are non-existing categories
168 		:_(category{
169 			done = p.demo ~= 'doc',--Outputs categories if not doc demo
170 			'Stub message templates',
171 			args.icon and
172 				'Stub message templates using icon parameter'
173 				or args.image and (
174 					mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists--do nothing if exists. category() will reject true
175 					or {k = 'B', t = page.text}
176 				)
177 				or 'Stub message templates without images',
178 			args.imagealt and {k = 'I', t = page.text},
179 		})
180 		:_((not p.demo or p.demo == 'doc') and--Add standard stub template documentation
181 			require('Module:Documentation').main{
182 				content = Buffer(page.text ~= 'Stub' and--This comparison performed in {{Asbox/stubtree}} before it invokes Module:Asbox stubtree
183 						-- TODO: update this instance of Asbox to Article stub box eventually
184 						require('Module:Asbox stubtree').subtree{args = {pagename = page.text}}
185 					)
186 					:_in'\n== About this template ==\nThis template is used to identify a':_(args.subject):_'stub':_(args.qualifier):_out' '--space
187 					:_'. It uses {{[[Template:Article stub box|article stub box]]}}, which is a meta-template designed to ease the process of creating and maintaining stub templates.\n=== Usage ===\nTyping '
188 					:_(mw.html.create'code'
189 						:wikitext('{{', page.text == 'Stub' and 'stub' or page.text, '}}')
190 					)
191 					:_' produces the message shown at the beginning, and adds the article to the following categor'
192 					:_(#stubCats > 1 and 'ies' or 'y')
193 					:_':\n'
194 					:_(population())
195 					:_(pageDoc.exists and--transclusion of /doc if it exists
196 						frame:expandTemplate{title = pageDoc.text}
197 					)
198 					:_'\n== General information ==\n'
199 					:_(frame:expandTemplate{title = tStubDoc.text})
200 					:_'\n\n'(),
201 				['link box'] = Buffer'This documentation is automatically generated by [[Module:Article stub box]].'
202 					:_in'The general information is transcluded from [[Template:Stub documentation]]. '
203 						:_(mw.html.create'span'
204 							:cssText'font-size:smaller;font-style:normal;line-height:130%'
205 							:node(('([%s edit] | [%s history])'):format(
206 								tStubDoc:fullUrl('action=edit', 'relative'),
207 								tStubDoc:fullUrl('action=history', 'relative')
208 							))
209 						)
210 						:_out()
211 					:_(page.protectionLevels.edit and page.protectionLevels.edit[1] == 'sysop' and
212 						"This template is [[WP:PROTECT|fully protected]] and any [[WP:CAT|categories]] should be added to the template's ["
213 						.. pageDoc:fullUrl('action=edit&preload=Template:Category_interwiki/preload', 'relative')
214 						.. '| /doc] subpage, which is not protected.'
215 					)' <br/>'
216 			}
217 		)()
218 end
219 
220 function p.main(frame, page)
221 	args, page = p:init(frame, page)
222 	local output = mw.html.create'div'
223 		:attr{role = 'note'}
224 		:addClass'metadata plainlinks asbox stub'
225 		:tag'table'
226 			:attr{role = 'presentation'}
227 			:tag'tr'
228 				:addClass'noresize'
229 				:node((args.icon or args.image) and
230 					mw.html.create'td'
231 						:wikitext(args.icon or ('[[File:%s|%spx|alt=%s]]'):format(
232 							args.image or '',
233 							args.pix or '40x30',
234 							args.imagealt or 'Stub icon'
235 						))
236 				)
237 				:tag'td'
238 					:tag'p'
239 						:addClass'asbox-body'
240 						:wikitext(
241 							Buffer'This':_(args.subject):_(args.article or 'article'):_(args.qualifier)' ',--space
242 							' is a [[Wikipedia:stub|stub]]. [',
243 							page:fullUrl('action=edit', 'relative'),
244 							' 加筆/訂正]の協力者を募集中です。'
245 						)
246 					:done()
247 					:node(args.note and
248 						mw.html.create()
249 							:tag'p'
250 								:addClass'asbox-note'
251 								:wikitext(args.note)
252 							:done()
253 					)
254 		:allDone()
255 		:node(args.name and
256 			require'Module:Navbar'._navbar{
257 				args.name,
258 				mini = 'yes',
259 			}
260 			)
261 	--[[
262 	Stub categories for templates include a sort key; this ensures that all stub tags appear at the beginning of their respective categories.
263 	Articles using the template do not need a sort key since they have unique names.
264 	When p.demo equals 'doc', the demo stub categories will appear as those for a stub template.
265 	Otherwise, any non-nil p.demo will emulate article space categories (plus any error cats unless set to 'art')
266 	]]
267 	if page.namespace == 0 then -- Main namespace
268 		category'All stub articles'
269 		catStub()
270 	elseif p.demo then
271 		if p.demo ~= 'doc' then catStub() end
272 		--Unless p.demo is set to 'art', it will also include error categories normally only shown on
273 		--the template but not in the article. The elseif after namespace == 0 means demo cats will never show in article space.
274 		p.demodoc = p.demo ~= 'art' and p.templatepage(frame, page)
275 		output = mw.html.create()
276 			:node(output)
277 			:tag'small':wikitext(
278 				'Demo categories: ',
279 				(category{done = true}:gsub('(%[%[)(Category:)([^|%]]-)(%|)', '%1%2%3|%2%3%4'):gsub('(%[%[)(Category:)', '%1:%2'))
280 			):done()
281 			:wikitext(p.demo == 'doc' and p.demodoc or nil)
282 	else
283 		--Checks for valid name; emulates original template's check using {{FULLPAGENAME:{{{name|}}}}}
284 		local normalizedName = mw.title.new(args.name or '')
285 		if normalizedName and normalizedName.fullText == page.fullText then
286 			output = mw.html.create():node(output):wikitext(p.templatepage(frame, page))
287 		elseif not page.isSubpage and page.namespace == 10 then-- Template namespace and not a subpage
288 			category{{k = args.name and 'E' or 'W', t = page.text}}
289 		end
290 	end
291 	return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. tostring(output:wikitext(not p.demo and category{done = true} or nil))
292 end
293 
294 return p