Use ### for 3rd-level headings.
[mkws-moved-to-github.git] / doc / whitepaper.markdown
1 % Embedded metasearching with the MasterKey Widget Set
2 % Mike Taylor
3
4
5 Introduction
6 ------------
7
8 There are lots of practical problems in building resource discovery
9 solutions. One of the biggest, and most ubiquitous is incorporating
10 metasearching functionality into existing web-sites -- for example,
11 content-management systems, library catalogues or intranets. In
12 general, even when access to core metasearching functionality is
13 provided by simple web-services such as
14 [Pazpar2](http://www.indexdata.com/pazpar2), integration work is seen
15 as a major part of most projects.
16
17 Index Data provides several different toolkits for communicating with
18 its metasearching middleware, trading off varying degrees of
19 flexibility against convenience:
20
21 * pz2.js -- a low-level JavaScript library for interrogating the
22   Service Proxy and Pazpar2. It allows the HTML/JavaScript programmer
23   to create JavaScript applications display facets, records, etc. that
24   are fetched from the metasearching middleware.
25
26 * masterkey-ui-core -- a higher-level, complex JavaScript library that
27   uses pz2.js to provide the pieces needed for building a
28   full-featured JavaScript application.
29
30 * MasterKey Demo UI -- an example of a searching application built on
31   top of masterkey-ui-core. Available as a public demo at
32   http://mk2.indexdata.com/
33
34 * MKDru -- a toolkit for embedding MasterKey-like searching into
35   Drupal sites.
36
37 All of these approaches require programming to a greater or lesser
38 extent. Against this backdrop, we introduced MKWS (the MasterKey
39 Widget Set) -- a set of simple, very high-level HTML+CSS+JavaScript
40 components that can be incorporated into any web-site to provide
41 MasterKey searching facilities. By placing `<div>`s with well-known
42 identifiers in any HTML page, the various components of an application
43 can be embedded: search-boxes, results areas, target information, etc.
44
45
46 Simple Example
47 --------------
48
49 The following is a complete MKWS-based searching application:
50
51     <html>
52       <head>
53         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
54         <title>MKWS demo client</title>
55         <script type="text/javascript" src="http://mkws.indexdata.com/mkws-complete.js"></script>
56         <link rel="stylesheet" href="http://mkws.indexdata.com/mkws.css" />
57       </head>
58       <body>
59         <div id="mkwsSearch"></div>
60         <div id="mkwsResults"></div>
61       </body>
62     </html>
63
64 Go ahead, try it! You don't even need a web-server. Just copy and
65 paste this HTML into a file on your computer -- `/tmp/magic.html`,
66 say -- and point your web-browser at it:
67 `file:///tmp/magic.html`. Just like that, you have working
68 metasearching.
69
70
71 How the example works
72 ---------------------
73
74 If you know any HTML, the structure of the file will be familar to
75 you: the `<html>` element at the top level contains a `<head>` and a
76 `<body>`. In addition to whatever else you might want to put on your
77 page, you can add MKWS elements.
78
79 These fall into two categories. First, the prerequisites in the HTML
80 header, which are loaded from the tool site mkws.indexdata.com:
81
82 * `mkws-complete.js`
83   contains all the JavaScript needed by the widget-set.
84
85 * `mkws.css`
86   provides the default CSS styling
87
88 Second, within the HTML body, `<div>` elements with special IDs that
89 begin `mkws` can be provided. These are filled in by the MKWS code,
90 and provide the components of the searching UI. The very simple
91 application above has only two such components: a search box and a
92 results area. But more are supported. The main `<div>`s are:
93
94 * `mkwsSearch` -- provides the search box and button.
95
96 * `mkwsResults` -- provides the results area, including a list of
97    brief records (which open out into full versions when clicked),
98    paging for large results sets, facets for refining a search,
99    sorting facilities, etc.
100
101 * `mkwsLang` -- provides links to switch between one of several
102    different UI languages. By default, English, Danish and German are
103    provided.
104
105 * `mkwsSwitch` -- provides links to switch between a view of the
106    result records and of the targets that provide them. Only
107    meaningful when `mkwsTargets` is also provided.
108
109 * `mkwsTargets` -- the area where per-target information will appear
110    when selected by the link in the `mkwsSwitch` area. Of interest
111    mostly for fault diagnosis rather than for end-users.
112
113 * `mkwsStat` --provides a status line summarising the statistics of
114    the various targets.
115
116 To see all of these working together, just put them all into the HTML
117 `<body>` like so:
118
119         <div id="mkwsSwitch"></div>
120         <div id="mkwsLang"></div>
121         <div id="mkwsSearch"></div>
122         <div id="mkwsResults"></div>
123         <div id="mkwsTargets"></div>
124         <div id="mkwsStat"></div>
125
126 Configuration
127 -------------
128
129 Many aspects of the behaviour of MKWS can be modified by setting
130 parameters into the `mkws_config` object. **This must be done *before*
131 including the MKWS JavaScript** so that when that code is executed it
132 can refer to the configuration values. So the HTML header looks like
133 this:
134
135         <script type="text/javascript">
136           var mkws_config = {
137             lang: "da",
138             sort_default: "title",
139             query_width: 60
140           };
141         </script>
142         <script type="text/javascript" src="http://mkws.indexdata.com/mkws-complete.js"></script>
143
144 This configuration sets the UI language to Danish (rather than the
145 default of English), initially sorts search results by title rather
146 than relevance (though as always this can be changed in the UI) and
147 makes the search box a bit wider than the default.
148
149 The full set of supported configuration items is described in the
150 reference guide below.
151
152
153 Control over HTML and CSS
154 -------------------------
155
156 More sophisticated applications will not simply place the `<div>`s
157 together, but position them carefully within an existing page
158 framework -- such as a Drupal template, an OPAC or a SharePoint page.
159
160 While it's convenient for simple applications to use a monolithic
161 `mkwsResults` area which contains record, facets, sorting options,
162 etc., customised layouts may wish to treat each of these components
163 separately. In this case, `mkwsResults` can be omitted, and the
164 following lower-level components provided instead:
165
166 * `mkwsTermlists` -- provides the facets
167
168 * `mkwsRanking` -- provides the options for how records are sorted and
169    how many are included on each page of results.
170
171 * `mkwsPager` -- provides the links for navigating back and forth
172    through the pages of records.
173
174 * `mkwsNavi` -- when a search result has been narrowed by one or more
175    facets, this area shows the names of those facets, and allows the
176    selected values to be clicked in order to remove them.
177
178 * `mkwsRecords` -- lists the actual result records.
179
180 Customisation of MKWS searching widgets can also be achieved by
181 overriding the styles set in the toolkit's CSS stylesheet. The default
182 styles can be inspected in `mkws.css` and overridden in any
183 styles that appears later in the HTML than that file. At the simplest
184 level, this might just mean changing fonts, sizes and colours, but
185 more fundamental changes are also possible.
186
187 To properly apply styles, it's necessary to understand how the HTML is
188 structured, e.g. which elements are nested within which
189 containers. The structures used by the widget-set are described in the
190 reference guide below.
191
192
193 Refinements
194 -----------
195
196
197 ### Message of the day
198
199 Some applications might like to open with content in the area that
200 will subsequently be filled with result-records -- a message of the
201 day, a welcome message or a help page. This can be done by placing an
202 `mkwsMOTD` division anywhere on the page. It will be moved into the
203 `mkwsResults` area and initially displayed, but will be hidden when a
204 search is made.
205
206
207 ### Customised display using Handlebars templates
208
209 Certain aspects of the widget-set's display can be customised by
210 providing Handlebars templates with well-known classes that begin with
211 the string `mkwsTemplate_`. At present, the supported templates are:
212
213 * `mkwsTemplate_Summary` -- used for each summary record in a list of
214   results.
215
216 * `mkwsTemplate_Record` -- used when displaying a full record.
217
218 For both of these the metadata record is passed in, and its fields can
219 be referenced in the template. As well as the metadata fields
220 (`md-*`), two special fields are provided to the `mkwsTemplate_Summary`
221 template, for creating popup links for full records. These are `_id`,
222 which must be provided as the `id` attribute of a link tag, and
223 `_onclick`, which must be provided as the `onclick` attribute.
224
225 For example, an application can install a simple author+title summary
226 record in place of the usual one providing the following template:
227
228         <script class="mkwsTemplate_Summary" type="text/x-handlebars-template">
229           {{#if md-author}}
230             <span>{{md-author}}</span>
231           {{/if}}
232           <a href="#" id="{{_id}}" onclick="{{_onclick}}">
233             <b>{{md-title}}</b>
234           </a>
235         </script>
236
237 For details of Handlebars template syntax, see
238 [the online documentation](http://handlebarsjs.com/).
239
240
241 ### Responsive design
242
243 Metasearching applications may need to appear differently on
244 small-screened mobile devices, or change their appearance when
245 screen-width changes (as when a small device is rotated). To achieve
246 this, MKWS supports responsive design which will move the termlists to
247 the bottom on narrow screens and to the sidebar on wide screens.
248
249 To turn on this behaviour, set the `responsive_design_width` to the desired
250 threshhold width in pixels. For example:
251
252         <script type="text/javascript">
253             var mkws_config = {
254                 responsive_design_width: 990
255             };
256         </script>
257
258 If individual result-related components are in use in place of the
259 all-in-one mkwsResults, then the redesigned application needs to
260 specify the locations where the termlists should appear in both
261 cases. In this case, wrap the wide-screen `mkwsTermlists` element in a
262 `mkwsTermlists-Container-wide` element; and provide an
263 `mkwsTermlists-Container-narrow` element in the place where the narrow-screen
264 termlists should appear.
265
266
267 ### Popup results with jQuery UI
268
269 The [jQuery UI library](http://en.wikipedia.org/wiki/JQuery_UI)
270 can be used to construct MKWS applications in which the only component
271 generally visible on the page is a search box, and the results appear
272 in a popup. The key part of such an application is this invocation of
273 the MKWS jQuery plugin:
274
275         <div class="mkwsSearch"></div>
276         <div class="mkwsPopup" popup_width="1024" popup_height="650" popup_modal="0" popup_autoOpen="0" popup_button="input.mkwsButton">
277           <div class="mkwsSwitch"></div>
278           <div class="mkwsLang"></div>
279           <div class="mkwsResults"></div>
280           <div class="mkwsTargets"></div>
281           <div class="mkwsStat"></div>
282         </div>
283
284 The necessary scaffolding can be seen in an example application,
285 http://example.indexdata.com/index-popup.html
286
287
288 ### Authentication and target configuration
289
290 By default, MKWS configures itself to use a demonstration account on a
291 service hosted by mkws.indexdata.com. This account (username `demo`,
292 password `demo`) provides access to about a dozen free data
293 sources. Authentication onto this service is via an authentication URL
294 on the same MKWS server, so no explicit configuration is needed.
295
296 In order to search in a customised set of targets, including
297 subscription resources, it's necessary to create an account with
298 Index Data's hosted service proxy, and protect that account with
299 authentication tokens (to prevent unauthorised use of subscription
300 resources). But in order to gain access to those resources, the
301 authentication tokens have to be available to the widgets in some way,
302 and simple embedding them in the JavaScript configuration is not
303 acceptable because they are easy to read from there.
304
305 The solution to this problem is in three steps.
306
307 <b>First</b>
308 the application's web-server creates a rewriting rule that takes an
309 innocuous URL like
310 http://example.indexdata.com/service-proxy-auth/
311 and rewrites it as an access to Index Data's authentication service
312 with authentication credentials embedded. This can be done using
313 Apache2 directives such as
314
315     RewriteEngine on
316     RewriteRule /service-proxy-auth/
317         http://mkws.indexdata.com/service-proxy/?command=auth&action=login&username=U&password=PW [P]
318
319 Because the credentials appear only in the application's web-server
320 configuration, they are not visible to malicious users.
321
322 <b>Second</b>, the broader application that includes MKWS widgets must
323 protect access to the authentication URL on its own web-server. This
324 can be done using IP authentication, a local username/password scheme,
325 Kerberos or any other means.
326
327 <b>Third</b>, the MKWS application must be configured to use the
328 application-hosted authentication URL instead of the default one. This
329 is done by means of the `service_proxy_auth` configuration element,
330 which should be set to the authentication URL.
331
332 Once these three steps are taken, the MKWS application will
333 authenticate by means of a special URL on the application's web
334 server, which the application prevents unauthorised access to, and the
335 underlying credentials are hidden.
336
337
338 Reference Guide
339 ---------------
340
341 ### Configuration object
342
343 The configuration object `mkws_config` may be created before including
344 the MKWS JavaScript code to modify default behaviour. This structure
345 is a key-value lookup table, whose entries are described in the table
346 below. All entries are optional, but if specified must be given values
347 of the specified type. If ommitted, each setting takes the indicated
348 default value; long default values are in footnotes to keep the table
349 reasonably narrow.
350
351 ---
352 Element                   Type    Default   Description
353 --------                  -----   --------- ------------
354 debug_level               int     1         Level of debugging output to emit. 0 = none, 1 = messages, 2 = messages with
355                                             datestamps, 3 = messages with datestamps and stack-traces.
356
357 facets                    array   *Note 1*  Ordered list of names of facets to display. Supported facet names are
358                                             `xtargets`, `subject` and `author`.
359
360 lang                      string  en        Code of the default language to display the UI in. Supported language codes are `en` =
361                                             English, `de` = German, `da` = Danish, and whatever additional languages are configured
362                                             using `language_*` entries (see below).
363
364 lang_options              array   []        A list of the languages to offer as options. If empty (the default), then all
365                                             configured languages are listed.
366
367 language_*                hash              Support for any number of languages can be added by providing entries whose name is
368                                             `language_` followed by the code of the language. See the separate section below for
369                                             details.
370
371 pazpar2_url               string  *Note 2*  The URL used to access the metasearch middleware. This service must be configured to
372                                             provide search results, facets, etc. It may be either unmediated or Pazpar2 the
373                                             MasterKey Service Proxy, which mediates access to an underlying Pazpar2 instance. In
374                                             the latter case, `service_proxy_auth` must be provided.
375
376 perpage_default           string  20        The initial value for the number of records to show on each page.
377
378 perpage_options           array   *Note 3*  A list of candidate page sizes. Users can choose between these to determine how many
379                                             records are displayed on each page of results.
380
381 query_width               int     50        The width of the query box, in characters.
382
383 responsive_design_width   int               If defined, then the facets display moves between two locations as the screen-width
384                                             varies, as described above. The specified number is the threshhold width, in pixels,
385                                             at which the facets move between their two locations.
386
387 service_proxy_auth        url     *Note 4*  A URL which, when `use_service_proxy` is true, is fetched once at the beginning of each
388                                             session to authenticate the user and establish a session that encompasses a defined set
389                                             of targets to search in.
390
391 service_proxy_auth_domain domain            Can be set to the domain for which `service_proxy_auth` proxies authentication, so
392                                             that cookies are rewritten to appear to be from this domain. In general, this is not
393                                             necessary, as this setting defaults to the domain of `pazpar2_url`.
394
395 show_lang                 bool    true      Indicates whether or not to display the language menu.
396
397 show_perpage              bool    true      Indicates whether or not to display the perpage menu.
398
399 show_sort                 bool    true      Indicates whether or not to display the sort menu.
400
401 show_switch               bool    true      Indicates whether or not to display the switch menu, for switching between showing
402                                             retrieved records and target information.
403
404 sort_default              string  relevance The label of the default sort criterion to use. Must be one of those in the `sort`
405                                             array.
406
407 sort_options              array   *Note 6*  List of supported sort criteria. Each element of the list is itself a two-element list:
408                                             the first element of each sublist is a pazpar2 sort-expression such as `data:0` and
409                                             the second is a human-readable label such as `newest`.
410
411 use_service_proxy         bool    true      If true, then a Service Proxy is used to deliver searching services rather than raw
412                                             Pazpar2.
413 ---
414
415 Perhaps we should get rid of the `show_lang`, `show_perpage`,
416 `show_sort` and `show_switch` configuration items, and simply display the relevant menus
417 only when their containers are provided -- e.g. an `mkwsLang` element
418 for the language menu. But for now we retain these, as an easier route
419 to lightly customise the display than my changing providing a full HTML
420 structure.
421
422 #### Notes
423
424 1. ["sources", "subjects", "authors"]
425
426 2. /pazpar2/search.pz2
427
428 3. [10, 20, 30, 50]
429
430 4. http://mkws.indexdata.com/service-proxy-auth
431
432 5. http://mkws.indexdata.com/service-proxy/
433
434 6. [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]]
435
436
437 ### Language specification
438
439 Support for another UI language can be added by providing an entry in
440 the `mkws_config` object whose name is `language_` followed by the
441 name of the language: for example, `language_French` to support
442 French. Then value of this entry must be a key-value lookup table,
443 mapping the English-language strings of the UI into their equivalents
444 in the specified language. For example:
445
446             var mkws_config = {
447               language_French: {
448                 "Authors": "Auteurs",
449                 "Subjects": "Sujets",
450                 // ... and others ...
451               }
452             }
453
454 The following strings occurring in the UI can be translated:
455 `Displaying`,
456 `Next`,
457 `Prev`,
458 `Records`,
459 `Search`,
460 `Sort by`,
461 `Targets`,
462 `Termlists`,
463 `and show`,
464 `found`,
465 `of`,
466 `per page`
467 and
468 `to`.
469
470 In addition, facet names can be translated:
471 `Authors`,
472 `Sources`
473 and
474 `Subjects`.
475
476 Finally, the names of fields in the full-record display can be
477 translated. These include, but may not be limited to:
478 `Author`,
479 `Date`,
480 `Location`,
481 `Subject`
482 and
483 `Title`.
484
485
486
487 ### jQuery UI popup invocation
488
489 The MasterKey Widget Set can be invoked in a popup window on top of the page.
490
491 Note that when using the `popup` layout, facilities from the jQuery UI
492 toolkit are used, so it's necessary to include both CSS and JavaScript
493 from that toolkit. The relevant lines are:
494
495     <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
496     <link rel="stylesheet" type="text/css"
497           href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
498
499     <div class="mkwsSearch"></div>
500     <div class="mkwsPopup" popup_width="1024" popup_height="650" popup_modal="0" popup_autoOpen="0" popup_button="input.mkwsButton">
501       <div class="mkwsSwitch"></div>
502       <div class="mkwsLang"></div>
503       <div class="mkwsResults"></div>
504       <div class="mkwsTargets"></div>
505       <div class="mkwsStat"></div>
506     </div
507
508 ---
509 Element    Type    Default           Description
510 --------   -----   ---------         ------------
511 popup_width     string     880       Width of the popup window (if used), in
512                                      pixels.
513
514 popup_height    string     760       Height of the popup window (if used), in
515                                      pixels.
516
517 popup_button    string      input.mkwsButton  (Never change this.)
518
519 popup_modal     string      0       Modal confirmation mode. Valid values are 0 or 1
520
521 popup_autoOpen  string      1       Open popup window on load. Valid values are 0 or 1
522
523 ---
524
525
526 ### The structure of the HTML generated by the MKWS widgets
527
528 In order to override the default CSS styles provided by the MasterKey Widget
529 Set, it's necessary to understand that structure of the HTML elements that are
530 generated within the components. This knowledge make it possible, for example,
531 to style each `<div>` with class `term` but only when it occurs inside an
532 element with ID `#mkwsTermlists`, so as to avoid inadvertently styling other
533 elements using the same class in the non-MKWS parts of the page.
534
535 The HTML structure is as follows. As in CSS, #ID indicates a unique identifier
536 and .CLASS indicates an instance of a class.
537
538     #mkwsSwitch
539       a*
540
541     #mkwsLang
542       ( a | span )*
543
544     #mkwsSearch
545       form
546         input#mkwsQuery type=text
547         input#mkwsButton type=submit
548
549     #mkwsBlanket
550       (no contents -- used only for masking)
551
552     #mkwsResults
553       table
554         tbody
555           tr
556             td
557               #mkwsTermlists
558                 div.title
559                 div.facet*
560                   div.termtitle
561                   ( a span br )*
562             td
563               div#mkwsRanking
564                 form#mkwsSelect
565                   select#mkwsSort
566                   select#mkwsPerpage
567               #mkwsPager
568               #mkwsNavi
569               #mkwsRecords
570                 div.record*
571                   span (for sequence number)
572                   a (for title)
573                   span (for other information such as author)
574                   div.details (sometimes)
575                     table
576                       tbody
577                         tr*
578                           th
579                           td
580     #mkwsTargets
581       #mkwsBytarget
582         table
583           thead
584             tr*
585               td*
586           tbody
587             tr*
588               td*
589
590     #mkwsStat
591       span.head
592       span.clients
593       span.records
594
595 - - -
596
597 Copyright (C) 2013-2014 by IndexData ApS, <http://www.indexdata.com>