tripal_core.tripal.api.inc 8.2 KB

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