「モジュール:Title」の版間の差分

提供:脳科学辞典
ナビゲーションに移動 検索に移動
bsd>Jarekt
(another small issue)
 
(1版 をインポートしました)
 
(相違点なし)

2020年1月29日 (水) 13:27時点における最新版

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

--[[  
  __  __           _       _       _____ _ _   _      
 |  \/  | ___   __| |_   _| | ___ |_   _(_) |_| | ___ 
 | |\/| |/ _ \ / _` | | | | |/ _ (_)| | | | __| |/ _ \
 | |  | | (_) | (_| | |_| | |  __/_ | | | | |_| |  __/
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)|_| |_|\__|_|\___|
                                                                             
This module is intended to be the engine behind "Template:Title"

Authors and maintainers:
* User:Jarekt - original version 
]]

require('Module:No globals') -- used for debugging purposes as it detects cases of unintended global variables

-- ==================================================
-- === Internal functions ===========================
-- ==================================================
local function langSwitch_(list,userLang)
	local langList = mw.language.getFallbacksFor(userLang)
	table.insert(langList,1,userLang)
	for _,language in ipairs(langList) do
		if list[language] then
			return list[language], language
		end
	end
	return nil, nil
end

-- ====================================================================
local function langWrapper(text, textLang, userLang) 
-- code equivalent to https://commons.wikimedia.org/wiki/Template:Description
	local dir = mw.language.new( textLang ):isRTL() and 'rtl' or 'ltr'   
	local str = string.format('<span class="language %s"><b>%s:</b></span>', textLang, mw.language.fetchLanguageName( userLang, textLang  ))
    return string.format('<div class="description mw-content-%s" dir="%s" lang="%s">%s %s</div>', dir, dir, textLang, str, text)
end

-- ====================================================================
local function style(str, textLang)
-- based on [[Template:Title/style]]
	if not str or #str==0 then
		return nil
	end
	str = mw.text.trim(str)
	local LUT = {ar='%s',he='%s' ,et='„%s“' ,ja='『%s』' ,mk='„%s“' ,ru='«%s»' ,zh='《%s》', ['zh-hans']='《%s》', en='<i>%s</i>'}
	--str = mw.language:ucfirst(str)
	local form, lang = langSwitch_(LUT, textLang)
	str = mw.ustring.format( form,  str)
	local dir = mw.language.new( lang ):isRTL() and 'rtl' or 'ltr'   
    str = string.format('<span dir="%s" lang="%s">%s</span>', dir, lang, str)
	return str
end

-- ===========================================================================
-- === get wikidata item ID (qid) based on P6243 stored in SDC             ===
-- ===========================================================================
local function qid_from_SDC()
	local page = mw.title.getCurrentTitle()
	local mid  = "M" .. page.id -- same as {{PAGEID}}
	local entity = mw.wikibase.getEntity(mid)
	if entity and entity.statements and entity.statements.P6243 then
		local statement = entity.statements.P6243[1]
		return statement.mainsnak.datavalue.value.id
	end
	return nil
end

-- ====================================================================
local function harvest_wikidata(entity, userLang)
	local data = {} -- structure similar to "args" but filled with wikidata data
	data.userLang = userLang
	if not entity then
		return data
	end

	-- get title (from 3 properties and label)
	local property = {P1476 = 'title', P1448='official_name', P1705='native_label'}
	for prop, field in pairs( property ) do
		local titleList = {}
		for _, statement in pairs( entity:getBestStatements(field)) do 
			if (statement.mainsnak.snaktype == "value") then 
				local val = statement.mainsnak.datavalue.value
				titleList[val.language] = val.text -- look for multiple values each with a language code
				data.title, data.lang = val.text, val.language
			end
		end
		if #titleList>1 then
			title, language = langSwitch_(titleList, userLang)
			if title then
				data.title, data.lang = title, language
			end
		end
		if data.title then
			break
		end	
	end
	
	-- get labels in all the langguages
	if entity.labels then
		for language, val in pairs(entity.labels) do -- loop over all labels
			if language~=data.lang then
				data[language] = val.value
			end
		end
	end
	
	return data
end

-- ==================================================
-- === External functions ===========================
-- ==================================================
local p = {}

-- ===========================================================================
-- === Version of the function to be called from other LUA codes
-- ===========================================================================
function p.title_(args)
	local line1, line2, Title, text, lang
	local qsTable = {}
	local qsCommand = '<div style="display: none;">label QS:L%s,"%s"</div>' -- text of invisible tag brodcasted by the template which allows creation of QuickStatements command used to add this info to Wikidata

	if args.lang and args.title then -- == Case 1: original language to be displayed ==
		args.lang = string.lower(args.lang)
		line1 = style(args.title, args.lang)
		if args.lang==args.userLang then -- user's language = title's language
			if args[args.lang] then -- the second line can be a modernization for example
				line2 = '<br/>' .. style(args[args.lang], args.userLang) 
			end
			Title = mw.ustring.format( '<span style="font-weight:bold">%s%s</span>', line1, line2 or '')
		else -- user's language != title's language
			-- line 1 original language 
			local langName = mw.language.fetchLanguageName( args.lang, args.userLang  )
			local colon    = mw.message.new( "colon" ):inLanguage(args.userLang ):plain()
			local wordsep  = mw.message.new( "Word-separator" ):inLanguage(args.userLang ):plain()
			line1 = langName .. colon .. wordsep .. line1
			if args.transliteration then -- the second line can be a modernization for example
				line1 = line1 .. '&#32;- '  .. args.transliteration
			end
			line1 = mw.ustring.format( '<span style="font-size:0.9em">%s</span>', line1)

			--  line 2 translation
			if args.translation then -- the second line can be a modernization for example
				line2 = args.translation
			else
				text, lang = langSwitch_(args, args.userLang)
				line2 = style(text, lang)
			end
			if line2 then
				line2 = mw.ustring.format( '<br/><span style="font-weight:bold">%s</span>', line2)
			end 
			Title = line1 .. (line2 or '')
		end
		if not args.title_ then -- make sure title did not originated on wikidata
			table.insert( qsTable, string.format('<div style="display: none;">title QS:P1476,%s:"%s"</div>', args.lang, args.title) )
			table.insert( qsTable, string.format(qsCommand, args.lang, args.title) )
		end
	else -- == Case 2: original language not relevant ==
		if args.title then 
			Title = args.title
		else
			text, lang = langSwitch_(args, args.userLang)
			Title = style(text, lang)
		end
		if Title then
			Title = mw.ustring.format( '<span style="font-weight:bold">%s</span>', Title)
		end
	end
	Title = Title or ''
	if args.comment then
		Title = mw.ustring.format( '%s<br /><span style="font-size:0.9em">%s<br /></span>', title, args.comment)
	end

	-- add text of invisible tag brodcasted by the template which allows creation of QuickStatements command used to add this info to Wikidata
	for lang, text in pairs( args ) do 
		if mw.language.isSupportedLanguage(lang) and not args[lang..'_'] then -- lang has tobe a valid language and the statement is not from wikidata
			table.insert( qsTable, string.format(qsCommand, lang, text) )
		end
	end

	return Title .. table.concat( qsTable, '\n')
end

function p.wikidata_title(entity, userLang)
	return p.title_(harvest_wikidata(entity, userLang))
end

-- ===========================================================================
-- === Versions of the function to be called from template namespace
-- ===========================================================================
function p.title(frame)
	local args = {}
	for name, value in pairs( frame:getParent().args ) do 
		if value ~= '' then -- nuke empty strings
			args[string.lower(name)] = value
		end
	end
	for name, value in pairs( frame.args ) do 
		if value ~= '' then -- nuke empty strings
			args[string.lower(name)] = value
		end
	end
	
	if not (args.userLang and mw.language.isSupportedLanguage(args.userLang)) then 
		args.userLang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
	end 

	-- merge wikidata with local variables
	local qid = args.wikidata or qid_from_SDC() -- get wikidata item ID based on P6243 stored in SDC
	local entity = mw.wikibase.getEntity(qid)
	if entity then
		local data = harvest_wikidata(entity, args.userLang)
		if not args.title and data.title and not args.lang and data.lang then
			args.title = data.title -- get title from wikidata
			args.lang  = data.lang
			args.title_ = 'from wikidata'
		end
		for lang, text in pairs( data ) do 
			if mw.language.isSupportedLanguage(lang) and not args[lang] then 
				args[lang] = data[lang]
				args[lang..'_'] = 'from wikidata'
			end
		end
	end
	return p.title_(args)
end

return p