From 771b07e6612e5501e8fdf970b6dc2da1208da91c Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Fri, 1 Mar 2019 20:34:11 +0100 Subject: [PATCH] rename-pages: new --- modules/rename-pages.pl | 69 +++++++++++++++++++++++++++++++++++++++++ t/rename-pages.t | 31 ++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 modules/rename-pages.pl create mode 100644 t/rename-pages.t diff --git a/modules/rename-pages.pl b/modules/rename-pages.pl new file mode 100644 index 00000000..757553a3 --- /dev/null +++ b/modules/rename-pages.pl @@ -0,0 +1,69 @@ +# Copyright (C) 2019 Alex Schroeder +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use strict; +use v5.10; + +our ($q, %Page, %Action, %IndexHash, @MyAdminCode); + +AddModuleDescription('rename-pages.pl', 'Rename Pages Extension'); + +$Action{'rename-page'} = \&RenamePage; + +sub RenamePage { + my $id = shift; + my $to = GetParam('to', ''); + # check target + ValidIdOrDie($to); + OpenPage($to); + ReportError(T('Target page already exists.'), '400 BAD REQUEST') + if $IndexHash{$to} and not PageMarkedForDeletion(); + # check the source + ValidIdOrDie($id); + OpenPage($id); + ReportError(T('Source page does not exist.'), '400 BAD REQUEST') + if not $IndexHash{$id} or PageMarkedForDeletion(); + { + # prevent posting from browsing the target right away + local *ReBrowsePage = sub {}; + # renaming is a minor change + SetParam('recent_edit', 'on'); + # copy text + SetParam('text', $Page{text}); + SetParam('summary', Ts('Copied from %s', FreeToNormal($id))); + DoPost($to); + # create redirect + SetParam('text', "#REDIRECT [[$to]]"); + SetParam('summary', Ts('Moved to %s', FreeToNormal($to))); + DoPost($id); + } + # and now that we're done, go to the target + ReBrowsePage($to); +} + +push(@MyAdminCode, \&RenamePageMenu); + +sub RenamePageMenu { + my ($id, $menuref, $restref) = @_; + my $name = FreeToNormal($id); + if ($id) { + push(@$menuref, GetFormStart() + . $q->label({-for=>'rename'}, Ts('Rename %s to:', $name) . ' ') + . GetHiddenValue('action', 'rename-page') + . GetHiddenValue('id', $id) + . $q->textfield(-name=>'to', -size=>20) + . $q->submit('Do it')); + } +} diff --git a/t/rename-pages.t b/t/rename-pages.t new file mode 100644 index 00000000..0a29a8e7 --- /dev/null +++ b/t/rename-pages.t @@ -0,0 +1,31 @@ +# Copyright (C) 2019 Alex Schroeder +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see . + +require './t/test.pl'; +package OddMuse; +use Test::More tests => 9; + +add_module('rename-pages.pl'); + +test_page(update_page('A', 'Alpha'), 'Alpha'); +test_page(get_page('action=rename-page'), 'Page name is missing'); +test_page(get_page('action=rename-page id=X to=Y'), 'Source page does not exist'); +test_page(get_page('action=rename-page id=A to=A'), 'Target page already exists'); +test_page(get_page('action=rename-page id=A to=B'), + 'Status: 302', + 'Location: .*wiki.pl/B'); +test_page(get_page('A'), + 'Status: 302', + 'Location: .*wiki.pl\?action=browse;oldid=A;id=B'); +test_page(get_page('B'), 'Alpha');