29
30:- module(plweb_wiki,
31 [ wiki_file_to_dom/2, 32 wiki_file_codes_to_dom/3, 33 wiki_page_title/2, 34 index_wiki_pages/0, 35 update_wiki_page_title/1, 36 wiki_extension/1, 37 file//2, 38 include//3, 39 extract_title/3, 40 title_text/2, 41 safe_file_name/1, 42 safe_dom/2 43 ]). 44:- reexport(library(pldoc/doc_html),
45 except([ file//2,
46 include//3
47 ])). 48
49:- use_module(library(pldoc/doc_wiki)). 50:- use_module(library(http/html_write)). 51:- use_module(library(http/http_wrapper)). 52:- use_module(library(http/http_dispatch)). 53:- use_module(library(readutil)). 54:- use_module(library(option)). 55:- use_module(library(apply)). 56:- use_module(library(lists)). 57:- use_module(library(filesex)). 58:- use_module(wiki_edit). 59
60:- predicate_options(file//2, 2,
61 [ absolute_path(atom),
62 label(any)
63 ]). 64:- predicate_options(include//3, 3,
65 [pass_to(pldoc_html:include/5, 3)]). 66
70
71wiki_file_to_dom(File, DOM) :-
72 read_file_to_codes(File, String, []),
73 wiki_file_codes_to_dom(String, File, DOM).
74
79
80wiki_file_codes_to_dom(String, File, DOM) :-
81 ( nb_current(pldoc_file, OrgFile)
82 -> setup_call_cleanup(
83 b_setval(pldoc_file, File),
84 wiki_codes_to_dom(String, [], DOM0),
85 b_setval(pldoc_file, OrgFile))
86 ; setup_call_cleanup(
87 b_setval(pldoc_file, File),
88 wiki_codes_to_dom(String, [], DOM0),
89 nb_delete(pldoc_file))
90 ),
91 safe_dom(DOM0, DOM).
92
100
101safe_dom(Var, Var) :-
102 var(Var), !.
103safe_dom(List0, List) :-
104 is_list(List0), !,
105 maplist(safe_dom, List0, List).
106safe_dom(\Goal, \Goal) :- !.
107safe_dom(Fmt-Args, Fmt-Args) :- !.
108safe_dom(Elem0, Elem) :-
109 compound(Elem0),
110 compound_name_arguments(Elem0, Name, [Attrs0, Content0]), !,
111 safe_attributes(Attrs0, Attrs),
112 safe_dom(Content0, Content),
113 compound_name_arguments(Elem, Name, [Attrs, Content]).
114safe_dom(Elem0, Elem) :-
115 compound(Elem0),
116 compound_name_arguments(Elem0, Name, [Content0]), !,
117 safe_dom(Content0, Content),
118 compound_name_arguments(Elem, Name, [Content]).
119safe_dom(DOM, DOM).
120
121safe_attributes(Var, Var) :-
122 var(Var), !.
123safe_attributes(List0, List) :-
124 is_list(List0), !,
125 maplist(safe_attributes, List0, List).
126safe_attributes(Attr0, Attr) :-
127 ( Attr0 = (Name=Value)
128 -> true
129 ; compound(Attr0),
130 compound_name_arguments(Attr0, Name, [Value])
131 ),
132 url_attribute(Name),
133 \+ safe_url(Value), !,
134 ( Attr0 = (Name=_)
135 -> Attr = (Name='#')
136 ; compound_name_arguments(Attr, Name, ['#'])
137 ).
138safe_attributes(Attr, Attr).
139
140url_attribute(href).
141url_attribute(src).
142url_attribute(action).
143url_attribute(formaction).
144url_attribute(data).
145url_attribute(poster).
146url_attribute(background).
147
153
154safe_url(URL) :-
155 atomic(URL),
156 atom_codes(URL, Codes0),
157 exclude([C]>>(C =< 0' ), Codes0, Codes),
158 atom_codes(Clean, Codes),
159 ( sub_atom(Clean, B, _, _, :),
160 sub_atom(Clean, 0, B, _, Scheme0),
161 \+ ( sub_atom(Scheme0, _, 1, _, C),
162 memberchk(C, [/, ?, #])
163 )
164 -> downcase_atom(Scheme0, Scheme),
165 safe_scheme(Scheme)
166 ; true 167 ).
168
169safe_scheme(http).
170safe_scheme(https).
171safe_scheme(ftp).
172safe_scheme(mailto).
173
174
175 178
180
181include(File, wiki, _Options) -->
182 { \+ safe_include_file(File, _) }, !,
183 html(code(class(nofile), ['[[',File,']]'])).
184include(File, wiki, Options) --> !,
185 { safe_include_file(File, Path) },
186 pldoc_html:include(Path, wiki,
187 [ map_extension([txt-html])
188 | Options
189 ]).
190include(Object, Type, Options) -->
191 pldoc_html:include(Object, Type,
192 [ map_extension([txt-html])
193 | Options
194 ]).
195
203
204safe_include_file(File, Path) :-
205 nb_current(pldoc_file, Current),
206 atom(Current),
207 is_absolute_file_name(Current),
208 absolute_file_name(document_root(.), Root,
209 [ file_type(directory),
210 access(read),
211 solutions(all)
212 ]),
213 atom_concat(Root, '/', RootDir),
214 sub_atom(Current, 0, _, _, RootDir),
215 absolute_file_name(File, Path,
216 [ relative_to(Current),
217 access(read),
218 file_errors(fail)
219 ]),
220 sub_atom(Path, 0, _, _, RootDir),
221 !.
222
226
227file(Path, Options) -->
228 { \+ option(label(_), Options),
229 file_base_name(Path, File),
230 file_name_extension(Label, txt, File), !,
231 file_href(Options, Options1)
232 },
233 pldoc_html:file(Path,
234 [ label(Label),
235 map_extension([txt-html]),
236 edit_handler(wiki_edit)
237 | Options1
238 ]).
239file(File, Options) -->
240 { file_href(Options, Options1)
241 },
242 pldoc_html:file(File,
243 [ map_extension([txt-html]),
244 edit_handler(wiki_edit)
245 | Options1
246 ]).
247
248
249file_href(Options0, Options) :-
250 \+ ( nb_current(pldoc_file, CFile),
251 CFile \== []
252 ),
253 option(absolute_path(Path), Options0),
254 absolute_file_name(document_root(.),
255 DocRoot,
256 [ file_type(directory),
257 access(read)
258 ]),
259 atom_concat(DocRoot, DocLocal, Path), !,
260 ensure_leading_slash(DocLocal, HREF),
261 Options = [ href(HREF) | Options0 ].
262file_href(Options, Options).
263
264ensure_leading_slash(Path, SlashPath) :-
265 ( sub_atom(Path, 0, _, _, /)
266 -> SlashPath = Path
267 ; atom_concat(/, Path, SlashPath)
268 ).
269
270 273
274:- multifile
275 prolog:doc_object_summary/4,
276 prolog:doc_object_link//2,
277 prolog:doc_object_page//2,
278 prolog:doc_category/3,
279 prolog:doc_file_index_header//2. 280
281prolog:doc_object_summary(wiki(Location), wiki, wiki, Summary) :-
282 wiki_page_title(Location, Summary).
283
284:- dynamic
285 wiki_page_title_cache/3, 286 wiki_pages_indexed/1. 287
291
292wiki_page_title(Location, Title) :-
293 wiki_pages_indexed(_), !,
294 wiki_page_title_cache(Location, Title, _).
295wiki_page_title(Location, Title) :-
296 nonvar(Location), !,
297 ( wiki_page_title_cache(Location, TitleRaw, _)
298 -> Title = TitleRaw
299 ; extract_wiki_page_title(Location, File, TitleRaw)
300 -> time_file(File, Modified),
301 assertz(wiki_page_title_cache(Location, TitleRaw, Modified)),
302 Title = TitleRaw
303 ; print_message(warning, wiki(no_title(Location))),
304 Title = 'No title'
305 ).
306wiki_page_title(Location, Title) :-
307 index_wiki_pages,
308 wiki_page_title(Location, Title).
309
310
311update_wiki_title_cache :-
312 wiki_locations(Pages),
313 maplist(update_wiki_page_title, Pages).
314
318
319update_wiki_page_title(Location) :-
320 wiki_page_title_cache(Location, _, Time), !,
321 location_wiki_file(Location, File),
322 time_file(File, Modified),
323 ( abs(Time-Modified) < 1
324 -> true
325 ; extract_wiki_page_title(Location, File, Title),
326 retractall(wiki_page_title_cache(Location, _, _)),
327 assertz(wiki_page_title_cache(Location, Title, Modified))
328 ).
329update_wiki_page_title(Location) :-
330 extract_wiki_page_title(Location, File, Title),
331 time_file(File, Modified),
332 assertz(wiki_page_title_cache(Location, Title, Modified)).
333
(Location, File, Title) :-
335 ( var(File)
336 -> location_wiki_file(Location, File, read)
337 ; true
338 ),
339 ( catch(wiki_file_to_dom(File, DOM), E,
340 ( print_message(warning, E),
341 fail
342 )),
343 dom_title(DOM, Title)
344 -> true
345 ; format(atom(Title), 'Wiki page at "~w"', Location)
346 ).
347
348
354
355dom_title([h1(_, TitleList)|_], Title) :-
356 maplist(to_atom, TitleList, TitleList2),
357 atomic_list_concat(TitleList2, Title).
358
359to_atom(Atomic, Atomic) :- atomic(Atomic).
360to_atom(predref(Name/Arity), Label) :-
361 atomic_list_concat([Name,/,Arity], Label).
362
363prolog:doc_object_link(wiki(Location), _Options) -->
364 { wiki_page_title(Location, Title) },
365 html([ '[wiki] ', Title ]).
366
367prolog:doc_object_page(wiki(Location), _Options) -->
368 { http_current_request(Request),
369 http_redirect(see_other, root(Location), Request)
370 }.
371
372prolog:doc_category(wiki, 60, 'Wiki pages').
373
374prolog:(wiki, _) --> [].
375
380
381index_wiki_pages :-
382 wiki_pages_indexed(_), !.
383index_wiki_pages :-
384 with_mutex(index_wiki_pages,
385 index_wiki_pages_sync).
386
387index_wiki_pages_sync :-
388 wiki_pages_indexed(_).
389index_wiki_pages_sync :-
390 wiki_locations(Locations),
391 maplist(wiki_page_title, Locations, _Titles),
392 get_time(Now),
393 asserta(wiki_pages_indexed(Now)).
394
395
399
400wiki_locations(Files) :-
401 findall(Dir, absolute_file_name(
402 document_root(.), Dir,
403 [ access(read),
404 file_type(directory),
405 solutions(all)
406 ]),
407 RootDirs),
408 maplist(wiki_locations, RootDirs, NestedFiles),
409 append(NestedFiles, Files).
410
411wiki_locations(Dir, Files) :-
412 phrase(wiki_locations(Dir, Dir), Files).
413
414wiki_locations([], _) --> !.
415wiki_locations([H|T], Root) --> !,
416 wiki_locations(H, Root),
417 wiki_locations(T, Root).
418wiki_locations(CurrentDir, Root) -->
419 { exists_directory(CurrentDir), !,
420 directory_files(CurrentDir, Members),
421 exclude(special, Members, Members2),
422 maplist(directory_file_path(CurrentDir), Members2, MemberPaths)
423 },
424 wiki_locations(MemberPaths, Root).
425wiki_locations(Entry, Root) -->
426 { file_name_extension(_, Ext, Entry),
427 wiki_extension(Ext), !,
428 directory_file_path(Root, Wiki, Entry)
429 },
430 [Wiki].
431wiki_locations(_, _) --> [].
432
433wiki_extension(txt).
434wiki_extension(md).
435
436special(.).
437special(..).
438
443
([H|T], Title, T) :-
445 title(H, Title), !.
446extract_title(DOM, 'SWI-Prolog', DOM).
447
448title(h1(_Attrs, Title), Title).
449title(h2(_Attrs, Title), Title).
450title(h3(_Attrs, Title), Title).
451title(h4(_Attrs, Title), Title).
452
459
460title_text(Title, Text) :-
461 maplist(atomic, Title), !,
462 atomics_to_string(Title, Text).
463title_text(Title, Text) :-
464 phrase(html(Title), Tokens),
465 with_output_to(string(HTML), print_html(Tokens)),
466 setup_call_cleanup(
467 open_string(HTML, In),
468 load_html(In, DOM, []),
469 close(In)),
470 xpath(element(div, [], DOM), /('*'(text)), Text).
471
476
477safe_file_name(Name) :-
478 must_be(atom, Name),
479 prolog_to_os_filename(FileName, Name),
480 \+ unsafe_name(FileName),
481 !.
482safe_file_name(Name) :-
483 permission_error(read, file, Name).
484
485unsafe_name(Name) :- Name == '..'.
486unsafe_name(Name) :- sub_atom(Name, 0, _, _, '../').
487unsafe_name(Name) :- sub_atom(Name, _, _, _, '/../').
488unsafe_name(Name) :- sub_atom(Name, _, _, 0, '/..')