migrate2.pl 5.5 KB

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