strip-host.pl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #! /usr/bin/perl -w
  2. # Copyright (C) 2018 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. sub ParseData {
  17. my $data = shift;
  18. my %result;
  19. while ($data =~ /(\S+?): (.*?)(?=\n[^ \t]|\Z)/sg) {
  20. my ($key, $value) = ($1, $2);
  21. $value =~ s/\n\t/\n/g;
  22. $result{$key} = $value;
  23. }
  24. return %result;
  25. }
  26. sub EncodePage {
  27. my @data = @_;
  28. my $result = '';
  29. $result .= (shift @data) . ': ' . EscapeNewlines(shift @data) . "\n" while (@data);
  30. return $result;
  31. }
  32. sub EscapeNewlines {
  33. $_[0] =~ s/\n/\n\t/g; # modify original instead of copying
  34. return $_[0];
  35. }
  36. sub main {
  37. die "There is no temp directory, here.\n"
  38. . "Perhaps this isn't an Oddmuse data directory?\n"
  39. unless -d 'temp';
  40. die "The main lock already exists in the temp directory.\n"
  41. if -d "temp/lockmain";
  42. mkdir "temp/lockmain" or die "Cannot create main lock in temp: $!\n";
  43. local $/ = undef; # Read complete files
  44. foreach my $dir (qw/keep page/) {
  45. warn "Did not find the $dir directory.\n" unless -d $dir;
  46. }
  47. # include dotfiles!
  48. my $t = 0;
  49. my $n = 0;
  50. foreach my $file (glob("page/*.pg page/.*.pg"),
  51. glob("keep/*/*.kp keep/.*.kp")) {
  52. $t++;
  53. open(my $fh, '<', $file) or die "Cannot read $file file: $!\n";
  54. my $data = <$fh>;
  55. close($fh);
  56. next unless $data;
  57. my %result = ParseData($data);
  58. if (exists($result{host}) or exists($result{ip})) {
  59. delete($result{host});
  60. delete($result{ip});
  61. open($fh,'>', "$file~") or die "Cannot $file~: $!\n";
  62. print $fh EncodePage(%result);
  63. close($fh);
  64. rename("$file~", $file) or die "Cannot rename $file~ to $file: $!\n";
  65. $n++;
  66. }
  67. }
  68. rmdir "temp/lockmain" or die "Cannot remove main lock: $!\n";
  69. print "I looked at $t files and found $n host or ip keys which I removed.\n";
  70. }
  71. if (@ARGV) {
  72. print qq{
  73. Usage: $0 [--page DIR]
  74. Goes through the wiki and removes the hostname or IP number from page and
  75. keep files. Make a backup before running this script! Run this script in
  76. your data directory.
  77. }
  78. } else {
  79. main ();
  80. }