migrate2.pl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. my $conf = Trog::Config::get();
  21. my $search_info = Trog::Data->new($conf);
  22. my @all = $search_info->get( raw => 1, limit => 0 );
  23. my %posts;
  24. foreach my $post (@all) {
  25. $posts{ $post->{id} } //= [];
  26. # Re-do the IDs
  27. push( @{ $posts{ $post->{id} } }, $post );
  28. }
  29. foreach my $timestamp ( keys(%posts) ) {
  30. my $file_to_kill = "$FindBin::Bin/../data/files/$timestamp";
  31. my $new_id = uuid();
  32. # Preserve old URLs
  33. foreach my $post ( @{ $posts{$timestamp} } ) {
  34. delete $post->{app};
  35. delete $post->{preview_file};
  36. delete $post->{wallpaper_file};
  37. delete $post->{scheme};
  38. delete $post->{route};
  39. delete $post->{domain};
  40. $post->{id} = $new_id;
  41. $post->{local_href} = "/posts/$new_id";
  42. $post->{aliases} = ["/posts/$timestamp"];
  43. $post->{callback} = "Trog::Routes::HTML::posts";
  44. $post->{method} = 'GET';
  45. @{ $post->{tags} } = grep { defined $_ } @{ $post->{tags} };
  46. $post->{content_type} //= 'text/html';
  47. $post->{form} = 'microblog.tx';
  48. $post->{form} = 'blog.tx' if grep { $_ eq 'blog' } @{ $post->{tags} };
  49. $post->{form} = 'file.tx' if $post->{content_type} =~ m/^video\//;
  50. $post->{form} = 'file.tx' if $post->{content_type} =~ m/^audio\//;
  51. $post->{form} = 'file.tx' if $post->{content_type} =~ m/^image\//;
  52. if ( grep { $_ eq 'about' } @{ $post->{tags} } ) {
  53. $post->{form} = 'profile.tx';
  54. $post->{local_href} = "/users/$post->{user}";
  55. $post->{callback} = "Trog::Routes::HTML::users";
  56. }
  57. if ( grep { $_ eq 'series' } @{ $post->{tags} } ) {
  58. $post->{form} = 'series.tx';
  59. $post->{callback} = "Trog::Routes::HTML::series";
  60. $post->{child_form} = 'microblog.tx';
  61. $post->{child_form} = 'blog.tx' if $post->{title} =~ m/^blog/i;
  62. $post->{child_form} = 'file.tx' if $post->{title} =~ m/^video\//;
  63. $post->{child_form} = 'file.tx' if $post->{title} =~ m/^audio\//;
  64. $post->{child_form} = 'file.tx' if $post->{title} =~ m/^image\//;
  65. $post->{local_href} = "/$post->{aclname}";
  66. $post->{aliases} = [ "/series/$timestamp", "/series/$new_id" ];
  67. }
  68. $search_info->write( [$post] );
  69. unlink $file_to_kill if -f $file_to_kill;
  70. }
  71. }
  72. # Rebuild the index
  73. Trog::SQLite::TagIndex::build_index($search_info);
  74. Trog::SQLite::TagIndex::build_routes($search_info);
  75. # Add in the series
  76. my $series = [
  77. {
  78. "aclname" => "series",
  79. "acls" => [],
  80. aliases => [],
  81. "callback" => "Trog::Routes::HTML::series",
  82. method => 'GET',
  83. "data" => "Series",
  84. "href" => "/series",
  85. "local_href" => "/series",
  86. "preview" => "/img/sys/testpattern.jpg",
  87. "tags" => [qw{series topbar}],
  88. visibility => 'public',
  89. "title" => "Series",
  90. user => $user,
  91. form => 'series.tx',
  92. child_form => 'series.tx',
  93. },
  94. {
  95. "aclname" => "about",
  96. "acls" => [],
  97. aliases => [],
  98. "callback" => "Trog::Routes::HTML::series",
  99. method => 'GET',
  100. "data" => "About",
  101. "href" => "/about",
  102. "local_href" => "/about",
  103. "preview" => "/img/sys/testpattern.jpg",
  104. "tags" => [qw{series topbar public}],
  105. visibility => 'public',
  106. "title" => "About",
  107. user => $user,
  108. form => 'series.tx',
  109. child_form => 'profile.tx',
  110. },
  111. {
  112. "aclname" => "admin",
  113. acls => [],
  114. aliases => [],
  115. "callback" => "Trog::Routes::HTML::config",
  116. 'method' => 'GET',
  117. "content_type" => "text/plain",
  118. "data" => "Config",
  119. "href" => "/config",
  120. "local_href" => "/config",
  121. "preview" => "/img/sys/testpattern.jpg",
  122. "tags" => [qw{admin}],
  123. visibility => 'private',
  124. "title" => "Configure tCMS",
  125. user => $user,
  126. },
  127. ];
  128. $search_info->add( @$series, @extra_series );