tripal_core.tripal.api.inc

Provides an application programming interface (API) for Tripal

The Tripal API currently provides generic insert/update/select functions for all chado content as well as some module specific functions that insert/update/delete/select specific chado content.

This API is currently in its infancy and some necessary functions might be missing. If you find a missing function that you think should be included go to the sourceforge feature request page and request it's inclusion in the API. Such feature requests with a working function definition will be given priority.

File

tripal_core/api/tripal_core.tripal.api.inc
View source
  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. // If we are not set to return debugging information and the severity string is debug
  96. // then don't report the error.
  97. if (($severity == TRIPAL_DEBUG) AND (getenv('TRIPAL_DEBUG') != 1)) {
  98. return FALSE;
  99. }
  100. // get the backtrace and include in the error message, but only if the
  101. // TRIPAL_DEBUG environment variable is set.
  102. if (getenv('TRIPAL_DEBUG') == 1) {
  103. $backtrace = debug_backtrace();
  104. $message .= "\nBacktrace:\n";
  105. $i = 1;
  106. for ($i = 1; $i < count($backtrace); $i++) {
  107. $function = $backtrace[$i];
  108. $message .= " $i) " . $function['function'] . "\n";
  109. }
  110. }
  111. // Send to watchdog.
  112. try {
  113. watchdog($type, $message, $variables, $severity);
  114. }
  115. catch (Exception $e) {
  116. print "CRITICAL (TRIPAL_CORE): Unable to register error message with watchdog: " . $e->getMessage(). "\n.";
  117. $options['print'] = TRUE;
  118. }
  119. // Format the message for printing (either to the screen, log or both)
  120. if (sizeof($variables) > 0) {
  121. $print_message = str_replace(array_keys($variables), $variables, $message);
  122. }
  123. else {
  124. $print_message = $message;
  125. }
  126. // If print option supplied then print directly to the screen.
  127. if (isset($options['print'])) {
  128. print $severity_string . ' (' . strtoupper($type) . '): ' . $print_message . "\n";
  129. }
  130. // Print to the Tripal error log but only if the severity is not info.
  131. if (($severity != TRIPAL_INFO)) {
  132. tripal_log('[' . strtoupper($type) . '] ' . $print_message . "\n", $severity_string);
  133. }
  134. }
  135. /**
  136. * Display messages to tripal administrators.
  137. *
  138. * This can be used instead of drupal_set_message when you want to target
  139. * tripal administrators.
  140. *
  141. * @param $message
  142. * The message to be displayed to the tripal administrators
  143. * @param $importance
  144. * The level of importance for this message. In the future this will be used
  145. * to allow administrators to filter some of these messages. It can be one
  146. * of the following:
  147. * - TRIPAL_CRITICAL: Critical conditions.
  148. * - TRIPAL_ERROR: Error conditions.
  149. * - TRIPAL_WARNING: Warning conditions.
  150. * - TRIPAL_NOTICE: Normal but significant conditions.
  151. * - TRIPAL_INFO: (default) Informational messages.
  152. * - TRIPAL_DEBUG: Debug-level messages.
  153. * @param $options
  154. * Any options to apply to the current message. Supported options include:
  155. * - return_html: return HTML instead of setting a drupal message. This can be
  156. * used to place a tripal message in a particular place in the page.
  157. * The default is FALSE.
  158. */
  159. function tripal_set_message($message, $importance = TRIPAL_INFO, $options = array()) {
  160. global $user;
  161. // Only show the message to the users with 'view dev helps' permission.
  162. if (!user_access('view dev helps')) {
  163. return '';
  164. }
  165. // set defaults
  166. $options['return_html'] = (isset($options['return_html'])) ? $options['return_html'] : FALSE;
  167. // Get human-readable severity string
  168. $importance_string = '';
  169. switch ($importance) {
  170. case TRIPAL_CRITICAL:
  171. $importance_string = 'CRITICAL';
  172. break;
  173. case TRIPAL_ERROR:
  174. $importance_string = 'ERROR';
  175. break;
  176. case TRIPAL_WARNING:
  177. $importance_string = 'WARNING';
  178. break;
  179. case TRIPAL_NOTICE:
  180. $importance_string = 'NOTICE';
  181. break;
  182. case TRIPAL_INFO:
  183. $importance_string = 'INFO';
  184. break;
  185. case TRIPAL_DEBUG:
  186. $importance_string = 'DEBUG';
  187. break;
  188. }
  189. // Mark-up the Message
  190. $full_message =
  191. '<div class="tripal-site-admin-message">'
  192. . '<span class="tripal-severity-string ' . strtolower($importance_string) . '">' . $importance_string . ': </span>'
  193. . $message
  194. . '</div>';
  195. // Handle whether to return the HTML & let the caller deal with it
  196. // or to use drupal_set_message to put it near the top of the page & let the theme deal with it
  197. if ($options['return_html']) {
  198. return '<div class="messages tripal-site-admin-only">' . $full_message . '</div>';
  199. }
  200. else {
  201. drupal_set_message($full_message, 'tripal-site-admin-only');
  202. }
  203. }
  204. /**
  205. * File-based Logging for Tripal
  206. *
  207. * @param $message
  208. * The message to be logged. Need not contain date/time information
  209. * @param $log_type
  210. * The type of log. Should be one of 'error' or 'job' although other types
  211. * are supported.
  212. * @param $options
  213. * An array of options where the following keys are supported:
  214. * - first_progress_bar: this sohuld be used for the first log call for a
  215. * progress bar.
  216. * - is_progress_bar: this option should be used for all but the first
  217. * print of a progress bar to allow it all to be printed on the same
  218. * line without intervening date prefixes.
  219. * @return
  220. * The number of bytes that were written to the file, or FALSE on failure
  221. */
  222. function tripal_log($message, $type = 'error', $options = array()) {
  223. global $base_url;
  224. $prefix = '[site ' . $base_url . '] [TRIPAL ' . strtoupper(check_plain($type)) . '] ';
  225. if (!isset($options['is_progress_bar'])) {
  226. $message = $prefix . str_replace("\n", "", trim($message));
  227. }
  228. if (isset($options['first_progress_bar'])) {
  229. $message = $prefix . trim($message);
  230. }
  231. return error_log($message);
  232. }