„Modul:JSTOR“ – Versionsunterschied
Erscheinungsbild
[gesichtete Version] | [gesichtete Version] |
Inhalt gelöscht Inhalt hinzugefügt
2022-10-21 |
2022-10-26 |
||
Zeile 1: | Zeile 1: | ||
local JSTOR = { suite = "JSTOR", |
local JSTOR = { suite = "JSTOR", |
||
serial = "2022-10- |
serial = "2022-10-26", |
||
⚫ | |||
item = 71782977 } |
item = 71782977 } |
||
--[=[ |
--[=[ |
||
Zeile 6: | Zeile 7: | ||
and other issues dealing with JSTOR ID. |
and other issues dealing with JSTOR ID. |
||
]=] |
]=] |
||
local Failsafe = JSTOR |
local Failsafe = JSTOR |
||
local Externals |
local Externals |
||
⚫ | |||
if mw.site.server:find( ".beta.wmflabs.org", 4, true ) then |
|||
require( "strict" ) |
|||
⚫ | |||
local Config = { |
|||
⚫ | |||
errCat = false, |
|||
errClass = "error_jstor", |
|||
errClasses = false, |
|||
errHide = false, |
|||
⚫ | |||
errInvalid = { en = "Invalid:", |
|||
⚫ | |||
errMissing = { en = "Missing ID", |
|||
⚫ | |||
errUnkown = { en = "Unkown parameter:", |
|||
⚫ | |||
showArticle = "JSTOR" |
|||
} |
|||
Zeile 38: | Zeile 17: | ||
-- apply -- string, with message key |
-- apply -- string, with message key |
||
-- Returns message text; at least english |
-- Returns message text; at least english |
||
local entry = |
local entry = JSTOR.config[ apply ] |
||
local r |
local r |
||
if entry then |
if entry then |
||
Zeile 46: | Zeile 25: | ||
end |
end |
||
else |
else |
||
local e = mw.html.create( "span" ) |
|||
:addClass( "error" ) |
|||
⚫ | |||
⚫ | |||
r = tostring( e ) |
|||
end |
end |
||
return r |
return r |
||
Zeile 78: | Zeile 60: | ||
-- about -- string, with explanation |
-- about -- string, with explanation |
||
-- Returns message with markup |
-- Returns message with markup |
||
local |
local e = mw.html.create( "span" ) |
||
local r |
local r |
||
JSTOR.config.first() |
|||
⚫ | |||
if JSTOR.config.suite and |
|||
⚫ | |||
JSTOR.config.suite ~= "" then |
|||
⚫ | |||
:wikitext( JSTOR.config.suite ) |
|||
end |
end |
||
e:wikitext( factory( alert ) ) |
|||
⚫ | |||
if not JSTOR.config.frame then |
|||
JSTOR.config.frame = mw.getCurrentFrame() |
|||
end |
end |
||
r = JSTOR.config.frame:callParserFunction( "REVISIONID", |
|||
{ [1] = "" } ) |
|||
⚫ | |||
e:addClass( JSTOR.config.errClass ) |
|||
Config.errHide = false |
|||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
e:addClass( "error" ) |
|||
⚫ | |||
⚫ | |||
e:css( { display = "none" } ) |
|||
⚫ | |||
style = "style='display:none'" |
|||
else |
|||
⚫ | |||
end |
end |
||
if |
if JSTOR.config.errClasses then |
||
e:addClass( JSTOR.config.errClasses ) |
|||
⚫ | |||
end |
end |
||
r = tostring( e ) |
|||
r = string.format( "<span class=\"%s\" %s>%s</span>", |
|||
⚫ | |||
if about then |
if about then |
||
r = string.format( "%s %s", r, about ) |
r = string.format( "%s %s", r, about ) |
||
end |
end |
||
if |
if JSTOR.config.errCat then |
||
if |
if JSTOR.config.errNS then |
||
local ns = mw.title.getCurrentTitle().namespace |
local ns = mw.title.getCurrentTitle().namespace |
||
local st = type( |
local st = type( JSTOR.config.errNS ) |
||
if st == "string" then |
if st == "string" then |
||
local space = string.format( ".*%%s%d%%s.*", ns ) |
local space = string.format( ".*%%s%d%%s.*", ns ) |
||
local spaces = string.format( " %s ", |
local spaces = string.format( " %s ", |
||
JSTOR.config.errNS ) |
|||
if spaces:match( space ) then |
if spaces:match( space ) then |
||
JSTOR.config.errNS = false |
|||
end |
end |
||
elseif st == "table" then |
elseif st == "table" then |
||
for i = 1, # |
for i = 1, #JSTOR.config.errNS do |
||
if |
if JSTOR.config.errNS[ i ] == ns then |
||
JSTOR.config.errNS = false |
|||
break -- for i |
break -- for i |
||
end |
end |
||
Zeile 126: | Zeile 108: | ||
end |
end |
||
end |
end |
||
if not |
if not JSTOR.config.errNS then |
||
r = string.format( "%s[[Category:%s]]", |
r = string.format( "%s[[Category:%s]]", |
||
⚫ | |||
end |
end |
||
end |
end |
||
Zeile 156: | Zeile 139: | ||
r = Externals.URIutil |
r = Externals.URIutil |
||
else |
else |
||
local e = mw.html.create( "span" ) |
|||
:addClass( "error" ) |
|||
⚫ | |||
r = tostring( e ) |
|||
end |
end |
||
end |
end |
||
return r |
return r |
||
end -- fetch() |
end -- fetch() |
||
JSTOR.config.first = function () |
|||
-- Initialize configuration |
|||
⚫ | |||
local lucky, json, storage |
|||
JSTOR.config.loaded = true |
|||
JSTOR.config.frame = JSTOR.config.frame or |
|||
mw.getCurrentFrame() |
|||
storage = JSTOR.config.frame:getTitle() |
|||
⚫ | |||
storage, JSTOR.setup ) |
|||
lucky, json = pcall( mw.loadJsonData, storage ) |
|||
if type( json ) == "table" then |
|||
for k, v in pairs( json ) do |
|||
if not JSTOR.config[ k ] then |
|||
⚫ | |||
⚫ | |||
end -- for k, v |
|||
⚫ | |||
⚫ | |||
end -- JSTOR.config.first() |
|||
Zeile 182: | Zeile 191: | ||
legal = r:match( "^i?[1-9]%d*$" ) or |
legal = r:match( "^i?[1-9]%d*$" ) or |
||
r:match( "^j%.%w+%.?%d*$" ) or |
r:match( "^j%.%w+%.?%d*$" ) or |
||
r:match( "^resrep%d |
r:match( "^resrep%d$" ) |
||
end |
end |
||
elseif suite == "journals" then |
elseif suite == "journals" then |
||
Zeile 211: | Zeile 220: | ||
if r then |
if r then |
||
local show = appear |
local show = appear |
||
if show then |
if type( show ) == "string" then |
||
show = mw.text.trim( show ) |
show = mw.text.trim( show ) |
||
if |
if show == "" or show == "-" then |
||
show = false |
show = false |
||
end |
end |
||
Zeile 223: | Zeile 232: | ||
suite, r, show ) |
suite, r, show ) |
||
if not appear then |
if not appear then |
||
JSTOR.config.first() |
|||
if JSTOR.config.showArticle and |
|||
JSTOR.config.showArticle ~= "-" then |
|||
r = string.format( "[[%s|JSTOR]]:%s", |
r = string.format( "[[%s|JSTOR]]:%s", |
||
JSTOR.config.showArticle, r ) |
|||
else |
else |
||
r = "JSTOR:" .. r |
r = "JSTOR:" .. r |
||
Zeile 306: | Zeile 317: | ||
-- Returns appropriate string |
-- Returns appropriate string |
||
local r, show, stuff, suite |
local r, show, stuff, suite |
||
JSTOR.config.first() |
|||
if argsF then |
if argsF then |
||
JSTOR.config.errCat = argsF.errCat |
|||
JSTOR.config.errClasses = argsF.errClasses |
|||
JSTOR.config.errHide = faculty( argsF.errHide ) |
|||
JSTOR.config.errNS = argsF.errNS |
|||
if argsF.showArticle ~= nil then |
if argsF.showArticle ~= nil then |
||
if argsF.showArticle == "" |
if argsF.showArticle == "" or |
||
argsF.showArticle == "-" then |
|||
JSTOR.config.showArticle = false |
|||
else |
else |
||
JSTOR.config.showArticle = argsF.showArticle |
|||
end |
end |
||
end |
end |
||
Zeile 352: | Zeile 365: | ||
k = false |
k = false |
||
elseif argsT.demo or faculty( argsT.NoCat ) then |
elseif argsT.demo or faculty( argsT.NoCat ) then |
||
JSTOR.config.errCat = false |
|||
JSTOR.config.errHide = false |
|||
k = false |
k = false |
||
end |
end |
||
Zeile 384: | Zeile 397: | ||
p.f = function ( frame ) |
p.f = function ( frame ) |
||
local lucky, r |
local lucky, r |
||
JSTOR.config.frame = frame |
|||
lucky, r = pcall( p.main, frame.args, frame:getParent().args ) |
lucky, r = pcall( p.main, frame.args, frame:getParent().args ) |
||
if not lucky then |
if not lucky then |
||
local e = mw.html.create( "span" ) |
|||
:addClass( "error" ) |
|||
:wikitext( string.format( "%s * %s", |
|||
frame:getTitle(), |
|||
r ) ) |
|||
r = tostring( e ) |
|||
end |
end |
||
return r |
return r |
Version vom 28. Oktober 2022, 15:06 Uhr
Vorlagenprogrammierung | Diskussionen | Lua | Unterseiten | |||
Modul | Deutsch | English
|
Modul: | Dokumentation |
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
Dies ist die (produktive) Mutterversion eines global benutzten Lua-Moduls.
Wenn die serial-Information nicht übereinstimmt, müsste eine Kopie hiervon in das lokale Wiki geschrieben werden.
Wenn die serial-Information nicht übereinstimmt, müsste eine Kopie hiervon in das lokale Wiki geschrieben werden.
Versionsbezeichnung auf WikiData:
2024-09-22
Updating notwendig
(lokal:2022-10-26
)local JSTOR = { suite = "JSTOR",
serial = "2022-10-26",
setup = "config.json",
item = 71782977 }
--[=[
Template:JSTOR
and other issues dealing with JSTOR ID.
]=]
local Failsafe = JSTOR
local Externals
JSTOR.config = { self = JSTOR.suite }
local function factory( apply )
-- Localization of messages
-- apply -- string, with message key
-- Returns message text; at least english
local entry = JSTOR.config[ apply ]
local r
if entry then
r = entry[ mw.language.getContentLanguage():getCode() ]
if not r then
r = entry.en
end
else
local e = mw.html.create( "span" )
:addClass( "error" )
:wikitext( string.format( "????.%s.????",
apply ) )
r = tostring( e )
end
return r
end -- factory()
local function faculty( adjust )
-- Test template arg for boolean
-- adjust -- string or nil
-- Returns boolean
local s = type( adjust )
local r
if s == "string" then
r = mw.text.trim( adjust )
r = ( r ~= "" and r ~= "0" )
elseif s == "boolean" then
r = adjust
else
r = false
end
return r
end -- faculty()
local function fault( alert, about )
-- Format message with class="error" or similar
-- alert -- string, with message key
-- about -- string, with explanation
-- Returns message with markup
local e = mw.html.create( "span" )
local r
JSTOR.config.first()
if JSTOR.config.suite and
JSTOR.config.suite ~= "" then
e:wikitext( " * " )
:wikitext( JSTOR.config.suite )
end
e:wikitext( factory( alert ) )
if not JSTOR.config.frame then
JSTOR.config.frame = mw.getCurrentFrame()
end
r = JSTOR.config.frame:callParserFunction( "REVISIONID",
{ [1] = "" } )
e:addClass( JSTOR.config.errClass )
if r == "" then
JSTOR.config.errCat = false
e:addClass( "error" )
elseif JSTOR.config.errHide then
e:css( { display = "none" } )
end
if JSTOR.config.errClasses then
e:addClass( JSTOR.config.errClasses )
end
r = tostring( e )
if about then
r = string.format( "%s %s", r, about )
end
if JSTOR.config.errCat then
if JSTOR.config.errNS then
local ns = mw.title.getCurrentTitle().namespace
local st = type( JSTOR.config.errNS )
if st == "string" then
local space = string.format( ".*%%s%d%%s.*", ns )
local spaces = string.format( " %s ",
JSTOR.config.errNS )
if spaces:match( space ) then
JSTOR.config.errNS = false
end
elseif st == "table" then
for i = 1, #JSTOR.config.errNS do
if JSTOR.config.errNS[ i ] == ns then
JSTOR.config.errNS = false
break -- for i
end
end -- for i
end
end
if not JSTOR.config.errNS then
r = string.format( "%s[[Category:%s]]",
r, JSTOR.config.errCat )
end
end
return r
end -- fault()
local function fetch( auxilary )
-- Fetch module
-- auxilary -- URIutil library, or false
-- Returns table of library, or string with error message
local r
if Externals then
r = Externals.URIutil
else
Externals = { }
if type( auxilary ) == "table" then
Externals.URIutil = auxilary
r = Externals.URIutil
end
end
if not r then
local lucky
lucky, r = pcall( require, "Module:URIutil" )
if type( r ) == "table" then
Externals.URIutil = r()
r = Externals.URIutil
else
local e = mw.html.create( "span" )
:addClass( "error" )
:wikitext( r )
r = tostring( e )
end
end
return r
end -- fetch()
JSTOR.config.first = function ()
-- Initialize configuration
if not JSTOR.config.loaded then
local lucky, json, storage
JSTOR.config.loaded = true
JSTOR.config.frame = JSTOR.config.frame or
mw.getCurrentFrame()
storage = JSTOR.config.frame:getTitle()
storage = string.format( "%s/%s",
storage, JSTOR.setup )
lucky, json = pcall( mw.loadJsonData, storage )
if type( json ) == "table" then
for k, v in pairs( json ) do
if not JSTOR.config[ k ] then
JSTOR.config[ k ] = v
end
end -- for k, v
end
end
end -- JSTOR.config.first()
JSTOR.feasible = function ( article, area, auxilary )
-- Check code, normalize
-- article -- string, with JSTOR ID
-- area -- string, with "stable", "journals", or false
-- auxilary -- URIutil library, or false
-- Returns string, with improved ID, or false
local r = article
local suite = area or "stable"
local legal, URIutil
if suite == "stable" then
if r:find( "/", 1, true ) then
URIutil = fetch( auxilary )
if type( URIutil ) == "table" then
legal = URIutil.isDOI( r )
end
else
legal = r:match( "^i?[1-9]%d*$" ) or
r:match( "^j%.%w+%.?%d*$" ) or
r:match( "^resrep%d$" )
end
elseif suite == "journals" then
URIutil = fetch( auxilary )
if type( URIutil ) == "table" then
legal = URIutil.isISSNvalid( r )
r = r:gsub( "-", "" )
:gsub( "x", "X" )
end
end
if not legal then
r = false
end
return r
end -- JSTOR.feasible()
JSTOR.format = function ( article, area, appear, auxilary )
-- Analyze code, create URL, format
-- article -- string, with JSTOR ID
-- area -- string, with "stable", "journals", or false
-- appear -- string, with link title, or false
-- auxilary -- URIutil library, or false
-- Returns string, with external link or error message
local r = JSTOR.feasible( article, area, auxilary )
local suite = area or "stable"
if r then
local show = appear
if type( show ) == "string" then
show = mw.text.trim( show )
if show == "" or show == "-" then
show = false
end
end
if not show then
show = article
end
r = string.format( "[http://www.jstor.org/%s/%s %s]",
suite, r, show )
if not appear then
JSTOR.config.first()
if JSTOR.config.showArticle and
JSTOR.config.showArticle ~= "-" then
r = string.format( "[[%s|JSTOR]]:%s",
JSTOR.config.showArticle, r )
else
r = "JSTOR:" .. r
end
end
else
r = string.format( "%s %s/''%s''",
fault( "errInvalid" ), suite, article )
end
return r
end -- JSTOR.format()
Failsafe.failsafe = function ( atleast )
-- Retrieve versioning and check for compliance
-- Precondition:
-- atleast -- string, with required version
-- or wikidata|item|~|@ or false
-- Postcondition:
-- Returns string -- with queried version/item, also if problem
-- false -- if appropriate
-- 2020-08-17
local since = atleast
local last = ( since == "~" )
local linked = ( since == "@" )
local link = ( since == "item" )
local r
if last or link or linked or since == "wikidata" then
local item = Failsafe.item
since = false
if type( item ) == "number" and item > 0 then
local suited = string.format( "Q%d", item )
if link then
r = suited
else
local entity = mw.wikibase.getEntity( suited )
if type( entity ) == "table" then
local seek = Failsafe.serialProperty or "P348"
local vsn = entity:formatPropertyValues( seek )
if type( vsn ) == "table" and
type( vsn.value ) == "string" and
vsn.value ~= "" then
if last and vsn.value == Failsafe.serial then
r = false
elseif linked then
if mw.title.getCurrentTitle().prefixedText
== mw.wikibase.getSitelink( suited ) then
r = false
else
r = suited
end
else
r = vsn.value
end
end
end
end
end
end
if type( r ) == "nil" then
if not since or since <= Failsafe.serial then
r = Failsafe.serial
else
r = false
end
end
return r
end -- Failsafe.failsafe()
-- JSTOR
local p = { }
p.main = function ( argsF, argsT )
-- Invocation
-- argsF -- table, with #invoke parameters, or false
-- argsT -- table, with template parameters
-- Returns appropriate string
local r, show, stuff, suite
JSTOR.config.first()
if argsF then
JSTOR.config.errCat = argsF.errCat
JSTOR.config.errClasses = argsF.errClasses
JSTOR.config.errHide = faculty( argsF.errHide )
JSTOR.config.errNS = argsF.errNS
if argsF.showArticle ~= nil then
if argsF.showArticle == "" or
argsF.showArticle == "-" then
JSTOR.config.showArticle = false
else
JSTOR.config.showArticle = argsF.showArticle
end
end
end
if type( argsT ) == "table" then
local s, unknown
r = { }
for k, v in pairs( argsT ) do
s = type( k )
if s == "number" then
if ( k == 1 ) then
stuff = mw.text.trim( v )
if ( stuff == "" ) then
stuff = false
end
k = false
elseif ( k == 2 ) then
v = mw.text.trim( v )
if ( v ~= "" ) then
show = v
end
k = false
else
k = tostring( k )
end
elseif s == "string" then
if argsT.show then
if ( argsT.show ~= "" ) then
show = argsT.show
end
k = false
elseif argsT.suite then
if ( argsT.suite ~= "" ) then
suite = argsT.suite
end
k = false
elseif argsT.demo or faculty( argsT.NoCat ) then
JSTOR.config.errCat = false
JSTOR.config.errHide = false
k = false
end
end
if k then
if not unknown then
unknown = { }
end
table.insert( unknown, k )
end
end -- for k, v
if unknown then
r = string.format( "'<code>%s</code>' in Template:JSTOR",
table.concat( unknown, " " ) )
r = fault( "errUnkown", r )
elseif not stuff then
r = fault( "errMissing" )
end
else
r = false
end
if type( r ) == "table" then
r = JSTOR.format( stuff, suite, show )
end
return r
end -- p.main()
p.f = function ( frame )
local lucky, r
JSTOR.config.frame = frame
lucky, r = pcall( p.main, frame.args, frame:getParent().args )
if not lucky then
local e = mw.html.create( "span" )
:addClass( "error" )
:wikitext( string.format( "%s * %s",
frame:getTitle(),
r ) )
r = tostring( e )
end
return r
end -- p.f()
p.failsafe = function ( frame )
-- Versioning interface
local s = type( frame )
local since
if s == "table" then
since = frame.args[ 1 ]
elseif s == "string" then
since = frame
end
if since then
since = mw.text.trim( since )
if since == "" then
since = false
end
end
return Failsafe.failsafe( since ) or ""
end -- p.failsafe
p.JSTOR = function ()
return JSTOR
end -- p.JSTOR()
setmetatable( p, { __call = function ( func, ... )
setmetatable( p, nil )
return Failsafe
end } )
return p -- JSTOR