maptain 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0498a91e2065a11bb4fa6772a18005887f09f773666cbf5af9f651acc4cff99c
4
- data.tar.gz: dfe86a2f2c5490985f5a4e1700ffdd382dd10e36c8687a0a77267a117cae807a
3
+ metadata.gz: 1c41d81bd515f92ad5b6c5e1729e59814b18ee67b9008c0c1f55f0d0e5187c2d
4
+ data.tar.gz: a833cf6bb636d14db0650958de7885b5b7d4de0bfb661ea354cf72a59fa6a678
5
5
  SHA512:
6
- metadata.gz: 25250799fa4124546c0ea7f4412c1a08a8750858ff2104d7ced15af5f9d7b84e749b050343464556129aa7f1277e9a373b376fbdf5d5b7c16277940b6e135ca7
7
- data.tar.gz: 3772bd00cf1d86ea88f40770e1355398be09234ffaec2365bba8c605c7926220b505ffbe00830f7b832932d0a5c217b02926945969d62cdee3c522e977b0a86a
6
+ metadata.gz: e8cc47e53287564a204af6e1088f561edfdd7e4a8aa692c280e79af705356a5e694d992f75de6d8167e452fa702d00f1ae38b88539084cdb36f0fd4edcfb0e1b
7
+ data.tar.gz: 56a2fac38ff8425b0142f3597e78faa7834dfd2d87b2225637423f4f191780dab1616b228050dc9e3992dcc2ce99917229cae9b8422f1eb1e6f12154f12e0ca9
data/CHANGELOG.md CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.3.0] — 2026-11-28
6
+
7
+ A build that dies midway is fully repaired by the next one, models without `updated_at` build, and IndexNow only hears about pages that changed.
8
+
9
+ **Breaking**
10
+ - A build refuses two sources sharing a slug, a list source and a relation source with the same slug included, which used to build.
11
+ - A `lastmod` block or method on a model without `updated_at` needs a `fingerprint`.
12
+ - A misspelled `lastmod` column raises a `SourceError` asking for a `fingerprint`, instead of an SQL error.
13
+ - Sources are found among named classes only: list an anonymous `Class.new(Maptain::Source)` in `config.sources`.
14
+
15
+ **Fixed**
16
+ - A build that died midway is repaired by the next one: files it started to rewrite or delete are written again, and the IndexNow URLs it had found (up to 10,000) are sent.
17
+ - Sources on models without `updated_at` build; their pages have no `lastmod`.
18
+ - Two sources with the same slug are refused before anything is written, instead of crashing midway. A source listed twice is built once, and sources unloaded by a Rails reload are no longer found.
19
+ - A `lastmod` column named by a String is watched like a Symbol.
20
+ - Action Text bodies in video, news and llms.txt fields are written as text, not HTML. A binary or invalid UTF-8 llms.txt title no longer stops the build.
21
+ - IndexNow is no longer sent unchanged pages when `shard_size` gets smaller, a range drops a file, or a list source becomes a relation (or back).
22
+
5
23
  ## [0.2.0] — 2026-11-25
6
24
 
7
25
  Sitemaps for videos and Google News, and the site's llms.txt, written by the same build.
@@ -22,6 +40,7 @@ First release: XML sitemaps for Rails sites in many languages, rebuilt in millis
22
40
  - IndexNow submissions of the pages that appeared, changed or disappeared, which never fail a build.
23
41
  - Checks without a single HTTP request: encoded URLs, W3C dates, hreflang codes, one line per repeated warning.
24
42
 
25
- [Unreleased]: https://github.com/levups/maptain/compare/v0.2.0...HEAD
43
+ [Unreleased]: https://github.com/levups/maptain/compare/v0.3.0...HEAD
44
+ [0.3.0]: https://github.com/levups/maptain/compare/v0.2.0...v0.3.0
26
45
  [0.2.0]: https://github.com/levups/maptain/compare/v0.1.0...v0.2.0
27
46
  [0.1.0]: https://github.com/levups/maptain/releases/tag/v0.1.0
data/README.md CHANGED
@@ -201,7 +201,8 @@ end
201
201
 
202
202
  A video without a title, a description, a thumbnail or a `player_loc` or `content_loc` is left out with a warning,
203
203
  as is one lasting more than 8 hours, one whose player is the page itself, or one with a date that lacks its day.
