Jump to content

Module:JCW: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
don't need ustring.gsub here, and this fixes "Lua error: not enough memory" and some hidden bugs at User:JL-Bot/Maintenance.cfg
p.selected: positional (numbered) DOI parameters work the same as named parameters; merge both types if used together; remove a redundant backslash from insource gsub
Line 25: Line 25:
else
else
sourcenote = ''
sourcenote = ''
end
local listDoi, nrDoi = {}, 0
local function addDoi(doi, i)
if doi then
nrDoi = nrDoi + 1
if i and listDoi[i] then
table.insert(listDoi, i, doi)
else
listDoi[nrDoi] = doi
end
end
end
end
local rows = {}
local rows = {}
for i, v in ipairs(TableTools.compressSparseArray(n)) do
for i, v in ipairs(TableTools.compressSparseArray(n)) do
if i == 1 then
if i == 1 then
rows[i] = { string.format('* [[:%s]]%s', v, sourcenote) }
rows[1] = { string.format('*[[:%s]]%s', v, sourcenote) }
elseif v:match('^10%.%d+$') then
addDoi(v)
else
else
rows[i] = string.format('** [[:%s]]', v)
table.insert(rows, string.format('**[[:%s]]', v))
end
end
end
end
Line 45: Line 58:
end
end
end
end

if n.doi and n.doi1 then
if n.doi and n.doi1 then
error('Use doi or doi1, not both', 0)
error('Use doi or doi1, not both', 0)
end
end
local search = 'https://en.wikipedia.org/w/index.php?sort=relevance&title=Special%3ASearch&profile=advanced&fulltext=1&advancedSearch-current={}&ns0=1&ns118=1&search=insource%3A'
local suffix = '%5C%2F%20*%2F'
for i = 1, 10 do
for i = 1, 10 do
local doi
local doi
Line 58: Line 68:
doi = n['doi' .. i]
doi = n['doi' .. i]
end
end
if not doi then
addDoi(doi, i)
end
break
local search = 'https://en.wikipedia.org/w/index.php?sort=relevance&title=Special%3ASearch&profile=advanced&fulltext=1&advancedSearch-current={}&ns0=1&ns118=1&search=insource%3A'
end
local suffix = '%5C%2F%20*%2F' -- '%20*' seems redundant but is needed for Special:Search to work ('x*' also works)
for i = 1, nrDoi do
local doi = listDoi[i]
table.insert(rows,
table.insert(rows,
string.format('** <code>&#123;{doi|[%s%s%s %s]}&#125;</code>', search, doi:gsub('10%.', '\/10\\.'), suffix, doi)
string.format('**<code>&#123;{doi|[%s%s%s %s]}&#125;</code>', search, doi:gsub('10%.', '/10\\.'), suffix, doi)
)
)
end
end

Revision as of 05:57, 1 November 2020

local p = {}
local mArguments = require('Module:Arguments')
local TableTools = require('Module:TableTools')

