Jump to content

Module:List of contributors

From SiIvaGunner Wiki

For more information on how to use this code, see SiIvaGunner Wiki:List of contributors.

Usage

This module is intended to be used by transcluding data from other pages into it. mw:Extension:Labeled Section Transclusion#Transclude a specific section can be used to transclude a specific section. For clarity, the examples here show the individual list items.

render_list

This function extracts wikilinks from bulleted list items in the input text; each link must be the first text in the respective bullet point. The function also sorts the list items alphabetically and case-insensitively.

The option |mode=no category links can be used to exclude any lines that contain category links in the format [[:Category:...]].

Code Output
{{#invoke:List of contributors|render_list|wikitext=
* [[AMUNO]]
* {{contrib2|Chaze the Chat}}
* {{contrib|BotanicSage}}
}}

compare_list

This function extracts wikilinks from bulleted list items in the input text and compares them to another list. The text "Category:" is automatically detected and removed from the comparison_list parameter for the purposes of the comparison; this allows outputs from Wiki Category Grabber to be directly pasted into the module invocation.

Code Output
{{#invoke:List of contributors|compare_list|wikitext=
* [[AMUNO]]
* {{contrib|BotanicSage}}
|comparison_list=
Category:AMUNO
Category:Chaze the Chat
}}

Duplicates in list

These entries appear in List of contributors more than once.

  1. (none found)

Missing from list

These entries are missing from List of contributors.

Wikitext Links
*{{contrib2|Chaze the Chat}}
  1. Chaze the Chat (category)

Missing from category

These entries are missing from Category:Rips by author. Note that known contributors with 0 known contributions do not need to have a category.

  1. BotanicSage (category)

category_counts

This function evaluates the statistics of up to 100 categories, specifically the number of pages in each category. The categories are sorted into four buckets based on the number of pages. The first parameter is used to indicate which 100 entries in the list should be evaluated; the default is "1" or the first hundred entries.

From the wikitext parameter, wikilinks from bulleted list items in the input text are extracted, as with the aforementioned functions. Note that the links should be to articles, not categories.

Code Output
{{#invoke:List of contributors|category_counts|1|wikitext=
* [[AMUNO]]
* {{contrib|BotanicSage}}
* {{contrib2|Chaze the Chat}}
}}

Showing results in the range #1 to #3.

Purge

10+ 4–9 1–3 Other
  1. Chaze the Chat (category)
  1. AMUNO (category)
  2. BotanicSage (category)

local p = {}

-- Create a sequence table by parsing bullet list items in wikitext
function p.list(page_text, mode)
	local page_lines = mw.text.split(page_text, '\n')
	local matches = {}
	for i, v in ipairs(page_lines) do
		-- For each line, pattern match a link target, if any
		-- The link must be at the start of an unordered list item
		local item_match = mw.ustring.match(v, '^%* *%[%[([^|%]]+)')
		if mode == "no category links" then
			if mw.ustring.match(v, '%[%[:Category:([^|%]]+)') then
				item_match = nil
			end
		end
		if item_match then
			table.insert(matches, item_match)
		end
	end
	return matches
end

-- Format a contributor name as an article link and a category link
function p.contrib(name)
	local title = mw.ustring.match(name, "^[^|]*")
	return string.format("[[%s]] ([[:Category:%s|category]])", name, title)
end

-- Format a contributor name as template code
function p.contrib2(name)
	return string.format("{{contrib2|%s}}", name)
end

-- Generate an unordered list based on wikitext
function p.render_list(frame)
	local wikitext = frame.args["wikitext"]
	local mode = frame.args["mode"]
	local wikitext_list = p.list(wikitext, mode)
	table.sort(wikitext_list, function(a, b) return mw.ustring.lower(a) < mw.ustring.lower(b) end)
	return '*[[' .. table.concat(wikitext_list, ']]\n*[[') .. ']]'
end

