এই মডিউলের জন্য মডিউল: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')
    if site == 'ইনভেণ্টেয়ার' then
        logo_file_name = 'Inv-icon-192.svg'
    else
        logo_file_name = site .. '-logo.svg'
    end
	html:tag('span')
		:css({['color'] = '#232388', ['font-size'] = '140%', ['line-height'] = '150%'})
		:wikitext('[[File:' .. logo_file_name .. '|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, headertemplate_left, headertemplate_right, 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 = 'অজ্ঞাত'
                    author_item = nil
                    author_sitelink = 'লেখক:অজ্ঞাত'
                else
                    author_qid = author.mainsnak.datavalue.value.id
                    author_label = mw.wikibase.getLabel(author_qid)
                    author_item = mw.wikibase.getEntity(author_qid)
                    author_sitelink = author_item:getSitelink()
                end
                if author_label == 'একাধিক লেখক' then
                    headertemplate:wikitext(parseLinkWikitext(author_label, 'লেখক'))
                else
                    headertemplate:wikitext(parseLinkWikitext('[[' .. author_sitelink .. '|' .. author_label .. ']]', 'লেখক'))
                end
                local author_name = mw.title.new( toAbsoluteTitle( 'লেখক:' .. author_label ) )
                if author_name.isRedirect then
                    author_name = author_name.redirectTarget
                end
                local category_name_author = mw.ustring.gsub(author_sitelink, 'লেখক:', '')
                headertemplate:wikitext('[[বিষয়শ্রেণী:' .. category_name_author .. ' রচিত]]')
                if wikidata_authors[index + 1] ~= nil then
                    headertemplate:wikitext(' ও ')
                end
            end
            local author_images = {}
            if next(author_item) ~= nil then
                local author_images_1 = author_item:getBestStatements('P18')
                if next( author_images_1 ) ~= nil then
                    table.insert(author_images, author_images_1[1].mainsnak.datavalue.value)
                end
            end
            if author_images[1] ~= nil then
                headertemplate_right:wikitext('[[চিত্র:' .. author_images[1] .. '|120px]]')
            end
            if author_images[2] ~= nil then
                headertemplate_left:wikitext('[[চিত্র:' .. author_images[2] .. '|120px]]')
            end
            headertemplate = headertemplate:done()
        end
end
	
    local headertemplate_top = main:tag('div')
        :attr('id', 'headertemplate')
        :addClass('ws-noexport')
        :css({display = 'table', width = "98%"})
    local headertemplate_left = headertemplate_top:tag('div'):addClass('headertemplate-left'):css({width = "16%", display = "table-cell", ["background-color"] = "#f4f0e5", ["border-top"] = "1px solid #A0A0A0"})
    local headertemplate = headertemplate_top:tag('div'):css({display = 'table-cell', width = "66%"})
    local headertemplate_right = headertemplate_top:tag('div'):addClass('headertemplate-right'):css({width = "16%", display = "table-cell", ["background-color"] = "#f4f0e5", ["border-top"] = "1px solid #A0A0A0"})

	--First row	
	local firstRow = headertemplate: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, headertemplate, headertemplate_left, headertemplate_right, 'P50')

	--Second row
	local secondRow = headertemplate_left: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

            interwikis:node(createLinkRow('<span class="plainlinks">[//inventaire.io/entity/wd:' .. item.id .. '?lang=bn ইনভেণ্টেয়ার]</span>', 'ইনভেণ্টেয়ার'))

	--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

	--genre
	categoryForGenre = mw.loadData('মডিউল:Work/genre')
	if item ~= nil then
		for _, statement in pairs(item:getBestStatements('P136')) do
			if statement.mainsnak.datavalue ~= nil then
				genreId = statement.mainsnak.datavalue.value.id
				if categoryForGenre[genreId] == nil then
					html:wikitext('[[বিষয়শ্রেণী:Pages using the template Work with a genre without Category]]')
				elseif categoryForGenre[genreId] then
					html:wikitext('[[বিষয়শ্রেণী:' .. categoryForGenre[genreId] .. ']]')
				end
			end
		end
	end

	--award
	categoryForAward = mw.loadData('মডিউল:Work/award')
	if item ~= nil then
		for _, statement in pairs(item:getBestStatements('P166')) do
			if statement.mainsnak.datavalue ~= nil then
				awardId = statement.mainsnak.datavalue.value.id
				if categoryForAward[awardId] == nil then
					html:wikitext('[[বিষয়শ্রেণী:Pages using the template Work with a award without Category]]')
				elseif categoryForAward[awardId] then
					html:wikitext('[[বিষয়শ্রেণী:' .. categoryForAward[awardId] .. ']]')
				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