Content deleted Content added
Fred Gandt (talk | contribs) testing redirected detection of explicits |
Fred Gandt (talk | contribs) test page content while we have it for redirect syntax; to finish later |
||
Line 1:
local function isEmpty(
local function notEmpty(
local function isNone(
local function alarmingMessage(
message = '<span style="color:#d33">[[Module:GetShortDescription]] '
if not preview then
message = message end return message
end
-- Grammatically reasonable concatenation of possible issues into one message per problematic link target.
local function previewWarning(
local message = ''
if quantity_of_things.params > 3 then
message = message end if quantity_of_things.descriptions > 1 then
message = message end if quantity_of_things.templates > 1 or notEmpty(
message = 'has detected that [[:'
quantity_of_things.templates mw.addWarning(
end
end
local function getWikidataDescription(
local wikidata_id = mw.wikibase.getEntityIdForTitle(
if isEmpty(
return nil end local wikidata_description, wikidata_description_lang = mw.wikibase.getDescriptionWithLang(
if isEmpty(
return nil end local result = {
if isEmpty(
-- According to the docs this is a possibility...
result.wikidata = require(
wikidata_description_lang,
wikidata_description,
Line 45 ⟶ 56:
end
local function getShortDescriptionTemplates(
local page_content = title_table:getContent()
-- Assume no content means a nonexistent title because it's cheaper than testing if it exists.
if isEmpty(
return { end local contents_of_all_short_description_templates = {}
-- Because there could be any number of short description templates, and not all where there should be; get all the templates.
for template in page_content:gmatch(
local short_description_content = mw.ustring.match(
if notEmpty(
-- Collect the contents of short description templates.
contents_of_all_short_description_templates[
end
-- An opportunity for efficiency gain exists - to break if another type of template is found e.g. citation templates,
-- but on an appallingly formatted page, a short description template down by the categories would likely be missed.
end
-- Better to lower/upper the case of the possibly very long page_content or this?
local redirect_page = mw.ustring.match(page_content, '^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*%[%[')
if redirect_page then
mw.log('redirect page')
-- pass this info along
end
return contents_of_all_short_description_templates
end
local function getShortDescription(
local contents_of_all_short_description_templates = {}
local redirected
Line 72 ⟶ 93:
-- Check for short description templates on redirect pages.
if title ~= args_name then
contents_of_all_short_description_templates = getShortDescriptionTemplates(
if contents_of_all_short_description_templates.redlink then
return contents_of_all_short_description_templates end redirected = false
end
if #contents_of_all_short_description_templates < 1 then
contents_of_all_short_description_templates = getShortDescriptionTemplates(
if notEmpty(
redirected = true end end
if contents_of_all_short_description_templates.redlink then
return contents_of_all_short_description_templates end if #contents_of_all_short_description_templates < 1 then
return nil end local quantity_of_things = {
Line 95 ⟶ 124:
-- Look through the short description templates:
for template_content_index, short_description_template_contents in ipairs(
-- Split the contents at pipes and trim.
local short_description_template_params = mw.text.split(
if #short_description_template_params > quantity_of_things.params then
quantity_of_things.params = #short_description_template_params
end
possible_short_descriptions[
-- Look through the params:
for i, param in ipairs(
if param == 'noreplace' or mw.ustring.match(
-- Take note of 'noreplace'-ing for establishment of hierarchy later.
possible_short_descriptions[
else
local has_equals = param:match(
if not has_equals or param:match(
-- Grab the short description.
if has_equals then
param = mw.ustring.gsub( end
-- If the template has both a numbered and an unnumbered short description;
-- whichever comes last (ltr) will be used by that template, so overwriting works out great.
possible_short_descriptions[
-- And we want to know the total quantity of descriptions being declared.
quantity_of_things.descriptions = quantity_of_things.descriptions + 1
Line 126 ⟶ 157:
-- Look through the possible short descriptions for definite short descriptions,
-- and prepare for working out which of possibly multiple short descriptions is actually being applied for the page:
for i, possible_short_description in ipairs(
if possible_short_description.description then
-- If a description is 'noreplace'-ing or 'none'; demote it.
if (
#possible_short_descriptions > 1 then -- But don't demote it if it's already at the bottom.
if i > 1 then
table.insert( else
else short_descriptions[ #short_descriptions+1 ] = possible_short_description end▼
end
else
end
end
end
-- Let previewWarning() work out if these numbers are bad.
previewWarning(
if #short_descriptions >= 1 then
-- Pop!
local short_description = short_descriptions[
if notEmpty(
return { end end
return nil
end
local function isSisterProjectLink(
local sister_project_prefixes = {
'wiktionary', 'wikt',
Line 167 ⟶ 205:
'phabricator', 'phab'
}
local pre_colon = title:match(
if pre_colon then
for i, sister in ipairs(
if pre_colon == sister then
return true end
end
end
Line 178 ⟶ 218:
-- Literally testing if title_table.isRedirect can be expensive;
-- processing this way resolves (multiple) redirects without the possibly expensive check.
local function getTitleAndTable(
local title_table = mw.title.new(
title_table = title_table.redirectTarget or title_table
local title = title_table.prefixedText
Line 185 ⟶ 225:
return title, title_table
end
return getTitleAndTable(
end
local function getDescription(
local args_name = args.name
if isEmpty(
return { end -- Keep the orginal name, cleaned up, and its title_table for later.
local args_name_title_table = mw.title.new(
args_name = args_name_title_table.prefixedText
if isSisterProjectLink(
return nil end local title, title_table = getTitleAndTable(
if title ~= args_name then
if isSisterProjectLink(
return nil end end
Line 209 ⟶ 255:
-- Pass args_name to getShortDescription() so previewWarning()s won't be confusing for redirects.
if notEmpty(
if only == 'explicit' then
return getShortDescription( end if only == 'wikidata' then
return getWikidataDescription( end return {
end
if notEmpty(
if prefer == 'explicit' then
local short_description = getShortDescription(
if notEmpty(
-- Assume a Wikidata search would be a bad idea for an assumed nonexistent title.
if short_description.redlink or (
return short_description end
end
return getWikidataDescription(
end
if prefer == 'wikidata' then
end
▲ if prefer == 'wikidata' then return getWikidataDescription( title, args ) or getShortDescription( args_name, args_name_title_table, title, title_table, true ) end
▲ return { alarm = 'accepts either "explicit" or "wikidata" as the value of |prefer=' }
end
end
local function main(
local result = getDescription(
if notEmpty(
if result.alarm then
result.alarm = alarmingMessage( end if args.stringify then
if result.alarm then
result = result.alarm else result = result.explicit or result.wikidata
if args.none_is_nil and isNone(
result = nil end
end
elseif not result.alarm and args.none_is_nil then
local description = result.explicit or result.wikidata
if description and args.none_is_nil and isNone(
result = nil end
end
end
Line 248 ⟶ 310:
local p = {}
function p.main(
local args = require(
if isEmpty(
return alarmingMessage( end
return main(
end
|