-- Compare a wikitext list to a plain list with line breaks as separators
function p.compare_list(frame)
	local lang = mw.language.new("en")
	local wikitext = frame.args["wikitext"]
	local comparison_list = mw.text.split(frame.args["comparison_list"], '\n')
	local wikitext_list = p.list(wikitext)
	local wikitext_index = {}
	local wikitext_duplicates = {}
	-- Make wikitext_index a table where the keys are fields of wikitext_list
	for i, v in ipairs(wikitext_list) do
		-- For matching, make the first character of the wikitext uppercase,
		-- change underscores to spaces and remove leading and trailing spaces
		local key = lang:ucfirst(mw.text.trim(mw.ustring.gsub(v, "[_ ]+", " "), " "))
		if wikitext_index[key] then
			-- If the key is already used, then mark as a duplicate
			table.insert(wikitext_duplicates, p.contrib(v))
		else
			wikitext_index[key] = v
		end
	end
	local not_in_wikitext = {}
	local not_in_wikitext_code = {}
	local not_in_comparison = {}
	for i, v in ipairs(comparison_list) do
		-- For matching, remove "Category:" at the start of the value
		local value = mw.title.new(v).text
		if wikitext_index[value] then
			-- Remove entries from wikitext_index that are in comparison list
			wikitext_index[value] = nil
		else
			-- Get intended name format from category pages
			local category_wikitext = frame:expandTemplate{title = 'Category:' .. value}
			local link = mw.ustring.match(category_wikitext, ' by %[%[(.-)%]%]') or mw.ustring.match(category_wikitext, ' by ([^\n%[%]]+)%.')
			-- If there's no link or the link target doesn't match the category name, use the category name instead
			if not link or link == '' or (value ~= mw.title.new(mw.ustring.gsub(link, '[|#].*', '')).text) then
				link = value
			end
			
			-- Populate not_in_wikitext with missing entries
			table.insert(not_in_wikitext, p.contrib(link))
			-- Populate not_in_wikitext_code with wikitext for missing entries
			table.insert(not_in_wikitext_code, p.contrib2(link))
		end
	end
	for k, v in pairs(wikitext_index) do
		-- Populate not_in_comparison with missing entries
		table.insert(not_in_comparison, p.contrib(v))
	end
	
	if #wikitext_duplicates == 0 then
		table.insert(wikitext_duplicates, "''(none found)''")
	end	
	if #not_in_wikitext == 0 then
		table.insert(not_in_wikitext, "''(none found)''")
	end	
	if #not_in_comparison == 0 then
		table.insert(not_in_comparison, "''(none found)''")
	end
	
	return '==Duplicates in list==\nThese entries appear in [[List of contributors]] more than once.\n#' .. table.concat(wikitext_duplicates, '\n#') .. '\n==Missing from list==\nThese entries are missing from [[List of contributors]].\n{| class="wikitable"\n!Wikitext\n!Links\n|- style="vertical-align:top"\n|\n<pre>\n*' .. table.concat(not_in_wikitext_code, '\n*') .. '</pre>\n|\n#' .. table.concat(not_in_wikitext, '\n#') .. '\n|}\n==Missing from category==\nThese entries are missing from [[:Category:Rips by author]]. Note that known contributors with 0 known contributions do not need to have a category.\n#' .. table.concat(not_in_comparison, '\n#')
end

-- Count number of pages of up to 100 categories and group them by amount
function p.category_counts(frame)
	local lang = mw.language.new("en")
	local wikitext = frame.args["wikitext"]
	local group = frame.args["1"] or 1
	local wikitext_list = p.list(wikitext)
	
	-- Determine start and end of the range to be analyzed
	local list_start = (group - 1) * 100 + 1
	local list_end = list_start + 99
	if list_start > #wikitext_list then
		return 'There are no results in the range #' .. list_start .. ' to #' .. list_end .. '.'
	end
	if list_end > #wikitext_list then
		list_end = #wikitext_list
	end
	
	-- Get the number of pages for each category
	local more_than_9 = {}
	local between_4_and_9 = {}
	local between_1_and_3 = {}
	local other = {}
	for i = list_start, list_end do
		local category_pages = mw.site.stats.pagesInCategory(wikitext_list[i], 'pages')
		if category_pages > 9 then
			table.insert(more_than_9, p.contrib(wikitext_list[i]))
		elseif category_pages > 3 then
			table.insert(between_4_and_9, p.contrib(wikitext_list[i]))
		elseif category_pages > 0 then
			table.insert(between_1_and_3, p.contrib(wikitext_list[i]))
		else
			table.insert(other, p.contrib(wikitext_list[i]))
		end
	end
	
	return 'Showing results in the range #' .. list_start .. ' to #' .. list_end .. ".\n\n'''[" .. mw.title.getCurrentTitle():fullUrl('action=purge') .. " Purge]'''" .. '\n{| class="wikitable"\n!10+\n!4–9\n!1–3\n!Other\n|- style="vertical-align:top"\n|\n#' .. table.concat(more_than_9, '\n#') .. '\n|\n#' .. table.concat(between_4_and_9, '\n#') .. '\n|\n#' .. table.concat(between_1_and_3, '\n#') .. '\n|\n#' .. table.concat(other, '\n#') .. '\n|}'
end

return p

Debug data: