migrate6.pl 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env perl
  2. # Display names
  3. use strict;
  4. use warnings;
  5. use FindBin;
  6. use lib "$FindBin::Bin/../lib";
  7. use Trog::SQLite;
  8. sub _dbh {
  9. my $file = 'schema/auth.schema';
  10. my $dbname = "config/auth.db";
  11. return Trog::SQLite::dbh($file,$dbname);
  12. }
  13. my $dbh = _dbh();
  14. #$dbh->do("ALTER TABLE user ADD COLUMN display_name TEXT DEFAULT NULL;");
  15. # Update all the profile type posts to have correct display names
  16. use Trog::Auth;
  17. use JSON::MaybeXS;
  18. use File::Slurper;
  19. use URI::Escape;
  20. use Data::Dumper;
  21. my $global_changes;
  22. opendir(my $dh, 'data/files');
  23. while (my $entry = readdir $dh) {
  24. my $fname = "data/files/$entry";
  25. next unless -f $fname;
  26. my $contents = File::Slurper::read_binary($fname);
  27. my $decoded = JSON::MaybeXS::decode_json($contents);
  28. next unless List::Util::any { $_->{is_profile} } @$decoded;
  29. # If the title on the profile post responsds to a username, then let's change that to a display name
  30. my $made_changes;
  31. foreach my $revision (@$decoded) {
  32. my $user = $revision->{title};
  33. my $display_name = Trog::Auth::username2display($user);
  34. next unless $display_name;
  35. print "converting $user to display name $display_name\n";
  36. $revision->{title} = $display_name;
  37. $revision->{local_href} = "/users/$display_name";
  38. $made_changes = 1;
  39. }
  40. next unless $made_changes;
  41. print "Writing changes to $fname\n";
  42. my $encoded = JSON::MaybeXS::encode_json($decoded);
  43. File::Slurper::write_binary($fname, $encoded);
  44. # Next, waste and rebuild the posts index for these user posts
  45. $global_changes=1;
  46. }
  47. print "Changes made. Please rebuild the posts index.\n" if $global_changes;