Module:Cat see if

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

There exist (or don't exist) categories in a category system difficult to overview, starting with either

  • Flags
  • Seals
  • Symbols
  • Coats of arms
  • (expandable to more topics)

followed by either 'of' or 'from' and continued with different text, as e.g. ' the community Xxxxx'.

The module is invoked by the template {{Cat see if}} intended to be transcluded from any one of these 8 categories;
it checks which one of these bundle exist and creates a {{Cat see also}} to the other categories, i.e. a maximum of 7.
The selection can be restricted to a maximum of the other 4 or 3 existing categories by a selector, which can be

  • o or of restricting to 'of'-categories
  • f or from restricting to 'from'-categories
  • * restricting to categories (of/from) as the invoking category.

Because of the performance of (max. 7) existence checks the template is considered "expensive".

Example[edit]

A transclusion of the template {{Cat see if}} within a category e.g. Seals of the Bahamas will check the existence of

when the selector is o: {{ Cat see ifo }} or, in that case, *: {{ Cat see if* }}
  • 'Flags of the Bahamas'
  • 'Symbols of the Bahamas'
  • 'Coats of arms of the Bahamas'
when the selector is f: {{ Cat see iff }}
  • 'Flags from the Bahamas'
  • 'Seals from the Bahamas'
  • 'Symbols from the Bahamas'
  • 'Coats of arms from the Bahamas'
and both groups when no selector is set: {{ Cat see if }}.

Code

local p = {}

-- function catseeif
function p.main (frame)
	local pprm = mw.getCurrentFrame(): getParent().args;
	local slct = mw.ustring.sub (pprm[1] or '', 1, 1 );	-- 'o' if only 'of', 'f' when only 'from'
	local titl = mw.title.getCurrentTitle();
	local nspc = titl.nsText;		-- category
	local part = {};
	local outt = {};
	local alts = {};
	alts[1] = 'Seals';
	alts[2] = 'Flags';
	alts[3] = 'Symbols';
	alts[4] = 'Coats of arms';

	part = mw.text.split( titl.text, ' of ', true)
	if	part[1] == 'Coats' then 
		part[2] = part[3]
		part[1] = alts[4]
	end
	if part[1] ~= nil and part[2] ~= nil then
		if slct == '*' then slct = 'o' end
		for i = 1, 4 do
			if alts[i] ~= part[1] then
				local namt = alts[i]..' of '..part[2]
				if slct ~= 'f' then 	-- not only 'from' ?
					local test = mw.title.new( namt, nspc )
					if test.exists then table.insert( outt, namt );  end
				end
			end
			local namt = alts[i]..' from '..part[2]
			if slct ~= 'o' then 	-- not only 'of' ?
				local test = mw.title.new( namt, nspc )	
				if test.exists then table.insert( outt, namt );  end
			end
		end
	end
--
	part = mw.text.split( titl.text, ' from ', true)
	if part[1] ~= nil and part[2] ~= nil then
		if slct == '*' then slct = 'f' end
		for i = 1, 4 do
			if alts[i] ~= part[1] then
				local namt = alts[i]..' from '..part[2]
				if slct ~= 'o' then 	-- not only 'of' ?
					local test = mw.title.new( namt, nspc )
					if test.exists then table.insert( outt, namt );  end
				end
			end
			local namt = alts[i]..' of '..part[2]
			if slct ~= 'f' then 	-- not only 'from' ?
				local test = mw.title.new( namt, nspc )
				if test.exists then table.insert( outt, namt );  end
			end
		end
	end
	if outt[1] ~= nil then
		table.insert( outt, "📎" );
		return mw.getCurrentFrame():expandTemplate{ title = 'Cat see also', args = outt };
	end
end


return p;