+let topological_sort ~dbd uris =
+ let module OrderedUri =
+ struct
+ type t = UriManager.uri
+ let compare = UriManager.compare
+ end in
+ let module Topo = HTopoSort.Make(OrderedUri) in
+ Topo.topological_sort uris
+ (fun uri -> fst (List.split (direct_deps ~dbd uri)))
+
+let sorted_uris_of_baseuri ~dbd baseuri =
+ let sql_pat =
+ Pcre.replace ~rex:(Pcre.regexp "_") ~templ:"\\_" baseuri ^ "%"
+ in
+ let query =
+ Printf.sprintf
+ ("SELECT source FROM %s WHERE source LIKE \"%s\" UNION "^^
+ "SELECT source FROM %s WHERE source LIKE \"%s\"")
+ (MetadataTypes.name_tbl ()) sql_pat
+ MetadataTypes.library_name_tbl sql_pat
+ in
+ let result = HSql.exec dbd query in
+ let map cols = match cols.(0) with
+ | Some s -> UriManager.uri_of_string s
+ | _ -> assert false
+ in
+ let uris = HSql.map result map in
+ let sorted_uris = topological_sort ~dbd uris in
+ let filter_map uri =
+ let s =
+ Pcre.replace ~rex:(Pcre.regexp "#xpointer\\(1/1\\)") ~templ:""
+ (UriManager.string_of_uri uri)
+ in
+ try ignore (Pcre.exec ~rex:(Pcre.regexp"#xpointer") s); None
+ with Not_found -> Some (UriManager.uri_of_string s)
+ in
+ HExtlib.filter_map filter_map sorted_uris
+