migrate2.pl 7.4 KB

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