admin.pl 4.2 KB

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