মডিউল:Work

উইকিসংকলন থেকে

এই মডিউলের জন্য মডিউল:Work/নথি-এ নথিপত্র তৈরি করা হয়ে থাকতে পারে

local translate2bn = require('মডিউল:সংখ্যা রূপান্তরকারী')._translate2bn

function errorMessage(text)
	-- Return a html formated version of text stylized as an error.
	local html = mw.html.create('div')
	html:addClass('error')
		:wikitext(text)
		:wikitext('[[বিষয়শ্রেণী:ভুল রচনা টেমপ্লেটযুক্ত পাতা]]')
	return tostring(html)
end

function toAbsoluteTitle( relative_title, base_title )
     --TODO: hacky implementation
    return mw.getCurrentFrame():callParserFunction( '#rel2abs', { relative_title, tostring( base_title or mw.title.getCurrentTitle() ) } )
end

function createLinkRow(link, site)
	-- Return some html stylised formated text of link
	local html = mw.html.create('div')
	html:tag('span')
		:css({['color'] = '#232388', ['font-size'] = '140%', ['line-height'] = '150%'})
		:wikitext('[[File:' .. site .. '-logo.svg|20px|' .. site .. ']]  ')
	html:wikitext(link)
	return html
end

function categorization(baseName, parameter)
	-- Return the categorisation wikitext for each element of parameter prefixed with baseName
	if parameter == nil then
		return ''
	end

	local wikitext = ''
	for _,param in pairs(mw.text.split(parameter, '/', true)) do
		wikitext = wikitext .. '[[বিষয়শ্রেণী:' .. baseName .. ' ' .. param .. ']]'
	end
	return wikitext
end

function qidForProperty(item, property)
	local statements = item:getBestStatements(property)
	if statements[1] ~= nil and statements[1].mainsnak.datavalue ~= nil then
		return statements[1].mainsnak.datavalue.value.id
	end
	return nil
end

function searchKey(key)
	-- normally the key does not contain any diacritic but it's a common misuse
	-- of the keyf = so handle at least the most usual diacritics, note than
	-- prename can be empty
	local name, prename = mw.ustring.match(key, '^([A-ZÉÈÀÇa-zéèàç-]*)[,]?[ ]*(.*)$')
	return prename .. ' ' .. 'intitle:' .. name
end

function formatLink( page, label, schema_property )
    local title = mw.title.new( toAbsoluteTitle( page ) )
    if title == nil then
        return tostring(mw.html.create('span')
            :attr('itemprop', schema_property)
            :attr('itemscope', '')
            :attr('itemtype', 'http://schema.org/Thing') --TODO: better types
            :wikitext('[[' .. page .. '|<span itemprop="name">' .. label .. '</span>]]')
        )
    end
    if title.isRedirect then
        title = title.redirectTarget
    end
    local tag = mw.html.create('span')
        :attr('itemprop', schema_property)
        :attr('itemscope', '')
        :attr('itemtype', 'http://schema.org/Thing') --TODO: better types
        :wikitext('[[' .. title.fullText .. '|<span itemprop="name">' .. label .. '</span>]]')
        :tag('link')
            :attr('itemprop', 'mainEntityOfPage')
            :attr('href', title:fullUrl(nil, 'canonical'))
            :done()
    local itemId = mw.wikibase.getEntityIdForTitle(title.fullText)
    if itemId ~= nil then
        tag:attr('itemid', 'http://www.wikidata.org/entity/' .. itemId)
    end
    return tostring(tag)
end


function parseLinkWikitext( wikitext, schema_property )
    wikitext = mw.ustring.gsub( wikitext, '%[%[([^|]*)|(.*)%]%]', function( page, link )
        return formatLink( page, link, schema_property )
    end )
    wikitext = mw.ustring.gsub( wikitext, '%[%[([^|]*)%]%]', function( page )
        return formatLink( page, mw.ustring.gsub( page, '%.*/', '') , schema_property )
    end )
    return wikitext
end


function main(frame)
	--create a keyan table of parameters with blank parameters removed
	local args = {}
	for k,v in pairs(frame:getParent().args) do
		if v ~= '' then
			args[k] = v
		end
	end

	local html = mw.html.create()

	--Use Wikidata if parameters not filled in
	local item = mw.wikibase.getEntity()
	if item == nil then
		html:wikitext('[[বিষয়শ্রেণী:উইকিউপাত্তের সঙ্গে অসংযুক্ত রচনা]]')
	else
		if args.name == nil then
			args.name = item:getLabel()
		else
			html:wikitext('[[বিষয়শ্রেণী:স্থানীয় নামযুক্ত রচনা]]')
		end
		--class
		class = qidForProperty(item, 'P31')
	end

	if args.name == nil then
		return errorMessage('The "name" parameter is mandatory and must contain the name of the work')
	end

	local main = html:tag('div')
		:addClass('vcard')
		:attr('itemscope', '')
		:attr('itemtype', 'http://schema.org/Person')
		:css({['background-color'] = '#F1F1DE', ['overflow'] = 'auto', ['border-radius'] = '0.7em', ['box-shadow'] = '0.2em 0.3em 0.2em #B7B7B7'})

	if item ~= nil and item.id ~= nil then
		main:attr('itemid', 'http://www.wikidata.org/entity/' .. item.id)
	end

