ผลต่างระหว่างรุ่นของ "มอดูล:Plain sister"

จาก วิกิซอร์ซ
เนื้อหาที่ลบ เนื้อหาที่เพิ่ม
Geonuch (คุย | ส่วนร่วม)
ปรับปรุงแม่แบบ
Geonuch (คุย | ส่วนร่วม)
ไม่มีความย่อการแก้ไข
บรรทัดที่ 3: บรรทัดที่ 3:
local sites = {
local sites = {
-- interwiki prefix, parameter, label and site id (for Wikidata)
-- interwiki prefix, parameter, label and site id (for Wikidata)
{ 'w', 'wikipedia', 'บทความที่วิกิพีเดีย', 'enwiki' },
{ 'w', 'wikipedia', 'บทความที่วิกิพีเดีย', 'thwiki' },
{ 'c', 'commons', 'คลังภาพที่คอมมอนส์', 'commonswiki' },
{ 'c', 'commons', 'คลังภาพที่คอมมอนส์', 'commonswiki' },
{ 'c:Category', 'commonscat', 'หมวดหมู่ที่คอมมอนส์', 'commonswiki' },
{ 'c:Category', 'commonscat', 'หมวดหมู่ที่คอมมอนส์', 'commonswiki' },
{ 'q', 'wikiquote', 'คำคม', 'enwikiquote' },
{ 'q', 'wikiquote', 'คำคม', 'thwikiquote' },
{ 'n', 'wikinews', 'ข่าวสาร', 'enwikinews' },
{ 'n', 'wikinews', 'ข่าวสาร', 'thwikinews' },
{ 'wikt', 'wiktionary', 'นิยาม', 'enwiktionary' },
{ 'wikt', 'wiktionary', 'นิยาม', 'thwiktionary' },
{ 'b', 'wikibooks', 'คู่มือตำรา', 'enwikibooks' },
{ 'b', 'wikibooks', 'คู่มือตำรา', 'thwikibooks' },
{ 'v', 'wikiversity', 'บทเรียน', 'enwikiversity' },
{ 'v', 'wikiversity', 'บทเรียน', 'thwikiversity' },
{ 'wikispecies', 'wikispecies', 'อนุกรมวิธาน', 'specieswiki' },
{ 'wikispecies', 'wikispecies', 'อนุกรมวิธาน', 'specieswiki' },
{ 'voy', 'wikivoyage', 'คู่มือนำเที่ยว', 'enwikivoyage' },
{ 'voy', 'wikivoyage', 'คู่มือนำเที่ยว', 'thwikivoyage' },
{ 'd', 'wikidata', 'รายการวิกิสนเทศ', 'wikidatawiki' },
{ 'd', 'wikidata', 'รายการวิกิสนเทศ', 'wikidatawiki' },
{ 'wikilivres', 'wikilivres', 'Wikilivres page', '' },
{ 'wikilivres', 'wikilivres', 'Wikilivres page', '' },

รุ่นแก้ไขเมื่อ 09:50, 14 ตุลาคม 2563

เอกสารการใช้งานสำหรับมอดูลนี้อาจสร้างขึ้นที่ มอดูล:Plain sister/doc

local p = {}

local sites = { 
-- interwiki prefix, parameter,     label and            site id (for Wikidata)
	{ 'w',           'wikipedia',   'บทความที่วิกิพีเดีย', 'thwiki' },
	{ 'c',           'commons',     'คลังภาพที่คอมมอนส์',   'commonswiki' },
	{ 'c:Category',  'commonscat',  'หมวดหมู่ที่คอมมอนส์',  'commonswiki' },
	{ 'q',           'wikiquote',   'คำคม',            'thwikiquote' },
	{ 'n',           'wikinews',    'ข่าวสาร',              'thwikinews' },
	{ 'wikt',        'wiktionary',  'นิยาม',        'thwiktionary' },
	{ 'b',           'wikibooks',   'คู่มือตำรา',          'thwikibooks' },
	{ 'v',           'wikiversity', 'บทเรียน',            'thwikiversity' },
	{ 'wikispecies', 'wikispecies', 'อนุกรมวิธาน',          'specieswiki' },
	{ 'voy',         'wikivoyage',  'คู่มือนำเที่ยว',      'thwikivoyage' },
	{ 'd',           'wikidata',    'รายการวิกิสนเทศ',     'wikidatawiki' },
	{ 'wikilivres',  'wikilivres',  'Wikilivres page',   '' }, 
	{ 'wikilivres',  'bibliowiki',  'Wikilivres page',   '' },
	{ 'm',           'meta',        'เมทา',              'metawiki' }
}

function p.interprojetPart( frame )
	local frame = frame:getParent()
	local item  = mw.wikibase.getEntityObject()
	local links = {}

	for _, site in pairs( sites ) do
		local val = frame.args[site[2]]             -- link provided as input parameter
		if val == '' then
			val = nil;
		end
		if not val and site[4] ~= '' and item then  -- fetch it from wikidata
			val = item:getSitelink( site[4] )
			if site[4] == 'wikidatawiki' then
				val = item.id 
			elseif site[4] == 'commonswiki' and val then -- we have link to commons 
				local catFlag = (#val>9 and string.sub(val,1,9) == 'Category:')
				if (site[2]=='commonscat' and catFlag==false) or (site[2]=='commons' and catFlag==true) then
					val = nil  -- link is to a wrong namespace so let's nuke it
				elseif (site[2]=='commonscat' and catFlag==true) then
					val = string.sub(val,10) -- trim 'Category:' from the front
				end
			end		
		end
		if not val and site[2] == 'commons' and item then
			local statements = item:getBestStatements('P935' ) -- get commons gallery page from P935 property
			if statements[1] and statements[1].mainsnak.datavalue  then
				val = statements[1].mainsnak.datavalue.value  
			end
		end	
		if not val and site[2] == 'commonscat' and item then
			local statements = item:getBestStatements('P373' )   -- get commons category page from P373 property
			if statements[1] and statements[1].mainsnak.datavalue  then
				val = statements[1].mainsnak.datavalue.value 
			end
		end		

		if val then
			table.insert( links, '[[' .. site[1] .. ':' .. val .. '|' .. site[3] .. ']]' )
		end
	end

	if #links == 0 then -- links table length is zero
		return ''
	end

	return '<li class="sisitem">'
		.. '<span class="sisicon" style="padding-right:1ex;">[[Image:Wikimedia-logo.svg|frameless|18px|link=Special:sitematrix|alt=Sister Projects.]]</span>'
		.. '[[Special:sitematrix|โครงการพี่น้อง]]:&#32;' .. table.concat( links, ',&#32;' )
		.. '.</li>'
end

return p