function p.selected(frame)
	local n = mArguments.getArgs(frame, {parentOnly = true})
	local note = n.note
	local wantSource = {
		['User:JL-Bot/Questionable.cfg'] = true,
	}
	--local abbrev = {BLJ = '[https://beallslist.weebly.com/standalone-journals.html BLJ]', BLJU='[https://beallslist.weebly.com/standalone-journals.html BLJU]', BLP='[https://beallslist.weebly.com/ BLP]', BLPU='[https://beallslist.weebly.com/ BLPU]', SPJJ = '[https://predatoryjournals.com/journals/ SPJJ]', SPJP = '[https://predatoryjournals.com/publishers/ SPJP]', DOAJ='[https://blog.doaj.org/2014/08/28/some-journals-say-they-are-in-doaj-when-they-are-not/ Lying about DOAJ]', QW ='[https://www.quackwatch.org/04ConsumerEducation/nonrecperiodicals.html QW]',	DEPS ='[[WP:DEPS|DEPS]]', URF ='[[#Unreliable fields|URF]]'}
	local abbrev = {Bohannon = 'Bohannon', BLJ = 'BLJ', BLJU= 'BLJU', BLP = 'BLP', BLPU = 'BLPU', SPJJ ='SPJJ', SPJP ='SPJP', DOAJ = 'DOAJ', QW='QW', DEPS ='DEPS', URF = 'URF', Unknown = 'Unknown'}
	local source
	if wantSource[mw.title.getCurrentTitle().fullText] then
		source = n.source or 'Unknown'
		source = abbrev[source] or source
	end
	local sourcenote
	if note and source then
		sourcenote = string.format('&nbsp;(%s) &#91;%s&#93;', source, note)
	elseif source then
		sourcenote = string.format('&nbsp;(%s)', source)
	elseif note then
		sourcenote = string.format('&nbsp;&#91;%s&#93;', note)
	else
		sourcenote = ''
	end
	local listDoi, nrDoi = {}, 0
	local function addDoi(doi, i)
		if doi then
			nrDoi = nrDoi + 1
			if i and listDoi[i] then
				table.insert(listDoi, i, doi)
			else
				listDoi[nrDoi] = doi
			end
		end
	end
	local rows = {}
	for i, v in ipairs(TableTools.compressSparseArray(n)) do
		if i == 1 then
			rows[1] = { string.format('*[[:%s]]%s', v, sourcenote) }
		elseif v:match('^10%.%d+$') then
			addDoi(v)
		else
			table.insert(rows, string.format('**[[:%s]]', v))
		end
	end
	if not rows[1] then
		error('Need at least one target parameter', 0)
	end
	for _, param in ipairs({ 'imprint', 'parent' }) do
		for i = 1, 10 do
			local arg = n[param .. i]
			if arg then
				table.insert(rows[1], string.format("''[[%s]]''", arg))
			end
		end
	end
	if n.doi and n.doi1 then
		error('Use doi or doi1, not both', 0)
	end
	for i = 1, 10 do
		local doi
		if i == 1 then
			doi = n.doi or n.doi1
		else
			doi = n['doi' .. i]
		end
		addDoi(doi, i)
	end
	local search = 'https://en.wikipedia.org/w/index.php?sort=relevance&title=Special%3ASearch&profile=advanced&fulltext=1&advancedSearch-current={}&ns0=1&ns118=1&search=insource%3A'
	local suffix = '%5C%2F%20*%2F'  -- '%20*' seems redundant but is needed for Special:Search to work ('x*' also works)
	for i = 1, nrDoi do
		local doi = listDoi[i]
		table.insert(rows,
			string.format('**<code>&#123;{doi|[%s%s%s %s]}&#125;</code>', search, doi:gsub('10%.', '/10\\.'), suffix, doi)
		)
	end
	rows[1] = table.concat(rows[1], ' / ')
	return table.concat(rows, '\n')
end

function p.exclude(frame)
	local n = mArguments.getArgs(frame, {parentOnly = true})
	local length = TableTools.length(n)
	local text = string.format('*[[:%s]]', n[1] or '')
	n[1] = nil
	for _, v in ipairs(TableTools.compressSparseArray(n)) do
		if length > 1 then
			text = text .. string.format(" ≠ [[:%s]]", v)
		end
	end
	return text
end

function p.pattern(frame)
	local rows = {}
	for i, v in ipairs(frame:getParent().args) do
		v = mw.text.trim(v)
		if v == '' then
			rows[i] = ''
		elseif i == 1 then
			rows[i] = string.format('*[[%s]]', v)
		else
			rows[i] = string.format("\n**<code>%s</code>", v
				:gsub("%.%*", "<b><font style=color:#006400;>.*</font></b>")
				:gsub("!", "<b><font style=color:#8B0000;>!</font></b>")
				:gsub("'", "&rsquo;")
			)
		end
	end
	if not rows[1] then
		error('pattern requires parameter 1', 0)
	end
	return table.concat(rows)
end

return p