DataSource.pm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 A NOTE ON WHAT A LISTING DISCLOSES
  34. A datasource turns something into a list, and a list of names is itself
  35. information. Two things follow that are worth thinking about before you write
  36. one.
  37. The first is that the names may be more sensitive than the things. A file whose
  38. contents the webserver will refuse to serve still has a name, and publishing that
  39. name publishes the fact of it. Decide deliberately what your source is willing
  40. to enumerate, rather than enumerating whatever it can reach and relying on
  41. whatever gates the contents.
  42. The second is that the webserver is part of the security model and you cannot see
  43. it from here. On a correctly provisioned site nginx auth_requests the
  44. /authenticated route -- which answers 200 for a request carrying a live session
  45. and 403 otherwise -- and gates www/assets/private on the result. That protection
  46. is real, it is why that route exists, and it is also invisible to this code and
  47. absent on a site provisioned some other way. Do not lean on it, and do not
  48. duplicate it either: your job is to decide what to put in the list.
  49. Trog::DataSource::DirIndex is the worked example, and documents where it drew
  50. that line and why.
  51. =head1 FUNCTIONS
  52. =head2 filter($query, @posts) = @posts
  53. The default search over synthesized posts: the query's C<like>, C<author>,
  54. C<older> and C<newer>, with the same meanings they have for stored posts.
  55. Deliberately not the tag, acl or version filtering Trog::DataModule::filter()
  56. also does. Those are answers about the datastore, and they are already settled
  57. before a datasource is reached: the series carries the acls, the reader got to
  58. its page by holding them, and every synthesized post belongs to that series.
  59. Applying them again would at best be redundant, and at worst would blank the
  60. page for a source whose posts carry no tags -- which nothing requires them to.
  61. Nor does it paginate. That is the route's job and is the same for every source
  62. -- see order(), which is the part a source does get a say in.
  63. A source with more to search than a title should say so by implementing filter()
  64. rather than by hoping this one covers it.
  65. =cut
  66. sub filter ( $query, @posts ) {
  67. $query //= {};
  68. if ( length( $query->{like} // '' ) ) {
  69. my $like = $query->{like};
  70. @posts = grep { searchable_match( $like, $_->{title}, $_->{data} ) } @posts;
  71. }
  72. @posts = grep { ( $_->{user} // '' ) eq $query->{author} } @posts if $query->{author};
  73. # Coerced to a number the same way Trog::DataModule::filter() does it, since
  74. # this arrives straight off a query string.
  75. if ( $query->{older} ) {
  76. ( my $older = $query->{older} ) =~ s/[^0-9]//g;
  77. @posts = grep { ( $_->{created} // 0 ) < $older } @posts if length $older;
  78. }
  79. if ( $query->{newer} ) {
  80. ( my $newer = $query->{newer} ) =~ s/[^0-9]//g;
  81. @posts = grep { ( $_->{created} // 0 ) > $newer } @posts if length $newer;
  82. }
  83. return @posts;
  84. }
  85. =head2 searchable_match($like, @values) = BOOL
  86. Whether any of @values contains $like, case insensitively.
  87. The same case insensitive substring test Trog::DataModule::filter() applies to a
  88. stored post's title and body, factored out so that a datasource can hand it the
  89. fields its own posts actually carry without having to restate what a search
  90. means. Quotemeta'd, so a search for '*' looks for an asterisk.
  91. Anything in @values which isn't a plain string is skipped, so an arrayref body
  92. or a nested structure is passed over rather than matched against its address.
  93. =cut
  94. sub searchable_match ( $like, @values ) {
  95. return 0 unless length( $like // '' );
  96. foreach my $value (@values) {
  97. next if !defined $value || ref $value;
  98. return 1 if $value =~ m/\Q$like\E/i;
  99. }
  100. return 0;
  101. }
  102. =head2 order($query, @posts) = @posts
  103. The order synthesized posts belong in: newest first, then by title, then by id.
  104. Pagination hands out page 2 of an order, so there had better be one, and it had
  105. better be the same order next time somebody asks -- a list that comes back
  106. shuffled makes page 2 a lottery rather than the next page. Sorting the ties out
  107. by title and then id is what makes this total rather than merely mostly decided.
  108. Newest-first matches how stored posts are listed. The tiebreaks carry the
  109. weight in practice, because a source that builds its posts on the fly tends to
  110. stamp them all with the time it built them: every libvirt guest comes back with
  111. the same created, so what this really does for that page is sort it by name.
  112. =cut
  113. sub order ( $query, @posts ) {
  114. return sort { ( $b->{created} // 0 ) <=> ( $a->{created} // 0 ) || ( $a->{title} // '' ) cmp ( $b->{title} // '' ) || ( $a->{id} // '' ) cmp ( $b->{id} // '' ) } @posts;
  115. }
  116. =head2 lang() = STRING
  117. What the search box searches on a page drawn from a datasource which hasn't said.
  118. Not the data model's answer: the model's query language describes what it can do
  119. to the datastore, and a datasource page is not being served out of the datastore.
  120. =cut
  121. sub lang { 'Case insensitive substring' }
  122. =head2 help() = STRING
  123. Where to read about it.
  124. =cut
  125. sub help { 'https://en.wikipedia.org/wiki/Substring' }
  126. =head2 for_type($form) = STRING or undef
  127. The datasource a post type names, or nothing when it names none.
  128. Checked against this namespace rather than requiring whatever we were told to:
  129. the name comes out of a sidecar, and a sidecar cannot be allowed to make us load
  130. an arbitrary module. Returns the module name without loading it; see load().
  131. =cut
  132. sub for_type ( $form = '' ) {
  133. return undef unless length( $form // '' );
  134. my $meta = Trog::DataModule::type_meta_for($form);
  135. my $source = $meta->{'x-tcms-datasource'};
  136. return undef unless $source && !ref $source;
  137. if ( $source !~ m/^Trog::DataSource::\w+$/ ) {
  138. WARN("Post type '$form' names a datasource '$source' which isn't one");
  139. return undef;
  140. }
  141. return $source;
  142. }
  143. =head2 load($source) = BOOL
  144. Load a datasource named by for_type(), returning whether it worked.
  145. =cut
  146. sub load ($source) {
  147. my $modpath = $source;
  148. $modpath =~ s{::}{/}g;
  149. $modpath .= '.pm';
  150. local $@;
  151. eval { require $modpath; 1 } or do {
  152. WARN("Datasource '$source' will not load: $@");
  153. return 0;
  154. };
  155. return 1;
  156. }
  157. 1;