Source: site.view [edit]
Function name: programming
Arguments:
Description: Help on programming WubHub
Page type: snippet
Render function:  
Module: site

Page source:

<h1>Programming Collaborama</h1>

<p>
This page describes how to program new functions in Collaborama.
Read the introductory
<a href="/webl/WubHub_DoIt?cmdline=site.help">help</a>
page first.
</p>

<p> To develop with Collaborama, you simply create new or edit existing
Collaborama functions.  Before editing pages, you must
<a href="/webl/WubHub_DoIt?cmdline=site.signup">signup for an account</a> and
<a href="/webl/WubHub_DoIt?cmdline=site.login">log in</a>.</p>

<h2>Collaborama Modules</h2>

When creating a new command, Collaborama will ask which Module you want to save it in.  Every user has their own "space", where they can save their own personal commands and customizations -- this module is the default when creating a new command.  Other modules include "global", which is in the default path of all Collaborama members, and "sandbox", where works-in-progress are usually kept.
<p>
When you type a function into the command line, your module path will be searched in order, first looking in your personal space, then at system modules, then at the global module, finally in the sandbox.  However, you can indicate which module to use by qualifying the function with the module name followed by a ".".  For example, you can edit the default Collaborama homepage and save your customization into your personal module.  When you access home, you will now see your customizations.  If you'd like to access the default homepage, you can reference 
"<a href=/webl/WubHub_DoIt?cmdline=site.home>site.home</a>". 
<p>
To see which modules you have readable and writable access to, and in what order modules will be searched when resolving commands, use the command "<a href=/webl/WubHub_DoIt?cmdline=permissions>permissions</a>".
</p>

<h2>Function Types</h2>

<p>Functions come in several types:</p>

<ul>

  <li> <i>Alias</i>: This is used when you want to call an existing command by a different name, or make a private "link" to an existing function.  Create your newly named command, make it of type alias, and in the 'page content' field, call the existing function, passing in variable surrounded by the % symbol. (Example: <a href="/webl/WubHub_DoIt?cmdline=view(dist)">dist</a>.) Note: if the target command has a render function, you should also specify this render function in the aliased version.</li>

  <li> <i>URL</i>: Embed variables into a URL, and return the
  specified page.  This is a quick way to do a Web search, for
  instance. (URL example: <a href="/webl/WubHub_DoIt?cmdline=view(gim)">gim</a>.)</li>

  <li> <i>WebL</i>: A WebL script that calculates some result, perhaps
  based on certain input parameters.  This can be used to scrape
  content from other sites.
  (WebL example: <a href="/webl/WubHub_DoIt?cmdline=view(describe)">describe</a>.)</li>

  </li><li> <i>HTML snippet</i>: Not a fully executable command, snippets
  define pieces of HTML that may be displayed in the browser.  They
  also support dynamic content generated using WebL.  Place any WebL
  scripts in the page surrounded by "&lt;webl&gt;" and
  "&lt;/webl&gt;".  (Snippet example: the "help" page,
   <a href="/webl/WubHub_DoIt?cmdline=view(site.help)">help</a>.)  </li>

  <li> <i>HTML page</i>: Similar to a snippet, but indicates that the result
  is a full page, to be displayed all by itself.  Unlike a snippet,
  the result is displayed completely on its own, not within the Collaborama
  display enviroment.</li>

  <li> <i>Text/data</i>: A way to store fully static content that
  is not in HTML or WebL form.</li>
</ul>


<h2>Optional Arguments</h2>

<p>Collaborama functions declare their (mandatory) arguments in the Arguments field of the edit form; if you call a function with less arguments than it declares, you will get an error message.  However, extra arguments passed to a function are bundled into an array called "OPTIONALARGS".  When writing a function, you can check the size(OPTIONALARGS) and access arguments.  Arguments are currently order-based, so you must presently document their existance in the "Description" field of the edit form.
</p>
<p> If additional arguments are sent (in addition to the cmdline argument), you can access them in the EXTRAARGS variable name.
</p>

<h2>WebL Scripting Language</h2>

<p> When writing WebL commands, you have access to the WebL language,
minus a few functions for security reasons ("exec", "exit", "import", etc.)  Instead of using WebL's <tt>Eval()</tt> function, use the provided <tt>WubEval()</tt> function instead.
Modules <tt>Str</tt> and <tt>Url</tt> have been imported and are
available for use.  In addition, a number of Collaborama
utility functions are available in module called <tt>Wub</tt>;
see the "<a href=/webl/WubHub_DoIt?cmdline=site.internalapi>internalapi</a>"
page for details on this internal API. </p>


<h2>Calling Other Functions</h2>

From inside a WebL block of code, you can call another Collaborama function using the <tt>WubCall(CmdName, ArgList)</tt> function:
<pre>
   var dist = WubCall("distance", ["Oakland, CA", "San Francisco, CA"]);
</pre>

From inside of HTML, a link can dispatch to another Collaborama function by using 
<tt>
  &lt;a href="/webl/WubHub_DoIt?cmdline=yourFun(arg1,arg2)"&gt;Link Text&lt;/a&gt;
</tt>.  Finally, you can create a form that passes arguments to a Collaborama function by specifying a hidden 'cmdline' field within the form.  The arguments specified in this field should refer to field names within the form:
<pre>
&lt;form action="/webl/WubHub_DoIt" method="post"&gt;
   &lt;input name="cmdline" value="distance(arg1, arg2)" type="hidden"/&gt;
   From: &lt;input name="arg1" value="" size="45" title="From address"/&gt;
   To:   &lt;input name="arg2" value="" size="45" title="To address"/&gt;
         &lt;input type="submit" value="Calculate Distance"&gt;
