View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2009-2015, VU University Amsterdam
    7
    8    This program is free software; you can redistribute it and/or
    9    modify it under the terms of the GNU General Public License
   10    as published by the Free Software Foundation; either version 2
   11    of the License, or (at your option) any later version.
   12
   13    This program is distributed in the hope that it will be useful,
   14    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16    GNU General Public License for more details.
   17
   18    You should have received a copy of the GNU General Public
   19    License along with this library; if not, write to the Free Software
   20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21
   22    As a special exception, if you link this library with other files,
   23    compiled with a Free Software compiler, to produce an executable, this
   24    library does not by itself cause the resulting executable to be covered
   25    by the GNU General Public License. This exception does not however
   26    invalidate any other reasons why the executable file might be covered by
   27    the GNU General Public License.
   28*/
   29
   30:- module(plweb_wiki,
   31	  [ wiki_file_to_dom/2,		% +File, -DOM
   32	    wiki_file_codes_to_dom/3,	% +Codes, +File, -DOM
   33	    wiki_page_title/2,		% +Location, -Title
   34	    index_wiki_pages/0,		%
   35	    update_wiki_page_title/1,	% +Location
   36	    wiki_extension/1,		% ?Extension
   37	    file//2,			% +File, +Options
   38	    include//3,			% +Object, +Type, +Options
   39	    extract_title/3,		% +DOM0, -Title, -DOM
   40	    title_text/2,		% +Title, -Text:atom
   41	    safe_file_name/1,		% +Name
   42	    safe_dom/2			% +DOM0, -DOM
   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
   67%%	wiki_file_to_dom(+File, +DOM) is det.
   68%
   69%	DOM is the HTML dom representation for the content of File.
   70
   71wiki_file_to_dom(File, DOM) :-
   72	read_file_to_codes(File, String, []),
   73	wiki_file_codes_to_dom(String, File, DOM).
   74
   75%%	wiki_codes_to_dom(+Codes, +File, -DOM)
   76%
   77%	DOM is the HTML dom representation for Codes that originate from
   78%	File.
   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
   93%%	safe_dom(+DOM0, -DOM) is det.
   94%
   95%	Make a DOM from wiki text safe for rendering.  PlDoc wiki text
   96%	escapes HTML, but turns e.g. `<javascript:...>` into a link.
   97%	This predicate replaces URL attributes that use a scheme other
   98%	than `http`, `https`, `ftp` or `mailto` by `#`.  Relative URLs
   99%	are kept.  Embedded \Goal terms are not touched.
  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
  148%	safe_url(+URL) is semidet.
  149%
  150%	True if URL is relative or uses a safe scheme.  Browsers ignore
  151%	white space and control characters in the scheme, so we remove
  152%	these before checking.
  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			% no scheme: relative URL
  167	).
  168
  169safe_scheme(http).
  170safe_scheme(https).
  171safe_scheme(ftp).
  172safe_scheme(mailto).
  173
  174
  175		 /*******************************
  176		 *	     RENDERING		*
  177		 *******************************/
  178
  179%%	include(+Object, +Type, +Options)//
  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
  196%%	safe_include_file(+File, -Path) is semidet.
  197%
  198%	Path is the absolute path for the wiki file File included using
  199%	[[File]].  File is resolved relative to the file being rendered
  200%	and must be inside the document root.  PlDoc itself resolves it
  201%	relative to the working directory, which allowed user content
  202%	such as comments to include arbitrary .txt and .md files.
  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
  223%%	file(+Path, Options)//
  224%
  225%	Trap translation of \file(+Path, Options)
  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		 /*******************************
  271		 *     OBJECT INTEGRATION	*
  272		 *******************************/
  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,	% Location, Title, Time
  286	wiki_pages_indexed/1.  287
  288%%	wiki_page_title(?Location, ?Title) is nondet.
  289%
  290%	True when Title is the title of the wiki page at Location.
  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
  315%%	update_wiki_page_title(Location) is det.
  316%
  317%	Update the cached information about a wiki file.
  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
  334extract_wiki_page_title(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
  349%%	dom_title(+DOM, -Title) is semidet.
  350%
  351%	Get the title as an atom from a parsed wiki page.
  352%
  353%	@tbd	Currently assumes no markup in the title.
  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:doc_file_index_header(wiki, _) --> [].
  375
  376%%	index_wiki_pages
  377%
  378%	Create a (title) index of  the   available  wiki  pages. This is
  379%	started from server/1 in a background thread.
  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
  396%%	wiki_locations(-Locations) is det.
  397%
  398%	True when Files is a list of all .txt files on the site.
  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
  439%!	extract_title(+DOM0, -Title, -DOM) is det.
  440%
  441%	Extract the title from a wiki page.  The title is considered
  442%	to be the first h<N> element.
  443
  444extract_title([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
  453%!	title_text(+Title, -Text:atom) is det.
  454%
  455%	Turn the title, represented as  an   argument  to html//1 into a
  456%	plain string. Turns it  into  HTML,   then  parses  the HTML and
  457%	finally extracts the string. First clause   avoids  this for the
  458%	common normal case.
  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
  472%!	safe_file_name(+Name)
  473%
  474%	True  when  Name  is  a  file    without  references  to  parent
  475%	directories.
  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, '/..')