forked from github/kensanata.oddmuse
rename-pages: new
This commit is contained in:
69
modules/rename-pages.pl
Normal file
69
modules/rename-pages.pl
Normal file
@@ -0,0 +1,69 @@
|
||||
# Copyright (C) 2019 Alex Schroeder <alex@gnu.org>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
31
t/rename-pages.t
Normal file
31
t/rename-pages.t
Normal file
@@ -0,0 +1,31 @@
|
||||
# Copyright (C) 2019 Alex Schroeder <alex@gnu.org>
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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');
|
||||
Reference in New Issue
Block a user