View source with formatted comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2014-2018, VU University Amsterdam
    7			      CWI, Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(swish_app,
   37	  [
   38	  ]).   39:- use_module(library(pldoc), []).   40:- use_module(library(pengines)).   41:- use_module(library(http/http_dispatch)).   42:- use_module(library(option)).   43:- use_module(library(apply)).   44:- use_module(library(settings)).   45
   46:- use_module(lib/messages).   47:- use_module(lib/paths).   48:- use_module(lib/config, []).   49:- use_module(lib/page, []).   50:- use_module(lib/storage).   51:- use_module(lib/include).   52:- use_module(lib/swish_csv).   53:- use_module(lib/examples).   54:- use_module(lib/profiles).   55:- use_module(lib/highlight).   56:- use_module(lib/markdown).   57:- use_module(lib/chat, []).   58:- use_module(lib/template_hint, []).   59:- if(exists_source(library(http/http_dyn_workers))).   60:- use_module(library(http/http_dyn_workers)).   61:- else.   62:- use_module(lib/plugin/http_dyn_workers, []).   63:- endif.   64:- use_module(lib/web).   65:- use_module(lib/version).   66
   67
   68		 /*******************************
   69		 *	      VERSION		*
   70		 *******************************/
   71
   72setup_versions :-
   73	prolog_load_context(directory, Dir),
   74	register_git_module(swish,
   75			    [ directory(Dir),
   76			      home_url('https://github.com/SWI-Prolog/swish')
   77			    ]),
   78	check_prolog_version(070717).
   79
   80:- initialization setup_versions.   81
   82
   83		 /*******************************
   84		 *	       CORS		*
   85		 *******************************/
   86
   87% By default, enable CORS
   88
   89:- set_setting_default(http:cors, [*]).   90
   91
   92		 /*******************************
   93		 *         LOCAL CONFIG		*
   94		 *******************************/
   95
   96% create the application first, so we can modify it inside the
   97% configuration files.
   98:- pengine_application(swish).   99
  100%!	load_config
  101%
  102%	Load files from config-enabled if  present. Currently loads from
  103%	a single config-enabled directory, either  found locally or from
  104%	the swish directory.
  105
  106load_config :-
  107	absolute_file_name(config_enabled(.), Path,
  108			   [ file_type(directory),
  109			     access(read),
  110			     file_errors(fail)
  111			   ]), !,
  112	atom_concat(Path, '/*.pl', Pattern),
  113	expand_file_name(Pattern, Files),
  114	maplist(user:ensure_loaded, Files).
  115load_config.
  116
  117:- initialization(load_config, now).  118
  119
  120		 /*******************************
  121		 *	      CONFIG		*
  122		 *******************************/
  123
  124:- multifile
  125	swish_config:config/2,
  126	swish_config:source_alias/2.  127
  128%%	swish_config:config(?Config, ?Value) is nondet.
  129%
  130%	All solutions of this predicate are  available in the JavaScript
  131%	object config.swish.config. Config must be an  atom that is also
  132%	a valid JavaScript identifier. Value  must   be  a value that is
  133%	valid for json_write_dict/2.  Defined config parameters:
  134%
  135%	  - show_beware
  136%	  If `true`, show the *Beware* modal dialog on startup
  137%	  - tabled_results
  138%	  If `true`, check the _table results_ checkbox by default.
  139%	  - application
  140%	  Name of the Pengine application.
  141%	  - csv_formats
  142%	  CSV output formats offered. For example, ClioPatria
  143%	  defines this as [rdf,prolog]. The first element is default.
  144%	  - community_examples
  145%	  Allow marking saved programs as example.  If marked, the
  146%	  programs are added to the Examples menu.
  147%	  - public_access
  148%	  If lib/authenticate.pl is loaded and this flag is `true`,
  149%	  _all_ access to SWISH demands authentication.  If false,
  150%	  only running queries and saving files is restricted. Note
  151%	  that this flag has no effect if no authentication module is
  152%	  loaded.
  153%	  - include_alias
  154%	  Alias for searching files for `:- include(Alias(Name)).`
  155%	  - ping
  156%	  Ping pengine status every N seconds.  Updates sparkline
  157%	  chart with stack usage.
  158%	  - notebook
  159%	  Dict holding options for notebooks:
  160%	    - eval_script
  161%	    Whether or not to evaluate JavaScript in cells
  162%	    - fullscreen
  163%	    Whether or not to start in fullscreen mode by default
  164%	  - fullscreen
  165%	  Dict holding options for fullscreen mode:
  166%	    - hide_navbar: hide the navigation bar when in fullscreen
  167%	      mode.
  168%	  - chat
  169%	  Activate the chat interface
  170%	  - chat_spam_protection
  171%	  Perform protection against spamming on chat messages.
  172%	  - default_query
  173%	  Initial query for the source search in an empty tab
  174%
  175%	These config options are commonly  overruled   using  one of the
  176%	configuration files. See `config-available` and `config-enabled`
  177%	directories.
  178%
  179%	The  defaults  below   are   for    small   installations.   See
  180%	`config-available/dim_large.pl` for a default   config for large
  181%	communities.
  182
  183% Allow other code to overrule the defaults from this file.
  184term_expansion(swish_config:config(Config, _Value), []) :-
  185	clause(swish_config:config(Config, _), _).
  186
  187swish_config:config(show_beware,        false).
  188swish_config:config(tabled_results,     false).
  189swish_config:config(application,        swish).
  190swish_config:config(csv_formats,        [prolog]).
  191swish_config:config(community_examples, true).
  192swish_config:config(public_access,      false).
  193swish_config:config(include_alias,	example).
  194swish_config:config(ping,		2).
  195swish_config:config(notebook,		_{ eval_script: true,
  196					   fullscreen: false
  197					 }).
  198swish_config:config(fullscreen,		_{ hide_navbar: true
  199					 }).
  200swish_config:config(chat,		true).
  201swish_config:config(chat_spam_protection, true).
  202swish_config:config(default_query,	'').
  203
  204%%	swish_config:source_alias(Alias, Options) is nondet.
  205%
  206%	Specify access for files below a given _alias_. Options define
  207%
  208%	  - access(Access)
  209%	  One of `read` or `both`.  Default is `read`.
  210%	  - if(Condition)
  211%	  Provide additional conditions.  Defined conditions are:
  212%	    - loaded
  213%	    Only provide access to the file if it is loaded.
  214
  215
  216% setup HTTP session management
  217:- use_module(lib/session).  218
  219
  220                 /*******************************
  221                 *   CREATE SWISH APPLICATION   *
  222                 *******************************/
  223
  224:- multifile
  225	pengines:prepare_module/3.  226
  227:- use_module(swish:lib/render).  228:- use_module(swish:lib/trace).  229:- use_module(swish:lib/projection).  230:- use_module(swish:lib/attvar).  231:- use_module(swish:lib/jquery).  232:- use_module(swish:lib/dashboard).  233:- use_module(swish:lib/md_eval).  234:- use_module(swish:lib/html_output).  235:- use_module(swish:lib/swish_debug).  236:- use_module(swish:library(pengines_io)).  237:- use_module(swish:library(solution_sequences)).  238:- use_module(swish:library(aggregate)).  239:- if((\+current_predicate((table)/1),exists_source(library(tabling)))).  240:- use_module(swish:library(tabling)).  241:- endif.  242
  243pengines:prepare_module(Module, swish, _Options) :-
  244	pengines_io:pengine_bind_io_to_html(Module).
  245
  246% Additional sandboxing rules.
  247:- use_module(lib/flags).  248
  249% Libraries that are nice to have in SWISH, but cannot be loaded
  250% because they use directives that are considered unsafe.  We load
  251% them here, so they only need to be imported, which is just fine.
  252
  253:- use_module(library(clpfd), []).  254:- use_module(library(clpb), []).  255:- if(exists_source(library(dcg/high_order))).  256:- use_module(library(dcg/high_order), []).  257:- endif.  258:- use_module(lib/swish_chr, []).  259
  260% load rendering modules
  261
  262:- use_module(swish(lib/render/sudoku),	  []).  263:- use_module(swish(lib/render/chess),	  []).  264:- use_module(swish(lib/render/table),	  []).  265:- use_module(swish(lib/render/codes),	  []).  266:- use_module(swish(lib/render/svgtree),  []).  267:- use_module(swish(lib/render/graphviz), []).  268:- use_module(swish(lib/render/c3),	  []).  269:- use_module(swish(lib/render/url),	  []).  270:- use_module(swish(lib/render/bdd),	  []).  271:- use_module(swish(lib/render/mathjax),  []).  272:- use_module(swish(lib/render/gvterm),   []).