adk_site/migrate/mod.rs
1//! Migration from an existing site or online presence (spec 3.5 §7.8 "Sources", "Sanitiser",
2//! "Feature mapping", "Freeze", "Probes"; §7.8.1 migration rows; §7.18 stages 1–3; A-I9, A-I10).
3//!
4//! | Stage | Types | Spec 3.5 |
5//! |-------|-------|----------|
6//! | Domain claim | [`claim::DomainClaim`], [`claim::ProvenDomain`], [`ClaimScope`] | §7.18 stage 1, A-I9 |
7//! | Sources | [`SiteSource`] → [`SiteInventory`]: [`sources::WordPressRest`], [`sources::Wxr`], [`sources::Crawl`], [`sources::Presence`], [`sources::IsolatedReplay`] | §7.8 "Sources", S2, S3 |
8//! | Outbound HTTP | [`http::HttpClient`]: every request signed through a [`http::CrawlSigner`] (Web Bot Auth), robots-respecting, rate-limited, egress-checked and recorded | §7.3 "Outbound", §7.8 |
9//! | Sanitiser | [`sanitize::Sanitiser`]: HTML to Markdown through an allowlist | §7.8 "Sanitiser" |
10//! | Detectors | [`detect`]: hidden-link spam, cloaking, executables and MIME mismatches, obfuscated scripts | §7.8 "Sanitiser", S3 |
11//! | Quarantine | [`quarantine::QuarantineBook`]: findings with evidence, released only by a per-item [`ApprovalGate`](crate::pipeline::ApprovalGate) decision | §7.8, §7.8.1 "Quarantine" |
12//! | Feature mapping | [`mapping::map_features`]: `CapabilityV2` drafts and owner decisions | §7.8 "Feature mapping", N5 |
13//! | Content | [`convert::to_content`]: inventory items as content items that keep their URLs | §7.8.2, S17 |
14//! | Freeze | [`freeze::Freeze`]: a crawled snapshot published as a release, forms mapped to edge capabilities | §7.8 "Freeze", S4 |
15//! | Parity | [`parity::MigrationParity`], a launch-gate extension: URL coverage, main-text similarity, metadata, and the perceptual difference when the owner keeps the design ([`parity::SiteDesign`]) | §7.8.1 |
16//! | Probes | [`probe::Prober`]: page digests against the release window, status, certificate, DNS, security headers; [`probe::Drift`] for the Guardian | §7.8 "Probes", §7.8.3, S5 |
17//! | Archive | [`archive::LegacyArchive`]: the legacy export as an encrypted artifact; no delete | A-I10 |
18//!
19//! Nothing here contacts a legacy site except through [`http::HttpClient`], and a site whose
20//! control the owner has not proved gets only polite public reads (A-I9).
21//!
22//! # Example
23//!
24//! Inventory a WordPress export and turn it into content items:
25//!
26//! ```rust,no_run
27//! use adk_site::migrate::{SiteSource, SourceContext, convert, sources::Wxr};
28//!
29//! # async fn demo(context: &SourceContext) -> adk_site::migrate::Result<()> {
30//! let export = std::fs::read("export.xml").unwrap();
31//! let inventory = Wxr::from_bytes(export).inventory(context).await?;
32//! let converted = convert::to_content(&inventory, &convert::ConvertOptions::new("en".parse().unwrap()))?;
33//! assert_eq!(converted.items.len(), inventory.items.len());
34//! # Ok(())
35//! # }
36//! ```
37
38pub mod archive;
39mod audit;
40pub mod claim;
41pub mod convert;
42pub mod detect;
43mod error;
44pub mod freeze;
45pub(crate) mod html;
46pub mod http;
47mod inventory;
48pub mod mapping;
49pub mod parity;
50pub mod probe;
51pub mod quarantine;
52pub mod robots;
53pub mod sanitize;
54pub mod serve;
55pub mod sources;
56mod xml;
57
58use std::sync::Arc;
59
60use adk_core::audit_log::AuditRecorder;
61use adk_core::{ActingContext, Clock, SystemClock};
62use async_trait::async_trait;
63
64pub use claim::{ClaimScope, ProvenDomain};
65pub use error::{MigrateError, Result};
66pub use inventory::{
67 DetectedFeature, Fact, FactSource, FeatureEvidence, FeatureKind, FormField, InventoryItem,
68 InventoryMedia, InventoryUrl, ItemStatus, Menu, MenuEntry, PageMeta, ReviewQuote,
69 SiteInventory, SourceKind, Taxonomy, Term,
70};
71
72/// What a source needs to read a site (spec 3.5 §7.8).
73///
74/// `scope` decides what a source may do: until the owner proves control of the domain
75/// ([`claim::DomainClaim`]) only polite public reads run (A-I9).
76#[derive(Clone)]
77pub struct SourceContext {
78 /// The site's own acting identity: drafts are read with credentials the broker issues to it.
79 pub acting: ActingContext,
80 /// Polite public reads, or everything a proven domain allows.
81 pub scope: ClaimScope,
82 /// The signed, robots-respecting, rate-limited client every request goes through.
83 pub http: Arc<http::HttpClient>,
84 /// The clock inventories are stamped with.
85 pub clock: Arc<dyn Clock>,
86 /// Where `site.migration.*` audit records go, when anywhere.
87 pub audit: Option<Arc<dyn AuditRecorder>>,
88 /// Upper bounds on what a source reads.
89 pub limits: SourceLimits,
90}
91
92impl std::fmt::Debug for SourceContext {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 f.debug_struct("SourceContext")
95 .field("acting", &self.acting.agent.id)
96 .field("scope", &self.scope)
97 .field("limits", &self.limits)
98 .finish_non_exhaustive()
99 }
100}
101
102impl SourceContext {
103 /// A context for `acting` with polite scope, the system clock and default limits.
104 pub fn new(acting: ActingContext, http: Arc<http::HttpClient>) -> Self {
105 Self {
106 acting,
107 scope: ClaimScope::Polite,
108 http,
109 clock: Arc::new(SystemClock),
110 audit: None,
111 limits: SourceLimits::default(),
112 }
113 }
114
115 /// Sets the scope, such as [`ClaimScope::Proven`] after a domain claim.
116 #[must_use]
117 pub fn with_scope(mut self, scope: ClaimScope) -> Self {
118 self.scope = scope;
119 self
120 }
121
122 /// Sets the clock.
123 #[must_use]
124 pub fn with_clock(mut self, clock: Arc<dyn Clock>) -> Self {
125 self.clock = clock;
126 self
127 }
128
129 /// Records `site.migration.*` audit records in `recorder`.
130 #[must_use]
131 pub fn with_audit(mut self, recorder: Arc<dyn AuditRecorder>) -> Self {
132 self.audit = Some(recorder);
133 self
134 }
135
136 /// Sets the limits.
137 #[must_use]
138 pub fn with_limits(mut self, limits: SourceLimits) -> Self {
139 self.limits = limits;
140 self
141 }
142
143 /// The proven domain, or [`MigrateError::NotProven`] naming `action`.
144 ///
145 /// # Errors
146 ///
147 /// [`MigrateError::NotProven`] when the scope is polite (A-I9).
148 pub fn require_proven(&self, domain: &str, action: &str) -> Result<&ProvenDomain> {
149 self.scope.require(domain, action)
150 }
151}
152
153/// Upper bounds on what a source reads.
154#[derive(Debug, Clone, Copy, PartialEq, Eq)]
155pub struct SourceLimits {
156 /// Pages fetched by a crawl.
157 pub max_pages: usize,
158 /// Links followed from the start page.
159 pub max_depth: usize,
160 /// Media files downloaded.
161 pub max_media: usize,
162 /// Bytes read from one response.
163 pub max_response_bytes: usize,
164 /// Items per WordPress REST collection page (at most 100).
165 pub per_page: usize,
166}
167
168impl Default for SourceLimits {
169 fn default() -> Self {
170 Self {
171 max_pages: 2_000,
172 max_depth: 12,
173 max_media: 5_000,
174 max_response_bytes: 20 * 1024 * 1024,
175 per_page: 100,
176 }
177 }
178}
179
180/// Reads an existing site or presence into a [`SiteInventory`] (spec 3.5 §7.8).
181///
182/// Implementations: [`sources::WordPressRest`], [`sources::Wxr`], [`sources::Crawl`],
183/// [`sources::Presence`] and [`sources::IsolatedReplay`]; [`sources::source_conformance`]
184/// checks any of them.
185#[async_trait]
186pub trait SiteSource: Send + Sync {
187 /// Reads the site: URLs, items, media, taxonomies, menus and detected dynamic features.
188 ///
189 /// # Errors
190 ///
191 /// Returns an error when the site cannot be read at all; pages that fail are recorded in
192 /// the inventory's `notes` instead.
193 async fn inventory(&self, ctx: &SourceContext) -> Result<SiteInventory>;
194}