יחידה:מזהים חיצוניים

מתוך ויקיפדיה, האנציקלופדיה החופשית

יחידה זו משמשת בסיס ליצירת תבניות פרופילים לקישורים חיצוניים.

שימוש בתבניות מזהים חיצוניים[עריכת קוד מקור]

  • נהוג להוסיף תבניות מזהים חיצוניים בפרק הקישורים החיצוניים
  • הקישורים מוצגים כפריטי רשימה
  • כאשר נדרשים בקישור החיצוני להחליף את כותרת הנושא (ברירת המחדל היא שם הדף):
    • לדריסה בכל האתרים שבתבנית - ניתן להוסיף פרמטר שם
    • לדריסה של כותרת באתר ספציפי - ניתן להוסיף פרמטר "שם-<שם האתר>"

יצירת תבניות פרופילים[עריכת קוד מקור]

{{#invoke:מזהים חיצוניים|פריטים|
|כותרת=פרופילים ב<נושא>:
|<מזהה ויקינתונים>-<שם אתר לתצוגה>=<קישור>
|<שם אתר לתצוגה>-סמליל=<שם קובץ>
}}

כאשר:

  • כותרת - היא כותרת כללית לכל הקישורים בתצוגה קומפקטית
  • כל קישור חיצוני מזוהה באמצעות שורה שבה:
    • מזהה תבנית הוא שדה (property) מוויקינתונים
    • שם אתר לתצוגה הוא שם האתר שיוצג
    • קישור
      • כאשר הקישור הוא מחרוזת ריקה הקישור נלקח מוויקינתונים
      • לחלופין ניתן לדרוס את הקישור ולהוסיף מחרוזת קישור ידנית ובה להוסיף $1 לציון מזהה (בדומה לתבניות URL שמוגדרות בd:Property:P1630). אפשרות זו שימושית למשל כאשר רוצים לדרוס כתובת URL לכתובת בגרסה העברית.
  • ניתן להגדיר סמלילים לקישורים חיצוניים באמצעות פרמטרים מסוג "שם האתר-סמליל". סמלילים מוצגים בפריסה מורחבת (כאשר יש מעט קישורים)

דוגמאות שימוש[עריכת קוד מקור]

ניתן להיעזר בתבניות אחרות שמשתמשות ביחידה זו כדוגמאות (מיוחד:דפים המקושרים לכאן/יחידה:מזהים חיצוניים)



--[[
Render links as
*[URL title] in siteA
*[URL title] in siteB
...
]]
local config = {
	compactLinks=false
}

function renderLinksList(links)
	local res = ''
	for k,linkStruct in pairs(links) do
		local logolink
		if  linkStruct.logo then
			logolink = mw.ustring.format(' [[File:%s|20px|link=|%s]]', linkStruct.logo, linkStruct.siteName)
		else
			logolink = ''
		end
		res = res..mw.ustring.format('\n*%s [%s %s] %s',  logolink, linkStruct.link, linkStruct.subject, linkStruct.linkDescription)
	end		
	return res
end

function renderCompactLinks(title, links)
local Navbox = require('Module:Navbox')
	local elementsCat = ''
	
	local outString = ''

	local formatedLinks = {}
	for k,linkStruct in pairs(links) do
		table.insert(formatedLinks, mw.ustring.format('\n* <span class="nowrap">[%s %s]</span>', linkStruct.link, linkStruct.siteName))
	end		

	if #formatedLinks > 0 then
		local pencil = ''
		local itemId = mw.wikibase.getEntityIdForCurrentPage()
		if itemId then
			local pencilLink = mw.title.makeTitle( 0, itemId, '', 'wikidata' ):fullUrl('uselang=he')
			pencil = mw.ustring.format(' [[File:Blue pencil RTL.svg|15px|link=%s|עריכת הנתון בוויקינתונים]]', pencilLink)
		end		
		outString = Navbox._navbox( {
			name  = title,
			navboxclass = 'external-ifentifiers',
			bodyclass = 'hlist',
			group1 = title ..pencil,
			list1 =  table.concat( formatedLinks ),
			liststyle = 'text-align:right;'
			} )
	end
	local stylesheet = mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = 'יחידה:מזהים חיצוניים/style.css' } }
	return stylesheet..outString
end

--[[
Render external links according to wikidata
]]
function renderLinks(frame)
	local templateParams =  frame.args
	local templateArgs = frame:getParent().args
	local DEFAULT_TITLE = 'מזהים חיצוניים'
	local DEFAULT_COMPACT_THRESHOLD = 0
	if templateParams['קומפקטי']  then config['compactLinks'] = true end
	local linksThreshold = templateParams['רשימה קומפקטית'] or DEFAULT_COMPACT_THRESHOLD -- threshold for rendering as compact list.  Default to 0 - always compact
	local title = templateParams['כותרת'] or DEFAULT_TITLE  -- no title provided to the compacts links
	local res=''
	
	local articleTitle = tostring(mw.title.getCurrentTitle())
	local subject = mw.ustring.gsub(articleTitle, " *[(].*[)]","")
	if templateArgs['שם'] then
		subject=templateArgs['שם'] 
	end
	
	local entityId = templateArgs['יישות'] 
	local entity = mw.wikibase.getEntityObject(entityId)
	if not entity then
		return -- no wikidata entity exist
	end

	local links = {}
	for k,linkTemplate in pairs(templateParams) do
		local propery, sitename= string.match(k, '(P[0-9]+)-(.+)$')
		if propery and sitename then
			local siteSubject = templateArgs['שם-'..sitename] 
			local sitelogo = templateParams[sitename..'-סמליל']
			local siteDescription = templateParams[sitename..'-תיאור']
			local properyVal = entity:getBestStatements( propery ) 
			local propValue = properyVal and #properyVal>0 and properyVal[1].mainsnak and properyVal[1].mainsnak.datavalue and properyVal[1].mainsnak.datavalue.value
			if propValue then
        		local link
        		-- if no link template provided, auto populate it using extraction from formatValue
        		if string.len( linkTemplate ) == 0 then
        			link = string.match(mw.wikibase.formatValue(properyVal[1].mainsnak), '%[([^ ]+)')
        		else
					propValue = frame:preprocess('{{urlencode:'..propValue..'|WIKI}}')--mw.uri.encode(propValue, 'WIKI') see T174239
        			propValue = mw.ustring.gsub( propValue, "%%", "%%%%" )
        			link = string.gsub( linkTemplate, '$1', propValue)        			
        		end
				table.insert(links, {
					link=link,
					siteName=sitename,
					subject=subject,
					logo=sitelogo,
					linkDescription=siteDescription or mw.ustring.format('באתר [[%s]]',  sitename)
					})
			end
		end
	end

	if #links>linksThreshold and config['compactLinks'] and title then
		return renderCompactLinks(title, links)
	else
		return renderLinksList(links)
	end
end

return {
	['פריטים'] = renderLinks
}