Database changes have finished applying - please report any issues you're (still) seeing to support@shoutwiki.com.
Moduuli:ArchiveAccess
Jedipediasta, vapaasta Tähtien sota-tietosanakirjasta tänään, 6. heinäkuuta 2025
Siirry navigaatioonSiirry hakuun
Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:ArchiveAccess/ohje
1 -- ArchiveAccess implements rendering for web page archive links.
2 local p = {}
3
4 -- NS_MAIN holds the namespace number for the Main namespace.
5 local NS_MAIN = 0
6 -- NS_FILE holds the namespace number for the File namespace.
7 local NS_FILE = 6
8
9 local NS_MODULE = 828
10
11 -- isCurrentPageMainSpaceOrFile determines whether the page being parsed is a mainspace or file page.
12 local function isCurrentPageMainSpaceOrFile()
13 local title = mw.title.getCurrentTitle()
14
15 return title.namespace == NS_MAIN or title.namespace == NS_FILE or title.namespace == NS_MODULE
16 end
17
18 -- getArchiveTrackingCategories returns tracking categories based on whether the explicit archive date used for this invocation
19 -- matches the implicit archive date sourced from the /Archive subpage of the template on behalf of which this module is invoked.
20 -- This is generally only useful if said /Archive subpage exists.
21 local function getArchiveTrackingCategories(explicitArchiveDate, knownArchiveDate, templateName)
22 return ''
23 --if not isCurrentPageMainSpaceOrFile() then
24 -- return ''
25 --end
26
27 -- User-provided explicit archive date parameter matches the implicit archive date from template data.
28 --if knownArchiveDate == explicitArchiveDate then
29 -- return '[[Luokka:' .. templateName .. ' usages with the same archivedate value]]'
30 --end
31
32 -- User-provided explicit archive date parameter does not match the implicit archive date from template data.
33 --if knownArchiveDate then
34 -- return '[[Luokka:' .. templateName .. ' usages with custom archivedate]]'
35 --end
36
37 --return '[[Luokka:' .. templateName .. ' usages with archived URLs not in Archive]]'
38 end
39
40 -- getMissingPermalinkTrackingCategory returns the tracking category to be used for the current owning template
41 -- if this invocation contained an archival link without a permalink timestamp.
42 -- It prefers to use a template-specific tracking category if one exists and a generic one otherwise.
43 local function getMissingPermalinkTrackingCategory(citationType, templateName)
44 if not isCurrentPageMainSpaceOrFile() then
45 return ''
46 end
47
48 --local templateSpecificCategory = citationType .. ' with missing permanent archival links/' .. templateName
49
50 -- Use the template-specific tracking category if possible
51 --if mw.title.makeTitle('Category', templateSpecificCategory).exists then
52 -- return '[[Luokka:' .. templateSpecificCategory .. ']]'
53 --end
54
55 return '[[Luokka:' .. citationType .. ', joissa on puuttuvia arkistolinkkejä]]'
56 end
57
58 -- getBackupLink generates a Wayback machine archive URL based on invocation parameters
59 -- with appropriate tracking categories appended.
60 local function getBackupLink(args)
61 -- No backup link is available, so return a customizable disclaimer
62 if args.nobackup then
63 return (args.nobackup_text or 'content obsolete and backup link not available')
64 end
65
66 -- If the original link is now inaccessible, render a customizable disclaimer
67 local obsoleteDisclaimer = args.nolive and (args.nolive_text or 'content now obsolete; ') or ''
68
69 local archiveFile = args.archivefile
70 if archiveFile then
71 return obsoleteDisclaimer .. '[[:' .. archiveFile .. '|kuvakaappaus]]'
72 end
73
74 local archiveUrl = args.archiveurl
75 local noArchive = args.no_archive
76 local archiveLinkText = args.text or 'linkki varmuuskopioon'
77 local use_lua_subpage = args.use_lua_subpage
78
79 -- Full Wayback URL given, so return it with appropriate tracking categories
80 if archiveUrl then
81 local trackingCategories = ''
82 if mw.ustring.find(archiveUrl, 'web.archive.org/web') and isCurrentPageMainSpaceOrFile() then
83 trackingCategories = '[[Luokka:Archiveurl usages with Wayback URLs]]'
84 end
85 return obsoleteDisclaimer .. '[' .. archiveUrl .. ' ' .. archiveLinkText .. ']' .. trackingCategories
86 end
87
88 -- Determine the Wayback URL to use based on the archive date and archive value params, if given.
89 local templateName = args.template_name or ''
90 local fullUrl = args.full_url or ''
91 local explicitArchiveDate = args.archivedate
92 local target = args.target
93 local knownArchiveDate
94
95 -- If the 'target' parameter was forwarded to this invocation, attempt fetch known archive dates
96 -- from a Lua module rather than the archive_value parameter.
97 if target and not noArchive then
98 if use_lua_subpage then
99 local templateData = require('Moduuli:ArchiveAccess/' .. templateName)
100 knownArchiveDate = templateData.knownArchiveDates[target]
101 else
102 knownArchiveDate = args.archive_value
103 end
104 end
105
106 -- If an explicit archive date was given, use it to point to the Wayback snapshot at that given time
107 -- and append appropriate tracking categories.
108 if explicitArchiveDate then
109 local trackingCategories = not noArchive and getArchiveTrackingCategories(explicitArchiveDate, knownArchiveDate, templateName) or ''
110 return obsoleteDisclaimer .. '[https://web.archive.org/web/' .. explicitArchiveDate .. '/' .. fullUrl .. ' ' .. archiveLinkText .. ']' .. trackingCategories
111 end
112
113 -- No explicit archive date was given, so use the archive date from the /Archive subpage of the owning template if possible
114 if not noArchive and knownArchiveDate then
115 return obsoleteDisclaimer .. '[https://web.archive.org/web/' .. knownArchiveDate .. '/' .. fullUrl .. ' ' .. archiveLinkText .. ']'
116 end
117
118 -- Neither an explicit nor implicit archive date is available, so point to the latest Wayback snapshot with a disclaimer
119 local citationType = args.citation_type or 'Sivut'
120 --local notVerifiedDisclaimer = ' [[Template:' .. templateName .. '|<span style="color: red">\'\'\'not verified!\'\'\'</span>]]'
121 local trackingCategory = getMissingPermalinkTrackingCategory(citationType, templateName)
122 return obsoleteDisclaimer .. '[https://web.archive.org/web/' .. fullUrl .. ' ' .. archiveLinkText .. ']' .. trackingCategory
123 end
124
125 -- render outputs a full Wayback link wrapped in a container.
126 function p.render(args)
127 local isSmallFont = args.font_size == 'small'
128 local wrapInParentheses = args.par
129 local useSpaceSeparator = args.space
130
131 return mw.ustring.format(
132 '%s%s%s%s%s',
133 isSmallFont and '<small>' or '',
134 wrapInParentheses and ' (' or (useSpaceSeparator and ' ' or ''),
135 getBackupLink(args),
136 wrapInParentheses and ')' or '',
137 isSmallFont and '</small>' or ''
138 )
139 end
140
141 --local getArgs = require('Moduuli:Arguments').getArgs
142 function p.main(frame)
143 --local args = getArgs(frame)
144 local args = {}
145 for k, v in pairs(frame:getParent().args) do
146 v = v:match('^%s*(.-)%s*$') -- trim whitespace
147 if v ~= '' then
148 args[k] = v
149 end
150 end
151 return p.render(args)
152 end
153
154 return p