admin.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright (C) 2004, 2005 Alex Schroeder <alex@emacswiki.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 2 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, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. use strict;
  19. use v5.10;
  20. our ($q, %Page, %Action, $IndexFile, $PageDir, $KeepDir, @MyAdminCode, $RefererDir);
  21. AddModuleDescription('admin.pl', 'Admin Power Extension');
  22. $Action{delete} = \&AdminPowerDelete;
  23. $Action{rename} = \&AdminPowerRename;
  24. sub AdminPowerDelete {
  25. my $id = GetParam('id', '');
  26. ValidIdOrDie($id);
  27. print GetHeader('', Ts('Deleting %s', $id), '');
  28. return unless UserIsAdminOrError();
  29. RequestLockOrError();
  30. print $q->p(T('Main lock obtained.'));
  31. OpenPage($id);
  32. my $status = DeletePage($id);
  33. if ($status) {
  34. print $q->p(GetPageLink($id) . ' ' . T('not deleted:') . ' ' . $status);
  35. } else {
  36. print $q->p(GetPageLink($id) . ' ' . T('deleted'));
  37. WriteRcLog($id, Ts('Deleted %s', $id), 0, $Page{revision},
  38. GetParam('username', ''), $q->remote_addr(), $Page{languages},
  39. GetCluster($Page{text}));
  40. }
  41. # Regenerate index on next request
  42. Unlink($IndexFile);
  43. ReleaseLock();
  44. print $q->p(T('Main lock released.'));
  45. PrintFooter();
  46. }
  47. sub AdminPowerRename {
  48. my $id = FreeToNormal(GetParam('id', ''));
  49. ValidIdOrDie($id);
  50. my $new = FreeToNormal(GetParam('new', ''));
  51. ValidIdOrDie($new);
  52. print GetHeader('', Tss('Renaming %1 to %2.', $id, $new), '');
  53. return unless UserIsAdminOrError();
  54. RequestLockOrError();
  55. print $q->p(T('Main lock obtained.'));
  56. # page file -- only check for existing or missing pages here
  57. my $fname = GetPageFile($id);
  58. ReportError(Ts('The page %s does not exist', $id), '400 BAD REQUEST') unless IsFile($fname);
  59. my $newfname = GetPageFile($new);
  60. ReportError(Ts('The page %s already exists', $new), '400 BAD REQUEST') if IsFile($newfname);
  61. # Regenerate index on next request -- remove this before errors can occur!
  62. Unlink($IndexFile);
  63. # page file
  64. CreateDir($PageDir); # It might not exist yet
  65. Rename($fname, $newfname)
  66. or ReportError(Tss('Cannot rename %1 to %2', $fname, $newfname) . ": $!", '500 INTERNAL SERVER ERROR');
  67. # keep directory
  68. my $kdir = GetKeepDir($id);
  69. my $newkdir = GetKeepDir($new);
  70. CreateDir($KeepDir); # It might not exist yet (only the parent directory!)
  71. Rename($kdir, $newkdir)
  72. or ReportError(Tss('Cannot rename %1 to %2', $kdir, $newkdir) . ": $!", '500 INTERNAL SERVER ERROR')
  73. if IsDir($kdir);
  74. # refer file
  75. if (defined(&GetRefererFile)) {
  76. my $rdir = GetRefererFile($id);
  77. my $newrdir = GetRefererFile($new);
  78. CreateDir($RefererDir); # It might not exist yet
  79. Rename($rdir, $newrdir)
  80. or ReportError(Tss('Cannot rename %1 to %2', $rdir, $newrdir) . ": $!", '500 INTERNAL SERVER ERROR')
  81. if IsDir($rdir);
  82. }
  83. # RecentChanges
  84. OpenPage($new);
  85. WriteRcLog($id, Ts('Renamed to %s', $new), 0, $Page{revision},
  86. GetParam('username', ''), $q->remote_addr(), $Page{languages},
  87. GetCluster($Page{text}));
  88. WriteRcLog($new, Ts('Renamed from %s', $id), 0, $Page{revision},
  89. GetParam('username', ''), $q->remote_addr(), $Page{languages},
  90. GetCluster($Page{text}));
  91. print $q->p(Tss('Renamed %1 to %2.', GetPageLink($id), GetPageLink($new)));
  92. ReleaseLock();
  93. print $q->p(T('Main lock released.'));
  94. PrintFooter();
  95. }
  96. push(@MyAdminCode, \&AdminPower);
  97. sub AdminPower {
  98. return unless UserIsAdmin();
  99. my ($id, $menuref, $restref) = @_;
  100. my $name = $id;
  101. $name =~ s/_/ /g;
  102. if ($id) {
  103. push(@$menuref, ScriptLink('action=delete;id=' . $id, Ts('Immediately delete %s', $name), 'delete'));
  104. push(@$menuref, GetFormStart()
  105. . $q->label({-for=>'new'}, Ts('Rename %s to:', $name) . ' ')
  106. . GetHiddenValue('action', 'rename')
  107. . GetHiddenValue('id', $id)
  108. . $q->textfield(-name=>'new', -size=>20)
  109. . $q->submit('Do it'));
  110. }
  111. }