migrate2.pl 6.6 KB

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