Module:Ident

From ArchivesWiki

Documentation for this module may be created at Module:Ident/doc

local cargo = mw.ext.cargo

-- --
-- Entry point.
function main( args )
	-- Find the ident.
	local queryArgs = {
		where = 'ident = \'' .. args.ident .. '\''
	}
	local identData = cargo.query( 'idents', '_pageName=page,ident=ident', queryArgs )

	-- Not found.
	if #identData ~= 1 then
		local err = mw.html.create( 'span' )
		err:attr( 'class', 'error' )
		err:wikitext( 'Warning: ident [[' .. args.ident .. '|<code>' .. args.ident .. '</code>]] pending (reload page)' )
		err:wikitext( '[[Category:Pages with unresolved idents]]' )
		return tostring( err )
	end
	local ident = identData[1]
	
	-- Wrong page.
	if ident.page ~= mw.title.getCurrentTitle().text then
		local err = mw.html.create( 'span' )
		err:attr( 'class', 'error' )
		err:wikitext( 'Warning: ident [[' .. ident.ident .. '|<code>' .. ident.ident .. '</code>]] is already in use for: [[' .. ident.page  .. ']]' )
		err:wikitext( '[[Category:Pages with clashing idents]]' )
		return tostring( err )
	end

	-- Found.
	local out = mw.html.create( 'dfn' )
	local identUri = mw.uri.canonicalUrl( ident.ident )
	out
		:attr( 'itemprop', 'identifier' )
		:attr( 'title', 'This page\'s identifier' )
		:attr( 'class', 'plainlinks' )
		:wikitext( '[[' .. ident.ident .. '|' .. identUri.hostPort .. identUri.path .. ']]' )
    return tostring( out )
end

return {
 main = function( frame ) return main( frame.args ) end;
}

-- Testing:
-- =p.main({args={ident='foo'}})