204
- Titles are cut at 100 characters, descriptions at 2,048.
204
+ Titles are cut at 100 characters, descriptions at 2,048. An Action Text body, here or in the `news` and `llms_txt`
205
+ blocks, is written as its text, not its HTML.
205
206
 
206
207
  Google News reads articles of the last two days only, and 1,000 per file at most: a source with a `news` block gets
207
208
  files that hold no more. Give the articles a source of their own, scoped to those days: an article that leaves the
@@ -255,7 +256,9 @@ of `config.locales`: a build refuses any other.
255
256
  ## The date of a page
256
257
 
257
258
  `lastmod` is read from `updated_at`. Name another column when the content has its own: `lastmod
258
- :content_updated_at`.
259
+ :content_updated_at`. A model without `updated_at` is listed without `lastmod`: its files are rewritten when
260
+ records are added to a range or leave it, and by the full build for any other change. On such a model, a
261
+ `lastmod` block or method needs a `fingerprint` (below), or the build stops.
259
262
 
260
263
  A column that moves when the content does not, such as an `updated_at` touched by a view counter, makes every
261
264
  build rewrite files for nothing and tells search engines a date that is not true. Give such a model a column of
@@ -277,9 +280,11 @@ end
277
280
  ## Files
278
281
 
279
282
  A source named `ProductSitemap` writes `sitemap-product-00000.xml.gz`, `sitemap-product-00001.xml.gz` and so
280
- on, one per range of ids; `slug "products"` changes the middle part, `config.filename` the first. The size of a
281
- range comes from the number of languages, so that a file stays under 50,000 URLs and 50 MiB: 50,000 ids with one
282
- language, 554 with 24. `shard_size 1_000` sets it yourself. A range that still outgrows a file continues in
283
+ on, one per range of ids; `slug "products"` changes the middle part, `config.filename` the first. Each source
284
+ needs a slug of its own: a build refuses two sources with the same slug, such as `ProductSitemap` and
285
+ `Admin::ProductSitemap`, or a source and a subclass that inherits its `slug`. The size of a range comes from the
286
+ number of languages, so that a file stays under 50,000 URLs and 50 MiB: 50,000 ids with one language, 554 with 24.
287
+ `shard_size 1_000` sets it yourself. A range that still outgrows a file continues in
283
288
  `sitemap-product-00000-2.xml.gz`.
284
289
 
285
290
  `sitemap.xml` is the index. It lists every file with the latest `lastmod` it holds, so a crawler fetches again
@@ -314,10 +319,11 @@ config.rebuild_every = 1.day # default: a week; nil leaves it to maptain:rebui
314
319
 
315
320
  After deploying a change to a `url` block, run `maptain:rebuild` rather than wait.
316
321
 
317
- A build notes the files it is about to add in `sitemap-manifest.json`, its memory of what each file held, then
318
- writes the sitemap files, then the index, then removes the files no range fills any more, then writes the
319
- manifest again. A build that fails midway is repaired by the next one. Changing a host, the languages, the
320
- size of a range or the version of Maptain rewrites everything it moves once.
322
+ A build notes the files it is about to change or remove, and the URLs it is to send to IndexNow, in
323
+ `sitemap-manifest.json`, its memory of what each file held, then writes the sitemap files, then the index, then
324
+ the `llms.txt`, then removes the files no range fills any more, then writes the manifest again. A build that fails
325
+ midway is repaired by the next one, which writes those files again and sends those URLs. Changing a host, the
326
+ languages, the size of a range or the version of Maptain rewrites everything it moves once.
321
327
 
322
328
  ## Storage
323
329
 
@@ -365,8 +371,8 @@ config.index_now_key = "c1f0e2b4a6d84f3e9b7a5c3d1e0f2a4b"
365
371
 
366
372
  Each build then publishes the key as `<key>.txt` at the root of the site, and sends, from the files it rewrote,
367
373
  the URLs whose `lastmod` is later than the previous build, and those that appeared or went away whatever their
368
- `lastmod`. The first build sends nothing, nor does a new configuration beyond the pages whose `lastmod` moved,
369
- and only URLs on the hosts of the site are sent.
374
+ `lastmod`. The first build sends nothing, nor does a new configuration, range size or a scope turned from a list into a relation (or
375
+ back) beyond the pages whose `lastmod` moved, and only URLs on the hosts of the site are sent.
370
376
 
371
377
  When the sitemaps are served from elsewhere (`config.sitemaps_url`), the key cannot follow them: IndexNow reads
372
378
  it on the site. Put `<key>.txt` in `public/` and set `config.index_now_key_location` to its path,
@@ -50,6 +50,8 @@ module Maptain
50
50
  @directory = directory
51
51
  @previous = Manifest.read(@storage, manifest_name)
52
52
  @previous_file_by_name = @previous.files.to_h { |file| [file["name"], file] }
53
+ # What a build that died started to publish: whatever its manifest says, the storage may hold something else.
54
+ @pending = @previous.pending.to_set
53
55
  built_at = Time.now
54
56
  @full = full || rebuild_due?(built_at)
55
57
  @reusable = !@full && @previous.digest == @configuration.digest
@@ -76,22 +78,33 @@ module Maptain
76
78
  @news_sources = @configuration.sources.select { |source_class| source_class.definitions.key?(:news) }.to_set { |source_class| source_class.slug }
77
79
 
78
80
  llms_txt_sources = []
79
- @configuration.sources.each do |source_class|
81
+ @configuration.sources.uniq.each do |source_class|
80
82
  source = source_class.new(@configuration)
81
83
  build_source(source)
82
84
  llms_txt_sources << source if @configuration.llms_txt && source.llms_txt?
83
85
  end
84
86
  # Rendered before anything is published: a block that raises stops the build before it changes the storage.
85
87
  llms_txt_by_name = render_llms_txt(llms_txt_sources) if @configuration.llms_txt
86
- publish_rendered
88
+ deleted = (@previous.file_names | @previous.pending | @previous.extras.keys) - @manifest.file_names -
89
+ @rendered.flat_map { |rendered| rendered.files.map(&:name) } - llms_txt_by_name.to_h.keys
90
+ @changed_urls.concat(@rendered.flat_map(&:changed_urls))
91
+ # Not the pages of moved ranges, nor the articles of a news file gone because they grew old, nor those of a
92
+ # range rendered again, which appeared_or_gone compared with all its previous files.
93
+ rendered_shards = @rendered.to_set { |rendered| [rendered.source_slug, rendered.shard] }
94
+ gone_files = deleted.reject do |name|
95
+ source_slug, shard = previous_shard_of(name)
96
+ @moved_sources.include?(source_slug) || @news_sources.include?(source_slug) || rendered_shards.include?([source_slug, shard])
97
+ end
98
+ @changed_urls.concat(urls_in(gone_files)) if @diffing
99
+ urls = urls_to_submit
100
+ changed_names = @rendered.flat_map(&:files).reject { |file| same_as_before?(file) }.to_set(&:name)
101
+ changed_llms_txt = llms_txt_by_name.to_h.reject { |name, content| same_llms_txt_as_before?(name, content) }
102
+ announce(changed_names + changed_llms_txt.keys + deleted, urls)
103
+ publish_rendered(changed_names)
87
104
  publish_index
88
- publish_llms_txt(llms_txt_by_name) if llms_txt_by_name
89
- deleted = (@previous.file_names | @previous.pending | @previous.extras.keys) - @manifest.file_names - @manifest.extras.keys
90
- # Not the pages of moved ranges, nor the articles of a news file gone because they grew old.
91
- @changed_urls.concat(urls_in(deleted.reject { |name| @moved_sources.include?(previous_source_of(name)) || @news_sources.include?(previous_source_of(name)) })) if @diffing
105
+ publish_llms_txt(llms_txt_by_name, changed_llms_txt) if llms_txt_by_name
92
106
  deleted.each { |name| @storage.delete(name) }
93
107
  publish_index_now_key
94
- urls = urls_to_submit
95
108
  # Written before submitting, with the URLs still to send: a build that dies while submitting loses none.
96
109
  @manifest.unsubmitted.replace(urls.last(MAX_UNSUBMITTED_URLS))
97
110
  upload(manifest_name, @manifest.to_json)
@@ -103,9 +116,11 @@ module Maptain
103
116
 
104
117
  def build_source(source)
105
118
  seen_urls = Set.new
106
- source.fingerprint_by_shard.sort_by { |shard, _fingerprint| shard.to_i }.each do |shard, fingerprint|
119
+ fingerprint_by_shard = source.fingerprint_by_shard
120
+ @moved_sources << source.slug if ranges_moved?(source.slug, fingerprint_by_shard)
121
+ fingerprint_by_shard.sort_by { |shard, _fingerprint| shard.to_i }.each do |shard, fingerprint|
107
122
  stored = @previous.shard(source.slug, shard) if @reusable
108
- if fingerprint && stored && stored["fingerprint"] == fingerprint
123
+ if fingerprint && stored && stored["fingerprint"] == fingerprint && stored["files"].none? { |file| @pending.include?(file["name"]) }
109
124
  keep(source, shard, stored)
110
125
  else
111
126
  render(source, shard, fingerprint, seen_urls)
@@ -134,10 +149,12 @@ module Maptain
134
149
  writer.close
135
150
  end
136
151
 
137
- if writer.files.all? { |file| same_as_before?(file) }
138
- changed_urls.clear
139
- elsif urls
140
- changed_urls.concat(appeared_or_gone(source.slug, shard, fingerprint, urls, gone: !source.news?))
152
+ # A file dropped from a range is a change too, even when the files left are as they were.
153
+ unchanged = writer.files.all? { |file| same_as_before?(file) }
154
+ changed_urls.clear if unchanged
155
+ stored_names = @previous.shard(source.slug, shard)&.fetch("files")&.map { |file| file["name"] } || []
156
+ if urls && !(unchanged && writer.files.map(&:name) == stored_names)
157
+ changed_urls.concat(appeared_or_gone(source.slug, shard, urls, gone: !source.news?))
141
158
  end
142
159
  fingerprint ||= writer.content_digest
143
160
  # Recorded now, published once every shard is rendered: the index keeps the order of the sources.
@@ -148,23 +165,30 @@ module Maptain
148
165
  # Read while the previous files are still in the storage: a page that appeared or went away is a change too,
149
166
  # whatever its lastmod says. Not when the ranges moved, which only carries pages from one file to another.
150
167
  # gone: false for news, whose articles leave when they grow old, not when their page does.
151
- def appeared_or_gone(source_slug, shard, fingerprint, urls, gone: true)
152
- stored = @previous.shard(source_slug, shard)
153
- if stored && fingerprint && stored["fingerprint"].to_s.split("/").first != fingerprint.split("/").first
154
- @moved_sources << source_slug
155
- return []
156
- end
168
+ def appeared_or_gone(source_slug, shard, urls, gone: true)
169
+ return [] if @moved_sources.include?(source_slug)
157
170
 
171
+ stored = @previous.shard(source_slug, shard)
158
172
  previous_urls = urls_in((stored&.fetch("files") || []).map { |file| file["name"] }).to_set
159
173
  urls = urls.to_set
160
174
  gone ? (urls - previous_urls).to_a + (previous_urls - urls).to_a : (urls - previous_urls).to_a
161
175
  end
162
176
 
163
- def publish_rendered
164
- changed_names = @rendered.flat_map(&:files).reject { |file| same_as_before?(file) }.to_set(&:name)
165
- announce_new_files(changed_names)
177
+ # Whether the size of the ranges, the first part of their fingerprints, is not the one the last build stored,
178
+ # or the source went from one file to ranges or back: its pages then sit in other files, which held other ids.
179
+ def ranges_moved?(source_slug, fingerprint_by_shard)
180
+ sizes = fingerprint_by_shard.map { |shard, fingerprint| range_size(shard, fingerprint) }
181
+ stored = @previous.shards_by_source.fetch(source_slug, {}).map { |shard, stored_shard| range_size(shard, stored_shard["fingerprint"]) }
182
+ sizes.any? && (stored - sizes).any?
183
+ end
184
+
185
+ # A range fingerprint starts with its size; a source rendered whole is one file, whatever its digest.
186
+ def range_size(shard, fingerprint)
187
+ (shard == Source::WHOLE) ? Source::WHOLE : fingerprint.to_s.split("/").first
188
+ end
189
+
190
+ def publish_rendered(changed_names)
166
191
  @rendered.each do |rendered|
167
- @changed_urls.concat(rendered.changed_urls)
168
192
  entries = rendered.files.map do |file|
169
193
  changed = changed_names.include?(file.name)
170
194
  publish(file, changed)
@@ -174,11 +198,12 @@ module Maptain
174
198
  end
175
199
  end
176
200
 
177
- def previous_source_of(file_name)
178
- @previous_source_by_file_name ||= @previous.shards_by_source.each_with_object({}) do |(source_slug, shards), source_by_name|
179
- shards.each_value { |stored| stored["files"].each { |file| source_by_name[file["name"]] = source_slug } }
201
+ # [source slug, shard] of a file the last build recorded, or nil.
202
+ def previous_shard_of(file_name)
203
+ @previous_shard_by_file_name ||= @previous.shards_by_source.each_with_object({}) do |(source_slug, shards), shard_by_name|
204
+ shards.each { |shard, stored| stored["files"].each { |file| shard_by_name[file["name"]] = [source_slug, shard] } }
180
205
  end
181
- @previous_source_by_file_name[file_name]
206
+ @previous_shard_by_file_name[file_name]
182
207
  end
183
208
 
184
209
  def urls_in(names)
@@ -191,15 +216,18 @@ module Maptain
191
216
  end
192
217
 
193
218
  def same_as_before?(file)
194
- @previous_file_by_name.dig(file.name, "digest") == file.digest
219
+ !@pending.include?(file.name) && @previous_file_by_name.dig(file.name, "digest") == file.digest
195
220
  end
196
221
 
197
- # Recorded before the first upload, so that a build dying midway leaves no file the next one cannot remove.
198
- def announce_new_files(names)
199
- new_names = names.to_a - @previous_file_by_name.keys - @previous.pending - @previous.extras.keys
200
- return if new_names.empty?
222
+ # Recorded before the first upload, so that a build dying midway leaves no file the next one cannot remove or
223
+ # would keep as it was before (or gone), and no URL to submit it cannot find again: the files that told which pages
224
+ # appeared or went away are gone by then, and a lastmod tells nothing once its page is deleted.
225
+ def announce(names, unsubmitted)
226
+ new_names = names.to_a - @previous.pending
227
+ return if new_names.empty? && (unsubmitted - @previous.unsubmitted).empty?
201
228
 
202
229
  @previous.pending.concat(new_names)
230
+ @previous.unsubmitted.replace(unsubmitted.last(MAX_UNSUBMITTED_URLS))
203
231
  upload(manifest_name, @previous.to_json)
204
232
  end
205
233
 
@@ -271,13 +299,15 @@ module Maptain
271
299
  end
272
300
  end
273
301
 
274
- # Each file is published when it differs from the last build's, the new ones announced together.
275
- def publish_llms_txt(content_by_name)
276
- changed = content_by_name.select { |name, content| @full || @previous.extras[name] != Digest::SHA256.hexdigest(content) }
277
- announce_new_files(changed.keys)
302
+ def same_llms_txt_as_before?(name, content)
303
+ !@pending.include?(name) && @previous.extras[name] == Digest::SHA256.hexdigest(content)
304
+ end
305
+
306
+ # Each file is published when it differs from the last build's, every one by a full build.
307
+ def publish_llms_txt(content_by_name, changed)
278
308
  content_by_name.each do |name, content|
279
309
  @manifest.extras[name] = Digest::SHA256.hexdigest(content)
280
- if changed.key?(name)
310
+ if @full || changed.key?(name)
281
311
  upload(name, content)
282
312
  @written << name
283
313
  else
@@ -43,7 +43,14 @@ module Maptain
43
43
 
44
44
  # Checked when a build starts, not when host_by_locale or llms_txt is set: locales may be set after them.
45
45
  def validate!
46
+ host!
46
47
  llms_txt&.locale_in(self)
48
+ sources.uniq.group_by(&:slug).each do |slug, same_slug|
49
+ next if same_slug.one?
50
+
51
+ raise ConfigurationError, "#{same_slug.map { |source_class| source_class.name || "a subclass of #{source_class.superclass.name || source_class.superclass.slug}" }.join(", ")} " \
52
+ "share the slug #{slug.inspect}, which names their files: give each a slug of its own"
53
+ end
47
54
  hreflangs = locales.map { |locale| Xml.hreflang(locale) }
48
55
  unknown = host_by_locale.keys - hreflangs
49
56
  return if unknown.empty?
@@ -86,7 +93,8 @@ module Maptain
86
93
  return @sources if @sources
87
94
 
88
95
  load_sources.call
89
- Source.descendants.select(&:scope?).sort_by(&:slug)
96
+ # Named: a Class.new(…) left by a test would be found until it is garbage collected.
97
+ Source.descendants.select { |source_class| source_class.name && source_class.scope? }.sort_by(&:slug)
90
98
  end
91
99
 
92
100
  # Whatever changes every file: a build under another digest starts over.
@@ -15,8 +15,9 @@ module Maptain
15
15
  MAX_RECORDS = 2_000
16
16
  # What I18n.t gives for a key it does not have, unless told default: nil: "Translation missing: en.key" ("translation
17
17
  # missing" before i18n 1.13), or "Translation missing. Options considered were:" with a default list. A title that
18
- # only starts with those words stays.
19
- MISSING_TRANSLATION = /\Atranslation missing(?:: [^\s.]+\..+\z|\. Options considered were:)/i
18
+ # only starts with those words stays: the key begins with a locale in lowercase letters, maybe with a region or a
19
+ # script ("en.", "pt-BR.", "zh_Hant."), where "Dr. No" does not.
20
+ MISSING_TRANSLATION = /\Atranslation missing(?:: (?-i:[a-z]{2,3}(?:[-_][A-Za-z0-9]{2,8})*)\..+\z|\. Options considered were:)/i
20
21
 
21
22
  Entry = Data.define(:section, :title, :url, :notes)
22
23
  # A file to write: its name, its language and the host it is for.
@@ -26,7 +27,11 @@ module Maptain
26
27
 
27
28
  # Text worth writing: neither blank nor a missing translation.
28
29
  def self.present?(text)
29
- text.present? && !text.to_s.match?(MISSING_TRANSLATION)
30
+ # A String is read through Xml.text alone: blank? raises on one that is not valid UTF-8.
31
+ return false if !text.is_a?(String) && text.blank?
32
+
33
+ string = Xml.text(text)
34
+ string.present? && !string.match?(MISSING_TRANSLATION)
30
35
  end
31
36
 
32
37
  # title, summary and details: a String, or a block run in the language of the file. locale: the language of
@@ -75,7 +80,7 @@ module Maptain
75
80
  lines = ["# #{line(title).presence || host}"]
76
81
  summary = line(summary)
77
82
  lines << "> #{summary}" unless summary.empty?
78
- details = details.to_s.strip
83
+ details = Xml.text(details)
79
84
  lines << details unless details.empty?
80
85
  entries.group_by(&:section).each do |section, section_entries|
81
86
  lines << "## #{line(section)}"
@@ -114,7 +119,7 @@ module Maptain
114
119
 
115
120
  # Markdown reads a line break as the end of an item or a heading.
116
121
  def line(text)
117
- text.to_s.gsub(/[[:space:]]+/, " ").strip
122
+ Xml.text(text).gsub(/[[:space:]]+/, " ").strip
118
123
  end
119
124
 
120
125
  # Serves /llms.txt from config.storage, the file of the request's host, for one host per language or sitemaps
@@ -2,9 +2,9 @@
2
2
 
3
3
  module Maptain
4
4
  # What the last build wrote: for each shard of each source, its fingerprint and its files. pending lists the
5
- # files a build started to publish before it could record them, so that the next one can remove them;
6
- # unsubmitted the URLs IndexNow did not take, for the next one to send again; extras the digest of each file
7
- # that is not a sitemap, such as llms.txt, by name.
5
+ # files a build was about to write or delete when it died, so that the next one writes or removes them again;
6
+ # unsubmitted the URLs IndexNow has yet to take, refused or not sent by a build that died, for the next one to
7
+ # send; extras the digest of each file that is not a sitemap, such as llms.txt, by name.
8
8
  class Manifest
9
9
  attr_reader :built_at, :rebuilt_at, :digest, :shards_by_source, :pending, :unsubmitted, :extras
10
10
 
@@ -8,6 +8,9 @@ module Maptain
8
8
  # url { |product, locale| product_url(product, locale:) }
9
9
  # end
10
10
  class Source
11
+ # Lets the Rails reloader drop the classes it unloads from Source.descendants, where sources are found.
12
+ extend ActiveSupport::DescendantsTracker
13
+
11
14
  # The protocol allows 50 MiB a file, uncompressed. Shards aim lower: the sizes below are estimates.
12
15
  TARGET_BYTES_PER_FILE = 40 * 1024 * 1024
13
16
  BYTES_PER_URL_ESTIMATE = 150
@@ -61,9 +64,10 @@ module Maptain
61
64
  end
62
65
 
63
66
  # A column, or a block for anything else. With a block on a relation, name what changes with it in
64
- # `fingerprint`: the column alone no longer tells a build what moved.
67
+ # `fingerprint`: the column alone no longer tells a build what moved. A column named by a String is the
68
+ # same column: the ranges watch it.
65
69
  def lastmod(column = nil, &block)
66
- definitions[:lastmod] = block || column
70
+ definitions[:lastmod] = block || (column.is_a?(String) ? column.to_sym : column)
67
71
  end
68
72
 
69
73
  def images(&block)
@@ -135,7 +139,7 @@ module Maptain
135
139
  # The sum of the ids moves when a record leaves a range and another enters it; the shard size, when the
136
140
  # ranges themselves move. Summed as offsets within the range: SQLite's SUM overflows on large ids.
137
141
  offset_sum = Arel.sql("SUM(#{model.quoted_table_name}.#{model.quoted_primary_key} % #{shard_size})")
138
- relation.reorder(nil).group(shard_sql).pluck(shard_sql, Arel.sql("COUNT(*)"), offset_sum, fingerprint_sql)
142
+ relation.reorder(nil).group(shard_sql).pluck(*[shard_sql, Arel.sql("COUNT(*)"), offset_sum, fingerprint_sql].compact)
139
143
  .to_h { |shard, count, sum, latest| [shard.to_i.to_s, "#{shard_size}/#{count}/#{sum.to_i}/#{precise(latest)}"] }
140
144
  end
141
145
 
@@ -395,11 +399,21 @@ module Maptain
395
399
  end
396
400
  end
397
401
 
402
+ # nil for a model without updated_at and a lastmod left as it is: its ranges then change with their ids alone.
398
403
  def fingerprint_sql
399
404
  column = definitions[:lastmod].is_a?(Symbol) ? definitions[:lastmod] : :updated_at
400
405
  expression = definitions[:fingerprint] ||
401
- "#{model.quoted_table_name}.#{model.adapter_class.quote_column_name(column)}"
402
- Arel.sql("MAX(#{expression})")
406
+ ("#{model.quoted_table_name}.#{model.adapter_class.quote_column_name(column)}" if model.column_names.include?(column.to_s))
407
+ return Arel.sql("MAX(#{expression})") if expression
408
+
409
+ lastmod = definitions[:lastmod]
410
+ return unless lastmod
411
+ return if lastmod == :updated_at && !model.attribute_names.include?("updated_at") &&
412
+ !model.attribute_alias?("updated_at") && !model.method_defined?(:updated_at)
413
+
414
+ # A date the pages show but no column holds: nothing would tell a build that it moved.
415
+ given = lastmod.respond_to?(:call) ? "block" : lastmod.inspect
416
+ raise SourceError, "#{self.class.name || slug}'s lastmod #{given} has no column of #{model.table_name} to watch: name it with fingerprint"
403
417
  end
404
418
  end
405
419
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Maptain
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/maptain/xml.rb CHANGED
@@ -123,11 +123,16 @@ module Maptain
123
123
  # Text XML 1.0 can hold: UTF-8 whatever the String's encoding, without invalid bytes or the control characters
124
124
  # XML forbids, trimmed.
125
125
  def text(value)
126
- string = value.to_s
126
+ string = plain_text(value)
127
127
  string = (string.encoding == Encoding::BINARY) ? string.dup.force_encoding(Encoding::UTF_8) : string.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "")
128
128
  string.scrub("").gsub(XML_FORBIDDEN, "").strip
129
129
  end
130
130
 
131
+ # An Action Text body as its text: its to_s is the HTML it renders.
132
+ def plain_text(value)
133
+ value.respond_to?(:to_plain_text) ? value.to_plain_text.to_s : value.to_s
134
+ end
135
+
131
136
  # At most limit characters as a reader counts them: an accent stays with its letter.
132
137
  def cut(text, limit)
133
138
  (text.length > limit) ? text.grapheme_clusters.first(limit).join : text
data/lib/maptain.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "active_support"
4
4
  require "active_support/core_ext/object/blank"
5
5
  require "active_support/core_ext/string/inflections"
6
+ require "active_support/descendants_tracker"
6
7
  require "active_support/notifications"
7
8
  require "date"
8
9
  require "digest"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maptain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Level UP Solutions