rename-pages.pl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2019–2023 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. our ($q, %Page, %Action, %IndexHash, @MyAdminCode);
  18. AddModuleDescription('rename-pages.pl', 'Rename Pages Extension');
  19. $Action{'rename-page'} = \&RenamePage;
  20. sub RenamePage {
  21. my $id = shift;
  22. my $to = GetParam('to', '');
  23. # check target
  24. ValidIdOrDie($to);
  25. OpenPage($to);
  26. ReportError(T('Target page already exists.'), '400 BAD REQUEST')
  27. if $IndexHash{$to} and not PageMarkedForDeletion();
  28. # check the source
  29. ValidIdOrDie($id);
  30. OpenPage($id);
  31. ReportError(T('Source page does not exist.'), '400 BAD REQUEST')
  32. if not $IndexHash{$id} or PageMarkedForDeletion();
  33. {
  34. # prevent posting from browsing the target right away
  35. local *ReBrowsePage = sub {};
  36. # renaming is a minor change
  37. SetParam('recent_edit', 'on');
  38. # copy text
  39. SetParam('text', $Page{text});
  40. SetParam('summary', Ts('Copied from %s', FreeToNormal($id)));
  41. DoPost($to);
  42. # create redirect
  43. SetParam('text', "#REDIRECT [[$to]]");
  44. SetParam('summary', Ts('Moved to %s', FreeToNormal($to)));
  45. DoPost($id);
  46. }
  47. # and now that we're done, go to the target
  48. ReBrowsePage($to);
  49. }
  50. push(@MyAdminCode, \&RenamePageMenu);
  51. sub RenamePageMenu {
  52. my ($id, $menuref, $restref) = @_;
  53. my $name = FreeToNormal($id);
  54. if ($id) {
  55. push(@$menuref, GetFormStart()
  56. . $q->label({-for=>'to'}, Ts('Rename %s to:', $name) . ' ')
  57. . GetHiddenValue('action', 'rename-page')
  58. . GetHiddenValue('id', $id)
  59. . $q->textfield(-name=>'to', -size=>20)
  60. . ' '
  61. . $q->submit('Do it')
  62. . $q->end_form());
  63. }
  64. }