1-module(wiki_utils).
2
3%% File    : wiki_utils.erl
4%% Author  : Joe Armstrong (joe@bluetail.com)
5%%         : Johan Bevemyr (jb@bluetail.com)
6%%         : Mickael Remond (mickael.remond@erlang-fr.org)
7%% Purpose : Wiki web utilities
8%%         : Find zombie pages
9%%         : Find all references to a given page
10
11-export([findallrefsto/2, zombies/1]).
12-export([getallrefs/2]).
13-export([getpages_by_prefix/2]).
14
15-import(lists,  [filter/2, member/2, reverse/1, sort/1, map/2]).
16-import(wiki, [p/1, h1/1, show/1]).
17-import(wiki_templates, [template2/5]).
18
19%% HTML structure of the backlink list
20findallrefsto(Page, Root) ->
21    Pages = getallrefs(Page, Root),
22    template2(Root, "References",  "References",
23         ["<p>The following pages contain references to ",
24          wiki_to_html:format_link(Page, Root),".",
25          "<ul>",
26          map(fun(F) ->
27                      [wiki_to_html:format_link(F, Root),"<br>"] end,
28              Pages),
29          "</ul>"], false).
30
31%% Backlinks list
32getallrefs(Page, Root) ->
33    All = wiki:ls(Root),
34    Pages = filter(fun(I) ->
35                           case wiki:read_page(I, Root) of
36                               {ok, Str} ->
37                                   Links = get_links(Str, []),
38                                   member(Page, Links);
39                               error ->
40                                   false
41                           end
42                   end, All),
43    sort(Pages).
44
45zombies(Root) ->
46    All = wiki:ls(Root),
47    {Reached, _Missing} = gc(["home"], [], [], Root),
48    %% Missing = Pages refered to but do not exists at all
49    %% This is not an error
50    NotReached = sort(All -- Reached),
51    template2(Root, "Zombies", "Zombies",
52         [p("These pages have no links to them."),
53          "<ul>",
54          map(fun(F) ->
55                      [wiki_to_html:format_link(F, Root),"<br>"] end,
56              NotReached),
57          "</ul>"], false).
58
59%% Return page name that match a specific prefix
60getpages_by_prefix(Prefix, Root) ->
61    Files = utils:fold_files(Root, Prefix ++ ".*\.wob", false,
62                         fun(F, AccIn)-> [F|AccIn] end, []),
63    Pages = lists:map(fun(I) -> filename:basename(I, ".wob") end,
64                      Files),
65    sort(Pages).
66
67
68gc([H|T], Visited, Missing, Root) ->
69    case member(H, Visited) or member(H, Missing) of
70        true ->
71            gc(T, Visited, Missing, Root);
72        false ->
73            case wiki:read_page(H, Root) of
74                {ok, Str} ->
75                    Links = get_links(Str, []),
76                    gc(Links ++ T, [H|Visited], Missing, Root);
77                error ->
78                    gc(T, Visited, [H|Missing], Root)
79            end
80    end;
81gc([], Visited, Missing, _Root) ->
82    {Visited, Missing}.
83
84get_links([$\\,_C|T], L) -> get_links(T, L);
85get_links([$~|T], L) ->
86    {Link, T1} = wiki_format_txt:collect_wiki_link(T),
87    get_links(T1, [Link|L]);
88get_links([_|T], L) ->
89    get_links(T, L);
90get_links([], L) ->
91    L.
92
93
94
95