Module:Post

All modules: book, chronology, kbd, module, photos, post, todo, wikidata link, yesno.



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.time( frame )
    local lang = mw.language.getContentLanguage()
    -- <time datetime="{{#time: c | {{{date}}} }}">{{#time:[[Y]] F j (l), g:iA|@{{#expr: {{#time:U|{{{date}}}}} + (60*60*{{{timezone|8}}}) }} }}</time><!--

	local timestamp = string.sub( lang:formatDate( 'YmU', frame.args.timestamp ), 7 )
    local displayTimestamp = timestamp
    if frame.args.timezone ~= nil and frame.args.timezone ~= '' then
        displayTimestamp = displayTimestamp + ( tonumber( args.timezone ) * 60 * 60 )
    end

	if tonumber( frame.args.timezone ) then
	else
	end

	-- HTML output.
    local timeEl = mw.html.create( 'time' )
    timeEl:attr( 'datetime', lang:formatDate( 'c', '@' .. timestamp ) )
    local format = '[[Y]] F j (l), g:iA'
    timeEl:wikitext( lang:formatDate( format, '@' .. displayTimestamp ) )
    return tostring( timeEl )
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 prevnext = ''
	local currentPost = mw.ext.cargo.query( 'posts', 'date', { where = '_pageName = BINARY "' .. currentPage .. '"', limit = 1 } )
	if currentPost ~= nil and currentPost[1] ~= nil then
		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 = ''
		} )
		if prev[1] ~= nil then
			prevnext = prevnext .. '[[' .. prev[1]._pageName .. '|← Previous]]'
		else
			prevnext = prevnext .. '<span><!-- empty when no previous --></span>'
		end
		if prev[1] ~= nil and next[1] ~= nil then
			prevnext = prevnext .. ''
		end
		if next[1] ~= nil then
			prevnext = prevnext .. '[[' .. next[1]._pageName .. '|Next →]]'
		end
	end

	local wikisworld = ''
	local twitter = ''
	if frame.args.wikisworld ~= nil and frame.args.wikisworld ~= '' then
		wikisworld = 'This post is also on [https://wikis.world/@samwilson/' .. frame.args.wikisworld .. ' Mastodon].'
	end
	if frame.args.twitter ~= nil and frame.args.twitter ~= '' then
		twitter = 'This post was also on [https://x.com/samwilson/status/' .. frame.args.twitter .. ' Twitter].'
	end

	local styles = mw.getCurrentFrame():extensionTag( 'templatestyles', nil, { src = 'Module:Post/styles.css' } )
	return styles
		.. '<div class="mdl-post-syndications">' .. wikisworld .. ' ' .. twitter .. '</div>'
		.. '<div class="mdl-post-prevnext">' .. prevnext .. '</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