DataSource.pm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package Trog::DataSource;
  2. use v5.36;
  3. use re '/aa';
  4. use Trog::DataModule ();
  5. use Trog::Log qw{:all};
  6. =head1 Trog::DataSource
  7. What a datasource is, and the default answers for the parts of it a given
  8. datasource doesn't care to implement.
  9. A post type can say its posts come from somewhere other than the datastore --
  10. see Trog::DataSource::Virt, which builds them out of libvirt guests. The series
  11. is still an ordinary post; only its children are synthesized, and they are
  12. rebuilt on every view rather than stored.
  13. Datasources are plain packages under this namespace, called as functions rather
  14. than methods, and named by a post type's sidecar in x-tcms-datasource.
  15. =head1 THE CONTRACT
  16. posts($series, $query) = @posts required
  17. The children of $series, as post hashrefs. Enough of a post to survive being
  18. rendered like one -- see Trog::DataSource::Virt::_post_for for the shape.
  19. EDITABLE optional constant
  20. Whether the wizard should generate an editor for a type drawn from this source.
  21. A source which builds its posts from somewhere else has nothing to edit, and
  22. saying nothing means no.
  23. filter($query, @posts) = @posts optional
  24. Apply the viewer's search to the synthesized posts. Defaulted below; implement
  25. it to search the fields your posts actually carry.
  26. order($query, @posts) = @posts optional
  27. The order they belong in. Defaulted below; implement it if your posts have an
  28. order of their own that a date and a title don't capture.
  29. lang() = STRING optional
  30. help() = STRING optional
  31. What the search box is searching, and where to read about it, for a page drawn
  32. from this source. Defaulted below.
  33. =head1 FUNCTIONS
  34. =head2 filter($query, @posts) = @posts
  35. The default search over synthesized posts: the query's C<like>, C<author>,
  36. C<older> and C<newer>, with the same meanings they have for stored posts.
  37. Deliberately not the tag, acl or version filtering Trog::DataModule::filter()
  38. also does. Those are answers about the datastore, and they are already settled
  39. before a datasource is reached: the series carries the acls, the reader got to
  40. its page by holding them, and every synthesized post belongs to that series.
  41. Applying them again would at best be redundant, and at worst would blank the
  42. page for a source whose posts carry no tags -- which nothing requires them to.
  43. Nor does it paginate. That is the route's job and is the same for every source
  44. -- see order(), which is the part a source does get a say in.
  45. A source with more to search than a title should say so by implementing filter()
  46. rather than by hoping this one covers it.
  47. =cut
  48. sub filter ( $query, @posts ) {
  49. $query //= {};
  50. if ( length( $query->{like} // '' ) ) {
  51. my $like = $query->{like};
  52. @posts = grep { searchable_match( $like, $_->{title}, $_->{data} ) } @posts;
  53. }
  54. @posts = grep { ( $_->{user} // '' ) eq $query->{author} } @posts if $query->{author};
  55. # Coerced to a number the same way Trog::DataModule::filter() does it, since
  56. # this arrives straight off a query string.
  57. if ( $query->{older} ) {
  58. ( my $older = $query->{older} ) =~ s/[^0-9]//g;
  59. @posts = grep { ( $_->{created} // 0 ) < $older } @posts if length $older;
  60. }
  61. if ( $query->{newer} ) {
  62. ( my $newer = $query->{newer} ) =~ s/[^0-9]//g;
  63. @posts = grep { ( $_->{created} // 0 ) > $newer } @posts if length $newer;
  64. }
  65. return @posts;
  66. }
  67. =head2 searchable_match($like, @values) = BOOL
  68. Whether any of @values contains $like, case insensitively.
  69. The same case insensitive substring test Trog::DataModule::filter() applies to a
  70. stored post's title and body, factored out so that a datasource can hand it the
  71. fields its own posts actually carry without having to restate what a search
  72. means. Quotemeta'd, so a search for '*' looks for an asterisk.
  73. Anything in @values which isn't a plain string is skipped, so an arrayref body
  74. or a nested structure is passed over rather than matched against its address.
  75. =cut
  76. sub searchable_match ( $like, @values ) {
  77. return 0 unless length( $like // '' );
  78. foreach my $value (@values) {
  79. next if !defined $value || ref $value;
  80. return 1 if $value =~ m/\Q$like\E/i;
  81. }
  82. return 0;
  83. }
  84. =head2 order($query, @posts) = @posts
  85. The order synthesized posts belong in: newest first, then by title, then by id.
  86. Pagination hands out page 2 of an order, so there had better be one, and it had
  87. better be the same order next time somebody asks -- a list that comes back
  88. shuffled makes page 2 a lottery rather than the next page. Sorting the ties out
  89. by title and then id is what makes this total rather than merely mostly decided.
  90. Newest-first matches how stored posts are listed. The tiebreaks carry the
  91. weight in practice, because a source that builds its posts on the fly tends to
  92. stamp them all with the time it built them: every libvirt guest comes back with
  93. the same created, so what this really does for that page is sort it by name.
  94. =cut
  95. sub order ( $query, @posts ) {
  96. return sort { ( $b->{created} // 0 ) <=> ( $a->{created} // 0 ) || ( $a->{title} // '' ) cmp ( $b->{title} // '' ) || ( $a->{id} // '' ) cmp ( $b->{id} // '' ) } @posts;
  97. }
  98. =head2 lang() = STRING
  99. What the search box searches on a page drawn from a datasource which hasn't said.
  100. Not the data model's answer: the model's query language describes what it can do
  101. to the datastore, and a datasource page is not being served out of the datastore.
  102. =cut
  103. sub lang { 'Case insensitive substring' }
  104. =head2 help() = STRING
  105. Where to read about it.
  106. =cut
  107. sub help { 'https://en.wikipedia.org/wiki/Substring' }
  108. =head2 for_type($form) = STRING or undef
  109. The datasource a post type names, or nothing when it names none.
  110. Checked against this namespace rather than requiring whatever we were told to:
  111. the name comes out of a sidecar, and a sidecar cannot be allowed to make us load
  112. an arbitrary module. Returns the module name without loading it; see load().
  113. =cut
  114. sub for_type ( $form = '' ) {
  115. return undef unless length( $form // '' );
  116. my $meta = Trog::DataModule::type_meta_for($form);
  117. my $source = $meta->{'x-tcms-datasource'};
  118. return undef unless $source && !ref $source;
  119. if ( $source !~ m/^Trog::DataSource::\w+$/ ) {
  120. WARN("Post type '$form' names a datasource '$source' which isn't one");
  121. return undef;
  122. }
  123. return $source;
  124. }
  125. =head2 load($source) = BOOL
  126. Load a datasource named by for_type(), returning whether it worked.
  127. =cut
  128. sub load ($source) {
  129. my $modpath = $source;
  130. $modpath =~ s{::}{/}g;
  131. $modpath .= '.pm';
  132. local $@;
  133. eval { require $modpath; 1 } or do {
  134. WARN("Datasource '$source' will not load: $@");
  135. return 0;
  136. };
  137. return 1;
  138. }
  139. 1;