Module:Item

From ArchivesWiki

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

local dateModule = require( 'Module:Date' )
local p = {}

-- Output a link to an item's storage location.
p.storageLocation = function ( frame )
	if frame.args.storage_location == nil or frame.args.storage_location == '' then
		return ''
	end
	local sl = frame.args.storage_location

	local key = nil
	local keyDisplay = ''
	if frame.args.storage_location_sort_key ~= nil and frame.args.storage_location_sort_key ~= '' then
		key = frame.args.storage_location_sort_key
		keyDisplay = '&nbsp;(<em>sort key:</em> ' .. key
		local prev = mw.ext.cargo.query( 'items', '_pageName, storage_location_sort_key', {
			where = 'storage_location = "' .. sl .. '" AND storage_location_sort_key < "' .. key .. '"',
			limit = 1,
			orderBy = 'storage_location_sort_key DESC',
			default = '',
			moreResultsText = ''
		} )
		if prev[1] ~= nil then
			keyDisplay = keyDisplay .. ' [[' .. prev[1]._pageName .. '|← Previous]]'
		end
		local next = mw.ext.cargo.query( 'items', '_pageName, storage_location_sort_key', {
			where = 'storage_location = "' .. sl .. '" AND storage_location_sort_key > "' .. key .. '"',
			limit = 1,
			orderBy = 'storage_location_sort_key ASC',
			default = '',
			moreResultsText = ''
		} )
		if next[1] ~= nil then
			keyDisplay = keyDisplay .. ' [[' .. next[1]._pageName .. '|Next →]]'
		end
		keyDisplay = keyDisplay .. ')'
	end
	return '[[' .. sl .. ']]' .. keyDisplay
end

-- Normalize a filename by stripping any leading 'File:' component.
p.cleanFilename = function ( frame )
	if string.lower( string.sub( frame.args.filename, 1, 4 ) ) == 'file' then
		return string.sub( frame.args.filename, 6 )
	end
	return frame.args.filename
end

-- Get wikitext for a single gallery item.
function getGalleryItem( item, showStorageLocation )
	local sortKey = ''
	if showStorageLocation and item.storage_location_sort_key then
		sortKey = '<strong title="Storage location sort key">' .. item.storage_location_sort_key .. '</strong>:&ensp;'
	end
	local header = '<div class="mdl-item-item-header">' .. sortKey .. '[[' .. item._pageName .. ']]</div>'
	local image = ''
	if item.image then
		image = '<div class="mdl-item-item-image">'
			.. '[[File:' .. item.image .. '|250x250px|link=' .. item._pageName .. ']]</div>'
	end
	local date = ''
	if item.date ~= nil and item.date ~= '' then
		date = dateModule.format( { args={ timestamp=item.date, precision=item.date_precision } } )
	else
		date = '<em>Undated</em>'
	end
	return '<div class="mdl-item-item">' .. header .. image .. date .. '</div>'
end

-- Get wikitext for a single list item.
function getListItem ( item )
	local li = '<li>'
	if item.type ~= nil and item.type ~= '' then
		li = li .. item.type .. ': '
	end
	if item.date then
		local date = dateModule.format( { args={ timestamp=item.date, precision=item.date_precision } } )
		li = li .. date .. ' '
	else
		li = li .. "''Undated''"
	end
	li = li .. '[[:' .. item._pageName .. ']] ' .. ( item.description or '' )
	return li .. '</li>'
end

function getPageIdent( pageId )
	local opts = {
		where = '_pageID=' .. pageId,
		limit = 1,
		format = 'list',
		noHtml = true,
		moreResultsText = ''
	}
    local results = mw.ext.cargo.query( 'idents', 'ident', opts )
	if #results == 1 then
		return results[ 1 ].ident
	end
	return ''
end

p.items = function ( frame )
	local fields = '_pageName=_pageName, parent_item_sort_key=parent_item_sort_key, image=image, type=type,'
		.. ' date=date, date_precision=date_precision, storage_location_sort_key=storage_location_sort_key,'
		.. ' description=description'
	local currentTitle = mw.title.getCurrentTitle().text
	local where = ''
	if frame.args and frame.args.where ~= nil and frame.args.where ~= '' then
		where = frame.args.where
	else
		where = 'storage_location = "' .. currentTitle .. '"'
			.. ' OR people HOLDS "' .. currentTitle .. '"'
			.. ' OR places HOLDS "' .. currentTitle .. '"'
			.. ' OR keywords HOLDS "' .. currentTitle .. '"'
		local ident = getPageIdent( mw.title.getCurrentTitle().id )
		if ident ~= '' then
			where = where .. ' OR storage_location = "' .. ident .. '"'
				.. ' OR people HOLDS "' .. ident .. '"'
				.. ' OR places HOLDS "' .. ident .. '"'
				.. ' OR keywords HOLDS "' .. ident .. '"'
		end
	end
	local order = 'date ASC'
	if frame.args.order ~= nil and frame.args.order ~= '' then
		order = frame.args.order
	end
    local cargoArgs = {
        where = where,
        orderBy = order,
        limit = 1000
    }
    local results = mw.ext.cargo.query( 'items', fields, cargoArgs )
    if #results == 0 then
    	if frame.args.default ~= nil then
    		return frame.args.default
    	else
	    	return 'No items found. You can add one via [[Main Page#create|the form on the homepage]].'
	    end
    end
    local out = ''
   	if frame.args.format and frame.args.format == 'list' then
	    out = out .. '<ol class="mdl-item-itemlist">'
	    for r = 1, #results do
	        out = out .. getListItem( results[r] )
	    end
	    out = out .. '</ol>'
   	else
	    out = out .. '<div class="mdl-item-items">'
   		local showStorageLocation = frame.args.show_storage_location and mw.text.trim( frame.args.show_storage_location ) ~= '' and frame.args.show_storage_location ~= nil
	    for r = 1, #results do
	        out = out .. getGalleryItem( results[r], showStorageLocation )
	    end
	    if #results < 4 then
	    	for i=#results + 1, 4, 1 do
	    		out = out .. '<div class="mdl-item-item mdl-item-item-placeholder"></div>'
	    	end
	    end
	    out = out .. '</div>'
    end
    return mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = "Module:Item/styles.css" } )
    	.. tostring( out )
end

-- =p.items({args={where="YEAR(date)=1918", show_storage_location=""}})
return p