Merge pull request #1754 from yashodha/CRM-13548
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourFour.php
index dadc4f00de0b15ef07d289eb5af0b610a0051087..2886e4f94d31e33b0bf24f2761327d48e620a092 100644 (file)
@@ -62,23 +62,23 @@ class CRM_Upgrade_Incremental_php_FourFour {
     if ($rev == '4.4.alpha1') {
       $config = CRM_Core_Config::singleton();
       if (!empty($config->useIDS)) {
-        $postUpgradeMessage .= '<br />' . ts("The setting to skip IDS check has been deprecated. Please use the permission 'skip IDS check' to bypass the IDS system");
+        $postUpgradeMessage .= '<br />' . ts("The setting to skip IDS check has been deprecated. Please use the permission 'skip IDS check' to bypass the IDS system.");
       }
     }
   }
 
   function upgrade_4_4_alpha1($rev) {
     // task to process sql
-    $this->addTask(ts('Upgrade DB to 4.4.alpha1: SQL'), 'task_4_4_x_runSql', $rev);
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.alpha1')), 'task_4_4_x_runSql', $rev);
 
     // Consolidate activity contacts CRM-12274.
-    $this->addTask(ts('Consolidate activity contacts'), 'activityContacts');
+    $this->addTask('Consolidate activity contacts', 'activityContacts');
 
     return TRUE;
   }
 
   function upgrade_4_4_beta1($rev) {
-    $this->addTask(ts('Upgrade DB to 4.4.beta1: SQL'), 'task_4_4_x_runSql', $rev);
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.beta1')), 'task_4_4_x_runSql', $rev);
 
     // add new 'data' column in civicrm_batch
     $query = 'ALTER TABLE civicrm_batch ADD data LONGTEXT NULL COMMENT "cache entered data"';
@@ -99,6 +99,8 @@ class CRM_Upgrade_Incremental_php_FourFour {
     // delete entries from civicrm_cache table
     $query = 'DELETE FROM civicrm_cache WHERE group_name = "batch entry"';
     CRM_Core_DAO::executeQuery($query);
+
+    $this->addTask('Migrate custom word-replacements', 'wordReplacements');
   }
 
   /**
@@ -203,6 +205,32 @@ WHERE       source_contact_id IS NOT NULL";
    return TRUE;
   }
 
+  /**
+   * Migrate word-replacements from $config to civicrm_word_replacement
+   *
+   * @return bool TRUE for success
+   * @see http://issues.civicrm.org/jira/browse/CRM-13187
+   */
+  static function wordReplacements(CRM_Queue_TaskContext $ctx) {
+    $query = "
+CREATE TABLE IF NOT EXISTS `civicrm_word_replacement` (
+     `id` int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Word replacement ID',
+     `find_word` varchar(255)    COMMENT 'Word which need to be replaced',
+     `replace_word` varchar(255)    COMMENT 'Word which will replace the word in find',
+     `is_active` tinyint    COMMENT 'Is this entry active?',
+     `match_type` enum('wildcardMatch', 'exactMatch')   DEFAULT 'wildcardMatch',
+     `domain_id` int unsigned    COMMENT 'FK to Domain ID. This is for Domain specific word replacement',
+    PRIMARY KEY ( `id` ),
+    UNIQUE INDEX `UI_find`(find_word),
+    CONSTRAINT FK_civicrm_word_replacement_domain_id FOREIGN KEY (`domain_id`) REFERENCES `civicrm_domain`(`id`)
+)  ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci  ;
+    ";
+    $dao = CRM_Core_DAO::executeQuery($query);
+
+    self::rebuildWordReplacementTable();
+    return TRUE;
+  }
+
   /**
    * (Queue Task Callback)
    */
@@ -236,4 +264,73 @@ WHERE       source_contact_id IS NOT NULL";
     );
     $queue->createItem($task, array('weight' => -1));
   }
+
+  /**
+   * Get all the word-replacements stored in config-arrays
+   * and convert them to params for the WordReplacement.create API.
+   *
+   * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
+   * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
+   * step behaves consistently even as the BAO evolves in future versions.
+   * However, if there's a bug in here prior to 4.4.0, we should apply the
+   * bugfix in both places.
+   *
+   * @param bool $rebuildEach whether to perform rebuild after each individual API call
+   * @return array Each item is $params for WordReplacement.create
+   * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
+   */
+  static function getConfigArraysAsAPIParams($rebuildEach) {
+    $wordReplacementCreateParams = array();
+    // get all domains
+    $result = civicrm_api3('domain', 'get', array(
+      'return' => array('locale_custom_strings'),
+    ));
+    if (!empty($result["values"])) {
+      foreach ($result["values"] as $value) {
+        $params = array();
+        $params["domain_id"] = $value["id"];
+        $params["options"] = array('wp-rebuild' => $rebuildEach);
+        // unserialize word match string
+        $localeCustomArray = unserialize($value["locale_custom_strings"]);
+        if (!empty($localeCustomArray)) {
+          $wordMatchArray = array();
+          // Traverse Language array
+          foreach ($localeCustomArray as $localCustomData) {
+            // Traverse status array "enabled" "disabled"
+            foreach ($localCustomData as $status => $matchTypes) {
+              $params["is_active"] = ($status == "enabled")?TRUE:FALSE;
+              // Traverse Match Type array "wildcardMatch" "exactMatch"
+              foreach ($matchTypes as $matchType => $words) {
+                $params["match_type"] = $matchType;
+                foreach ($words as $word => $replace) {
+                  $params["find_word"] = $word;
+                  $params["replace_word"] = $replace;
+                  $wordReplacementCreateParams[] = $params;
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+    return $wordReplacementCreateParams;
+  }
+
+  /**
+   * Get all the word-replacements stored in config-arrays
+   * and write them out as records in civicrm_word_replacement.
+   *
+   * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
+   * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
+   * step behaves consistently even as the BAO evolves in future versions.
+   * However, if there's a bug in here prior to 4.4.0, we should apply the
+   * bugfix in both places.
+   */
+  public static function rebuildWordReplacementTable() {
+    civicrm_api3('word_replacement', 'replace', array(
+      'options' => array('match' => array('domain_id', 'find_word')),
+      'values' => self::getConfigArraysAsAPIParams(FALSE),
+    ));
+    CRM_Core_BAO_WordReplacement::rebuild();
+  }
 }