Merge pull request #5386 from colemanw/CRM-15706
[civicrm-core.git] / CRM / Core / Error.php
index 7d21a983ddc9df0ac178635c3ec47b7f5c99ab26..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;
@@ -83,20 +84,17 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * We only need one instance of this object. So we use the singleton
    * pattern and cache the instance in this variable
    * @var object
-   * @static
    */
   private static $_singleton = NULL;
 
   /**
-   * The logger object for this application
+   * The logger object for this application.
    * @var object
-   * @static
    */
   private static $_log = NULL;
 
   /**
    * If modeException == true, errors are raised as exception instead of returning civicrm_errors
-   * @static
    */
   public static $modeException = NULL;
 
@@ -110,7 +108,6 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * @param string $stackClass
    *
    * @return object
-   * @static
    */
   public static function &singleton($package = NULL, $msgCallback = FALSE, $contextCallback = FALSE, $throwPEAR_Error = FALSE, $stackClass = 'PEAR_ErrorStack') {
     if (self::$_singleton === NULL) {
@@ -120,7 +117,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Constructor
+   * Constructor.
    */
   public function __construct() {
     parent::__construct('CiviCRM');
@@ -152,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
    */
@@ -171,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
    */
@@ -195,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();
@@ -204,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;
@@ -295,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.
@@ -307,7 +309,6 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * @throws Exception
    *
    * @return void
-   * @static
    */
   public static function fatal($message = NULL, $code = NULL, $email = NULL) {
     $vars = array(
@@ -385,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
@@ -394,7 +395,6 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * @param Exception $exception
    *
    * @return void
-   * @static
    */
   public static function handleUnhandledException($exception) {
     try {
@@ -467,17 +467,16 @@ 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
    *
    * @return string
    *   the generated output
-   * @static
    */
   public static function debug($name, $variable = NULL, $log = TRUE, $html = TRUE, $checkPermission = TRUE) {
     $error = self::singleton();
@@ -529,12 +528,11 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    *   the generated output
    *
    *
-   * @static
    *
    * @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,
@@ -567,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
@@ -579,7 +577,6 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    *   format of the backtrace
    *
    *
-   * @static
    */
   public static function debug_log_message($message, $out = FALSE, $comp = '') {
     $config = CRM_Core_Config::singleton();
@@ -595,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);
       }
     }
 
@@ -631,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
    *
@@ -681,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.
@@ -702,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.
@@ -780,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
@@ -816,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
@@ -861,7 +858,6 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * @param null $redirect
    * @param string $title
    * @return void
-   * @static
    */
   public static function statusBounce($status, $redirect = NULL, $title = NULL) {
     $session = CRM_Core_Session::singleton();
@@ -879,9 +875,8 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * Reset the error stack
+   * Reset the error stack.
    *
-   * @static
    */
   public static function reset() {
     $error = self::singleton();
@@ -907,7 +902,6 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    *   The PEAR_ERROR object.
    * @return object
    *   $obj
-   * @static
    */
   public static function nullHandler($obj) {
     CRM_Core_Error::debug_log_message("Ignoring exception thrown by nullHandler: {$obj->code}, {$obj->message}");
@@ -955,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
@@ -965,8 +959,8 @@ class CRM_Core_Error extends PEAR_ErrorStack {
   }
 
   /**
-   * @param $error
-   * @param const $type
+   * @param array $error
+   * @param int $type
    *
    * @return bool
    */
@@ -979,6 +973,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     }
     return FALSE;
   }
+
 }
 
 $e = new PEAR_ErrorStack('CRM');