DataSource.pm 9.1 KB

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