Module:CopyEmbeddedStories: Difference between revisions
From Seven Sages of Rome
(Created page with "-- Module:CopyTemplates local p = {} -- Helper function to extract all EmbeddedStory templates from text local function extractEmbeddedStories(text) local stories = {} -- Match all EmbeddedStory templates for story in mw.ustring.gmatch(text, "{{EmbeddedStory.-}}") do table.insert(stories, story) end return stories end -- Helper function to append templates to a page local function appendToPage(title, templates) local page = mw.title.new(...") |
No edit summary |
||
Line 1: | Line 1: | ||
-- Module: | -- Module:CopyEmbeddedStories | ||
local p = {} | local p = {} | ||
Line 81: | Line 81: | ||
local currentPage = mw.title.getCurrentTitle().text | local currentPage = mw.title.getCurrentTitle().text | ||
-- | -- Get list of pages in main namespace | ||
local options = {} | local options = {} | ||
for | for page in mw.allpages.allpages{namespace = 0} do | ||
if | if page.title ~= currentPage then | ||
table.insert(options, string.format("* %s", page.title)) | table.insert(options, string.format("* %s", page.title)) | ||
end | end | ||
Line 93: | Line 92: | ||
local form = string.format([[ | local form = string.format([[ | ||
<div class="template-copy-form"> | <div class="template-copy-form"> | ||
=== Copy | === Copy Embedded Stories === | ||
<select id="targetPage"> | <select id="targetPage"> | ||
%s | %s | ||
</select> | </select> | ||
<button onclick="copyTemplates()">Copy | <button onclick="copyTemplates()">Copy Stories</button> | ||
</div> | </div> | ||
Line 108: | Line 107: | ||
new mw.Api().post({ | new mw.Api().post({ | ||
action: 'parse', | action: 'parse', | ||
text: '{{#invoke: | text: '{{#invoke:CopyEmbeddedStories|copyTemplates|' + currentPage + '|' + targetPage + '}}', | ||
contentmodel: 'wikitext' | contentmodel: 'wikitext' | ||
}).done(function(response) { | }).done(function(response) { |
Revision as of 19:54, 13 November 2024
Documentation for this module may be created at Module:CopyEmbeddedStories/doc
-- Module:CopyEmbeddedStories
local p = {}
-- Helper function to extract all EmbeddedStory templates from text
local function extractEmbeddedStories(text)
local stories = {}
-- Match all EmbeddedStory templates
for story in mw.ustring.gmatch(text, "{{EmbeddedStory.-}}") do
table.insert(stories, story)
end
return stories
end
-- Helper function to append templates to a page
local function appendToPage(title, templates)
local page = mw.title.new(title)
if not page then return false, "Invalid page title" end
local status, err = pcall(function()
local pageText = mw.text.trim(mw.title.new(title):getContent() or "")
-- Add a newline if the page isn't empty
if pageText ~= "" then
pageText = pageText .. "\n\n"
end
-- Append all templates
for _, template in ipairs(templates) do
pageText = pageText .. template .. "\n"
end
-- Save the page
local success = page:update({
text = pageText,
summary = "Copied EmbeddedStory templates",
minor = false
})
if not success then
error("Failed to save page")
end
end)
return status, err
end
-- Main function to copy templates
function p.copyTemplates(frame)
local sourcePage = frame.args[1]
local targetPage = frame.args[2]
if not sourcePage or not targetPage then
return "Error: Source and target pages must be specified"
end
-- Get source page content
local sourceTitle = mw.title.new(sourcePage)
if not sourceTitle then
return "Error: Invalid source page title"
end
local sourceContent = sourceTitle:getContent()
if not sourceContent then
return "Error: Could not read source page"
end
-- Extract templates
local stories = extractEmbeddedStories(sourceContent)
if #stories == 0 then
return "No EmbeddedStory templates found on source page"
end
-- Copy templates to target page
local success, err = appendToPage(targetPage, stories)
if not success then
return "Error copying templates: " .. (err or "unknown error")
end
return string.format("Successfully copied %d templates to %s", #stories, targetPage)
end
-- Function to generate form
function p.showForm(frame)
local currentPage = mw.title.getCurrentTitle().text
-- Get list of pages in main namespace
local options = {}
for page in mw.allpages.allpages{namespace = 0} do
if page.title ~= currentPage then
table.insert(options, string.format("* %s", page.title))
end
end
-- Generate form HTML
local form = string.format([[
<div class="template-copy-form">
=== Copy Embedded Stories ===
<select id="targetPage">
%s
</select>
<button onclick="copyTemplates()">Copy Stories</button>
</div>
<script>
function copyTemplates() {
var targetPage = document.getElementById('targetPage').value;
var currentPage = '%s';
// Call the Lua module via API
new mw.Api().post({
action: 'parse',
text: '{{#invoke:CopyEmbeddedStories|copyTemplates|' + currentPage + '|' + targetPage + '}}',
contentmodel: 'wikitext'
}).done(function(response) {
mw.notify(response.parse.text['*']);
});
}
</script>
]], table.concat(options, "\n"), currentPage)
return form
end
return p