+
+(* Because of redirects, getting the list of pages which link to this
+ * page isn't a matter of just doing 'select from_url from links ...'.
+ * We also look at pages which redirect to this URL (for redirections
+ * of degrees 1 through 4 = max_redirect).
+ *)
+let what_links_here (dbh : Dbi.connection) hostid page =
+ (* Build up the complete list of URLs which redirect to the target
+ * page, within max_redirect redirections. This is sort of like
+ * Prim's algorithm.
+ *)
+ let urls = ref [page] in
+ let found = ref true in
+ let i = ref 1 in
+ while !found && !i <= max_redirect do
+ let qs = Dbi.placeholders (List.length !urls) in
+ let sql =
+ "select url from pages
+ where hostid = ?
+ and url is not null and redirect is not null
+ and url not in " ^ qs ^ " and redirect in " ^ qs in
+ let sth = dbh#prepare_cached sql in
+ let args = List.map (fun s -> `String s) !urls in
+ sth#execute (`Int hostid :: (args @ args));
+ let new_urls = sth#map (function [`String s] -> s | _ -> assert false) in
+ urls := !urls @ new_urls;
+ found := new_urls <> [];
+ incr i
+ done;
+
+ let urls = !urls in
+
+ (* Now find any pages which link to one of these target pages. For
+ * convenience we also select out the titles.
+ *)
+ let qs = Dbi.placeholders (List.length urls) in
+ let sth =
+ dbh#prepare_cached
+ ("select li.from_url, p.title
+ from links li, pages p
+ where li.hostid = ? and li.to_url in " ^ qs ^ "
+ and li.hostid = p.hostid and li.from_url = p.url") in
+ sth#execute (`Int hostid :: (List.map (fun s -> `String s) urls));
+
+ sth#map (function
+ | [`String url; `String title] -> url, title
+ | _ -> assert false)