Module:Sandbox/ElanHR/test-category-module
Appearance
-- For unit tests, see [[Module:Bananas/testcases]]
local p = {}
local messageBox = require('Module:Message box')
local mArguments -- Lazily initialise Module:Arguments
local wd = require('Module:Wikidata')
local wikidata_category_subclasses = {'Q28326730', 'Q23894246', 'Q13331174', 'Q20010800', 'Q58118449', 'Q38084761', 'Q30330522', 'Q24514938', 'Q24046192', 'Q23894233', 'Q20769287', 'Q59542487', 'Q59541917', 'Q56428020', 'Q54662266', 'Q30432511', 'Q24574745', 'Q24571879', 'Q15647814', 'Q15407973', 'Q4167836'}
-- local wd_id = 'Q4115189'
local wd_id = wd.pageId()
local set_cat_desc = 'Wikipedia:Categorization#Set category'
local topic_cat_desc = 'Wikipedia:Categorization#Topic category'
function p.get_page_type()
local page_types = {}
-- check if it's an instance of (P31) a country (Q6256) and terminate the chain if it is
local inst = mw.wikibase.getAllStatements(wd_id, "P31")
if #inst > 0 then
for k, v in ipairs(inst) do
local instid = v.mainsnak.datavalue.value.id
-- stop if it's a country (or a country within the United Kingdom if skip is true)
--if instid == "Q6256" or (skip and instid == "Q3336843") then
if instid == 'Q59542487' then
page_types['SET'] = true
elseif instid == 'Q59541917' then
page_types['TOPIC'] = true
elseif instid == 'Q15647814' then
page_types['ADMIN'] = true
end
end
end
return page_types
end
-- {{#invoke:Message box|cmbox}}
-- {{set cat}} definition
-- {{Cmbox
-- | type = notice
-- | text = This is a '''[[Wikipedia:Categorization#Set category|set category]]'''. It should only contain pages that are '''{{#if:{{{first|}}}|{{{first}}}|{{#if:{{{alternative|}}}|{{{alternative}}}|{{PAGENAME}}}}}}''' or '''lists of {{#if:{{{alternative|}}}|{{{alternative}}}|{{PAGENAME}}}}''', as well as '''[[Wikipedia:Categorization#Subcategorization|subcategories]]''' containing those things. Topics about {{#if:{{{alternative|}}}|{{{alternative}}}|{{PAGENAME}}}} in general should be placed in {{#if:{{{topic|}}}|[[:Category:{{{topic}}}]] or one of it subcategories|relevant [[Wikipedia:Categorization#Topic category|topic categories]]}}. {{#if:{{{single}}}|Categories related to only an individual {{{single}}} should not be in this category.}}
-- }}
-- {{Single namespace|category|[[Category:Set categories]]}
-- {{Wikipedia category|container=yes|hidden=yes|tracking=yes|shortcut=WP:CINA}}
local function get_sparql_queries()
local sparql_queries = {}
for i, item in pairs(mw.wikibase.getBestStatements(wd_id, 'P3921' )) do
sparql_queries[i] = item['mainsnak']['datavalue']['value']
end
return sparql_queries
end
-- {{#if:{{{sparql|}}}|
-- [//query.wikidata.org/# {{urlencode:{{{sparql|}}}|PATH}} SPARQL] {{!}} }}
function p.get_wdqs_link_for_query_with_en_articles(query)
local select_query = 'SELECT ?item ?itemLabel ?itemDescription ?article WHERE {SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }' .. query .. ' ?article schema:about ?item . ?article schema:isPartOf <https://en.wikipedia.org/>. }'
local link_text = ''
if query then
link_text = "https://query.wikidata.org/#" .. mw.uri.encode( select_query, "PATH" ) ..""
end
return link_text
end
function p.get_wdqs_link_for_query_without_en_articles(query)
local select_query = 'SELECT ?item ?itemLabel ?itemDescription ?article WHERE {SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }' .. query .. ' FILTER NOT EXISTS { ?article schema:about ?item . ?article schema:isPartOf <https://en.wikipedia.org/>. } }'
local link_text = ''
if query then
link_text = "https://query.wikidata.org/#" .. mw.uri.encode( select_query, "PATH" ) ..""
end
return link_text
end
local function get_category_contains()
return ''
end
function p.hello()
return "Hello, world!"
end
function p.test_template()
local box = messageBox.main( 'cmbox', {
type='notice',
text='This is a test template. Wikidata ID:' .. wd.pageId(),
-- More parameters...
})
return box
end
function p.get_set_cat_template(set_item, set_item_pluralized, topic)
local _item
if set_item then
_item = set_item
elseif set_item_pluralized then
_item = set_item_pluralized
else
_item = mw.title.getCurrentTitle().text
end
local _item_pluralized
if set_item then
_item_pluralized = set_item
elseif set_item_pluralized then
_item_pluralized = set_item_pluralized
else
_item_pluralized = mw.title.getCurrentTitle().text
end
local _topic
if topic then
_topic = topic
else
_topic = "[[:Category:{{{topic}}}]]"
end
-- local set_cat_item = {}
-- set_cat_item['entity-type'] = 'item'
-- set_cat_item['numeric-id'] = '59542487'
local set_cat_text = "This is a [[".. set_cat_desc .."|set category]]. It should only contain pages that are " .. _item .. " or lists of " .. _item_pluralized .. "as well as [[Wikipedia:Categorization#Subcategorization|subcategories]] containing those things."
-- "Topics about " .. _item_pluralized .. " in general should be placed in " .. _topic .. " or one of it subcategories|relevant [[Wikipedia:Categorization#Topic category|topic categories]]}}. {{#if:{{{single}}}|Categories related to only an individual {{{single}}} should not be in this category.}}"
local sparql_queries = get_sparql_queries()
if sparql_queries then
set_cat_text = set_cat_text .. '\n\n\nTools:\n<div class="toccolours mw-collapsible mw-collapsed";">\n'
for i, item in pairs(sparql_queries) do
set_cat_text = set_cat_text .. '* ['.. p.get_wdqs_link_for_query_with_en_articles(item) ..' EN articles]\n'
set_cat_text = set_cat_text .. '* ['.. p.get_wdqs_link_for_query_without_en_articles(item) ..' No EN articles]\n'
set_cat_text = set_cat_text .. '* [https://petscan.wmflabs.org/?common_wiki_other=&edits%5Bbots%5D=both&ns%5B0%5D=1&wikidata_item=any&interface_language=en&language=en&search_max_results=500&categories='.. mw.title.getCurrentTitle().text ..'&edits%5Bflagged%5D=both&edits%5Banons%5D=both&cb_labels_any_l=1&project=wikipedia&cb_labels_no_l=1&depth=5&cb_labels_yes_l=1&langs_labels_any=&doit= petscan]'
set_cat_text = set_cat_text .. '</div>'
end
end
local output_wikitext = messageBox.main( 'cmbox', {
type='notice',
text=set_cat_text
-- More parameters...
})
return output_wikitext
end
function p._main(args)
local page_types = p.get_page_type()
if page_types['SET'] then
return p.get_set_cat_template(args['first'], args['alternative'])
--.. mw.title.getCurrentTitle().prefixedText .. '\n ' .. mw.title.getCurrentTitle().text .. '\n ' .. mw.title.getCurrentTitle().baseText .. '\n ' .. mw.title.getCurrentTitle().subpageText .. ' '
elseif page_types['TOPIC'] then
return 'TOPIC CAT'
end
--if page_types['TOPIC'] then
return tostring(page_types) ..' | HELLO |'.. tostring(wd_id)
--end
end
function p.set_cat_template(frame)
mArguments = require('Module:Arguments')
local args = mArguments.getArgs(frame, {removeBlanks = false})
local ret = p._main(args)
return ret or ''
end
function p.dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
return p