migrate2.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Migrate early on tcms3 flatfile sites to 'all posts are series code' (august 2021) code
  5. use FindBin;
  6. use lib "$FindBin::Bin/../lib";
  7. use Trog::Config;
  8. use Trog::Data;
  9. use List::Util;
  10. use UUID::Tiny;
  11. use Trog::SQLite;
  12. use Trog::SQLite::TagIndex;
  13. # Kill the post index
  14. unlink "$FindBin::Bin/../data/posts.db";
  15. $ENV{NOHUP} = 1;
  16. sub uuid { return UUID::Tiny::create_uuid_as_string(UUID::Tiny::UUID_V1, UUID::Tiny::UUID_NS_DNS); }
  17. # Modify these variables to suit your installation.
  18. my $user = 'george';
  19. my @extra_series = (
  20. );
  21. my $conf = Trog::Config::get();
  22. my $search_info = Trog::Data->new($conf);
  23. my @all = $search_info->get( raw => 1, limit => 0 );
  24. my %posts;
  25. foreach my $post (@all) {
  26. $posts{$post->{id}} //= [];
  27. # Re-do the IDs
  28. push(@{$posts{$post->{id}}},$post);
  29. }
  30. foreach my $timestamp (keys(%posts)) {
  31. my $file_to_kill = "$FindBin::Bin/../data/files/$timestamp";
  32. my $new_id = uuid();
  33. # Preserve old URLs
  34. foreach my $post (@{$posts{$timestamp}}) {
  35. delete $post->{app};
  36. delete $post->{preview_file};
  37. delete $post->{wallpaper_file};
  38. delete $post->{scheme};
  39. delete $post->{route};
  40. delete $post->{domain};
  41. $post->{id} = $new_id;
  42. $post->{local_href} = "/posts/$new_id";
  43. $post->{aliases} = ["/posts/$timestamp"];
  44. $post->{callback} = "Trog::Routes::HTML::posts";
  45. $post->{method} = 'GET';
  46. @{$post->{tags}} = grep { defined $_ } @{$post->{tags}};
  47. $post->{content_type} //= 'text/html';
  48. $post->{form} = 'microblog.tx';
  49. $post->{form} = 'blog.tx' if grep {$_ eq 'blog' } @{$post->{tags}};
  50. $post->{form} = 'file.tx' if $post->{content_type} =~ m/^video\//;
  51. $post->{form} = 'file.tx' if $post->{content_type} =~ m/^audio\//;
  52. $post->{form} = 'file.tx' if $post->{content_type} =~ m/^image\//;
  53. if (grep {$_ eq 'about' } @{$post->{tags}}) {
  54. $post->{form} = 'profile.tx';
  55. $post->{local_href} = "/users/$post->{user}";
  56. $post->{callback} = "Trog::Routes::HTML::users";
  57. }
  58. if (grep {$_ eq 'series' } @{$post->{tags}}) {
  59. $post->{form} = 'series.tx';
  60. $post->{callback} = "Trog::Routes::HTML::series";
  61. $post->{child_form} = 'microblog.tx';
  62. $post->{child_form} = 'blog.tx' if $post->{title} =~ m/^blog/i;
  63. $post->{child_form} = 'file.tx' if $post->{title} =~ m/^video\//;
  64. $post->{child_form} = 'file.tx' if $post->{title} =~ m/^audio\//;
  65. $post->{child_form} = 'file.tx' if $post->{title} =~ m/^image\//;
  66. $post->{local_href} = "/$post->{aclname}";
  67. $post->{aliases} = ["/series/$timestamp", "/series/$new_id"];
  68. }
  69. $search_info->write([$post]);
  70. unlink $file_to_kill if -f $file_to_kill;
  71. }
  72. }
  73. # Rebuild the index
  74. Trog::SQLite::TagIndex::build_index($search_info);
  75. Trog::SQLite::TagIndex::build_routes($search_info);
  76. # Add in the series
  77. my $series = [
  78. {
  79. "aclname" => "series",
  80. "acls" => [],
  81. aliases => [],
  82. "callback" => "Trog::Routes::HTML::series",
  83. method => 'GET',
  84. "data" => "Series",
  85. "href" => "/series",
  86. "local_href" => "/series",
  87. "preview" => "/img/sys/testpattern.jpg",
  88. "tags" => [qw{series topbar}],
  89. visibility => 'public',
  90. "title" => "Series",
  91. user => $user,
  92. form => 'series.tx',
  93. child_form => 'series.tx',
  94. },
  95. {
  96. "aclname" => "about",
  97. "acls" => [],
  98. aliases => [],
  99. "callback" => "Trog::Routes::HTML::series",
  100. method => 'GET',
  101. "data" => "About",
  102. "href" => "/about",
  103. "local_href" => "/about",
  104. "preview" => "/img/sys/testpattern.jpg",
  105. "tags" => [qw{series topbar public}],
  106. visibility => 'public',
  107. "title" => "About",
  108. user => $user,
  109. form => 'series.tx',
  110. child_form => 'profile.tx',
  111. },
  112. {
  113. "aclname" => "admin",
  114. acls => [],
  115. aliases => [],
  116. "callback" => "Trog::Routes::HTML::config",
  117. 'method' => 'GET',
  118. "content_type" => "text/plain",
  119. "data" => "Config",
  120. "href" => "/config",
  121. "local_href" => "/config",
  122. "preview" => "/img/sys/testpattern.jpg",
  123. "tags" => [qw{admin}],
  124. visibility => 'private',
  125. "title" => "Configure tCMS",
  126. user => $user,
  127. },
  128. ];
  129. $search_info->add(@$series,@extra_series);