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