function process_authors(wikidata_item, headertemplate, property_id)
        local wikidata_authors = wikidata_item:getBestStatements(property_id)
        if next(wikidata_authors) ~= nil then
            headertemplate = headertemplate:tag('div')
                :addClass('headertemplate-author'):css({['text-align']='center'})
            for index, author in pairs(wikidata_authors) do
                local author_qid = ''
                local author_label = ''
                if author.mainsnak.snaktype == 'somevalue' then
                    author_qid = 'Q4233718'
                    author_label = 'অজ্ঞাত'
                else
                    author_qid = author.mainsnak.datavalue.value.id
                    author_label = mw.wikibase.getLabel(author_qid)
                end
                if author_label == 'একাধিক লেখক' then
                    headertemplate:wikitext(parseLinkWikitext(author_label, 'লেখক'))
                else
                    headertemplate:wikitext(parseLinkWikitext('[[লেখক:' .. author_label .. '|' .. author_label .. ']]', 'লেখক'))
                end
                local author_name = mw.title.new( toAbsoluteTitle( 'লেখক:' .. author_label ) )
                if author_name.isRedirect then
                    author_name = author_name.redirectTarget
                end
                headertemplate:wikitext('[[বিষয়শ্রেণী:' .. author_name.text .. ' রচিত]]')
                if wikidata_authors[index + 1] ~= nil then
                    headertemplate:wikitext(' ও ')
                end
            end
            headertemplate = headertemplate:done()
        end
end
	
	--First row	
	local firstRow = main:tag('div')

		--Title
		firstRow:tag('h1')
			:addClass('fn')
			:attr('itemprop', 'name')
			:css({['text-align'] = 'center', ['font-size'] = '160%', ['font-weight'] = 'bold', ['border-bottom'] = 'none'})
			:wikitext(args.name)

    --লেখক
        wikidata_item = mw.wikibase.getEntity()
        process_authors(wikidata_item, main, 'P50')

	--Second row
	local secondRow = main:tag('div')

		--Interwikis
		local interwikis = secondRow:tag('div')
			:css({['float'] = 'left', ['height'] = '50%', ['font-size'] = '90%', ['text-indent'] = '2em'})
			if item ~= nil and item:getSitelink('bnwiki') ~= nil then
				interwikis:node(createLinkRow('[[w:' .. item:getSitelink('bnwiki') .. '|নিবন্ধ]]', 'wikipedia'))
			else
			end

			if item ~= nil and item.id ~= nil then
				interwikis:node(createLinkRow('[[d:' .. item.id .. '|উপাত্ত]]', 'wikidata'))
			else
				interwikis:node(createLinkRow('<span class="plainlinks">[//www.wikidata.org/w/index.php?title=Special:NewItem&site=bnwikisource&page=' .. mw.uri.encode(mw.title.getCurrentTitle().fullText) .. '&label=' .. mw.uri.encode(args.name) .. ' Données&nbsp;structurées</span>]</span>', 'উইকিউপাত্ত'))
			end

	--genre

	if args.key ~= nil then
		html:wikitext(frame:preprocess('{{DEFAULTSORT:' .. args.key .. '}}'))
	end
	
	--form
	categoryForForm = mw.loadData('মডিউল:Work/form')
	if item ~= nil then
		for _, statement in pairs(item:getBestStatements('P7937')) do
			if statement.mainsnak.datavalue ~= nil then
				formId = statement.mainsnak.datavalue.value.id
				if categoryForForm[formId] == nil then
					html:wikitext('[[বিষয়শ্রেণী:Pages using the template Work with a form without Category]]')
				elseif categoryForForm[formId] then
					html:wikitext('[[বিষয়শ্রেণী:' .. categoryForForm[formId] .. ']]')
				end
			end
		end
	end

	return tostring(html)
end

local p = {};

function p.Work( frame )
	return main( frame )
end

function p.getDatepaomTimeStatements(statements, field)
    return getDatepaomTimeStatements(statements, field)
end

return p