Module:Sandbox/Smalljim/DiscussionIndexTest

--[[

20130330: I have unashamedly copied this from User:Dragons flight's DiscussionIndex module, whose comments follow:

This module provides a formatted index of the discussions that have occurred on a target page.

]]

p = {}; pp = require( 'Module:Sandbox/Smalljim/ParsePageTest' ); --a set of page parsing functions

function p.summary( frame ) local target = frame.args[1] or frame.args.target or ''; --the page to parse local exc = frame.args.excerpts or false; --opt to show an excerpt from start of each section if type( exc ) == 'string' then --deal with optional ways of specifying show excerpt exc = exc:lower; if exc == 'false' or exc == '' or exc == '0' or exc == 'no' then exc = false; else exc = true; --anything but 'false', '', '0' or 'no' (case insensitive) end end local rt = ( tonumber( frame.args.recent ) or 120 )*60; --green cell if < 120 min or use "recent" to specify local ot = ( tonumber( frame.args.old ) or 60*24*2 )*60; --red cell if > 2 days or use "old" to specify

if target == '' then --no target page specified return ''; end local text = frame:preprocess( '' ); --does this expand templates in target page?

return frame:preprocess( p._summary( text, target, exc, rt, ot ) ); end function p._summary( text, target, exc, rt, ot ) local lang = mw.getContentLanguage; local now = lang:formatDate( 'U' ); local sections, headings = pp.getSections( text, 2 ); --2 is header level local result; result = '== Discussion summary report for ' .. target .. ' ==\n\n' result = result .. 'This is an automated summary of the discussions occurring on ' .. target .. '.\n\n' .. 'It was last generated at ' .. lang:formatDate( 'l, j F Y, H:i:s' ) .. ' UTC ' .. '([ update now]).\n\n' .. 'The current version of this report is still experimental and may contain ' .. 'significant inaccuracies.\n\n' if exc then result = result .. '{| class="wikitable" \n'; else result = result .. '{| class="wikitable sortable" \n'; --no excerpts, so can make it sortable end result = result .. '! Section title !! data-sort-type="number" | Age ' .. ' !! data-sort-type="number" | ' .. ' Last Comment !! Bytes !! Originator !! data-sort-type="number" | Other Participants \n|-\n|'; local result_lines = {}; for k, tt in ipairs( sections ) do       local users = pp.getUsers( tt, false, true ); local times = pp.getTimestamps( tt ); local min_time, max_time local min_time_string, max_time_string local line_item = {};

for k, v in ipairs (times) do --drop times in the future if now - v[2] < 0 then table.remove( times, k)           end end for k, v in ipairs( times ) do           if min_time == nil or min_time > v[2] then min_time = v[2]; end if max_time == nil or max_time < v[2] then max_time = v[2]; end end for k, v in ipairs( times ) do           if min_time == v[2] then min_time_string = v[1]; end if max_time == v[2] then max_time_string = v[1]; end end min_time = now - (min_time or now); max_time = now - (max_time or now);

local user_list = {} for _, tt in pairs( users ) do           table.insert( user_list, tt[2] ); end if headings[k] ~= '' and user_list[1] ~= nil then line_item = { pp.formatSectionLink( target, headings[k] ), p._formatTimeDiff(min_time, rt, ot ), p._formatTimeDiff(max_time, rt, ot ), tostring( #tt ), user_list[1] };

table.remove( user_list, 1 ); table.sort( user_list ); table.insert( line_item, tostring( #user_list ) .. ': ' .. table.concat( user_list, ', ' ) ) if exc then table.insert( line_item, pp.getExcerpt( tt, 500 ) ) end table.insert( result_lines, line_item ); end end

comp = function( a, b ) local a2, b2       a2 = mw.ustring.gsub( a[3], '%b<>', '' ); a2 = mw.ustring.gsub( a2, '.*|%s*', '' ); b2 = mw.ustring.gsub( b[3], '%b<>', '' ); b2 = mw.ustring.gsub( b2, '.*|%s*', '' ); a2 = tonumber( mw.ustring.match( a2, '^(%d-)_' ) ) or 0; b2 = tonumber( mw.ustring.match( b2, '^(%d-)_' ) ) or 0; return a2 < b2; end table.sort( result_lines, comp );

local result_lines2 = {}; local row; for k, tt in ipairs( result_lines ) do       if exc then local excerpt = tt[#tt]; table.remove( tt ); row = table.concat( tt, '||' ); table.insert( result_lines2, row ); table.insert( result_lines2,                ' colspan=6 style="padding-left:2em; padding-top:0.5em; padding-bottom:0.5em;" | ' ..excerpt .. ' ' ); else row = table.concat( tt, '||' ); table.insert( result_lines2, row ); end end

result = result .. table.concat( result_lines2, '\n|-\n|' ) .. "\n|}"; return result; end

function p._formatTimeDiff( tv, rt, ot ) local tv_string; tv_string = ' ' .. tostring(tv) .. '_ ' .. mw.ustring.gsub( pp.formatDateDiff(tv), ' ', ' ' ); if tv < rt then tv_string = ' style="background:#EFE" | ' .. tv_string; --green cell elseif tv > ot then tv_string = ' style="background:#FEE" | ' .. tv_string; --red cell end return tv_string; end

return p;