migrate2.pl 6.7 KB

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