blob: 544f5d6188c1303728b0aacd2708b64f3d33fd55 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
(ns weepinbell.migration
"Migration utilities that is meant to be used as a command in the -main
function. Not to be used inside the system.
Features may be removed when they are not needed."
(:require [weepinbell.storage.h2 :as h2]
[weepinbell.storage.edn-storage :as edns]
[weepinbell.storage.spec]
[weepinbell.metadata.microformats :refer [parse-microformats]]
[weepinbell.utils.url :refer [normalize]]
[weepinbell.utils.digest :refer [digest]]
[clojure.java.io :as io]
[fast-edn.core :as edn]))
(defn mf->jf2
"Given a Webmention object with microformats, convert it to
jf2, and remove original microformats."
[wm]
{:pre [(map? wm) (contains? wm :microformats)]}
(-> wm
(assoc :jf2 (parse-microformats (:microformats wm)))
(dissoc :microformats)))
(defn re-normalize
"Given a Webmention object, recalculate its hash and return a new map."
[wm]
{:pre [(map? wm) (contains? wm :webmention)]}
(let [{:keys [source target]} (:webmention wm)
normal-source (normalize source)
normal-target (normalize target)
newhash (digest (str normal-source normal-target))]
(assoc wm :webmention
(-> (:webmention wm)
(assoc :source normal-source)
(assoc :target normal-target)
(assoc :id newhash)))))
(defn edn->h2
"Given an EDN file and a SQLite DB path, migrate data from that
edn file to SQLite DB. It appends data to DB so the destination
db can be empty or it can has some data.
This also converts microformats to jf2, as the new H2 schema is
incompatible with microformats."
[edn-file h2-db]
{:pre [(some? edn-file) (some? h2-db)]}
(let [ds (h2/make-datasource h2-db)]
(h2/create-tables ds)
(edns/with-webmentions edn-file
(fn [wms]
(doseq [wm wms]
(let [id (get-in wm [:webmention :id])]
(try
(let [wm (if (contains? wm :microformats)
(mf->jf2 wm) wm)
wm (re-normalize wm)]
(h2/insert-webmention ds wm)
(println id "->" (get-in wm [:webmention :id])))
(catch Exception e
(println "Something unexpected happened.")
(println (.getMessage e))))))))
(println "Done")))
(defn- file? [file]
(or (string? file)
(instance? java.io.File file)
(instance? java.net.URL file)))
(defn edn-jf2ify
"Given an EDN file with deprecated microformats data, converts
microformats to jf2 and generate a new file in new format."
[old-edn new-edn]
{:pre [(file? old-edn) (file? new-edn)]}
(io/make-parents new-edn)
(.createNewFile (io/file new-edn))
(with-open [rdr (io/reader old-edn)]
(doseq [wm (map edn/read-string (line-seq rdr))]
(println (get-in wm [:webmention :id]))
(let [new-data (if (contains? wm :microformats)
(mf->jf2 wm) wm)]
(spit new-edn (prn-str new-data) :append true)))))
|