upgrade.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (C) 2014–2015 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation; either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. use utf8;
  17. AddModuleDescription('upgrade.pl', '2014-06-17 New Directory Structure');
  18. our ($q, %Action, %InterSite, $ModuleDir, $IndexFile, $DataDir, $PageDir, $KeepDir, $InterMap, $JoinerDir, $JoinerEmailDir, $LocalNamesPage, $GotobarName, $NearMap, $RefererDir, $SidebarName);
  19. # We are now running in InitModules. InitVariables will be called later.
  20. # We want to prevent any calls to GetPageContent and the like.
  21. *UpgradeOldInitVariables = \&InitVariables;
  22. *InitVariables = \&UpgradeNewInitVariables;
  23. sub UpgradeNewInitVariables {
  24. $InterMap = undef;
  25. $LocalNamesPage = undef;
  26. $SidebarName = undef;
  27. $NearMap = undef;
  28. $GotobarName = undef;
  29. UpgradeOldInitVariables(@_);
  30. }
  31. *DoBrowseRequest = \&DoUpgrade;
  32. sub DoUpgrade {
  33. # The only thing allowed besides upgrading is login and unlock
  34. my $action = lc(GetParam('action', ''));
  35. if (($action eq 'password' or $action eq 'unlock')
  36. and $Action{$action}) {
  37. return &{$Action{$action}}(GetId());
  38. }
  39. # Only admins may upgrade
  40. ReportError(T('Upgrading Database'),
  41. '403 FORBIDDEN', 0,
  42. $q->p(T('This operation is restricted to administrators only...'))
  43. . $q->p(ScriptLink('action=password', T('Login'), 'password')))
  44. unless UserIsAdmin();
  45. ReportError(T('Upgrading Database'),
  46. '403 FORBIDDEN', 0,
  47. $q->p(T('Did the previous upgrade end with an error? A lock was left behind.'))
  48. . $q->p(ScriptLink('action=unlock', T('Unlock wiki'), 'unlock')))
  49. unless RequestLockDir('main');
  50. print GetHeader('', T('Upgrading Database')),
  51. $q->start_div({-class=>'content upgrade'});
  52. if (IsFile($IndexFile)) {
  53. Unlink($IndexFile);
  54. }
  55. print "<p>Renaming files...";
  56. for my $ns ('', keys %InterSite) {
  57. next unless -d "$DataDir/$ns";
  58. print "<br />\n<strong>$ns</strong>" if $ns;
  59. for my $dirname ($PageDir, $KeepDir, $RefererDir, $JoinerDir, $JoinerEmailDir) {
  60. next unless $dirname;
  61. my $dir = $dirname; # copy in order not to modify the original
  62. $dir =~ s/^$DataDir/$DataDir\/$ns/ if $ns;
  63. for my $old (Glob("$dir/*/*"), Glob("$dir/*/.*")) {
  64. next if $old =~ /\/\.\.?$/;
  65. print "<br />\n$old";
  66. my $new = $old;
  67. $new =~ s!/([A-Z]|other)/!/!;
  68. if ($old eq $new) {
  69. print " does not fit the pattern!";
  70. } elsif (not Rename($old, $new)) {
  71. print " → $new failed!";
  72. }
  73. }
  74. for my $subdir (grep(/\/([A-Z]|other)$/, Glob("$dir/*"), Glob("$dir/.*"))) {
  75. next if substr($subdir, -2) eq '/.' or substr($subdir, -3) eq '/..';
  76. RemoveDir($subdir); # ignore errors
  77. }
  78. }
  79. }
  80. print $q->end_p();
  81. if (Rename("$ModuleDir/upgrade.pl", "$ModuleDir/upgrade.done")) {
  82. print $q->p(T("Upgrade complete."))
  83. } else {
  84. print $q->p(T("Upgrade complete. Please remove $ModuleDir/upgade.pl, now."))
  85. }
  86. ReleaseLock();
  87. print $q->end_p(), $q->end_div();
  88. PrintFooter();
  89. }