&lt;/form&gt;
</pre>

<h2>Tags and Flags</h2>  When defining a function, you may associate "tags" and "flags" with the function.  Tags are user-defined labels that categorize a function as belonging to a particular group or having a particular feature.  Flags are "special" tags that are referenced explicitly by other Collaborama functions, therefore having a functional "use".  For instance, the "test" flag is used to indicate that a function should be included in Unit Test runs, and the "render" flag marks render functions.
<p>
User-defined tags (entered in the "Tags:" input box for a function) will be stored internally in the form "tag.YourTag"), and user-specified flags (entered in the "Flags:" input box) are stored in the form "flag.YourFlag".  In addition, Collaborama automatically tags functions with system-level flags to enable easy searching for functions.  Examples include "type.FunctionType", where FunctionType is one of (alias, url, webl, snippet, text), "system.cached", and "createdby.UserName".

<h2>Return Values</h2>

<p>It is helpful to realize that any page has a WebL
return value.  Text/data pages are strings, HTML pages are "page"
types, and HTML snippets are "pieces".  The return value of a
WebL page depends on the code within that page.  A WebL page
can call other Collaborama pages, of any type, using the
<tt>WubCall(CmdName, ArgList)</tt> function, and combine its results to
give a new answer.  The type of a return value also affects
how Collaborama displays the final answer to the user: strings
and HTML snippets are displayed within the Collaborama environment,
but HTML pages take up the whole browser window.</p>

<h2>Caching page results</h2>

Most Collaborama scripts return results quickly, but some commands that query many web sources (e.g. cultureBonk(sf))or combine many commands (e.g., runAllUnitTests) may take a while to run. To improve performance, we have implemented caching of command results. Here's how it works: 
<UL>
  <LI> Commands with checkbox "Cache results" will persist their results for each unique input they receive. If executed again with arguments it has seen before, the saved result is returned immediately rather than running the command. This caching is GLOBAL across all users, so use with care. 
  <LI> If you fill out an "Expiration Date", cached results will expire. 
You may request that the system automatically refresh values at a certain rate (e.g. 1D = every day, 1H = every hour, or combinations such as "3W 2H"). The refresh threads run as as the "anonymous" user, so this will only work for functions accessible when you are logged out (e.g. in module "global"). 
  <LI> To clear the cache for a function, you can execute "clearCache(funcName)" 
</ul>

<h2>Session Workspace</h2>
<p>For logged in users, there is also available a "session workspace",
called <tt>wubvars</tt>, that functions can use to store return values
that may be used by other commands.
It is simply a globally accessible WebL variable that may be written
to.  For instance, a function could assign <tt>wubvars.answer := 42</tt>,
and a Collaborama function called later could check the value of
<tt>wubvars.answer</tt>.  Each user has their own wubvars session (wubvars
is not shared across users).
The <a href="/webl/WubHub_DoIt?cmdline=wubvars">wubvars</a>
command can be used to inspect this variable.  Users begin with
an empty object <tt>[. .]</tt> for <tt>wubvars</tt>.
Note this feature is <i>not</i> currently available to anonymous users;
for them, <tt>wubvars</tt> is nil.  It may be advisable for
functions to check a user is logged in before using it.
</p>

<h2>WubInfo Variable</h2>
<p>Occasionally, WebL functions may wish to obtain information
about the request they are handling.  The <tt>wubinfo</tt> global
varaible is an object holding many useful fields:
<ul>
   <li> <tt>wubinfo.cmdline</tt>: Full command line executed by user (string).</li>
   <li> <tt>wubinfo.cmd</tt>: Name of function executed by user (string).</li>
   <li> <tt>wubinfo.args</tt>: Arguments passed to function executed by user (list).</li>
   <li> <tt>wubinfo.user</tt>: Username of current user; nil if user not logged in (string).</li>
   <li> <tt>wubinfo.fullname</tt>: Name of current user; nil if user not logged in (string).</li>
   <li> <tt>wubinfo.wubversion</tt>: Version of Collaborama system (string).</li>
   <li> <tt>wubinfo.weblversion</tt>: Version of WebL used by Collaborama.</li>
</ul>
The <a href="/webl/WubHub_DoIt?cmdline=wubinfo">wubinfo</a>
command can be used to inspect this variable.
</p>

<h2>Advanced Programming</h2>
<p>See the <a href="/webl/WubHub_DoIt?cmdline=site.advancedProgramming">advancedProgramming</a> page to see topics such as
working with Tries, with Lucene (search and local search), and MongoDB (databases).

<h2>More Information</h2>
<ul>
   <li><a href="WubHub_DoIt?cmdline=internalapi">Collaborama Internal API (cmd=internalapi)</a>)</li>
   <li><a target="_blank" href="http://www.hpl.hp.com/downloads/crl/webl/library.html">WebL Homepage</a></li>
   <li><a target="_blank" href="http://www.hpl.hp.com/techreports/Compaq-DEC/SRC-TN-1997-029.pdf">WebL Overview Paper</a></li>
   <li><a target="_blank" href="http://www.hpl.hp.com/downloads/crl/webl/webl.pdf">WebL Documentation</a> <a target="_blank" href="http://www.hpl.hp.com/downloads/crl/webl/htmldocu/WebL.html">[HTML]</a> <a target="_blank" href="http://www.wubhub.com:4110/static/webl.pdf">[PDF]</a></li>
</ul>