anonymize.pl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #! /usr/bin/perl -w
  2. # Copyright (C) 2013 Alex Schroeder <alex@gnu.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. =head1 Anonymize oldrc.log Files
  17. This script will read your oldrc.log file and replace the host field
  18. with 'Anonymous'. This is what the main script started doing
  19. 2013-11-30.
  20. When you run this script, it sets the main lock to prevent maintenance
  21. from running. You can therefore run it on a live system.
  22. =cut
  23. use strict;
  24. sub verify_setup {
  25. if (not -f 'oldrc.log') {
  26. die "Run this script in your data directory.\n"
  27. . "The oldrc.log file should be in the same directory.\n";
  28. }
  29. if (not -d 'temp') {
  30. die "Run this script in your data directory.\n"
  31. . "The temp directory should be in the same directory.\n";
  32. }
  33. }
  34. sub request_lock {
  35. if (-d 'temp/lockmain') {
  36. die "The wiki is currently locked.\n"
  37. . "Rerun this script later.\n";
  38. }
  39. mkdir('temp/lockmain') or die "Could not create 'temp/lockmain'.\n"
  40. . "You probably don't have the file permissions necessary.\n";
  41. }
  42. sub release_lock {
  43. rmdir('temp/lockmain') or die "Could not remove 'temp/lockmain'.\n"
  44. }
  45. sub anonymize {
  46. open(F, 'oldrc.log') or die "Could not open 'oldrc.log' for reading.\n";
  47. open(B, '>oldrc.log~') or die "Could not open 'oldrc.log~' for writing.\n"
  48. . "I will not continue without having a backup available.\n";
  49. my $FS = "\x1e"; # The FS character is the RECORD SEPARATOR control char
  50. my @lines = ();
  51. while (my $line = <F>) {
  52. next if $line eq "\n"; # some rc.log files are broken and contain empty lines
  53. my ($ts, $id, $minor, $summary, $host, @rest) = split(/$FS/o, $line);
  54. if ($id eq '[[rollback]]') {
  55. # rollback markers are very different
  56. push(@lines, $line);
  57. } else {
  58. # anonymize
  59. push(@lines, join($FS, $ts, $id, $minor, $summary, 'Anonymous', @rest));
  60. }
  61. print B $line;
  62. }
  63. close(F);
  64. open(F, '>', 'oldrc.log') or die "Could not open 'oldrc.log' for writing.\n";
  65. for my $line (@lines) {
  66. print F $line; # @rest ends with a newline
  67. }
  68. close(F);
  69. print "Wrote anonymized 'oldrc.log'.\n";
  70. print "Saved a backup as 'oldrc.log~'\n";
  71. }
  72. sub main {
  73. verify_setup();
  74. request_lock();
  75. anonymize();
  76. release_lock();
  77. }
  78. main();