README.txt 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ------------------------------------------------------------------------------
  2. js module Readme
  3. http://drupal.org/project/js
  4. ------------------------------------------------------------------------------
  5. Contents:
  6. =========
  7. 1. ABOUT
  8. 2. TECHNICAL
  9. 3. INSTALLATION
  10. 4. MODULE INTEGRATION API
  11. 5. CREDITS
  12. 1. ABOUT
  13. ========
  14. JavaScript callback handler is an interim solution for high-performance server
  15. requests including (but not limited to) AHAH, AJAX, JSON, XML, etc.
  16. Note that this module does nothing on itself, only install it if another module
  17. leverages its functionality and instructs you so.
  18. By copying the included js.php front controller to Drupal's root directory,
  19. setting up clean URLs, and adding an Apache RewriteRule (see README for full
  20. installation instructions), any JavaScript code is able to retrieve information
  21. from Drupal at lightning speed.
  22. 2. PERFORMANCE
  23. ==============
  24. JavaScript callback handler is an interim solution for high-performance querying
  25. of contents including (but not limited to) AHAH, AJAX, JSON, XML, etc.
  26. Apache benchmarks speak for itself:
  27. Using index.php as usual:
  28. ab -n20 -c1 http://example.com/index.php?q=js/mymodule/callback
  29. Requests per second: 2.24 [#/sec] (mean)
  30. Time per request: 446.846 [ms] (mean)
  31. Using js.php:
  32. ab -n20 -c1 http://example.com/js.php?q=js/mymodule/callback
  33. Requests per second: 16.84 [#/sec] (mean)
  34. Time per request: 59.371 [ms] (mean)
  35. Note that this module does nothing on itself.
  36. For a full description visit the project page:
  37. http://drupal.org/project/js
  38. Bug reports, feature suggestions and latest developments:
  39. http://drupal.org/project/issues/js
  40. 3. TECHNICAL
  41. ============
  42. This module is not really one. Actually it is mainly a conditional replacement
  43. for Drupal's index.php based on Apache .htaccess directives.
  44. With mod_rewrite enabled ("clean urls"), it catches all calls to callback
  45. paths starting with "js/" and passes them to a reduced loader instead of
  46. the default index.php file. Invoking only the explicitely defined dependencies
  47. instead of a complete Drupal load, it saves lots of processing time and thus
  48. speeds up small Ajax requests.
  49. 4. INSTALLATION
  50. ===============
  51. * Install as usual, see http://drupal.org/node/70151 for further information.
  52. * Copy the js.php file from the module to the root directory
  53. of your Drupal installation (the place where your Drupal .htaccess and
  54. index.php is located).
  55. To enable clean URL support (optional):
  56. * Enable clean URLs in drupal at admin/settings/clean-urls
  57. For Apache:
  58. * Goto the configuration page: admin/config/development/performance/js.
  59. * Add the lines of .htaccess code from the configuration page in front of the
  60. existing RewriteRules in your .htaccess file.
  61. For nginx:
  62. * The JS module is supported out of the box via http://drupal.org/node/1876418
  63. Any other server:
  64. * Make sure any request starting with /js/ is forwarded to js.php.
  65. 5. MODULE INTEGRATION API
  66. =========================
  67. This module requires your server to point all paths starting with js/ to js.php
  68. rather than index.php. See above for how to do this in apache.
  69. To integrate your modules, they must point to specific callback paths:
  70. The 2nd argument after the mandatory initial js/ determines the implementing
  71. module, which must implement hook_js() for security reasons.
  72. Apart from security, modules may also specify additionally required includes
  73. files and module dependencies to load. Apart from security, modules may also
  74. specify another bootstrap level than the default DRUPAL_BOOTSTRAP_DATABASE,
  75. and additionally required includes files and module dependencies to load.
  76. As an example, we'll let example.module expose its function
  77. example_somefunction() to js.php. Its hook_js() implementation might look like
  78. this (see the api documentation for a full explanation):
  79. <code>
  80. function example_js() {
  81. return array(
  82. 'path' => array(
  83. 'callback' => 'example_somefunction',
  84. 'includes' => array('theme', 'unicode'),
  85. 'dependencies' => array('locale', 'filter', 'user'),
  86. 'bootstrap' => DRUPAL_BOOTSTRAP_CONSTANT,
  87. 'file' => 'includes/example.inc',
  88. 'access arguments' => array('e.g. permission'),
  89. 'access callback' => 'callback function',
  90. 'page arguments' => array(),
  91. 'skip_hook_init' => FALSE,
  92. ),
  93. );
  94. }
  95. </code>
  96. - Handling -
  97. The hook_js() implementation above makes JS accept the following URL:
  98. http://example.com/js/example/somefunction.
  99. ^ ^
  100. module name -| |
  101. info array key -|
  102. When called, it loads the requested include files and modules and calls the
  103. callback function.
  104. You can optionaly set the page arguments which are to be passed to the callback.
  105. It only support parts of the path (integer, starting with 0). If no page
  106. arguments are passed all the arguments that are left after the valid callback \
  107. are passed as arguments. IE: when you have a callback of 'example/dosomething'
  108. and the url for this is /js/example/example/dosomething/further/arguments the
  109. callback will receive 'further' and 'arguments' as the values for the first and
  110. second parameter.
  111. The path can be a complex path, containing simple wildcards (no auto loading
  112. support though) and slashes. It follows the same logic as the
  113. As stated above, js.php bootstraps Drupal to DRUPAL_BOOTSTRAP_DATABASE and
  114. includes the following inc files: bootstrap.inc, common.inc, locale.inc,
  115. module.inc, path.inc, unicode.inc, this means that url(), l(), and t() functions
  116. are available (t() lacks translation though, since that would require
  117. locale.module to be loaded. This can be easily "fixed" by adding locale to the
  118. dependencies array). Theme functions and potentially required functions of other
  119. modules, however, are not available by default, which is the main reason for the
  120. speed gain of js.php.
  121. - includes and dependencies -
  122. The js handler loads the least amount of code to function. This has the
  123. effect that not all modules are loaded that might be required to make your
  124. callback work. For example, when the overlay module is enabled the user module
  125. needs to be enabled because it uses the user_access method.
  126. This can be fixed using the option dependencies or includes. If your callback
  127. throws an error indicating it's missing a function, include the module or
  128. include file in these options and do this untill all errors are resolved.
  129. - Access checks -
  130. By default the js module doesn't peform any kind of access check.
  131. Limited access checks are supported by adding the access arguments and the
  132. access callback (optional) to the js callback.
  133. The access arguments are passed to the access callback. The access arguments
  134. implementation is very basic and does not support dynamic arguments. The
  135. variables here are passed through as provided. The access callback is the
  136. function that will be called to verify if the user has access to the callback.
  137. The access callback is optional and defaults to user_access.
  138. Note on performance:
  139. The js module automatically bootstraps to DRUPAL_BOOTSTRAP_SESSION
  140. when an access callback or access arguments are provided. This has a negative
  141. impact on the performance. If you don't need access checks, don't provide the
  142. options in the callback.
  143. - Internationalization -
  144. The JS module support internationalization. When using a path prefix it's
  145. automatically detected. When not using a path prefix you can set, for each
  146. callback, that it uses internationalization (using the i18n option).
  147. It does come at a cost though. Because it's required to boot to
  148. DRUPAL_BOOTSTRAP_LANGUAGE almost all of the bootstrap logic is executed again,
  149. severely reducing the effect of the JS module. The only gain that is left is
  150. that not all the modules are loaded. Be carefull when using this and only enable
  151. it when you must.
  152. - Output -
  153. js.php outputs the return value of the callback function using drupal_to_js().
  154. To use a custom output format, output the data on your own and exit()
  155. afterwards, or simply return nothing.
  156. - Fallback -
  157. Note that it is wise to also register a corresponding menu path in hook_menu()
  158. to provide fallback functionality when js.php is not available:
  159. <code>
  160. $items[] = array(
  161. 'path' => 'js/example/somefunction',
  162. 'callback' => 'example_somefunction',
  163. 'type' => MENU_CALLBACK,
  164. );
  165. </code>
  166. 6. NOTES
  167. ========
  168. Due to the nature of the way this module works not all modules that are active
  169. are included. Sometimes this may cause an error while processing a callback and
  170. sometimes this will result in a hook that is normally called, not beeing called
  171. in a js callback. Be carefull about which logic you implement in a js callback.
  172. If you encounter any problems with the default set of include files and modules
  173. please create an issue in the queue and we'll look at including it by default.
  174. 7. CREDITS
  175. ==========
  176. Project page: http://drupal.org/project/js
  177. - Drupal 7 -
  178. Authors:
  179. * David Herminghaus (doitDave) - http://drupal.org/user/833794
  180. * Michiel Nugter (michielnugter) - http://drupal.org/user/1023784
  181. The Drupal 7 update has been sponsored by SYNETIC
  182. Full service Drupal specialist. From custom made webapplications to content
  183. management systems, intranet and e-commerce shops. Visit http://www.synetic.nl
  184. for more information.
  185. - Drupal 6 -
  186. Authors:
  187. * Daniel F. Kudwien (sun) - http://drupal.org/user/54136
  188. * Stefan M. Kudwien (smk-ka) - http://drupal.org/user/48898
  189. The Drupal 6 version has been sponsored by UNLEASHED MIND
  190. Specialized in consulting and development of Drupal powered sites, our services
  191. include installation, development, theming, customization, and hosting to
  192. get you started.