Module:CopyEmbeddedStories
From The Seven Sages of Rome
This module extracts all instances of the `EmbeddedStory` template from a specified source page. It is useful for retrieving and reusing embedded story elements across different pages in a MediaWiki environment.
Usage
The module can be invoked using `#invoke` in a template or wiki page.
{{#invoke:CopyEmbeddedStories|fetchEmbeddedStories | sourcePage=ExamplePage }}
Parameters
- `sourcePage` - The title of the wiki page from which `EmbeddedStory` templates should be retrieved.
Behavior
1. The module reads the raw wikitext content of the specified `sourcePage`. 2. It searches for all instances of `Template:EmbeddedStory ...` within the page. 3. The found templates are returned as a newline-separated list. 4. If the page is not found or empty, an error message is returned.
Example Output
If `ExamplePage` contains:
{{EmbeddedStory|Title=Story 1}} Some text here. {{EmbeddedStory|Title=Story 2}}
Then the module will return:
{{EmbeddedStory|Title=Story 1}} {{EmbeddedStory|Title=Story 2}}
Notes
- The module does not modify the extracted templates; it simply returns them as raw wikitext.
- If no `EmbeddedStory` templates are found, it returns an empty string.
- If `sourcePage` is not specified, an error message is displayed.
-- Module:CopyEmbeddedStories
local p = {}
-- [Previous helper functions remain the same]
-- 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
local html = string.format([[
<div class="template-copy-form">
<h3>Copy Embedded Stories</h3>
<div style="margin: 10px 0;">
<input type="text" id="targetPage" placeholder="Enter target page name" style="width: 300px; padding: 5px;"/>
<button onclick="copyTemplates()" style="margin-left: 10px; padding: 5px 10px;">Copy Stories</button>
</div>
<div id="copyResult" style="margin-top: 10px;"></div>
</div>
<script type="text/javascript">
function copyTemplates() {
var targetPage = document.getElementById('targetPage').value;
if (!targetPage) {
mw.notify('Please enter a target page name');
return;
}
var currentPage = %s;
new mw.Api().post({
action: 'parse',
text: '{{#invoke:CopyEmbeddedStories|copyTemplates|' + currentPage + '|' + targetPage + '}}',
contentmodel: 'wikitext'
}).done(function(response) {
mw.notify(response.parse.text['*']);
document.getElementById('copyResult').innerHTML = response.parse.text['*'];
}).fail(function(error) {
mw.notify('Error copying templates');
document.getElementById('copyResult').innerHTML = 'Error copying templates';
});
}
</script>
]], mw.text.jsonEncode(currentPage))
return frame:preprocess(html)
end
return p