|
|
(2 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| -- Module:CopyEmbeddedStories
| |
| local p = {} | | local p = {} |
|
| |
|
| -- Helper function to extract all EmbeddedStory templates from text | | -- Function to fetch EmbeddedStory templates from a given page |
| local function extractEmbeddedStories(text)
| | function p.fetchEmbeddedStories(frame) |
| local stories = {} | | local pageTitle = frame.args["sourcePage"] |
| -- Match all EmbeddedStory templates | | if not pageTitle then |
| for story in mw.ustring.gmatch(text, "{{EmbeddedStory.-}}") do
| | return "Error: No source page specified." |
| table.insert(stories, story)
| |
| end | | end |
| return stories
| |
| end
| |
|
| |
|
| -- Helper function to append templates to a page | | -- Get the wikitext content of the source page |
| local function appendToPage(title, templates)
| | local content = mw.title.new(pageTitle):getContent() |
| local page = mw.title.new(title)
| | if not content then |
| if not page then return false, "Invalid page title" end
| | return "Error: Source page not found or empty." |
|
| | 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 | | -- Find all EmbeddedStory templates |
| function p.copyTemplates(frame)
| | local templates = {} |
| local sourcePage = frame.args[1] | | for embeddedStory in content:gmatch("{{EmbeddedStory.-}}") do |
| local targetPage = frame.args[2] | | table.insert(templates, embeddedStory) |
|
| |
| 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 | | 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
| |
|
| |
| -- Create the HTML form using mw.html builder
| |
| local builder = mw.html.create()
| |
|
| |
| -- Add the form container
| |
| builder
| |
| :tag('div')
| |
| :addClass('template-copy-form')
| |
| :tag('h3')
| |
| :wikitext('Copy Embedded Stories')
| |
| :done()
| |
| :tag('div')
| |
| :css('margin', '10px 0')
| |
| :tag('input')
| |
| :attr('type', 'text')
| |
| :attr('id', 'targetPage')
| |
| :attr('placeholder', 'Enter target page name')
| |
| :css('width', '300px')
| |
| :css('padding', '5px')
| |
| :done()
| |
| :tag('button')
| |
| :attr('onclick', 'copyTemplates()')
| |
| :css('margin-left', '10px')
| |
| :css('padding', '5px 10px')
| |
| :wikitext('Copy Stories')
| |
| :done()
| |
| :done()
| |
| :tag('div')
| |
| :attr('id', 'copyResult')
| |
| :css('margin-top', '10px')
| |
| :done()
| |
| :done()
| |
|
| |
| -- Add the JavaScript
| |
| local script = string.format([[
| |
| <script>
| |
| function copyTemplates() {
| |
| var targetPage = document.getElementById('targetPage').value;
| |
| if (!targetPage) {
| |
| mw.notify('Please enter a target page name');
| |
| return;
| |
| }
| |
| 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['*']);
| |
| 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 tostring(builder) .. script | | -- Return templates as a single concatenated string |
| | return table.concat(templates, "\n") |
| end | | end |
|
| |
|
| return p | | return p |