Merge pull request #5487 from eileenmcnaughton/CRM-16165
[civicrm-core.git] / CRM / Core / Error.php
index 8a16fa3652b70674bb2ed818c1d7698351a40ac4..cb4c217a3a1cbd3de5016f7e2ca5ee7456620d8d 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  * Start of the Error framework. We should check out and inherit from
@@ -57,13 +57,14 @@ class CRM_Exception extends PEAR_Exception {
    *  - PEAR_Exception(string $message, array $causes);
    *  - PEAR_Exception(string $message, array $causes, int $code);
    *
-   * @param string exception message
+   * @param string $message exception message
    * @param int $code
    * @param Exception $previous
    */
   public function __construct($message = NULL, $code = 0, Exception $previous = NULL) {
     parent::__construct($message, $code, $previous);
   }
+
 }
 
 /**
@@ -72,7 +73,7 @@ class CRM_Exception extends PEAR_Exception {
 class CRM_Core_Error extends PEAR_ErrorStack {
 
   /**
-   * Status code of various types of errors
+   * Status code of various types of errors.
    */
   const FATAL_ERROR = 2;
   const DUPLICATE_CONTACT = 8001;
@@ -87,7 +88,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   private static $_singleton = NULL;
 
   /**
-   * The logger object for this application
+   * The logger object for this application.
    * @var object
    */
   private static $_log = NULL;
@@ -116,7 +117,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Constructor
+   * Constructor.
    */
   public function __construct() {
     parent::__construct('CiviCRM');
@@ -148,7 +149,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Status display function specific to payment processor errors
+   * Status display function specific to payment processor errors.
    * @param $error
    * @param string $separator
    */
@@ -167,7 +168,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * the errors we expect are from the pear modules DB, DB_DataObject
    * which currently use PEAR::raiseError to notify of error messages.
    *
-   * @param object PEAR_Error
+   * @param object $pearError PEAR_Error
    *
    * @return void
    */
@@ -191,7 +192,16 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     $error['type'] = $pearError->getType();
     $error['user_info'] = $pearError->getUserInfo();
     $error['to_string'] = $pearError->toString();
-    if (function_exists('mysql_error') &&
+
+    // We access connection info via _DB_DATAOBJECT instead
+    // of, e.g., calling getDatabaseConnection(), so that we
+    // can avoid infinite loops.
+    global $_DB_DATAOBJECT;
+
+    if (!isset($_DB_DATAOBJECT['CONFIG']['database'])) {
+      // we haven't setup sql, so it's not our sql error...
+    }
+    elseif (preg_match('/^mysql:/', $_DB_DATAOBJECT['CONFIG']['database']) &&
       mysql_error()
     ) {
       $mysql_error = mysql_error() . ', ' . mysql_errno();
@@ -200,13 +210,9 @@ class CRM_Core_Error extends PEAR_ErrorStack {
       // execute a dummy query to clear error stack
       mysql_query('select 1');
     }
-    elseif (function_exists('mysqli_error')) {
+    elseif (preg_match('/^mysqli:/', $_DB_DATAOBJECT['CONFIG']['database'])) {
       $dao = new CRM_Core_DAO();
 
-      // we do it this way, since calling the function
-      // getDatabaseConnection could potentially result
-      // in an infinite loop
-      global $_DB_DATAOBJECT;
       if (isset($_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5])) {
         $conn = $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5];
         $link = $conn->connection;
@@ -291,7 +297,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Display an error page with an error message describing what happened
+   * Display an error page with an error message describing what happened.
    *
    * @param string $message
    *   The error message.
@@ -380,7 +386,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Display an error page with an error message describing what happened
+   * Display an error page with an error message describing what happened.
    *
    * This function is evil -- it largely replicates fatal(). Hopefully the
    * entire CRM_Core_Error system can be hollowed out and replaced with
@@ -461,11 +467,11 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * Outputs pre-formatted debug information. Flushes the buffers
    * so we can interrupt a potential POST/redirect
    *
-   * @param string name of debug section
-   * @param mixed reference to variables that we need a trace of
-   * @param bool should we log or return the output
-   * @param bool whether to generate a HTML-escaped output
-   * @param bool should we check permissions before displaying output
+   * @param string $name name of debug section
+   * @param $variable mixed reference to variables that we need a trace of
+   * @param bool $log should we log or return the output
+   * @param bool $html whether to generate a HTML-escaped output
+   * @param bool $checkPermission should we check permissions before displaying output
    *                useful when we die during initialization and permissioning
    *                subsystem is not initialized - CRM-13765
    *
@@ -526,7 +532,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * @see CRM_Core_Error::debug()
    * @see CRM_Core_Error::debug_log_message()
    */
-  static function debug_var(
+  public static function debug_var(
     $variable_name,
     $variable,
     $print = TRUE,
@@ -559,7 +565,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Display the error message on terminal
+   * Display the error message on terminal.
    *
    * @param $message
    * @param bool $out
@@ -586,7 +592,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     if ($config->userFrameworkLogging) {
       // should call $config->userSystem->logger($message) here - but I got a situation where userSystem was not an object - not sure why
       if ($config->userSystem->is_drupal and function_exists('watchdog')) {
-        watchdog('civicrm', $message, NULL, WATCHDOG_DEBUG);
+        watchdog('civicrm', '%message', array('%message' => $message), WATCHDOG_DEBUG);
       }
     }
 
@@ -622,7 +628,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Obtain a reference to the error log
+   * Obtain a reference to the error log.
    *
    * @param string $comp
    *
@@ -672,7 +678,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Render a backtrace array as a string
+   * Render a backtrace array as a string.
    *
    * @param array $backTrace
    *   Array of stack frames.
@@ -693,7 +699,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Render a backtrace array as an array
+   * Render a backtrace array as an array.
    *
    * @param array $backTrace
    *   Array of stack frames.
@@ -771,7 +777,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Render an exception as HTML string
+   * Render an exception as HTML string.
    *
    * @param Exception $e
    * @return string
@@ -807,7 +813,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Write details of an exception to the log
+   * Write details of an exception to the log.
    *
    * @param Exception $e
    * @return string
@@ -869,7 +875,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Reset the error stack
+   * Reset the error stack.
    *
    */
   public static function reset() {
@@ -943,7 +949,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Terminate execution abnormally
+   * Terminate execution abnormally.
    */
   protected static function abend($code) {
     // do a hard rollback of any pending transactions
@@ -967,6 +973,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     }
     return FALSE;
   }
+
 }
 
 $e = new PEAR_ErrorStack('CRM');