tripal_core.tripal.api.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for Tripal
  5. *
  6. * The Tripal API currently provides generic insert/update/select functions for
  7. * all chado content as well as some module specific functions that
  8. * insert/update/delete/select specific chado content.
  9. *
  10. * This API is currently in its infancy and some necessary functions might be
  11. * missing. If you find a missing function that you think should be included
  12. * go to the sourceforge feature request page and request it's inclusion in
  13. * the API. Such feature requests with a working function definition will be
  14. * given priority.
  15. */
  16. /**
  17. * @defgroup tripal_api Tripal API
  18. * @{
  19. * Provides an application programming interface (API) for Tripal
  20. *
  21. * The Tripal API currently provides generic insert/update/select functions for
  22. * all chado content as well as some module specific functions that
  23. * insert/update/delete/select specific chado content.
  24. *
  25. * This API is currently in its infancy and some necessary functions might be
  26. * missing. If you find a missing function that you think should be included
  27. * go to the sourceforge feature request page and request it's inclusion in
  28. * the API. Such feature requests with a working function definition will be
  29. * given priority.
  30. * @}
  31. */
  32. // Globals used by Tripals Error catching functions
  33. // Should match those defined by watchdog
  34. define('TRIPAL_CRITICAL',2);
  35. define('TRIPAL_ERROR',3);
  36. define('TRIPAL_WARNING',4);
  37. define('TRIPAL_NOTICE',5);
  38. define('TRIPAL_INFO',6);
  39. define('TRIPAL_DEBUG',7);
  40. /**
  41. * Provide better error notice for Tripal. If the environment variable
  42. * 'TRIPAL_DEBUG' is set to 1 then this function will add backtrace
  43. * information to the message.
  44. *
  45. * @param $type
  46. * The catagory to which this message belongs. Can be any string, but the
  47. * general practice is to use the name of the module.
  48. * @param $severity
  49. * The severity of the message; one of the following values:
  50. * - TRIPAL_CRITICAL: Critical conditions.
  51. * - TRIPAL_ERROR: Error conditions.
  52. * - TRIPAL_WARNING: Warning conditions.
  53. * - TRIPAL_NOTICE: (default) Normal but significant conditions.
  54. * - TRIPAL_INFO: Informational messages.
  55. * - TRIPAL_DEBUG: Debug-level messages.
  56. * @param $message
  57. * The message to store in the log. Keep $message translatable by not
  58. * concatenating dynamic values into it! Variables in the message should be
  59. * added by using placeholder strings alongside the variables argument to
  60. * declare the value of the placeholders. See t() for documentation on how
  61. * $message and $variables interact.
  62. * @param $variables
  63. * Array of variables to replace in the message on display or NULL if message
  64. * is already translated or not possible to translate.
  65. * @param $options
  66. * An array of options. Some available options include:
  67. * - print: prints the error message to the terminal screen. Useful when
  68. * display is the command-line
  69. *
  70. * @ingroup tripal_api
  71. */
  72. function tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) {
  73. // Get human-readable severity string
  74. $severity_string = '';
  75. switch ($severity) {
  76. case TRIPAL_CRITICAL:
  77. $severity_string = 'CRITICAL';
  78. break;
  79. case TRIPAL_ERROR:
  80. $severity_string = 'ERROR';
  81. break;
  82. case TRIPAL_WARNING:
  83. $severity_string = 'WARNING';
  84. break;
  85. case TRIPAL_NOTICE:
  86. $severity_string = 'NOTICE';
  87. break;
  88. case TRIPAL_INFO:
  89. $severity_string = 'INFO';
  90. break;
  91. case TRIPAL_DEBUG:
  92. $severity_string = 'DEBUG';
  93. break;
  94. }
  95. // get the backtrace and include in the error message, but only if the
  96. // TRIPAL_DEBUG environment variable is set.
  97. if (getenv('TRIPAL_DEBUG') == 1) {
  98. $backtrace = debug_backtrace();
  99. $message .= "\nBacktrace:\n";
  100. $i = 1;
  101. for ($i = 1; $i < count($backtrace); $i++) {
  102. $function = $backtrace[$i];
  103. $message .= " $i) " . $function['function'] . "\n";
  104. }
  105. }
  106. // Send to watchdog
  107. try {
  108. watchdog($type, $message, $variables, $severity);
  109. }
  110. catch (Exception $e) {
  111. print "CRITICAL (TRIPAL_CORE): Unable to register error message with watchdog: " . $e->getMessage(). "\n.";
  112. $options['print'] = TRUE;
  113. }
  114. // Format the message for printing (either to the screen, log or both)
  115. if (sizeof($variables) > 0) {
  116. $print_message = str_replace(array_keys($variables), $variables, $message);
  117. }
  118. else {
  119. $print_message = $message;
  120. }
  121. // If print option supplied then print directly to the screen
  122. if (isset($options['print'])) {
  123. print $severity_string . ' (' . strtoupper($type) . '): ' . $print_message . "\n";
  124. }
  125. // Print to the Tripal error log
  126. tripal_log('[' . strtoupper($type) . '] ' . $print_message . "\n", $severity_string);
  127. }
  128. /**
  129. * Display messages to tripal administrators. This can be used instead of
  130. * drupal_set_message when you want to target tripal administrators.
  131. *
  132. * @param $message
  133. * The message to be displayed to the tripal administrators
  134. * @param $importance
  135. * The level of importance for this message. In the future this will be used
  136. * to allow administrators to filter some of these messages. It can be one
  137. * of the following:
  138. * - TRIPAL_CRITICAL: Critical conditions.
  139. * - TRIPAL_ERROR: Error conditions.
  140. * - TRIPAL_WARNING: Warning conditions.
  141. * - TRIPAL_NOTICE: Normal but significant conditions.
  142. * - TRIPAL_INFO: (default) Informational messages.
  143. * - TRIPAL_DEBUG: Debug-level messages.
  144. * @param $options
  145. * Any options to apply to the current message. Supported options include:
  146. * - return_html: return HTML instead of setting a drupal message. This can be
  147. * used to place a tripal message in a particular place in the page.
  148. * The default is FALSE.
  149. */
  150. function tripal_set_message($message, $importance = TRIPAL_INFO, $options = array()) {
  151. global $user;
  152. // Only show the message to the users with 'view dev helps' permission.
  153. if (!user_access('view dev helps')) {
  154. return '';
  155. }
  156. // set defaults
  157. $options['return_html'] = (isset($options['return_html'])) ? $options['return_html'] : FALSE;
  158. // Get human-readable severity string
  159. $importance_string = '';
  160. switch ($importance) {
  161. case TRIPAL_CRITICAL:
  162. $importance_string = 'CRITICAL';
  163. break;
  164. case TRIPAL_ERROR:
  165. $importance_string = 'ERROR';
  166. break;
  167. case TRIPAL_WARNING:
  168. $importance_string = 'WARNING';
  169. break;
  170. case TRIPAL_NOTICE:
  171. $importance_string = 'NOTICE';
  172. break;
  173. case TRIPAL_INFO:
  174. $importance_string = 'INFO';
  175. break;
  176. case TRIPAL_DEBUG:
  177. $importance_string = 'DEBUG';
  178. break;
  179. }
  180. // Mark-up the Message
  181. $full_message =
  182. '<div class="tripal-site-admin-message">'
  183. . '<span class="tripal-severity-string ' . strtolower($importance_string) . '">' . $importance_string . ': </span>'
  184. . $message
  185. . '</div>';
  186. // Handle whether to return the HTML & let the caller deal with it
  187. // or to use drupal_set_message to put it near the top of the page & let the theme deal with it
  188. if ($options['return_html']) {
  189. return '<div class="messages tripal-site-admin-only">' . $full_message . '</div>';
  190. }
  191. else {
  192. drupal_set_message($full_message, 'tripal-site-admin-only');
  193. }
  194. }
  195. /**
  196. * File-based Logging for Tripal
  197. *
  198. * @param $message
  199. * The message to be logged. Need not contain date/time information
  200. * @param $log_type
  201. * The type of log. Should be one of 'error' or 'job' although other types
  202. * are supported.
  203. * @param $options
  204. * An array of options where the following keys are supported:
  205. * - first_progress_bar: this sohuld be used for the first log call for a
  206. * progress bar.
  207. * - is_progress_bar: this option should be used for all but the first
  208. * print of a progress bar to allow it all to be printed on the same
  209. * line without intervening date prefixes.
  210. * @return
  211. * The number of bytes that were written to the file, or FALSE on failure
  212. */
  213. function tripal_log($message, $type = 'error', $options = array()) {
  214. global $base_url;
  215. $prefix = '[site ' . $base_url . '] [TRIPAL ' . strtoupper(check_plain($type)) . '] ';
  216. if (!isset($options['is_progress_bar'])) {
  217. $message = $prefix . str_replace("\n", "", trim($message));
  218. }
  219. if (isset($options['first_progress_bar'])) {
  220. $message = $prefix . trim($message);
  221. }
  222. return error_log($message);
  223. }