Module:Post

Logic for the {{post}} template.


local p = {}

function p.isMainOrArchive( frame )
	return isMainOrArchive( frame.args.title )
end

function isMainOrArchive( title )
	return title == 'Welcome' or string.match( title, '[0-9]+[ _]archive' ) ~= nil
end

function p.main( frame )
	local args = frame.args or {}
	local currentPage = mw.title.getCurrentTitle().prefixedText
	local out = ''
	if mw.getCurrentFrame():getParent() ~= nil and not isMainOrArchive( currentPage ) then
		local cargoStore = '{{#cargo_store: _table=posts | '
			.. '| date     = ' .. ( args.date or '' )
			.. '| location = ' .. ( args.location or '' )
			.. '| timezone = ' .. ( args.timezone or '' )
			.. '| keywords = ' .. ( args.keywords or args.categories or '' )
			.. '| description = ' .. ( args.description or '' )
			.. '}}'
		local parentFrame = mw.getCurrentFrame():getParent()
		out = out
			.. parentFrame:preprocess( '<includeonly>' .. cargoStore .. '</includeonly>' )
			.. parentFrame:callParserFunction( '#seo', { '',
				 description = args.description or 'A blog post by Sam Wilson.',
				 keywords = ( args.keywords or args.categories or nil ),
				 author = 'Sam Wilson',
				 published_time = args.date or nil
			} ) 
	end
	return out
end

function p.footer( frame )
	local currentPage = ''
	if frame.args.current_page == nil then
		currentPage = mw.title.getCurrentTitle().prefixedText
	else
		currentPage = frame.args.current_page
	end
	if isMainOrArchive( currentPage ) then
		return ''
	end
	local currentPost = mw.ext.cargo.query( 'posts', 'date', { where = '_pageName="' .. currentPage .. '"' } )
	if currentPost == nil or currentPost[1] == nil then
		return ''
	end
	local prev = mw.ext.cargo.query( 'posts', '_pageName,date', {
		where = 'date < "' .. currentPost[1].date .. '"',
		limit = 1,
		orderBy = 'date DESC',
		default = '',
		moreResultsText = ''
	} )
	local next = mw.ext.cargo.query( 'posts', '_pageName,date', {
		where = 'date > "' .. currentPost[1].date .. '"',
		limit = 1,
		orderBy = 'date ASC',
		default = '',
		moreResultsText = ''
	} )
	local out = ''
	if prev[1] ~= nil then
		out = out .. '[[' .. prev[1]._pageName .. '|← Previous]]'
	else
		out = out .. '<span><!-- empty when no previous --></span>'
	end
	if prev[1] ~= nil and next[1] ~= nil then
		out = out .. ''
	end
	if next[1] ~= nil then
		out = out .. '[[' .. next[1]._pageName .. '|Next →]]'
	end
	local styles = mw.getCurrentFrame():extensionTag( 'templatestyles', nil, { src = 'Module:Post/styles.css' } )
	return styles .. '<div class="mdl-post-prevnext">' .. out .. '</div>'
end

function p.keywordsList()
	local cargo = mw.ext.cargo
    local fields = 'posts__keywords._value=keyword, COUNT(*)=count'
    local args = {
        where = 'posts__keywords._value IS NOT NULL',
        groupBy = 'posts.keywords',
        orderBy = 'COUNT(*) DESC',
        limit = '1000'
    }
    local results = cargo.query( 'posts', fields, args )
    local out = ''
    for r = 1, #results do
        local result = results[r]
        local keywordTitle = mw.title.new( result.keyword )
        local redirTitle = keywordTitle.redirectTarget
        local link = ''
        if redirTitle then
        	link = '[[' .. redirTitle.text .. '|' .. keywordTitle.text .. ']] / [[' .. redirTitle.text .. ']]'
        else
        	link = '[[' .. keywordTitle.text .. ']]'
        end
        local url = 'https://samwilson.id.au/Special:Drilldown/posts?drilldown=posts&_search_keywords' .. mw.uri.encode( '[0]' ) .. '=' ..  mw.uri.encode( result.keyword )
        out = out .. link .. ' ([' .. url .. ' ' .. result.count .. ']) '
    end
    return '<span class="plainlinks">' .. out .. '</span>'
end

return p